Re: How to transform a NSString to a NSString C string format?
Re: How to transform a NSString to a NSString C string format?
- Subject: Re: How to transform a NSString to a NSString C string format?
- From: Michael Ash <email@hidden>
- Date: Thu, 5 Feb 2009 20:28:02 -0500
On Thu, Feb 5, 2009 at 5:52 PM, Iceberg-Dev <email@hidden> wrote:
> Problem:
> --------
>
> I would like to transform a NSString to a NSString conforming to the C
> string format.
>
> Examples:
> ---------
>
> toto -> toto
>
> "toto -> \"toto
>
> toto -> toto\ntiti
> titi
>
>
> My Current Solution:
> --------------------
>
> I can do this using a NSMutableString and a series of call to:
>
> - replaceOccurrencesOfString:withString:options:range:
>
> The problem I see with this solution is that I will probably forget some
> cases.
>
> Question:
> ---------
>
> Would there be a better solution? (I couldn't find a method in NSString,
> NSMutableString CFStringRef APIs but I might missed it).
Depending on your goal, it might be better to forget about special
cases and just escape *everything*.
E.g. instead of transforming "toto -> "\"toto", transform it to
"\x22\x74\x6f\x74\x6f" or to { 0x22, 0x74, 0x6f, 0x74, 0x6f, 0x00 }.
This obviously produces larger and somewhat less readable output, but
you're assured that it will work on any input and requires no special
casing.
If this happens to be part of a build script, also consider using
/usr/bin/xxd -i which will produce this sort of output for you with no
work on your part.
Mike
_______________________________________________
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