Browse Source

Publish System.MemPos.

Rika Ichinose 6 months ago
parent
commit
f7f8c9a615
3 changed files with 28 additions and 28 deletions
  1. 2 28
      rtl/inc/astrings.inc
  2. 25 0
      rtl/inc/generic.inc
  3. 1 0
      rtl/inc/systemh.inc

+ 2 - 28
rtl/inc/astrings.inc

@@ -832,32 +832,6 @@ end;
 {$endif FPC_HAS_ANSISTR_COPY}
 
 
-{$if not defined(FPC_HAS_POS_SHORTSTR_ANSISTR)
-  or not defined(FPC_HAS_POS_ANSISTR_ANSISTR)}
-function Find(needle : PByte; nNeedle : SizeUint; haystack : PByte; nHaystack : SizeUint): SizeInt;
-var
-  p,d,pmaxplus1,iNeedle : SizeUint;
-begin
-  p:=0;
-  if SizeUint(nNeedle-1)<nHaystack then { (nNeedle > 0) and (nNeedle <= nHaystack) }
-    begin
-      pmaxplus1:=nHaystack-nNeedle+1;
-      iNeedle:=0;
-      repeat
-        if iNeedle=0 then
-          iNeedle:=nNeedle;
-        dec(iNeedle);
-        d:=IndexByte(haystack[p+iNeedle],pmaxplus1-p,needle[iNeedle])+1;
-        inc(p,d);
-      until (d=0) or (CompareByte(haystack[p-1],needle^,nNeedle)=0);
-      if d=0 then
-        p:=0;
-    end;
-  result:=SizeInt(p)-1;
-end;
-{$endif need Find}
-
-
 {$ifndef FPC_HAS_POS_SHORTSTR_ANSISTR}
 {$define FPC_HAS_POS_SHORTSTR_ANSISTR}
 Function Pos(Const Substr : ShortString; Const Source : RawByteString; Offset : Sizeint = 1) : SizeInt;
@@ -868,7 +842,7 @@ begin
   result:=0;
   dec(Offset);
   if SizeUint(Offset)<SizeUint(nsource) then { (Offset >= 0) and (Offset < nsource) }
-    result:=Find(@Substr[1],length(Substr),pointer(Source)+Offset,nsource-Offset)+1;
+    result:=MemPos(PByte(@Substr[1]),length(Substr),PByte(Source)+Offset,nsource-Offset)+1;
   if result>0 then
     inc(result,Offset);
 end;
@@ -885,7 +859,7 @@ begin
   result:=0;
   dec(Offset);
   if SizeUint(Offset)<SizeUint(nsource) then { (Offset >= 0) and (Offset < nsource) }
-    result:=Find(pointer(Substr),length(Substr),pointer(Source)+Offset,nsource-Offset)+1;
+    result:=MemPos(PByte(Substr),length(Substr),PByte(Source)+Offset,nsource-Offset)+1;
   if result>0 then
     inc(result,Offset);
 end;

+ 25 - 0
rtl/inc/generic.inc

@@ -671,6 +671,31 @@ end;
 {$endif not FPC_SYSTEM_HAS_COMPARECHAR0}
 
 
+{$ifndef FPC_SYSTEM_HAS_MEMPOS_PBYTE}
+function MemPos(needle:PByte;nNeedle:SizeUint;haystack:PByte;nHaystack:SizeUint):SizeInt;
+var
+  p,d,pmaxplus1,iNeedle : SizeUint;
+begin
+  p:=0;
+  if SizeUint(nNeedle-1)<nHaystack then { (nNeedle > 0) and (nNeedle <= nHaystack) }
+    begin
+      pmaxplus1:=nHaystack-nNeedle+1;
+      iNeedle:=0;
+      repeat
+        if iNeedle=0 then
+          iNeedle:=nNeedle;
+        dec(iNeedle);
+        d:=IndexByte(haystack[p+iNeedle],pmaxplus1-p,needle[iNeedle])+1;
+        inc(p,d);
+      until (d=0) or (CompareByte(haystack[p-1],needle^,nNeedle)=0);
+      if d=0 then
+        p:=0;
+    end;
+  result:=SizeInt(p)-1;
+end;
+{$endif not FPC_SYSTEM_HAS_MEMPOS_PBYTE}
+
+
 {****************************************************************************
                               Object Helpers
 ****************************************************************************}

+ 1 - 0
rtl/inc/systemh.inc

@@ -923,6 +923,7 @@ function  CompareDWord(const buf1,buf2;len:SizeInt):SizeInt; {$if defined(cpui38
 procedure MoveChar0(const buf1;var buf2;len:SizeInt);
 function  IndexChar0(const buf;len:SizeInt;b:Ansichar):SizeInt;
 function  CompareChar0(const buf1,buf2;len:SizeInt):SizeInt;
+function  MemPos(needle:PByte;nNeedle:SizeUint;haystack:PByte;nHaystack:SizeUint):SizeInt;
 procedure Prefetch(const mem);[internproc:fpc_in_prefetch_var];
 
 procedure ReadBarrier; {$ifdef SYSTEMINLINE}inline;{$endif}