Recent

Author Topic: Iteration over string array literal truncates strings  (Read 1417 times)

jflipp

  • Newbie
  • Posts: 5
Iteration over string array literal truncates strings
« on: April 18, 2021, 09:38:43 pm »
Hi!

I am currently learning Lazarus / Free Pascal (I am familiar with other programming languages), and I ran into the following problem.

This code:

Code: Pascal  [Select][+][-]
  1. var
  2.   TestFileName: string;
  3.  
  4. begin
  5.   for TestFileName in ['abc', 'defghi'] do
  6.     WriteLn(TestFileName);

produces this output:

Code: Text  [Select][+][-]
  1. abc
  2. def

You see, the second string is truncated, it should be defghi.

Now I wonder ...
Is this a bug?
Is it a feature?
Is there an explanation for this behavior?
How do I write string array literals that work?

Thanks for your help.

Regards,
jflipp

ccrause

  • Hero Member
  • *****
  • Posts: 845
Re: Iteration over string array literal truncates strings
« Reply #1 on: April 18, 2021, 10:08:15 pm »
This is a known bug, see #32043, #37338 and #38732.

A possible work-around mentioned in #32043:
Code: Pascal  [Select][+][-]
  1. uses
  2.   SysUtils;
  3. type
  4.   TStringArray = specialize TArray<String>;
  5. var
  6.   s: String;
  7. begin
  8.   for s in TStringArray(['1char','2chars']) do
  9.     WriteLn(s);
  10. end.
  11.  

Blade

  • Full Member
  • ***
  • Posts: 177
Re: Iteration over string array literal truncates strings
« Reply #2 on: April 18, 2021, 11:24:44 pm »
For some fun, other ways (which might be closer to your original) that shouldn't have any truncation issues.

Example A
Code: Pascal  [Select][+][-]
  1. var
  2.         TestFileName : string;
  3.         aList : array[1..3] of string = ('abc','defghi','wxyz');
  4. begin
  5.         for TestFileName in aList do ShowMessage(TestFileName);
  6. end.
  7.  

Example B
Code: Pascal  [Select][+][-]
  1. type
  2.         tMakeArr = array of string;
  3. var
  4.         TestFileName : string;
  5. begin
  6.         for TestFileName in tMakeArr.Create('abc','defghi','wxyz') do ShowMessage(TestFileName);
  7. end.
  8.  

Example C
Code: Pascal  [Select][+][-]
  1. var
  2.         TestFileName : string;
  3.         aList : array of string;
  4. begin
  5.         aList := aList.Create('abc','defghi','wxyz');
  6.         for TestFileName in aList do ShowMessage(TestFileName);
  7.        
  8.         aList := aList.Create('jkl','mnopqr','suvwx','yz');
  9.         for TestFileName in aList do ShowMessage(TestFileName);
  10. end.
  11.  
« Last Edit: April 19, 2021, 09:18:55 am by Blade »

jamie

  • Hero Member
  • *****
  • Posts: 6090
Re: Iteration over string array literal truncates strings
« Reply #3 on: April 18, 2021, 11:37:39 pm »
Hi!

I am currently learning Lazarus / Free Pascal (I am familiar with other programming languages), and I ran into the following problem.

This code:

Code: Pascal  [Select][+][-]
  1. var
  2.   TestFileName: string;
  3.  
  4. begin
  5.   for TestFileName in ['abc', 'defghi'] do
  6.     WriteLn(TestFileName);

produces this output:

Code: Text  [Select][+][-]
  1. abc
  2. def

You see, the second string is truncated, it should be defghi.

Now I wonder ...
Is this a bug?
Is it a feature?
Is there an explanation for this behavior?
How do I write string array literals that work?

Thanks for your help.

Regards,
jflipp
it is what you call a bug!

It's already known about.

The problem is it takes the length of the first element as the length for the remaining elements.

Now, you need to ask your self "Do you feel lucky?", that is, lucky if it gets fixed..

Personally I've never had a need for an inline const string like that and I need to check Delphi because I am not even sure if it's supported there.


Make a Const MyLits :array of string =('....','...') etc

something like that.

EDIT

Const
 MyList  : Array[ 0..1 ] of string = ('one','Three'); etc
« Last Edit: April 19, 2021, 12:28:33 am by jamie »
The only true wisdom is knowing you know nothing

jflipp

  • Newbie
  • Posts: 5
Re: Iteration over string array literal truncates strings
« Reply #4 on: April 19, 2021, 11:20:07 pm »
Thank you all for your exhaustive answers.

I think I'm not going to wait for the bugfix ::)
but instead use some of your proposed workarounds, like

Code: Pascal  [Select][+][-]
  1. for s in TStringArray(['1char','2chars']) do

or

Code: Pascal  [Select][+][-]
  1. for TestFileName in tMakeArr.Create('abc','defghi','wxyz') do ShowMessage(TestFileName);

Regards,
jflipp

 

TinyPortal © 2005-2018