Re: NUL characters in NSString cause unexpected results
Re: NUL characters in NSString cause unexpected results
- Subject: Re: NUL characters in NSString cause unexpected results
- From: Jerry Krinock <email@hidden>
- Date: Wed, 22 Nov 2006 22:24:58 -0500
- Thread-topic: NUL characters in NSString cause unexpected results
on 06/11/22 21:47, Chris Suter at email@hidden wrote:
> What happens if you encode the NUL character as "\xc0\x80" in UTF8?
The result is better. The -length of the concatenated string now equals the
sum of the -lengths of the two strings. But the NUL seems to eat up the
following character, the lower-case "e" in the following example:
****** CODE ******
NSString* s1 = @"A\xc0\x80end" ; // @"A[NUL]end" (*)
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] ;
****** CONSOLE OUTPUT ******
s1 = A¿nd
s2 = CD
sC = A¿ndCD
length of s1:5
length of s2:2
length of sC:7
That first line also drew a couple of complier warnings:
warning: hex escape sequence out of range
warning: non-ASCII character in CFString literal
(*) I'm not that familiar with using \x in string literals. Maybe I'm not
doing that correctly.
However, getting back to the original issue, of course I would never put a
0x0 NUL in an NSString myself. But I discovered this issue when I was
testing today. My app reads in a supposed text file as data of unknown
string encoding. After doing a few tests to determine the encoding, it uses
the data to initialize an NSString. If some nasty person puts an ASCII 0x0
NUL in the file, it flows right through to my NSString and causes problems
later.
_______________________________________________
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