• 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: Best Way to Append Bytes With Encoding To CFMutableString
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Best Way to Append Bytes With Encoding To CFMutableString


  • Subject: Re: Best Way to Append Bytes With Encoding To CFMutableString
  • From: Andreas Grosam <email@hidden>
  • Date: Sat, 31 Mar 2012 18:20:56 +0200

Thank you all for your answers and suggestions.

My use case is to create CFStrings from Unicode - which in the majority of cases are "short" strings - say, less than 100 characters. In this case, I create an immutable CFString directly in one go.

Less frequently in the typical use case of the application, I have to deal with larger strings, say base64 encoded images, or something like this. Yet, I need to create a CFString. Since the content will be received over the net, I naturally get content in chunks anyway (namely NSData objects received from a connection). So, in order to avoid a large temporary buffer which holds the complete string, I use a smaller buffer, e.g. 4 KByte and then append this small buffer to the resulting CFString, until it is complete.

As Greg suggested, I'll try my solution first and test whether it will work efficiently:

CFStringRef tmp = CFStringCreateWithBytesNoCopy(kCFAllocatorDefault, bytes, numBytes, encoding, NO, kCFAllocatorNull);
CFStringAppend(myMutableString, tmp);
CFRelease(tmp);


Nonetheless, if there is a chance that a possible implementation

void CFStringAppendBytes (
 CFMutableStringRef theString,
 const void* bytes,
 CFIndex numBytes,
 CFStringEncoding encoding
);


will be faster, or uses less memory, I'll also file an enhancement request.



Thanks All!

Regards
Andreas



On Mar 28, 2012, at 9:24 PM, Greg Parker wrote:

> On Mar 28, 2012, at 11:00 AM, Charles Srstka <email@hidden> wrote:
>> Unicode NULL is the least of your problems. In UTF16, each character in the normal ASCII range is going to contain a zero as one of its two bytes (which one, of course, depending on whether the encoding is big- or little-endian). CFStringAppendCString(), along with the other functions that take C strings, stops at the first zero byte it hits, which means that unless your entire file is in a non-Western script, it’s going to get cut short.
>>
>> CFStringAppendCString() is not what you want if you might be using UTF16.
>
> That's right. The first thing CFStringAppendCString() does is call strlen().
>
> CoreFoundation does have a function internally that would do what you want. You could file a bug report asking for a new API to match. However, it requires almost as much work as using a temporary CFString object anyway, except in some ASCII and UTF-16 cases. I would not expect your CFStringCreateWithBytesNoCopy() solution to be much slower unless you're performing a large number of short appends with one of CFString's preferred encodings.
>
>
> --
> Greg Parker      email@hidden     Runtime Wrangler

_______________________________________________

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


  • Follow-Ups:
    • Re: Best Way to Append Bytes With Encoding To CFMutableString
      • From: Jean-Daniel Dupas <email@hidden>
References: 
 >Best Way to Append Bytes With Encoding To CFMutableString (From: Andreas Grosam <email@hidden>)
 >Re: Best Way to Append Bytes With Encoding To CFMutableString (From: "email@hidden" <email@hidden>)
 >Re: Best Way to Append Bytes With Encoding To CFMutableString (From: Andreas Grosam <email@hidden>)
 >Re: Best Way to Append Bytes With Encoding To CFMutableString (From: "email@hidden" <email@hidden>)
 >Re: Best Way to Append Bytes With Encoding To CFMutableString (From: Charles Srstka <email@hidden>)
 >Re: Best Way to Append Bytes With Encoding To CFMutableString (From: Greg Parker <email@hidden>)

  • Prev by Date: Re: drawRect using a Category
  • Next by Date: iPad 5.1 simulator
  • Previous by thread: Re: Best Way to Append Bytes With Encoding To CFMutableString
  • Next by thread: Re: Best Way to Append Bytes With Encoding To CFMutableString
  • Index(es):
    • Date
    • Thread