Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2004-03

feature request: enhanced "pos"

8 messages · 6 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

David Emerson

Dunno if this is the right place to post such a message, but here goes...

I would love to see a "pos" function that takes an additional parameter, being the position in the string where it should START scanning. e.g.:

my_str = 'Hello, everybody... Hello, world!';
//        123456789012345678901
x := pos ('Hello', my_str, 6);

So it starts scanning at position 6 (after the first Hello) and finds the second Hello at position 21; returns 21. (not 21-6)

Passing it a starting position 2 (instead of 6) would also skip the first Hello, and return the same 21.

(( Of course, passing it a starting position greater than the length(str) - length(target) should return 0; also it should return 0 if the target is not found between startpos and the end of the string. ))

This would save so much processing time spent doing delete and copy!

Cheers,
David

Reply by email

Jeff Pohlmeyer
Show quoted text 17 lines
Dunno if this is the right place to post such a message, but here goes... I would love to see a "pos" function that takes an additional parameter, being the position in the string where it should START scanning. e.g.: my_str = 'Hello, everybody... Hello, world!'; x := pos ('Hello', my_str, 6); So it starts scanning at position 6 (after the first Hello) and finds the second Hello at position 21; returns 21. (not 21-6) Passing it a starting position 2 (instead of 6) would also skip the first Hello, and return the same 21. (( Of course, passing it a starting position greater than the length(str) - length(target) should return 0; also it should return 0 if the target is not found between startpos and the end of the string. ))

Dunno if this is the right place to post such an answer, but here goes...

program postest;
{$H+}{$MODE OBJFPC}
uses strings;
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;
// Testing ...
var
  subject, pattern : string;
  i:integer;
begin
  subject := 'Hello, everybody... Hello, world!';
  pattern := 'Hello';
  for i:=1 to length(subject) do
     WriteLn('Position from ', i, ' is ',  PosFrom(pattern, subject, i));
end.

__________________________________
Do you Yahoo!?
Yahoo! Search - Find what you�re looking for faster http://search.yahoo.com

Reply by email

Thomas Schatzl
to David Emerson: Dunno if this is the right place to post such a message, but here goes... I would love to…

Hello,

Dunno if this is the right place to post such a message, but here goes...

I think you're completely right...

I would love to see a "pos" function that takes an additional parameter,

being the position in the string where it >should START scanning. e.g.:

Show quoted text 6 lines
my_str = 'Hello, everybody... Hello, world!'; // 123456789012345678901 x := pos ('Hello', my_str, 6);

PosEx() from strutils?

Regards,
Thomas

Reply by email

Jonas Maebe
to Jeff Pohlmeyer: Dunno if this is the right place to post such an answer, but here goes... program postest;
Jeff Pohlmeyer wrote 12 lines
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

Reply by email

Olle Raab
to Thomas Schatzl: I think you're completely right... being the position in the string where it >should…

04-03-06 11.25, skrev Thomas Schatzl följande:

Show quoted text 10 lines
> I would love to see a "pos" function that takes an additional parameter, being the position in the string where it >should START scanning. e.g.: > > my_str = 'Hello, everybody... Hello, world!'; > > // 123456789012345678901 > > x := pos ('Hello', my_str, 6); PosEx() from strutils?

Cant find it in strutils..

Olle

Reply by email

Thomas Schatzl
to Olle Raab

Hello,

Show quoted text 5 lines
>> x := pos ('Hello', my_str, 6); > > PosEx() from strutils? Cant find it in strutils..

Just checked the sources again, at least the 1.9.x branch contains a PosEx method, line 93ff, from today's sources =)

Regards,
Thomas

Reply by email

Marco van de Voort
to Thomas Schatzl
Show quoted text 8 lines
>>> x := pos ('Hello', my_str, 6); >> >> PosEx() from strutils? > >Cant find it in strutils.. Just checked the sources again, at least the 1.9.x branch contains a PosEx method, line 93ff, from today's sources =)

I implemented some files last weeks in the *utils category.

Not everything is fully tested, so please beware (convutils also exists since yesterday, but maybe isn't added to all makefiles yet)

Reply by email

Olle Raab
to Thomas Schatzl: Just checked the sources again, at least the 1.9.x branch contains a PosEx method, line…

04-03-21 16.56, skrev Thomas Schatzl följande:

Show quoted text 10 lines
Hello, >>> x := pos ('Hello', my_str, 6); >> >> PosEx() from strutils? > > Cant find it in strutils.. Just checked the sources again, at least the 1.9.x branch contains a PosEx method, line 93ff, from today's sources =)

Sorry I bothered you, it is there, I did use home/end instead of pgup/pgdowm and did only se the top/bottom of the file. I have a laptop and these keys is only available as key kombinations, thats why i interchanged the keys.

Anyway, thanks.

Olle

Reply by email