String concatenation failure (again)

classic Classic list List threaded Threaded
4 messages Options
Reply | Threaded
Open this post in threaded view
|

String concatenation failure (again)

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?


_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: String concatenation failure (again)

Jürgen Hestermann
Am 2016-08-02 um 12:25 schrieb Jürgen Hestermann:
 > ------------------------------------------
 > 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).

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: String concatenation failure (again)

José Mejuto
El 02/08/2016 a las 16:51, Jürgen Hestermann escribió:

> 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.


--

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal
Reply | Threaded
Open this post in threaded view
|

Re: String concatenation failure (again)

Jürgen Hestermann
Am 2016-08-02 um 20:12 schrieb José Mejuto:
 > 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).

_______________________________________________
fpc-pascal maillist  -  [hidden email]
http://lists.freepascal.org/cgi-bin/mailman/listinfo/fpc-pascal