[fpc-devel] Unnecessary string copy from Utf8String to AnsiString if destination CP is UTF8
Ondrej Pokorny
lazarus at kluug.net
Sun Apr 28 09:55:06 CEST 2019
Hello,
IMO there is an unnecessary Move() operation in fpc_AnsiStr_To_AnsiStr
if (orgcp=cp).
fpc_AnsiStr_To_AnsiStr creates a copy of the AnsiString even if the
destination and source codepages are equal. See:
program AnsiUtf8;
var
Utf8Str: UTF8String;
RawStr: RawByteString;
Str: string;
begin
DefaultSystemCodePage := CP_UTF8;
Utf8Str := 'hello';
Str := Utf8Str; // this makes a copy (fpc_AnsiStr_To_AnsiStr -> Move)
RawStr := 'hello';
SetCodePage(RawStr, CP_UTF8, False);
Str := RawStr; // this doesn't make a copy
end.
Is there a reason for this? See the attached patch.
Thanks
Ondrej
-------------- next part --------------
Index: rtl/inc/astrings.inc
===================================================================
--- rtl/inc/astrings.inc (revision 41892)
+++ rtl/inc/astrings.inc (working copy)
@@ -446,7 +446,10 @@
begin
cp:=TranslatePlaceholderCP(cp);
orgcp:=TranslatePlaceholderCP(StringCodePage(S));
- if (orgcp=cp) or (orgcp=CP_NONE) then
+ if orgcp=cp then
+ Result := S
+ else
+ if orgcp=CP_NONE then
begin
SetLength(result,Size);
Move(S[1],result[1],Size);
More information about the fpc-devel
mailing list