Procházet zdrojové kódy

* Apply patch to improve pos() for widestring (mimicking unicodestring), bug ID #29790

git-svn-id: trunk@33210 -
michael před 9 roky
rodič
revize
463c9f6e42
1 změnil soubory, kde provedl 31 přidání a 30 odebrání
  1. 31 30
      rtl/inc/wstrings.inc

+ 31 - 30
rtl/inc/wstrings.inc

@@ -584,10 +584,10 @@ var
   pc : pwidechar;
 begin
   Pos:=0;
-  if (Length(SubStr)>0) and (Offset>0) and (Offset<Length(Source)) then
+  if (Length(SubStr)>0) and (Offset>0) and (Offset<=Length(Source)) then
    begin
-     MaxLen:=Length(source)-Length(SubStr);
-     i:=Offset-1;
+     MaxLen:=Length(source)-Length(SubStr)-(Offset-1);
+     i:=0;
      pc:=@source[Offset];
      while (i<=MaxLen) do
       begin
@@ -595,7 +595,7 @@ begin
         if (SubStr[1]=pc^) and
            (CompareWord(Substr[1],pc^,Length(SubStr))=0) then
          begin
-           Pos:=i;
+           Pos:=Offset+i-1;
            exit;
          end;
         inc(pc);
@@ -610,18 +610,20 @@ var
   i: SizeInt;
   pc : pwidechar;
 begin
-  pos:=0;
-  if (Offset<1) or (Offset>Length(s)) then exit;
-  pc:=@s[Offset];
-  for i:=Offset to length(s) do
+  if (Offset>0) and (Offset<=length(s)) then
    begin
-     if pc^=c then
-      begin
-        pos:=i;
-        exit;
-      end;
-     inc(pc);
+     pc:=@s[OffSet];
+     for i:=OffSet to length(s) do
+       begin
+         if pc^=c then
+           begin
+             pos:=i;
+             exit;
+           end;
+         inc(pc);
+       end;
    end;
+  pos:=0;
 end;
 
 { DO NOT inline these! Inlining a managed typecast creates an implicit try..finally
@@ -653,27 +655,26 @@ Function Pos (const c : WideString; Const s : RawByteString;Offset : SizeInt = 1
 { pos(c: char; const s: shortstring) also exists, so otherwise   }
 { using pos(char,pchar) will always call the shortstring version }
 { (exact match for first argument), also with $h+ (JM)           }
-Function Pos (c : Char; Const s : WideString;Offset : SizeInt = 1) : SizeInt;
+Function Pos (c : Char; Const s : WideString; Offset : SizeInt = 1) : SizeInt;
 var
   i: SizeInt;
   wc : widechar;
   pc : pwidechar;
 begin
-  Pos:=0;
-  if (Offset<1) or (OffSet>Length(S)) then 
-    exit;
-  wc:=c;
-  
-  pc:=@s[offset];
-  for i:=Offset to length(s) do
-   begin
-     if pc^=wc then
-      begin
-        pos:=i;
-        exit;
-      end;
-     inc(pc);
-   end;
+  if (Offset>0) and (Offset<=Length(S)) then
+    begin
+      wc:=c;
+      pc:=@s[OffSet];
+      for i:=OffSet to length(s) do
+       begin
+         if pc^=wc then
+          begin
+            pos:=i;
+            exit;
+          end;
+         inc(pc);
+       end;
+    end;
   pos:=0;
 end;