• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: NSString "midstring()"
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: NSString "midstring()"


  • Subject: Re: NSString "midstring()"
  • From: Quincey Morris <email@hidden>
  • Date: Sun, 17 Apr 2011 14:53:52 -0700

On Apr 17, 2011, at 14:08, JAMES ROGERS wrote:

> 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.

Sorry, didn't mean to be overwhelming. :)

Yes, the compiler is no help to you here. Testing might not show a problem either. If your text view happens to contain only ASCII characters (which are represented by a single element in both UTF-8 and UTF-16, and which fit in an 8-bit 'char'), you won't see a problem.

Here's how to change your code to avoid the 8-bit vs 16 bit and UTF-8 vs UTF-16 problem:

> unichar 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++;
> }
>
> substring = [NSString stringWithCharacters:sndBuffer length:j];

Or, more compactly as Mike suggested:

> int length = MIN ([string length] - characterIndex, 65);
> substring = [string substringWithRange: NSMakeRange (characterIndex, length)];
> characterIndex += length;

However, neither version solves the problem of breaking off in the middle of a multi-element UTF-16 sequence.

Incidentally, looking at your original code again, it sure looks like 'characterIndex' is getting over-incremented. You're going to skip over the first character of every substring. Shouldn't it be:

> unichar sndBuffer[65];
> int j;
>
> for (j = 0; j < 65; j++)
>    sndBuffer[j] = [string characterAtIndex:++characterIndex]; // is always sitting at the last character sent so advance to the next character in string.
>
> substring = [NSString stringWithCharacters:sndBuffer length:j];


?

_______________________________________________

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

References: 
 >NSString "midstring()" (From: JAMES ROGERS <email@hidden>)
 >Re: NSString "midstring()" (From: Quincey Morris <email@hidden>)
 >Re: NSString "midstring()" (From: JAMES ROGERS <email@hidden>)

  • Prev by Date: Re: NSString "midstring()"
  • Next by Date: Re: Proper way to create a singleton without @synchronized ?
  • Previous by thread: Re: NSString "midstring()"
  • Next by thread: Re: NSString "midstring()"
  • Index(es):
    • Date
    • Thread