strings.parser: support "\x" ascii hex escapes.

db4
John Benediktsson 2012-07-26 19:24:25 -07:00
parent fbd79553e0
commit a208946b63
2 changed files with 14 additions and 8 deletions

View File

@ -34,3 +34,6 @@ IN: strings.parser.tests
[ "\"\\" ] [ "\"\\" ] unit-test [ "\"\\" ] [ "\"\\" ] unit-test
[ "\e" ] [ "\u00001b" ] unit-test
[ "\e" ] [ "\x1b" ] unit-test

View File

@ -1,8 +1,8 @@
! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman. ! Copyright (C) 2008, 2009 Slava Pestov, Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: accessors assocs kernel lexer make math math.parser USING: accessors arrays assocs combinators kernel lexer make
namespaces parser sequences splitting strings arrays math math.order math.parser namespaces parser sequences
math.order ; splitting strings ;
IN: strings.parser IN: strings.parser
ERROR: bad-escape char ; ERROR: bad-escape char ;
@ -27,6 +27,9 @@ name>char-hook [
[ "Unicode support not available" throw ] [ "Unicode support not available" throw ]
] initialize ] initialize
: hex-escape ( str -- ch str' )
2 cut-slice [ hex> ] dip ;
: unicode-escape ( str -- ch str' ) : unicode-escape ( str -- ch str' )
"{" ?head-slice [ "{" ?head-slice [
CHAR: } over index cut-slice CHAR: } over index cut-slice
@ -37,11 +40,11 @@ name>char-hook [
] if ; ] if ;
: next-escape ( str -- ch str' ) : next-escape ( str -- ch str' )
"u" ?head-slice [ dup first {
unicode-escape { CHAR: u [ 1 tail-slice unicode-escape ] }
] [ { CHAR: x [ 1 tail-slice hex-escape ] }
unclip-slice escape swap [ drop unclip-slice escape swap ]
] if ; } case ;
: (unescape-string) ( str -- ) : (unescape-string) ( str -- )
CHAR: \\ over index dup [ CHAR: \\ over index dup [