Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In ICU UnicodeString what is the difference between countChar32() and length()?

From the docs;

The length is the number of UChar code units are in the UnicodeString. If you want the number of code points, please use countChar32().

and

Count Unicode code points in the length UChar code units of the string.

A code point may occupy either one or two UChar code units. Counting code points involves reading all code units.

From this I am inclined to think that a code point is an actual character and a code unit is just one possible part of a character.

For example.

Say you have a unicode string like:

'foobar'

Both the length and countChar32 will be 6. Then say you have a string composed of 6 chars that take the full 32 bits to encode the length would be 12 but the countChar32 would be 6.

Is this correct?

like image 601
Ben McNiel Avatar asked Nov 05 '25 08:11

Ben McNiel


1 Answers

The two values will only differ if you use characters out of the Base Multilingual Plane (BMP). These characters are represented in UTF-16 as surrogate pairs. Two 16-bit characters make up one logical character. If you use any of these, each pair counts as one 32-bit character but two elements of length.

like image 156
bmargulies Avatar answered Nov 06 '25 22:11

bmargulies



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!