Sign in Join

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

Mailing lists / fpc-pascal / 2007-05

Widestrings length and character iteration

8 messages · 2 people · –

Reply by email Subscribe to this list Your subscription

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

Christos Chryssochoidis

Hi,

Are there any rtl functions to compute the length (in characters) of a widestring and to iterate over its characters?
In my system (Mac OS X/fpc 2.1.3), a widestring is implemented as a UTF-8 encoded string, as I found out.

Thanks

Daniël Mantione
to Christos Chryssochoidis

Op Mon, 7 May 2007, schreef Christos Chryssochoidis:

Hi, Are there any rtl functions to compute the length (in characters) of a widestring and to iterate over its characters?

If you treat it as UCS-2: length(widestring_variable)

In my system (Mac OS X/fpc 2.1.3), a widestring is implemented as a UTF-8 encoded string, as I found out.

Not possible, a widestring is UCS-2/UTF-16.

Daniël

Christos Chryssochoidis
Daniël Mantione wrote 11 lines
Op Mon, 7 May 2007, schreef Christos Chryssochoidis: > Hi, > > Are there any rtl functions to compute the length (in characters) of a > widestring and to iterate over its characters? If you treat it as UCS-2: length(widestring_variable) > In my system (Mac OS X/fpc 2.1.3), a widestring is implemented as a

UTF-8

> encoded string, as I found out. Not possible, a widestring is UCS-2/UTF-16.

I defined a widestring with 7 characters (code points), and the length() function returned the value 15. Of the 7 code points of that widestring only one of them was greater than $07FF (the maximum code point which can be encoded in 2 bytes under UTF-8). When I changed that character with another one with code not greater than $07FF, length() returned value 14... I also printed the byte values of one of the widestring's widechars, and the values printed indicated UTF-8 encoding.

Thanks,
Christos

Show quoted text 10 lines
Daniël ------------------------------------------------------------------------ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal
Daniël Mantione
to Christos Chryssochoidis

Op Mon, 7 May 2007, schreef Christos Chryssochoidis:

Show quoted text 11 lines
Daniël Mantione wrote: > > Not possible, a widestring is UCS-2/UTF-16. I defined a widestring with 7 characters (code points), and the length() function returned the value 15. Of the 7 code points of that widestring only one of them was greater than $07FF (the maximum code point which can be encoded in 2 bytes under UTF-8). When I changed that character with another one with code not greater than $07FF, length() returned value 14... I also printed the byte values of one of the widestring's widechars, and the values printed indicated UTF-8 encoding.

Yes, the program output is utf-8 on OS-X, because this is the native encoding for OS-X. However, widestrings are not utf-8. Can you show your code?

Daniël

Christos Chryssochoidis
Daniël Mantione wrote 6 lines
Op Mon, 7 May 2007, schreef Christos Chryssochoidis: > Daniël Mantione wrote: >> Not possible, a widestring is UCS-2/UTF-16. > I defined a widestring with 7 characters (code points), and the

length()

> function returned the value 15. Of the 7 code points of that

widestring only

> one of them was greater than $07FF (the maximum code point which can be > encoded in 2 bytes under UTF-8). When I changed that character with

another

> one with code not greater than $07FF, length() returned value 14...

I also

> printed the byte values of one of the widestring's widechars, and

the values

Show quoted text 15 lines
> printed indicated UTF-8 encoding. Yes, the program output is utf-8 on OS-X, because this is the native encoding for OS-X. However, widestrings are not utf-8. Can you show your code? Daniël ------------------------------------------------------------------------ _______________________________________________ fpc-pascal maillist - http://lists.freepascal.org/mailman/listinfo/fpc-pascal

OK, I figured out what happened. The source file was saved in UTF-8 encoding, but I hadn't put in my source file the compiler directive {$CODEPAGE UTF8}. After including this directive in my code almost everything worked fine: length() was returning the right number of unicode characters, and subscripting the widestring returned the right character. But as the widechar and widestring encoding is, as you said, UTF-16, while my Mac OS X console uses UTF-8 encoding, for the output results to be displayed correctly I had to wrap the individual widechars or the whole widestring with the function utf8encode(), prior to output them with write()...

Thanks for your help,

Christos

Daniël Mantione
to Christos Chryssochoidis

Op Wed, 9 May 2007, schreef Christos Chryssochoidis:

Show quoted text 9 lines
OK, I figured out what happened. The source file was saved in UTF-8 encoding, but I hadn't put in my source file the compiler directive {$CODEPAGE UTF8}. After including this directive in my code almost everything worked fine: length() was returning the right number of unicode characters, and subscripting the widestring returned the right character. But as the widechar and widestring encoding is, as you said, UTF-16, while my Mac OS X console uses UTF-8 encoding, for the output results to be displayed correctly I had to wrap the individual widechars or the whole widestring with the function utf8encode(), prior to output them with write()...

This means no widestring manager was used; the default dumb widestring manager converts to iso-8859-1. Use the "cwstring" unit will use the system encoding when reading/writing widestrings.

Daniël

Christos Chryssochoidis
Daniël Mantione wrote
Op Wed, 9 May 2007, schreef Christos Chryssochoidis: > OK, I figured out what happened. The source file was saved in

UTF-8 encoding,

> but I hadn't put in my source file the compiler directive
{$CODEPAGE UTF8}.
> After including this directive in my code almost everything

worked fine:

> length() was returning the right number of unicode characters, and > subscripting the widestring returned the right character. But as

the widechar

> and widestring encoding is, as you said, UTF-16, while my Mac OS

X console

> uses UTF-8 encoding, for the output results to be displayed

correctly I had to

> wrap the individual widechars or the whole widestring with the

function

> utf8encode(), prior to output them with write()... This means no widestring manager was used; the default dumb

widestring

Show quoted text 7 lines
manager converts to iso-8859-1. Use the "cwstring" unit will use the system encoding when reading/writing widestrings. Daniël

------------------------------------------------------------------------

Show quoted text 5 lines
_______________________________________________ fpc-pascal maillist - fpc-pascal-PD4FTy7X32k2wBtHl531yWD2FQJk+8 http://lists.freepascal.org/mailman/listinfo/fpc-pascal

I called setCWideStringManager() from my code, but it did not had any effect. A call to utf8encode() is still needed from proper output.

Daniël Mantione
to Christos Chryssochoidis

Op Wed, 9 May 2007, schreef Christos Chryssochoidis:

I called setCWideStringManager() from my code, but it did not had any effect. A call to utf8encode() is still needed from proper output.

It probably means the widestring manager thinks your system is iso-8859-1 encoded. If you think this is wrong, please report a bug.

Daniël