NUL characters in NSString cause unexpected results
NUL characters in NSString cause unexpected results
- Subject: NUL characters in NSString cause unexpected results
- From: Jerry Krinock <email@hidden>
- Date: Wed, 22 Nov 2006 21:33:05 -0500
- Thread-topic: NUL characters in NSString cause unexpected results
Every reference I can find on UTF8 states that any valid ASCII character is
a valid UTF8 character, so this should include the ASCII 'NUL', 0x0. But
when I init an NSString from data including a NUL, using UTF8 encoding, and
then send some messages to it, most messages behave as though the string
terminates at the NUL. For example,
1. The -description of the string terminates at the NUL.
2. When sent the message -stringByAppendingString, the result only includes
the string up to the NUL. It looks like, under the hood, this method is
just strcat!
But -length gives the "correct" answer, including the part past the NUL.
This can lead to some unexpected exceptions when using -length to determine
the safe NSRange one can operate upon.
Is this a bug or am I missing some documentation?
Jerry Krinock
****** DEMONSTRAITON ******
The following code constructs two NSStrings:
s1 = "A[NULL]B"
s2 = "CD"
and then concatenates them into a third string, sC, using
-stringByAppendingString.
I expect sc = "A[NULL]BCD".
I get sc = "ACD".
****** CODE ******
char* buf = (char*)malloc(3) ;
buf[0] = 'A' ;
buf[1] = 0x0 ;
buf[2] = 'B' ;
NSString* s1 = [[NSString alloc] initWithBytes:buf
length:3
encoding:NSUTF8StringEncoding] ;
NSLog(@"s1 = %@", s1) ;
NSString* s2 = @"CD" ;
NSLog(@"s2 = %@", s2) ;
NSString* sC = [s1 stringByAppendingString:s2] ;
NSLog(@"sC = %@", sC) ;
NSLog(@"length of s1:%i", [s1 length]) ;
NSLog(@"length of s2:%i", [s2 length]) ;
NSLog(@"length of sC:%i", [sC length]) ;
[s1 release] ;
free(buf) ;
****** CONSOLE OUTPUT ******
s1 = A
s2 = CD
sC = ACD
length of s1:3
length of s2:2
length of sC:3
_______________________________________________
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