Recent

Author Topic: [SOLVED] TStringHelper.LastIndexOf() with large String returns -1  (Read 593 times)

trexet

  • New Member
  • *
  • Posts: 35
Hi. More than 10 years passed since last time I tried creating GUI stuff with Lazarus as a kid...

I'm trying to deserialize a pretty large (>1000 chars) string, but LastIndexOf() seems to be joking on me:

Code: Pascal  [Select][+][-]
  1. var FieldStart, FieldEnd: SizeInt;
  2. begin
  3.     FieldName := 'Timetable'; // actually it's a function argument
  4.     FieldStart := S.IndexOf(FieldName + '=');
  5.     if FieldStart < 0 then exit;
  6.     FieldEnd := S.LastIndexOf(';', FieldStart);
  7.     if FieldEnd < 0 then exit;
  8.  

And the string S:
Quote
Timetable={Stations={EList=station;HTFL={HashTable={{ID=3;EType=station;Name="Ст1";Directions={EList=direction;HTFL={HashTable={{ID=1;EType=direction;Name="NoDirection";Stations={};};};FreeList={};};};Routes={1};};{ID=2;EType=station;Name="Ст2";Directions={EList=direction;HTFL={HashTable={{ID=1;EType=direction;Name="NoDirection";Stations={};};};FreeList={};};};Routes={1};};{ID=1;EType=station;Name="Ст3";Directions={EList=direction;HTFL={HashTable={{ID=1;EType=direction;Name="NoDirection";Stations={};};};FreeList={};};};Routes={1};};{ID=4;EType=station;Name="Ст4";Directions={EList=direction;HTFL={HashTable={{ID=1;EType=direction;Name="NoDirection";Stations={};};};FreeList={};};};Routes={1};};};FreeList={};};};Routes={EList=route;HTFL={HashTable={{ID=1;EType=route;Name="П1";Stations={1,2,3,4};Trains={EList=train;HTFL={HashTable={{ID=3;EType=train;Name="1033";Schedule={{Station=1;NonStop=False;Arrival=62;Stop=184;Dep=306;Trav=428;};};};{ID=2;EType=train;Name="1032";Schedule={};};{ID=1;EType=train;Name="1031";Schedule={};};{ID=4;EType=train;Name="1034";Schedule={};};};FreeList={};};};};};FreeList={};};};};

FieldStart is set correctly to 0, but for some reason FieldEnd is -1. The char I'm looking for is literally the last one.

FPC 3.2.2, Lazarus 3.4
« Last Edit: June 13, 2024, 04:53:51 am by trexet »

trexet

  • New Member
  • *
  • Posts: 35
Re: TStringHelper.LastIndexOf() with large String returns -1
« Reply #1 on: June 13, 2024, 04:53:01 am »
Should've read the docs with more attention:

Quote
The search can be refined by specifying a (zero based) index StartIndex. When specified, the search will start at the given character and proceeds towards the beginning of the string.

For some reason I supposed that StartIndex works the same as for IndexOf().

ASerge

  • Hero Member
  • *****
  • Posts: 2289
Re: TStringHelper.LastIndexOf() with large String returns -1
« Reply #2 on: June 13, 2024, 06:49:34 am »
For some reason...
Maybe the Delphi documentation is confusing:
"StartIndex specifies the offset in this 0-based string where the LastIndexOf method begins the search, and Count specifies the end offset where the search ends".

As far as I know, in Delphi, as in other languages, the search is performed from the end (or StartIndex) to the beginning.

 

TinyPortal © 2005-2018