Re: Confused re unichar
Re: Confused re unichar
- Subject: Re: Confused re unichar
- From: Keary Suska <email@hidden>
- Date: Sat, 17 Feb 2007 12:26:07 -0700
- Thread-topic: Confused re unichar
on 2/17/07 9:09 AM, email@hidden purportedly said:
> Presumably a unichar is a single unicode character. But is it defined
> as 2 bytes in size (UTF-16) or is it variable (UTF-8)? I can't find
> its defintion anywhere.
I believe it is 2 bytes, but you could verify using the sizeof() operator.
> @implementation NSString (TMString)
>
> - (NSString *) stringByTrimmingWhitespaceAtStart {
> int length = [self length];
> unichar *buffer = unichar[length];
> [self getCharacters: buffer];
> int idx = 0;
> unichar ch;
> while (idx < length) {
> ch = buffer[idx++];
> if (ch == '\x0020' || ch == '\x0009' || ch == '\x000A' || ch ==
> '\x000D' || ch == '\x0085') {
> continue;
> }
> break;
> }
> return [self substringFromIndex: idx];
> }
>
>
> The compiler complains with "error: parse error before unichar"
That's because it's syntactically incorrect. For a dynamic array of unichar,
you could do:
unichar buffer[length];
I would recommend, however, if you don't really need to optimize this
routine, to do something like:
int i, length = [self length];
NSCharacterSet *whitesp = [NSCharacterSet
whitespaceAndNewlineCharacterSet];
for( i=0; i<length; i++ ) {
if( ! [whitesp characterIsMember:[self characterAtIndex:i]] )
break;
}
return [self substringFromIndex:i];
Then you don't have to worry about the size of unichar and other possible
gotchas.
Keary Suska
Esoteritech, Inc.
"Demystifying technology for your home or business"
_______________________________________________
Cocoa-dev mailing list (email@hidden)
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