From 76f75244364dd8fb991fa97b415d85098c4279e3 Mon Sep 17 00:00:00 2001 From: Juha Date: Sat, 16 Apr 2022 12:41:31 +0300 Subject: [PATCH] LazUtils: In UCS2BEToUTF8 and UCS2LEToUTF8 skip little/big endian BOM. Merge request !88 by Zaher Dirkey. --- components/lazutils/lconvencoding.pas | 14 ++++++++++++-- 1 file changed, 12 insertions(+), 2 deletions(-) diff --git a/components/lazutils/lconvencoding.pas b/components/lazutils/lconvencoding.pas index 2bc7082d69..6338f9a5e8 100644 --- a/components/lazutils/lconvencoding.pas +++ b/components/lazutils/lconvencoding.pas @@ -505,8 +505,13 @@ begin len:=length(s) div 2; if len=0 then exit(''); - SetLength(Result,len*3);// UTF-8 is at most 3/2 times the size Src:=PWord(Pointer(s)); + if (Src^=$FEFF) then // Skip BOM + begin + inc(Src); + dec(len); + end; + SetLength(Result,len*3);// UTF-8 is at most 3/2 times the size Dest:=PChar(Result); for i:=1 to len do begin c:=LEtoN(Src^); @@ -536,8 +541,13 @@ begin len:=length(s) div 2; if len=0 then exit(''); - SetLength(Result,len*3);// UTF-8 is at most three times the size Src:=PWord(Pointer(s)); + if (Src^=$FFFE) then // Skip BOM + begin + inc(Src); + dec(len); + end; + SetLength(Result,len*3);// UTF-8 is at most three times the size Dest:=PChar(Result); for i:=1 to len do begin c:=BEtoN(Src^);