On Sep 28, 2008, at 5:53 AM, Gerriet M. Denkmann wrote:


On Sun, 28 Sep 2008 03:27:48 -0500, Michael Gardner <[EMAIL PROTECTED] > wrote:

On Sep 27, 2008, at 2:23 PM, David Niemeijer wrote:

Hi,

I have been trying to find this in the documentation and list
archives but without success so far. What is the best way to count
the number of characters in an NSString taking account of the fact
that some characters may take up multiple 16 bit slots. Using "-
(NSUInteger)length" is thus not the right way.

If I am reading you right, you are saying that -length will give you
the wrong results because some characters in Unicode are represented
by multibyte sequences. This is incorrect: -length will give you the
number of Unicode characters in a string [...].

This surprises me. I always thought that "length" gives you the number of shorts in the Utf-16 encoding of the string, which - as I used to think - is not the same as the number of Unicode code points in this string.

But maybe you are right and I am confused.

Upon further investigation, I may be wrong. I based my assertion upon Apple's NSString documentation ("Returns the number of Unicode characters in the receiver"), and upon some quick tests I ran. But this reply made me look into the issue in greater depth.

I re-did my tests more throughly, and it does appear that -length returns the number of 16-bit words (code units), not the number of Unicode characters (code points), in the string. If this is true, I would call it a bug either in the code or in the documentation, which David should submit to Apple.

I apologize for the apparent misinformation in my previous, hasty reply.

In the meanwhile, David, perhaps you can find a library that can work with UTF-8 strings. What are you using the length values for?

-Michael
_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [EMAIL PROTECTED]

Reply via email to