فهرست منبع

+ support copy with two parameters, resolves #22964

git-svn-id: trunk@22575 -
florian 12 سال پیش
والد
کامیت
9a5e64442f
3فایلهای تغییر یافته به همراه77 افزوده شده و 12 حذف شده
  1. 1 0
      .gitattributes
  2. 24 12
      compiler/ninl.pas
  3. 52 0
      tests/webtbs/tw22964.pp

+ 1 - 0
.gitattributes

@@ -12912,6 +12912,7 @@ tests/webtbs/tw2289.pp svneol=native#text/plain
 tests/webtbs/tw2291.pp svneol=native#text/plain
 tests/webtbs/tw2294.pp svneol=native#text/plain
 tests/webtbs/tw2296.pp svneol=native#text/plain
+tests/webtbs/tw22964.pp svneol=native#text/pascal
 tests/webtbs/tw22992.pp svneol=native#text/pascal
 tests/webtbs/tw2300.pp svneol=native#text/plain
 tests/webtbs/tw2305.pp svneol=native#text/plain

+ 24 - 12
compiler/ninl.pas

@@ -1628,7 +1628,7 @@ implementation
          if is_dynamic_array(paradef) then
           begin
             { Only allow 1 or 3 arguments }
-            if (counter<>1) and (counter<>3) then
+            if not(counter in [1..3]) then
              begin
                CGMessage1(parser_e_wrong_parameter_size,'Copy');
                exit;
@@ -3741,6 +3741,14 @@ implementation
             ppn:=tcallparanode(ppn.right);
           end;
         paradef:=ppn.left.resultdef;
+
+        { fill up third parameter }
+        if counter=2 then
+          begin
+            paras:=ccallparanode.create(cordconstnode.create(torddef(sinttype).high,sinttype,false),paras);
+            counter:=3;
+          end;
+
         if is_ansistring(resultdef) then
           { keep the specific kind of ansistringdef as result }
           result:=ccallnode.createinternres('fpc_ansistr_copy',paras,resultdef)
@@ -3755,17 +3763,21 @@ implementation
         else if is_dynamic_array(resultdef) then
           begin
             { create statements with call }
-            if (counter=3) then
-             begin
-               highppn:=tcallparanode(paras).left.getcopy;
-               lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
-             end
-            else
-             begin
-               { copy the whole array using [0..high(sizeint)] range }
-               highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
-               lowppn:=cordconstnode.create(0,sinttype,false);
-             end;
+            case counter of
+              1:
+                begin
+                  { copy the whole array using [0..high(sizeint)] range }
+                  highppn:=cordconstnode.create(torddef(sinttype).high,sinttype,false);
+                  lowppn:=cordconstnode.create(0,sinttype,false);
+                end;
+              3:
+                begin
+                  highppn:=tcallparanode(paras).left.getcopy;
+                  lowppn:=tcallparanode(tcallparanode(paras).right).left.getcopy;
+                end;
+              else
+                internalerror(2012100701);
+            end;
 
             { create call to fpc_dynarray_copy }
             npara:=ccallparanode.create(highppn,

+ 52 - 0
tests/webtbs/tw22964.pp

@@ -0,0 +1,52 @@
+
+{$ifdef fpc}
+  {$mode delphi}
+{$else}
+  {$apptype console}
+{$endif}
+
+uses types;
+
+
+function CopyTest(const S: string): string;
+begin
+  writeln(Copy(S, 1, 5));
+  writeln(Copy(S, 2));
+  // writeln(Copy(S));
+end;
+
+function arrTest(const S: TIntegerDynArray): string;
+var x : TIntegerDynArray;
+    i : integer;
+begin
+  x:=Copy(S, 1, 5);
+  write('1:');
+  for i:=0 to length(x)-1 do
+    write(' ',x[i]:5);
+  writeln;
+  x:=Copy(S, 2);
+  write('2:');
+  for i:=0 to length(x)-1 do
+    write(' ',x[i]:5);
+  writeln;
+end;
+
+var testarr : TIntegerDynArray;
+    i:integer;
+begin
+ setlength(testarr,10);
+ for i:=0 to 9 do
+   testarr[i]:=i;     // element values 0 based
+ copytest('1234567'); // element values 1 based
+ arrtest(testarr);
+end.
+
+{
+
+Delphi XE output:
+
+12345
+234567
+1:     1     2     3     4     5
+2:     2     3     4     5     6     7     8     9
+}