{ "foo" } [ "\"\\\nfoo\"" eval( -- obj ) ] unit-test
{ "foo" } [ "\"foo\\\n\"" eval( -- obj ) ] unit-test
{ "foo bar" } [ "\"foo \\\nbar\"" eval( -- obj ) ] unit-test
+
+{ "a" } [ "\"\\141\"" eval( -- obj ) ] unit-test
+{ "\0" } [ "\"\\0\"" eval( -- obj ) ] unit-test
+{ "\x01" } [ "\"\\01\"" eval( -- obj ) ] unit-test
+{ "\n" } [ "\"\\012\"" eval( -- obj ) ] unit-test
+{ "\x01d2" } [ "\"\\01d2\"" eval( -- obj ) ] unit-test
+{ "\x018" } [ "\"\\018\"" eval( -- obj ) ] unit-test
! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs combinators kernel kernel.private lexer
-math math.parser namespaces sbufs sequences splitting strings ;
+math math.order math.parser namespaces sbufs sequences splitting
+strings ;
IN: strings.parser
ERROR: bad-escape char ;
: hex-escape ( str -- ch str' )
2 cut-slice [ hex> ] dip ;
+: oct-escape ( str -- ch/f str' )
+ dup [
+ 3 short head-slice [ CHAR: 0 CHAR: 7 between? not ] find drop
+ ] keep '[ _ length ] unless* [ f ] when-zero
+ [ cut-slice [ oct> ] dip ] [ f swap ] if* ;
+
: unicode-escape ( str -- ch str' )
"{" ?head-slice [
CHAR: } over index cut-slice [
] if ;
: next-escape ( str -- ch str' )
- unclip-slice {
- { CHAR: u [ unicode-escape ] }
- { CHAR: x [ hex-escape ] }
- { CHAR: \n [ f swap ] }
- [ escape swap ]
- } case ;
+ oct-escape over [
+ nip unclip-slice {
+ { CHAR: u [ unicode-escape ] }
+ { CHAR: x [ hex-escape ] }
+ { CHAR: \n [ f swap ] }
+ [ escape swap ]
+ } case
+ ] unless ;
<PRIVATE
{ { $snippet "\\u" { $emphasis "xxxxxx" } } { "The Unicode code point with hexadecimal number " { $snippet { $emphasis "xxxxxx" } } } }
{ { $snippet "\\u{" { $emphasis "xx" } "}" } { "The Unicode code point with hexadecimal number " { $snippet { $emphasis "xx" } } } }
{ { $snippet "\\u{" { $emphasis "name" } "}" } { "The Unicode code point named " { $snippet { $emphasis "name" } } } }
+ { { $snippet "\\xxx" } "an octal escape specified by one, two, or three octal digits" }
} ;
ARTICLE: "syntax-strings" "Character and string syntax"