On Jan 26, 2012, at 11:30 AM, Jens Alfke wrote:

> To work around this I suggest using single-quotes instead, and preprocessing 
> the string to insert a backslash in front of any exclamation point or 
> single-quote. I *think* that will be enough.

After a single quote, the *only* character that has a special meaning is 
another single quote, which ends the quotation. Double quotes, back slashes, 
dollar signs, exclamation marks, et al. have absolutely no special significance 
between single quotes..

Thus, the simplest way to safely quote an arbitrary string is to preprocess the 
string to replace each single quote (') with the sequence single quote, 
backslash, single quote, single quote ('\''), and then wrap the resulting 
string in single quotes.

As an (unnecessary) refinement, scan that sequence again, replacing each 
sequence of three consecutive single quotes with one, to shorten the string 
that results when the original string contained consecutive single quotes. As 
another (unnecessary) refinement, pre-scan the original string to see if it 
contains only safe characters, in which case no quoting needs to be done at 
all. It's probably not worth bothering to do either of these.


Almost as easy to program, but generally producing longer quoted strings, is to 
prepend a backslash to any character that is not a lowercase letter or a digit. 
Do not wrap the resulting string in any kind of quotes. (Non-ASCII characters 
are never special to the shell, but I'm not sure what would happen if you 
inserted backslashes in the middle of a multi-byte character, nor whether the 
behavior will change in later versions of the shell, so be sure you're 
prepending backslashes to characters, not bytes.)

-Ron Hunsinger


_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

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:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to