KBD

Keith Devens .com

Tuesday, October 14, 2008 Flag waving
I was always told, that if I ain't gonna be part of the greatest, I gotta be the greatest... – Busta Rhymes
← An Eye for Annai (animated short)Brad Cox's Objective-C Book →

Daily link icon Friday, November 4, 2005

String escaping behavior

Which do you think is the more appropriate behavior for escapes that aren't escapes in your programming language: Say you have an escape that doesn't mean anything, like "\g". Should your programming language turn that into "g" or leave it as just "\g"?

$>python -c "print \"\g\""
\g

$>perl -e "print \"\g\""
g

$>ruby -e "print \"\g\""
g

$>php -r "print \"\g\";"
\g

$>lua -e "print \"\g\""
g

$>cat test.c
#include<stdio.h>
int main(){printf("\g");}

$>gcc test.c
test.c:2:19: warning: unknown escape sequence '\g'

$>a
g

$>MzScheme -e "(display \"\g\")"
Welcome to MzScheme version 299.400...
#f::9: read: unknown escape sequence \g in string
> ^Z

Anyone want to test C# and Java for me (since I don't have the interpreters on my system)? More importantly, want to tell me which you prefer? What does JSON do? (Update: it's not defined in JSON, but an alert("\g") in Firefox's JS console gives 'g').

← An Eye for Annai (animated short)Brad Cox's Objective-C Book →

Comments XML gif

DJ Hannibal wrote:

C# exhibits the same behaviour as C/C++, which you can generally count on. Java should throw a compiler error, but who knows with that obtuse, daft, and inane language.

∴ DJ Hannibal | 4-Nov-2005 9:15am est | #8606

Anonymous wrote:

My copy of C# from VS2005:
Program.cs(11,31): error CS1009: Unrecognized escape sequence
No runtime behavior since the build fails

∴ Anonymous | 4-Nov-2005 9:51am est | #8608

Davd wrote:

Wow, I'm really upset at Python. I love Python, but clearly an escaped character that doesn't need to be escaped is just a regular character. You should be able to escape every character in a string and just get that string back.

What is "escaping" if not "take the next character literally".

Consider it this way... how to you get a "" in the result string. Shouldn't you only get a "" from "\". The first "" in any situation is a control character and should get eaten.

Wow... this is a very crushing revelation. As my regexps get more complex, I'm going to have to watch out for this.

∴ Davd | 4-Nov-2005 11:43am est | #8609

Keith (http://keithdevens.com/) wrote:

What is "escaping" if not "take the next character literally".

It doesn't mean that at all. "\n" means "newline", "\b" means "Bell", "\uEF29" means Unicode character... and so on.

You should be able to escape every character in a string and just get that string back.

Huh? No, you'd wind up with a string full of newlines and tabs and such.

Keith | 5-Nov-2005 1:17pm est | http://keithdevens.com/ | #8618

Feel free to post a comment below. Please see my comment policy.

Formatting Rules (No HTML):

  • **bold**, *italic*, _underlined_, --strikeout--
  • "text"="url" creates a link, and URLs are auto-highlighted
  • Blockquote: Like e-mail, begin paragraph with > (greater-than sign)
  • Lists: begin paragraph with *,-, or + (unordered), or # (ordered)
  • Code block: ?!code:language=perl|php|sql|javascript|etc.{\n}...{\n}?!/code

:
(will be your IP address if blank)
: (optional)
(Will not be shown on site)

: (optional)
:

October 2008
SunMonTueWedThuFriSat
 1234
567891011
12131415161718
19202122232425
262728293031 



RSS feed RSS feed for Keith's Weblog
Atom feed Atom feed for Keith's Weblog
Weblog archive
Recent comments
  on 6 posts

Recent comments XML

new⇒Spider solitaire

I have now won, at the "Difficult"​level, 186 games of Spider​Solitaire.  I...

75.179.28.113: Oct 13, 9:34am

Girls, please don't get breast implants

Please, don't marry him.

You​want to get surgery to make​yourself more i...

gigi: Oct 12, 11:47pm

I hate PHP

Elliot Anderson,

Dude!! You the​man! The reverse replacement for​array_u...

Alex Ndungu: Oct 11, 1:35am

Call a function from a string in Python

?!code:
some_object.__getattribute​__('method_name')()
?!/code

is​the s...

Patrick Corcoran: Oct 8, 3:53pm

Sed one-liners

Hi.

I wanted to let you know​that I wrote an article "Famous Sed​One-Lin...

Peteris Krumins: Oct 8, 3:05am

Generated in about 0.676s.

(Used 8 db queries)

mobile phone