Re: NSString "midstring()"
Re: NSString "midstring()"
- Subject: Re: NSString "midstring()"
- From: JAMES ROGERS <email@hidden>
- Date: Sun, 17 Apr 2011 16:08:10 -0500
Quincy,
I have stepped this through with the debugger and no flags were raised. The code compiles without an error or a warning of any kind. I am afraid your response has overwhelmed me.
One thing I will mention, I am not changing string. So that point is a moot one. I am just copying data out of the string.
The "substring = [NSString stringWithUTF8String:sndBuffer]" was not my own and came from a website as a result of my query on "C string to NSString" and it works.
I appreciate your input and will study this a bit more.
Jim
On Apr 17, 2011, at 3:50 PM, Quincey Morris wrote:
> On Apr 17, 2011, at 13:09, JAMES ROGERS wrote:
>
>> char sndBuffer[65];
>> int j;
>>
>> characterIndex++; // is always sitting at the last character sent so advance to the next character in string.
>> for (j = 0; j < 65; j++) {
>> sndBuffer[j] = [string characterAtIndex:characterIndex];
>> characterIndex++;
>> }
>> sndBuffer[j] = 0x00;
>> substring = [NSString stringWithUTF8String:sndBuffer];
>>
>> It works flawlessly, but there oughta be a way without reverting to C?
>
> This code is just wrong, BTW.
>
> The return value from -[NSString characterAtIndex:] is 'unichar', which is a 16-bit quantity, so assigning it to a 'char' variable isn't a good idea.
>
> Also, the return value is *not* a character, in the sense of being a Unicode code point. It's an element of the UTF-16 representation of the string. That means you can't just stop extracting elements at an arbitrary position, without potentially breaking the current substring and/or the next substring.
>
> Either you're going to have to analyze the sequence of elements manually according to the UTF-16 definition to find a good break point, or use something like -[NSString rangeOfComposedCharacterSequence...] (which doesn't do the same thing, but does guarantees a usable UTF-16 break point).
>
> Also, re-creating the NSString (substring) using a UTF-16 string but pretending it's a UTF-8 string isn't going to work very well.
>
> Also, it matters what's going to happen to your substring next. The UTF-8, UTF-16 and Unicode code point byte-lengths of a string are all different, so your assumptions about what fits into a fixed-size buffer need to be carefully re-examined.
>
>
JIM ROGERS
email@hidden
http://web.me.com/jimrogers_w4atk
_______________________________________________
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