Author Topic: ShortString, DELPHIUNICODE and difference Delph vs. FPC  (Read 1262 times)

DRIGUS

  • Jr. Member
  • **
  • Posts: 64
ShortString, DELPHIUNICODE and difference Delph vs. FPC
« on: October 20, 2025, 03:48:15 pm »
Hi *.*.

  (Delphi 11.3 / Lazarus 4.2, FPC 3.2.2)

  While try to use some Delphi 11.3 code in Lazarus, I stumbled about something I can't explain. Take a simple Win32 GUI application with a TEdit control and its OnChange-Event:

Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. var
  3.   TmpString: String;
  4.   TmpShortString: ShortString;
  5.  
  6. begin
  7.   TmpString := Edit1.Text;
  8.   TmpShortString := TmpString;
  9.   ShowMessage(IntToStr(Length(TmpString)));
  10.   ShowMessage(IntToStr(Length(TmpShortString)));
  11. end;
  12.  

  In order to achieve string compatibility to Delphi I use mode DELPHIUNICODE for the Lazarus project.

  If I run the Delphi Application and type "ä" (the german umlaut), I get "1" and "1" as output. Running the Lazarus project and typing "ä" I get "1" and "2". Why did I get a length of two for the shortstring? And how to get the same result as in Delphi 11.3?

Thanks.
DRIGUS GmbH

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #1 on: October 20, 2025, 03:50:16 pm »
Lazarus sets the system encoding to UTF8. Delphi does not.  Shortstring uses the system encoding, and not having it in unicode is lossing. Delphi is lagging here.


DRIGUS

  • Jr. Member
  • **
  • Posts: 64
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #2 on: October 20, 2025, 04:08:33 pm »
First, thanks for your answer.

  But I thought the encoding / code page is only relevant for AnsiString, and not for ShortString. And if you are correct, why I get the message

Quote
Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "ShortString"

for the line

Code: Pascal  [Select][+][-]
  1. TmpShortString := TmpString;

while compiling?
DRIGUS GmbH

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #3 on: October 20, 2025, 04:18:36 pm »
First, thanks for your answer.

  But I thought the encoding / code page is only relevant for AnsiString, and not for ShortString. And if you are correct, why I get the message

Quote
Warning: Implicit string type conversion with potential data loss from "UnicodeString" to "ShortString"

for the line

Code: Pascal  [Select][+][-]
  1. TmpShortString := TmpString;

while compiling?

Because FPC during compilation doesn't know that Lazarus applications set the runtime encoding differently during startup (iow runtime)
However because Lazarus does NOT default to delphiunicode mode, but instead to objfpc,  a lot of the LCL is still UTF8 ansistring, so it is dangerous to turn default encoding as utf8 off.

If it is an exception and for some reason you really need 1-byte non UTF-8, assign it to an unicodestring and convert it to an array of ansichar buffer with https://learn.microsoft.com/en-us/windows/win32/api/stringapiset/nf-stringapiset-widechartomultibyte

But it is best to minimize or remove this kind of charset legacy stuff if you can.
« Last Edit: October 20, 2025, 04:20:14 pm by marcov »

Bart

  • Hero Member
  • *****
  • Posts: 5800
    • Bart en Mariska's Webstek
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #4 on: October 20, 2025, 04:22:23 pm »
Or use Utf8ToWinCP?

Bart

marcov

  • Administrator
  • Hero Member
  • *
  • Posts: 13052
  • FPC developer.
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #5 on: October 20, 2025, 04:26:29 pm »
Or use Utf8ToWinCP?

Won't work if you enabled utf8 in the manifest. You need something where you can really specify target encoding.

DRIGUS

  • Jr. Member
  • **
  • Posts: 64
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #6 on: October 20, 2025, 04:54:03 pm »
Because FPC during compilation doesn't know that Lazarus applications set the runtime encoding differently during startup (iow runtime)

Thank you for the explanation, I think I get it. My mistake was to assume that the UTF8 encoding only affects AnsiString and not ShortString.

We have a lot of (old) class definitions using (P)ShortString for string handling and storage in Delphi 11, distributed in dozens of units and the goal was to use the same code for Delphi 11 and Lazarus 4. Perhaps it is the right moment to kick out (P)ShortString and switch to Unicode.
DRIGUS GmbH

