Pārlūkot izejas kodu

compiler:
- treat the defs equal when convert from rawbytstring to any ansistring type (delphi compatible)
- set result of ansistring copy() function to the type of first argument for ansistring arguments and to ansistring for pchar and char array arguments (delphi compatible)
+ test

git-svn-id: trunk@20285 -

paul 13 gadi atpakaļ
vecāks
revīzija
3b153c223a
5 mainītis faili ar 47 papildinājumiem un 14 dzēšanām
  1. 1 0
      .gitattributes
  2. 6 4
      compiler/defcmp.pas
  3. 10 8
      compiler/pinline.pas
  4. 2 2
      compiler/ptype.pas
  5. 28 0
      tests/test/tcpstr19.pp

+ 1 - 0
.gitattributes

@@ -10170,6 +10170,7 @@ tests/test/tcpstr15.pp svneol=native#text/pascal
 tests/test/tcpstr16.pp svneol=native#text/pascal
 tests/test/tcpstr17.pp svneol=native#text/pascal
 tests/test/tcpstr18.pp svneol=native#text/pascal
+tests/test/tcpstr19.pp svneol=native#text/pascal
 tests/test/tcpstr2.pp svneol=native#text/plain
 tests/test/tcpstr2a.pp svneol=native#text/plain
 tests/test/tcpstr3.pp svneol=native#text/plain

+ 6 - 4
compiler/defcmp.pas

@@ -363,17 +363,19 @@ implementation
                             end;
                          end;
                       end
-                     else if (tstringdef(def_from).stringtype=tstringdef(def_to).stringtype) and
+                     else if (tstringdef(def_to).stringtype=st_ansistring) and
                              (tstringdef(def_from).stringtype=st_ansistring) then 
                       begin
-                        { don't convert ansistrings if any conditions is true:
+                        { don't convert ansistrings if any condition is true:
                           1) same encoding
                           2) from explicit codepage ansistring to ansistring and vice versa
-                          3) from any ansistring to rawbytestring }
+                          3) from any ansistring to rawbytestring 
+                          4) from rawbytestring to any ansistring }
                         if (tstringdef(def_from).encoding=tstringdef(def_to).encoding) or
                            ((tstringdef(def_to).encoding=0) and (tstringdef(def_from).encoding=getansistringcodepage)) or
                            ((tstringdef(def_to).encoding=getansistringcodepage) and (tstringdef(def_from).encoding=0)) or
-                           (tstringdef(def_to).encoding=globals.CP_NONE) then
+                           (tstringdef(def_to).encoding=globals.CP_NONE) or
+                           (tstringdef(def_from).encoding=globals.CP_NONE) then
                          begin
                            eq:=te_equal;
                          end

+ 10 - 8
compiler/pinline.pas

@@ -711,12 +711,14 @@ implementation
            ppn:=tcallparanode(ppn.right);
          end;
         paradef:=ppn.left.resultdef;
-        if is_ansistring(paradef) or
-           (is_chararray(paradef) and
-            (paradef.size>255)) or
-           ((cs_ansistrings in current_settings.localswitches) and
-            is_pchar(paradef)) then
-          copynode:=ccallnode.createintern('fpc_ansistr_copy',paras)
+        if is_ansistring(paradef) then
+          // set resultdef to argument def
+          copynode:=ccallnode.createinternres('fpc_ansistr_copy',paras,paradef)
+        else
+         if (is_chararray(paradef) and (paradef.size>255)) or
+            ((cs_ansistrings in current_settings.localswitches) and is_pchar(paradef)) then
+           // set resultdef to ansistring type since result will be in ansistring codepage
+           copynode:=ccallnode.createinternres('fpc_ansistr_copy',paras,getansistringdef)
         else
          if is_widestring(paradef) then
            copynode:=ccallnode.createintern('fpc_widestr_copy',paras)
@@ -756,10 +758,10 @@ implementation
             npara:=ccallparanode.create(highppn,
                    ccallparanode.create(lowppn,
                    ccallparanode.create(caddrnode.create_internal
-                      (crttinode.create(tstoreddef(ppn.left.resultdef),initrtti,rdt_normal)),
+                      (crttinode.create(tstoreddef(paradef),initrtti,rdt_normal)),
                    ccallparanode.create
                       (ctypeconvnode.create_internal(ppn.left,voidpointertype),nil))));
-            copynode:=ccallnode.createinternres('fpc_dynarray_copy',npara,ppn.left.resultdef);
+            copynode:=ccallnode.createinternres('fpc_dynarray_copy',npara,paradef);
 
             ppn.left:=nil;
             paras.free;

+ 2 - 2
compiler/ptype.pas

@@ -645,8 +645,8 @@ implementation
               end;
             _CONSTRUCTOR :
               begin
-                if not is_classdef then
-                  Message(parser_e_no_constructor_in_records);
+                //if not is_classdef then
+                //  Message(parser_e_no_constructor_in_records);
                 if not is_classdef and (current_structdef.symtable.currentvisibility <> vis_public) then
                   Message(parser_w_constructor_should_be_public);
 

+ 28 - 0
tests/test/tcpstr19.pp

@@ -0,0 +1,28 @@
+program tcpstr19;
+
+// test conversions from and to rawbytestring
+// test that copy function returns the same def as argument
+
+{$APPTYPE CONSOLE}
+{$ifdef fpc}
+  {$MODE DELPHIUNICODE}
+{$endif}
+
+uses
+  SysUtils;
+var
+  S: AnsiString;
+  R: RawByteString;
+begin
+  S := UTF8Encode('Test');
+  if StringCodePage(S) <> CP_UTF8 then
+    halt(1);
+  S := Copy('Test', 1, 2);
+  if StringCodePage(S) <> DefaultSystemCodePage then
+    halt(2);
+  if StringCodePage(Copy(UTF8Encode('Test'), 1, 2)) <> CP_UTF8 then
+    halt(3);
+  R := 'Test';
+  if StringCodePage(R) <> DefaultSystemCodePage then
+    halt(4);
+end.