[fpc-pascal]feature request: enhanced "pos"

Jonas Maebe jonas.maebe at elis.ugent.be
Sat Mar 6 19:38:06 CET 2004


On 6 mrt 2004, at 07:56, Jeff Pohlmeyer wrote:

> function PosFrom( const needle:AnsiString; const haystack:AnsiString;
>                   StartPt:LongInt):LongInt;
> var
>   p:pChar;
> begin
>   Result:=0;
>   if (StartPt <= Length(haystack))
>   then begin
>     p:=StrPos(pChar(@haystack[StartPt]), pChar(needle));
>     if ( p <> nil ) then Result:= ( p - @haystack[1] ) + 1;
>   end
> end;

Note that this indeed will only work with ansistrings, as shortstrings 
are not null-terminated (which strpos expects). Of course, you can pass 
shortstrings to this routine and they will be converted to ansistrings 
on the fly, but that will more than negate any possible speed gains you 
could expect over using the plain "pos" function.


Jonas





More information about the fpc-pascal mailing list