Sign in Join

Live chat for Free Pascal, Lazarus, Delphi and every Pascal programmer.

Mailing lists / fpc-pascal / 2016-08

String concatenation failure (again)

4 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

Read-only archive. Use j and k to move between messages.

Jürgen Hestermann

I just came accross another issue of string concatenation within my program that worked okay before FPC 3 but fails after updating to FPC 3. I had not used this function since the update so I found the problem only now (by accident):

------------------------------------------
type MyStringType = string;
var S     : MyStringType;
     Liste : array of MyStringType;
     i     : SizeInt;
begin
[..] // "Liste" is setup with many string entries (which are also shown correctly with Ctrl+F7 (evaluate/modify)
S := '';
for i := Low(Liste) to High(Liste) do
    S := S+Liste[i]+LineEnding;
SetLength(Liste,0);
ShowMessage(S);
end;
------------------------------------------

"S" now always consists of only one line
even when "Liste" has many more.
The for-loop is passed multiple times
as expected but only the first pass adds
a line to "S".

Any idea why this code no longer works?

Jürgen Hestermann
to Jürgen Hestermann
Jürgen Hestermann wrote 15 lines
------------------------------------------ type MyStringType = string; var S : MyStringType; Liste : array of MyStringType; i : SizeInt; begin [..] // "Liste" is setup with many string entries (which are also shown correctly with Ctrl+F7 (evaluate/modify) S := ''; for i := Low(Liste) to High(Liste) do S := S+Liste[i]+LineEnding; SetLength(Liste,0); ShowMessage(S); end; ------------------------------------------

I just tested a bit more and it seems that the problem is located in the way the strings are fed into "Liste" which is done like this from a Windows API function:

------------------------------------------
if FindNextFileNameW(H,Laenge,@Pfad[1]) then
    begin
    SetLength(Liste,Length(Liste)+1);
    Liste[High(Liste)] := copy(Pfad,1,Laenge);
    end;
------------------------------------------

where "Pfad" is a "widestring" (because of the Windows W-function), "H" is of type "hwnd" and "Laenge" is "SizeInt".

I would expect that with the new string encoding handling widestring will be converted automatically to "MyStringType" (which is string in my case). This assumption is encouraged by the fact that "Liste" is shown with the correct list of all strings in the evaluate window when debugging.

The interesting thing is, that *one* (the first) string from "Liste" is added to "S" (but all others are missing).

José Mejuto
to Jürgen Hestermann
Jürgen Hestermann wrote 8 lines
I would expect that with the new string encoding handling widestring will be converted automatically to "MyStringType" (which is string in my case). This assumption is encouraged by the fact that "Liste" is shown with the correct list of all strings in the evaluate window when debugging. The interesting thing is, that *one* (the first) string from "Liste" is added to "S" (but all others are missing).

Hello,

Check the length of the string I think you have a NULL char at the end of every string, so only the first one (up to #00 char) is displayed.

Jürgen Hestermann
to José Mejuto
José Mejuto wrote
Check the length of the string I think you have a NULL char at the end of every string, so only the first one (up to #00 char) is displayed.

Yes, you are right.
When I change the code

----------------------------
for i := Low(Liste) to High(Liste) do
    S := S+Liste[i]+LineEnding;
----------------------------

to

----------------------------
for i := Low(Liste) to High(Liste) do
    S := S+pchar(Liste[i])+LineEnding;
----------------------------

then it works okay.
The Win-API call retrieves the ending null byte too so it ends up in my array of strings.

Strange is that it already worked with FPC 2.x.
Of course I had to convert UTF16 to UTF8 manually
but I don't have the old code anymore.
It could be that I had to use pchar in that case too which would explain why it worked before (I then removed "unneccessary" convertion routines and it seems
I removed the pchar calls too).