How to get the real length of WideString variable.
Thanks in advance.

Mailing lists / fpc-pascal / 2010-10
7 messages · 4 people · –
Reply by email Subscribe to this list Your subscription
Read-only archive. Use j and k to move between messages.
How to get the real length of WideString variable.
Thanks in advance.
Zaher Dirkey wroteHow 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));
//= 8It is different with
ws:= UTF8Decode('زاهر');
Label3.Caption := inttostr(length(ws));
//= 4Here 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.
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.
How to get the real length of WideString variable. Thanks in advance. Zaher Dirkey
Zaher Dirkey wroteHow 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
Op 2010-10-21 08:17, Felipe Monteiro de Carvalho het geskryf:
Length() returns number of bytes, not number of characters. The incorrect assumption was made back in Delphi (before unicode support).
Regards,
- Graeme -
Op 2010-10-21 08:29, Graeme Geldenhuys het geskryf:
"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 -
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.