Lazarus

Free Pascal => Beginners => Topic started by: jflipp on April 18, 2021, 09:38:43 pm

Title: Iteration over string array literal truncates strings
Post by: jflipp 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
Title: Re: Iteration over string array literal truncates strings
Post by: ccrause on April 18, 2021, 10:08:15 pm
This is a known bug, see #32043 (https://bugs.freepascal.org/view.php?id=32034), #37338 (https://bugs.freepascal.org/view.php?id=37338) and #38732 (https://bugs.freepascal.org/view.php?id=38732).

A possible work-around mentioned in #32043 (https://bugs.freepascal.org/view.php?id=32034):
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.  
Title: Re: Iteration over string array literal truncates strings
Post by: Blade 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.  
Title: Re: Iteration over string array literal truncates strings
Post by: jamie 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
Title: Re: Iteration over string array literal truncates strings
Post by: jflipp 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