Recent

Author Topic: Weird behaviour of LastDelimiter/RightStr/LeftStr  (Read 5775 times)

kormoran

  • New Member
  • *
  • Posts: 31
Weird behaviour of LastDelimiter/RightStr/LeftStr
« on: April 12, 2011, 12:49:15 am »
I'm writing a function that split the PATHEXT environment variable in many substrings.
Since PATHEXT it's a semicolon-separated values string, it uses LastDelimiter(';',pathext_string) to find the split point. But it behaves really weird, and I can't figure out what's wrong in my code. The code fragment is the following... (hope it's clear enough)

Code: [Select]
procedure my_splitpath(Path: ansistring);
var
  rest: ansistring;
  Idx: integer;
begin
rest:=Path;
Idx:= LastDelimiter(';', rest);
MMVarValueLarge.Lines.Append(RightStr(rest, Idx));
rest:=LeftStr(rest, Idx -1);
while Idx<>0 do
  begin
  Idx:= LastDelimiter(';', rest);
  MMVarValueLarge.Lines.Append(RightStr(rest, Idx));
  rest:=LeftStr(rest, Idx -1);
  end;
end;

The output obtained is this:

// .COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC starting string!

.COM;.EXE;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;
;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH;.MSC
;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF;.WSH
;.BAT;.CMD;.VBS;.VBE;.JS;.JSE;.WSF
;.BAT;.CMD;.VBS;.VBE;.JS;.JSE
E;.BAT;.CMD;.VBS;.VBE;.JS
;.BAT;.CMD;.VBS;.VBE
;.BAT;.CMD;.VBS
;.BAT;.CMD
;.BAT

I tried to swap RightStr() and LeftStr() calls: no effect. Same output.
I can't figure out what's going wrong and why.
Maybe a bug in the FPC string functions? I'm working under win7 x64 professional. Have lazarus 0.9.30.

Or I'm simply, cluelessly, doing some big mistake?   :(

thanks...

Blaazen

  • Hero Member
  • *****
  • Posts: 3241
  • POKE 54296,15
    • Eye-Candy Controls
Re: Weird behaviour of LastDelimiter/RightStr/LeftStr
« Reply #1 on: April 12, 2011, 01:21:26 am »
Adding (2 times)
Code: [Select]
length(rest)gives good results.
Code: [Select]
procedure my_splitpath(Path: ansistring);
var
  rest: ansistring;
  Idx: integer;
begin
rest:=Path;
Idx:= LastDelimiter(';', rest);
MMVarValueLarge.Lines.Append(RightStr(rest, length(rest)-Idx));  //here
rest:=LeftStr(rest, Idx -1);
while Idx<>0 do
  begin
  Idx:= LastDelimiter(';', rest);
  MMVarValueLarge.Lines.Append(RightStr(rest, length(rest)-Idx));  //and here
  rest:=LeftStr(rest, Idx -1);
  end;
end;
Lazarus 2.3.0 (rev main-2_3-2863...) FPC 3.3.1 x86_64-linux-qt Chakra, Qt 4.8.7/5.13.2, Plasma 5.17.3
Lazarus 1.8.2 r57369 FPC 3.0.4 i386-win32-win32/win64 Wine 3.21

Try Eye-Candy Controls: https://sourceforge.net/projects/eccontrols/files/

kormoran

  • New Member
  • *
  • Posts: 31
Re: Weird behaviour of LastDelimiter/RightStr/LeftStr
« Reply #2 on: April 12, 2011, 01:37:53 am »
Adding (2 times)
Code: [Select]
length(rest)gives good results.

After some minutes of heavy thinking I now understand that RightStr counts from RIGHT and LeftStr counts from LEFT...  :-[
thanks for the tip ::)

(edit) we can also mark this thread as "solved"...
« Last Edit: April 12, 2011, 01:44:07 am by kormoran »

 

TinyPortal © 2005-2018