Browse Source

o patch and test by Andrey Gusev:
* properly replace first occurence of a pattern, resolves #10141

git-svn-id: trunk@9192 -

florian 18 years ago
parent
commit
05d744deb1
2 changed files with 22 additions and 2 deletions
  1. 5 2
      packages/base/regexpr/regexpr.pp
  2. 17 0
      packages/base/regexpr/testreg1.pp

+ 5 - 2
packages/base/regexpr/regexpr.pp

@@ -1,3 +1,5 @@
+{ $DEFINE DEBUG}
+
 {
 {
     This unit implements basic regular expression support
     This unit implements basic regular expression support
 
 
@@ -1122,7 +1124,7 @@ unit regexpr;
       oldlength : PtrInt;
       oldlength : PtrInt;
     begin
     begin
       pos:=pchar(src);
       pos:=pchar(src);
-      lastpos:=pos;
+      lastpos:=nil;
       first:=true;
       first:=true;
       Result:=0;
       Result:=0;
       { estimate some length }
       { estimate some length }
@@ -1130,8 +1132,9 @@ unit regexpr;
       while RegExprPos(RegExprEngine,pos,index,len) do
       while RegExprPos(RegExprEngine,pos,index,len) do
         begin
         begin
           inc(pos,index);
           inc(pos,index);
-          if pos>lastpos then
+          if (lastpos = nil) or (pos>lastpos) then
             begin
             begin
+              if lastpos = nil then lastpos := pchar(src);
               { copy skipped part }
               { copy skipped part }
 
 
               { because we cheat with SetLength a SetLength(...,0) isn't what we want
               { because we cheat with SetLength a SetLength(...,0) isn't what we want

+ 17 - 0
packages/base/regexpr/testreg1.pp

@@ -856,5 +856,22 @@ begin
     (Dest<>'asdasdasdasd') then
     (Dest<>'asdasdasdasd') then
     do_error(2009);
     do_error(2009);
   DestroyregExprEngine(r);
   DestroyregExprEngine(r);
+
+  initok:=GenerateRegExprEngine('fa',[],r);
+  if not initok then
+    do_error(2000);
+  if (RegExprReplaceAll(r,'fasdfasdf','',Dest)<>2) or
+    (Dest<>'sdsdf') then
+    do_error(2010);
+  DestroyregExprEngine(r);
+
+  initok:=GenerateRegExprEngine('fa',[],r);
+  if not initok then
+    do_error(2011);
+  if (RegExprReplaceAll(r,'fasdfafaasdasdfafaasd','',Dest)<>5) or
+    (Dest<>'sdasdasdasd') then
+    do_error(2012);
+  DestroyregExprEngine(r);
+
    writeln('*** Testing unit regexpr was successful ***');
    writeln('*** Testing unit regexpr was successful ***');
 end.
 end.