Re: How to count composed characters in NSString?
Re: How to count composed characters in NSString?
- Subject: Re: How to count composed characters in NSString?
- From: "Michael Ash" <email@hidden>
- Date: Sun, 28 Sep 2008 11:46:46 -0400
On Sat, Sep 27, 2008 at 3:23 PM, David Niemeijer <email@hidden> 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. Using a series of calls to
> "rangeOfComposedCharacterSequenceAtIndex:" seems like a possibility, but I
> am not sure this would be the most efficient way. Is there a simple and
> straightforward solution? I would like to be able to display the number of
> characters in a string and not report the wrong results for foreign
> languages (which I would get if I simply took the length of the string). I
> need a solution that does not only work in Leopard (i.e. CFStringTokenizer
> is not an option) and that does not require using the lower level
> UCFindTextBreak.
First I recommend you simply give up on the concept. You've stumbled
into a tough problem, one which is not all that useful, and it may be
better to skip it. Of course I don't know what you're using it for,
but in general counting the number of characters in a string is not a
useful thing to do.
That said, if you want to continue, I'd suggest that you first figure
out what you mean by a "character". You mention composed character
sequences, but conceptually those are separate characters which happen
to display as a single unit. Your description sounds like you want to
catch UTF-16 surrogate pairs. Do you also want to catch things like é
(accented e), which can be encoded as two separate unicode code
points? Is space a character? Is a ligature like the "fi" glyph found
in many fonts one character or many? Note that these are not
rhetorical questions, and there is more than one right answer to each
depending on what you want to do.
Mike
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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:
This email sent to email@hidden