瀏覽代碼

+ optimize <string>+'' and ''+<string>

git-svn-id: trunk@3522 -
florian 19 年之前
父節點
當前提交
454fb81c5b
共有 3 個文件被更改,包括 33 次插入0 次删除
  1. 1 0
      .gitattributes
  2. 14 0
      compiler/nadd.pas
  3. 18 0
      tests/test/taddstr1.pp

+ 1 - 0
.gitattributes

@@ -5619,6 +5619,7 @@ tests/test/opt/treg2.pp svneol=native#text/plain
 tests/test/opt/treg3.pp svneol=native#text/plain
 tests/test/opt/treg4.pp svneol=native#text/plain
 tests/test/tabstrcl.pp svneol=native#text/plain
+tests/test/taddstr1.pp svneol=native#text/plain
 tests/test/talign.pp svneol=native#text/plain
 tests/test/talign1.pp svneol=native#text/plain
 tests/test/talign2.pp svneol=native#text/plain

+ 14 - 0
compiler/nadd.pas

@@ -1561,6 +1561,20 @@ implementation
         case nodetype of
           addn:
             begin
+              if (left.nodetype=stringconstn) and (tstringconstnode(left).len=0) then
+                begin
+                  result:=right;
+                  left:=nil;
+                  right:=nil;
+                  exit;
+                end;
+              if (right.nodetype=stringconstn) and (tstringconstnode(right).len=0) then
+                begin
+                  result:=left;
+                  left:=nil;
+                  right:=nil;
+                  exit;
+                end;
               { create the call to the concat routine both strings as arguments }
               result := ccallnode.createintern('fpc_'+
                 tstringdef(resulttype.def).stringtypname+'_concat',

+ 18 - 0
tests/test/taddstr1.pp

@@ -0,0 +1,18 @@
+{ tests if '' is optimized properly in string concatenations }
+var
+  s1 : string;
+  s2 : string;
+
+begin
+  s1:='asdf';
+  if s1+''<>s1 then
+    halt(1);
+  s1:='asdf';
+  if ''+s1<>s1 then
+    halt(1);
+
+  if ''+s2+''+s1+''<>s2+s1 then
+    halt(1);
+
+  writeln('ok');
+end.