Re: NSString character manipulation...
Re: NSString character manipulation...
- Subject: Re: NSString character manipulation...
- From: Hsu <email@hidden>
- Date: Mon, 1 Jul 2002 23:41:05 -0700
After much poking, it seems that on OSX, isalnum takes an int, while
characterAtIndex returns an unsigned short... those should be
comparable, shouldn't they (doesn't the C standard garantee that an int
is at least a short and no more than a long)?
karl
On Monday, July 1, 2002, at 11:30 AM, Greg Titus wrote:
Hi Hsu,
On Monday, July 1, 2002, at 12:24 AM, Hsu wrote:
Hullo,
Is it safe to do something like:
...
Or am I going to have false positives because of unicode characters?
if ( (isalnum([myNSString characterAtIndex:0]))
This is not safe. If isalnum() was a real function, then the 2-byte
unichar will get cast to a 1-byte char as it is past into the function,
and you'll end up with false positives. However, isalnum() is a macro
which basically does a lookup in a table, using the character as an
index. So I'd expect you might even have your app crash if you pass in
too large of a value here.
You need to do something like this instead:
if ([[NSCharacterSet alphanumericCharacterSet]
characterIsMember:[myNSString characterAtIndex:0])
... which will work with all unicode characters. See the NSCharacterSet
documentation in Foundation.
|| ([myNSString characterAtIndex:0] == '.') ) {
This, on the other hand, will work just fine. The '.' will be implicitly
promoted to the right size for the comparison, and the Unicode standard
specifically made the low characters have exactly the same values as
ASCII so that comparisons like this are correct.
Hope this helps,
--Greg
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.
--
People usually get what's coming to them, unless it was sent in the mail.
Homepage:
http://homepage.mac.com/khsu/index.html
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.