Recent

Author Topic: [SOLVED]string.Split() function  (Read 5181 times)

avk

  • Hero Member
  • *****
  • Posts: 826
[SOLVED]string.Split() function
« on: March 18, 2020, 07:03:57 am »
During entertainment last weekend, an interesting feature of the string.Split () function in rtl 3.3.1 was discovered.
For example:
Code: Pascal  [Select][+][-]
  1. procedure test;
  2. var
  3.   a: array of string;
  4.   I: Integer;
  5.   s: string;
  6. begin
  7.   a := '.a,b.c!'.Split(['.', ',', '!'], TStringSplitOptions.ExcludeEmpty);
  8.   for I := 0 to High(a) do
  9.     begin
  10.       if a[I] <> '' then
  11.         s := a[I]
  12.       else
  13.         s := 'empty';
  14.       WriteLn(Format('a[%d] = %s', [I, s]));
  15.     end;
  16. end;
  17.  
prints
Code: Text  [Select][+][-]
  1. a[0] = a
  2. a[1] = b
  3. a[2] = c
  4. a[3] = empty
  5.  
Is this really a feature or just a bug?
« Last Edit: March 26, 2020, 08:08:06 am by avk »

Thaddy

  • Hero Member
  • *****
  • Posts: 19247
  • Glad to be alive.
Re: string.Split() function
« Reply #1 on: March 18, 2020, 08:02:04 am »
Looks like a bug to me.
objects are fine constructs. You can even initialize them with constructors.

440bx

  • Hero Member
  • *****
  • Posts: 6528
Re: string.Split() function
« Reply #2 on: March 18, 2020, 08:03:07 am »
Just an opinion but, the presence of a delimiter, in this case "!" can be taken as implying that something follows it and, if it doesn't then "empty" is correct.

I guess it's a feature.

As you can see, opinions vary. ;)
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

bytebites

  • Hero Member
  • *****
  • Posts: 787
Re: string.Split() function
« Reply #3 on: March 18, 2020, 08:30:31 am »
Patch to fix it

Index: rtl/objpas/sysutils/syshelp.inc
===================================================================
--- rtl/objpas/sysutils/syshelp.inc   (revision 44086)
+++ rtl/objpas/sysutils/syshelp.inc   (working copy)
@@ -1225,9 +1225,12 @@
     begin
     T:=SubString(LastSep);
 //    Writeln('Examining >',T,'< at pos,',LastSep,' till pos ',Sep);
-    MaybeGrow(Len);
-    Result[Len]:=T;
-    Inc(Len);
+    If (T<>'') or (not (TStringSplitOptions.ExcludeEmpty=Options)) then
+      begin
+      MaybeGrow(Len);
+      Result[Len]:=T;
+      Inc(Len);
+      end;
     end;
   SetLength(Result,Len);
 end;

Ally

  • Jr. Member
  • **
  • Posts: 82
Re: string.Split() function
« Reply #4 on: March 18, 2020, 09:10:07 am »

avk

  • Hero Member
  • *****
  • Posts: 826
Re: string.Split() function
« Reply #5 on: March 19, 2020, 05:12:13 am »
Thank you all.
Looks like a bug to me.

For me as well.
By passing the TStringSplitOptions.ExcludeEmpty parameter I explicitly indicate that I don't need empty strings in the result array.

440bx

  • Hero Member
  • *****
  • Posts: 6528
Re: string.Split() function
« Reply #6 on: March 19, 2020, 06:17:24 am »
I explicitly indicate that I don't need empty strings in the result array.
For some reason the "ExcludeEmpty" didn't register in my mind when I read the code.  Given that explicit request, I also think it's a bug.
FPC v3.2.2 and Lazarus v4.0rc3 on Windows 7 SP1 64bit.

Thaddy

  • Hero Member
  • *****
  • Posts: 19247
  • Glad to be alive.
Re: string.Split() function
« Reply #7 on: March 19, 2020, 10:46:49 am »
I explicitly indicate that I don't need empty strings in the result array.
For some reason the "ExcludeEmpty" didn't register in my mind when I read the code.  Given that explicit request, I also think it's a bug.
I guess it was an oversight while fixing a related bug?
objects are fine constructs. You can even initialize them with constructors.

avk

  • Hero Member
  • *****
  • Posts: 826
Re: string.Split() function
« Reply #8 on: March 20, 2020, 05:33:26 am »
It seems fixed.

 

TinyPortal © 2005-2018