Recent

Author Topic: DelimitedText in Tstrings does not work correctly with StrictDelimited := True  (Read 4660 times)

jamie

  • Hero Member
  • *****
  • Posts: 6077
Please examine this example.. , done with Lazarus 2.0.6 release.
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button5Click(Sender: TObject);
  2. Var
  3.   S :String;
  4.   L:TStringList;
  5. begin
  6.   L := TStringList.Create;
  7.   L.StrictDelimiter :=True;
  8.   S:= '"1 One", "2 Two", "3 There"';
  9.   L.DelimitedText := S;
  10.   Memo1.Lines.Text := L.Text; {experiment here using L.DelimetedText and L.Text}
  11.   L.Free;
  12. end;                        
  13.  
--Results-
1 One
 "2 Two"
 "3 There"
---
If you use L.DelimetedText it will add a " " around the string, I guess that is how it suppose to work.

But using just TEXt, you see that it has quotes but this isn't coming from getting the final text this is from when it reads the initial text using StrictDelimited, it stores them with those quotes.
From what I've read from the Delphi help, the quotes are to be used as guides to resolve the text within to storage into the StringList. They aren't suppose to actually store the quotes too, unless you have a double quote there.

 The strictDelimited is suppose to ignore spaces as separators but it looks like its doing more than that.

 I looked at the code that handles that, it leaves me with no words to describe it.

 I think I can come up with a code frag that is efficient and works as subscribed. 
 
 Unless this has already been fixed which I really don't think it has, it needs to be..
The only true wisdom is knowing you know nothing

eljo

  • Sr. Member
  • ****
  • Posts: 468
1)Double quotes as a string encapsulator is a feature of the CSV specifications not the stringlist delimeted breaker.
2) Can you provide links to the documentation you are talking about? I'm curious to read it.

eljo

  • Sr. Member
  • ****
  • Posts: 468
1)Double quotes as a string encapsulator is a feature of the CSV specifications not the stringlist delimeted breaker.
2) Can you provide links to the documentation you are talking about? I'm curious to read it.

Its broke for sure, I've reference some old Delphi Docs and this one..
http://docs.embarcadero.com/products/rad_studio/delphiAndcpp2009/HelpUpdate2/EN/html/delphivclwin32/Classes_TStrings_DelimitedText.html

 The text is not suppose to store any quotes when resolving a limited string unless they are side by side like "" . This works fine if you turn off the strictDelimited, which is only about spaces..
that's for single quotes <'>not double quotes <">. single quotes are string enclosers and two single quotes side by side is considered something like a escape pair for a enclosuded single quote. Double quotes are string enclosers in basic and some sql engines.

EDIT:
  Sorry I've been a bit to hasty answering, I took a look on the documentation and there is the quotechar property that might be what you are talking about. I need to take a close look when I have a clear head to comment any farther.
« Last Edit: March 31, 2020, 01:02:01 am by eljo »

egsuh

  • Hero Member
  • *****
  • Posts: 1266
I believe you must have some reason to use DelimitedText instead of CommaText.
I strongly recommend you to use CommaText if possible. I have experimented TStrings.Text, TStrings.CommaText and TStrings.DelimitedText in many times, but the final conclusion is always that CommaText is the most reliable and safest one, and over the  Internet as well.

mangakissa

  • Hero Member
  • *****
  • Posts: 1131
this is your problem:
Code: Pascal  [Select][+][-]
  1. S:= '"1 One", "2 Two", "3 There"';
  2.  
TStringlist can work with double quotes, but it look at the first character of the splitted string. It's a space and not a double quote. That's why it shows your result.

This is correct:
Code: Pascal  [Select][+][-]
  1. S:= '"1 One","2 Two","3 There"';
  2.  
1 One
2 Two
3 There

If the string comes from a file or another source, try using reg expressions.
Lazarus 2.06 (64b) / FPC 3.0.4 / Windows 10
stucked on Delphi 10.3.1

FTurtle

  • Sr. Member
  • ****
  • Posts: 292
The Space being in front of the quote is suppose to be ignored and and it is not suppose to disrupt the Quote parsing operation, which it does.

Just do not play with additional spaces and do not find anventures for yourself. And then everything will be ok.

the final conclusion is always that CommaText is the most reliable and safest one, and over the  Internet as well.

As for me, "the most reliable and safest" is using of DelimitedText with setting own Delimiter (usually '|'):

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Button2Click(Sender: TObject);
  2. Var
  3.   S :String = '1 One|2 Two|3 Three';
  4.   L:TStringList;
  5. begin
  6.   L := TStringList.Create;
  7.   L.StrictDelimiter :=True;
  8.   L.Delimiter:='|';
  9.   L.DelimitedText := S;
  10.   Memo1.Lines.Text := L.Text; {experiment here using L.DelimetedText and L.Text}
  11.   Label1.Caption:=L.Text;
  12.   L.Free;
  13. end;
  14.  
« Last Edit: March 31, 2020, 11:40:34 pm by FTurtle »

ASerge

  • Hero Member
  • *****
  • Posts: 2212
  This makes it compiler efficient and faster due to the use of CONST string I believe..
Maybe it's better to test it? There is at least one error - uninitialized SC is used in the check.

eljo

  • Sr. Member
  • ****
  • Posts: 468
I disagree with your conclusion. Strict delimeter means exactly that, only address the delimeter and keep the individual parts intact. That does not mean trim the parts if the have string enclosure characters in them nor any other type of processing that you think should be done. That is left for the end user to do after the string break in to the parts.

jamie

  • Hero Member
  • *****
  • Posts: 6077
I guess you didn't fully read It. try again and maybe a littler harder this time.

There is no trimming of spaces in strict mode.

Don't you see what is happening here ? A monkey can see inserting a blank line isn't the answer.

its an error and works fine if you are not in strict mode..

I took all the specs from CSV files and it is obvious this is where the intent was suppose to be but it fails.

 Currently it's useless using strict mode because it inserts blank lines where it should not be doing it.

 That's fine, I'll use my own corrected version of the operation, others can use theirs too or live with the broken one and scratch their heads for the remainder of their buggy app to figure out why..

 I'll investigate how to remove this post altogether..
The only true wisdom is knowing you know nothing

eljo

  • Sr. Member
  • ****
  • Posts: 468
I guess you didn't fully read It. try again and maybe a littler harder this time.

There is no trimming of spaces in strict mode.

Don't you see what is happening here ? A monkey can see inserting a blank line isn't the answer.

its an error and works fine if you are not in strict mode..

I took all the specs from CSV files and it is obvious this is where the intent was suppose to be but it fails.

 Currently it's useless using strict mode because it inserts blank lines where it should not be doing it.

 That's fine, I'll use my own corrected version of the operation, others can use theirs too or live with the broken one and scratch their heads for the remainder of their buggy app to figure out why..

 I'll investigate how to remove this post altogether..
Ye sI can see what is happening. yes under certain conditions it might be considered as a bug but none of these conditions require the strict delimeter to be set. Once that property is set all your conditions fly out the window. So this monkey strongly believes that if strictdelimeter is set then no processing on the parts is to be run.
Sorry but for me its that obvious.

In any way this monkey is not part of the team and has no voice on what is included or not its just expressing its opinion. So open a bug report with the code attached and let the team decide what to do about  it.

Good luck.

 

TinyPortal © 2005-2018