LeP

  • Guest
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #7 on: October 20, 2025, 05:08:14 pm »
Code: Pascal  [Select][+][-]
  1. procedure TForm1.Edit1Change(Sender: TObject);
  2. var
  3.   TmpString: String;
  4.   TmpShortString: ShortString;
  5.  
  6. begin
  7.   TmpString := Edit1.Text;
  8.   TmpShortString := TmpString;
  9.   ShowMessage(IntToStr(Length(TmpString)));
  10.   ShowMessage(IntToStr(Length(TmpShortString)));
  11. end;
  12.  
If I run the Delphi Application and type "ä" (the german umlaut), I get "1" and "1" as output. Running the Lazarus project and typing "ä" I get "1" and "2". Why did I get a length of two for the shortstring? And how to get the same result as in Delphi 11.3?

Lazarus sets the system encoding to UTF8. Delphi does not.  Shortstring uses the system encoding, and not having it in unicode is lossing. Delphi is lagging here.

Sorry, Delphi shortstring and string are generic UNICODE STRING and the "Length" rappresent the number of characters, not the number of bytes or other things.
So Delphi acts in correct way.

Shortstrings are defined like, and are simple limited in "Length":
Code: Pascal  [Select][+][-]
  1. type
  2.   shortstring  = string[255];
  3.  

In Delphi you cannot access to element 0 of a string, but you can access to element 0 of a shortstring .... BUT DON'T DO IT.

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #8 on: October 20, 2025, 05:16:18 pm »
Code: Pascal  [Select][+][-]
  1. var
  2.   TmpString: AnsiString;// explict
  3.   TmpShortString: ShortString;
  4. begin
  5.   TmpString := Edit1.Text;//converts
  6.   TmpShortString := TmpString;
As long as your strings can always convert to the OEM codepage of your system this will be fine in both delphi and Lazarus.
Tested both with a codepage that supports German
« Last Edit: October 20, 2025, 06:04:35 pm by Thaddy »
Any "programmer" that knows only one programming language is not a programmer

Remy Lebeau

  • Hero Member
  • *****
  • Posts: 1618
    • Lebeau Software
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #9 on: October 20, 2025, 06:26:00 pm »
But I thought the encoding / code page is only relevant for AnsiString, and not for ShortString.

Like AnsiString, ShortString is an AnsiChar-based string type, so assigning a Unicode string to it requires an ANSI charset/codepage conversion at runtime.  A charset-aware AnsiString knows the encoding it's currently holding (stored in the string's metadata), but a ShortString does not (nowhere to store it), so it relies on the system encoding.

Sorry, Delphi shortstring and string are generic UNICODE STRING

A ShortString, whether in Delphi or FreePascal, is NOT a Unicode string from the compiler's perspective, although it can hold UTF-8 elements at runtime.
Remy Lebeau
Lebeau Software - Owner, Developer
Internet Direct (Indy) - Admin, Developer (Support forum)

LeP

  • Guest
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #10 on: October 20, 2025, 07:35:55 pm »
A ShortString, whether in Delphi or FreePascal, is NOT a Unicode string from the compiler's perspective, although it can hold UTF-8 elements at runtime.
@Remy you are quite right.
Even if the character length is consistent across shortstrings, the content isn't if it contains mixed data.
I dont'use shortstrings (used in the old days), and I always tought that they are like strings in the modern world.

Good to know, thanks.

creaothceann

  • Sr. Member
  • ****
  • Posts: 481
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #11 on: October 20, 2025, 07:55:17 pm »
I dont'use shortstrings (used in the old days)

They're still useful for storing text on the stack where it can't disturb (i.e. fragment) heap memory, and where it requires the least effort to de-/allocate.

Thaddy

  • Hero Member
  • *****
  • Posts: 19978
  • Pointless static linking author
Re: ShortString, DELPHIUNICODE and difference Delph vs. FPC
« Reply #12 on: October 21, 2025, 09:22:18 am »
And they are most useful in storage records: there is no pointer dereference, so it is flat when used in a record.
Also, shortstrings are stack friendly and therefor fast.
Any "programmer" that knows only one programming language is not a programmer

 

TinyPortal © 2005-2018