Sign in Join

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

Mailing lists / fpc-pascal / 2010-10

Real length of WideString

7 messages · 4 people · –

Reply by email Subscribe to this list Your subscription

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

Zaher Dirkey
to Zaher Dirkey
Zaher Dirkey wrote
How to get the real length of WideString variable.

Hmm, I think i found my problem but not understand it, i used Lazarus as editor to write the code

var
  ws: widestring;
begin
  ws:= 'زاهر';
  Label3.Caption := inttostr(length(ws));
//= 8

It is different with

  ws:= UTF8Decode('زاهر');
  Label3.Caption := inttostr(length(ws));
//= 4
Zaher Dirkey

Reply by email

Felipe Monteiro de Carvalho
to Zaher Dirkey
Zaher Dirkey wrote 6 lines
var   ws: widestring; begin   ws:= 'زاهر';   Label3.Caption := inttostr(length(ws)); //= 8

Here the compiler assumes that your source code is encoded in the default system encoding and then it converts your string to utf-16. Probably your system encoding is not UTF-8. so your string is corrupted by a wrong conversion. Then length 8 seams to indicate that the string was corrupted.

You could tell the compiler that your source code is in UTF-8, which should allow the code above to work, but then it will break all utf-8 ansistrings. There is no real solution for this issue while FPC lacks full Unicode support. In the mean time I recommend using your second version of the code.

Show quoted text 5 lines
It is different with   ws:= UTF8Decode('زاهر');   Label3.Caption := inttostr(length(ws)); //= 4

This is more like the standard Lazarus way of doing things. The string is encoded in the file as utf-8 and assigned to a ansistring. Then UTF8Decode converts this to UTF-16. A correct conversion takes place, because you didn't let the compiler decide for you what to convert, but you decided yourself.

Felipe Monteiro de Carvalho

Reply by email

Graeme Geldenhuys
to Felipe Monteiro de Carvalho

Op 2010-10-21 08:17, Felipe Monteiro de Carvalho het geskryf:

Show quoted text 7 lines
On Thu, Oct 21, 2010 at 1:41 AM, Zaher Dirkey wrote: > How to get the real length of WideString variable. Length in which unit? WideChars (2 bytes each) or Bytes? Length() returns the amount of WideChars AFAIK

Length() returns number of bytes, not number of characters. The incorrect assumption was made back in Delphi (before unicode support).

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net:8080/fpgui/

Reply by email

Graeme Geldenhuys
to Graeme Geldenhuys

Op 2010-10-21 08:29, Graeme Geldenhuys het geskryf:

Show quoted text 5 lines
Length() returns number of bytes, not number of characters. The incorrect assumption was made back in Delphi (before unicode support). From Delphi 7 & Kylix 3 help:

"For single-byte and multibyte strings, Length returns the number of bytes used by the string. For Unicode (WideString) strings, Length returns the number of bytes divided by two."

FPC help does not specify what the value is that Length() returns - bytes or characters. So I default to the Delphi/Kylix help.

Regards,
- Graeme -

fpGUI Toolkit - a cross-platform GUI toolkit using Free Pascal http://opensoft.homeip.net:8080/fpgui/

Reply by email

Marco van de Voort
to Graeme Geldenhuys: Op 2010-10-21 08:17, Felipe Monteiro de Carvalho het geskryf: Length() returns number of…

In our previous episode, Graeme Geldenhuys said:

Length() returns number of bytes, not number of characters. The incorrect assumption was made back in Delphi (before unicode support).

Afaik before unicode.

Delphi 3 (which introduced widestrings) was released August 5th, 1997.

Unicode 3.0 that adds surrogate points was only released in 2000.

Note that there was no other way anyway, since parsing characters for length would be horribly slow.

Reply by email