From 9d34bf831ea33d73bd300a18aa3af5daa271ebc0 Mon Sep 17 00:00:00 2001 From: Mirek Fidler Date: Tue, 24 Aug 2021 18:05:56 +0200 Subject: [PATCH] Core: Utf8BOM (skips UTF8 file header) --- uppsrc/Core/Bom.cpp | 9 +++++++++ uppsrc/Core/CharSet.h | 3 +++ uppsrc/Core/src.tpp/CharSet_en-us.tpp | 6 ++++++ 3 files changed, 18 insertions(+) diff --git a/uppsrc/Core/Bom.cpp b/uppsrc/Core/Bom.cpp index 74e2e970c..ef5abb71b 100644 --- a/uppsrc/Core/Bom.cpp +++ b/uppsrc/Core/Bom.cpp @@ -2,6 +2,15 @@ namespace Upp { +bool Utf8BOM(Stream& in) +{ + int64 pos = in.GetPos(); + if(in.Get() == 0xef && in.Get() == 0xbb && in.Get() == 0xbf) + return true; + in.Seek(pos); + return false; +} + static void sLoadBom(Stream& in, String *t, WString *wt, byte def_charset) { if(in.IsOpen()) { diff --git a/uppsrc/Core/CharSet.h b/uppsrc/Core/CharSet.h index a389fbb3a..e8d19a28b 100644 --- a/uppsrc/Core/CharSet.h +++ b/uppsrc/Core/CharSet.h @@ -251,6 +251,9 @@ bool SaveFileBOM(const char *path, const WString& data); bool SaveStreamBOMUtf8(Stream& out, const String& data); bool SaveFileBOMUtf8(const char *path, const String& data); +bool Utf8BOM(Stream& in); + + // Deprecated word UnicodeCombine(word chr, word combine); diff --git a/uppsrc/Core/src.tpp/CharSet_en-us.tpp b/uppsrc/Core/src.tpp/CharSet_en-us.tpp index 831729c1f..5a9d59449 100644 --- a/uppsrc/Core/src.tpp/CharSet_en-us.tpp +++ b/uppsrc/Core/src.tpp/CharSet_en-us.tpp @@ -705,6 +705,12 @@ true on success.&] true on success.&] [s3; &] [s4; &] +[s5;:Upp`:`:Utf8BOM`(Upp`:`:Stream`&`): [@(0.0.255) bool]_[* Utf8BOM]([_^Upp`:`:Stream^ Str +eam][@(0.0.255) `&]_[*@3 in])&] +[s2;%% Tests for and skips UTF`-8 BOM mark in the seekable Stream +[%-*@3 in].&] +[s3;%% &] +[s4; &] [s5;:FromUtf8`(const char`*`,int`): [_^WString^ WString]_[* FromUtf8]([@(0.0.255) const]_[@(0.0.255) c har]_`*[*@3 `_s], [@(0.0.255) int]_[*@3 len])&] [s2;%% Converts UTF`-8 to UNICODE string. Any wrong bytes and sequences