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

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);
_______________________________________________
fpc-devel maillist  -  fpc-devel@lists.freepascal.org
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-devel

Reply via email to