]> gitweb.factorcode.org Git - factor.git/commitdiff
prettyprint: print unprintables by hex escaping.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 29 Jul 2012 17:14:54 +0000 (10:14 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 29 Jul 2012 17:14:54 +0000 (10:14 -0700)
basis/prettyprint/backend/backend.factor
basis/prettyprint/prettyprint-tests.factor

index b43387d93e30945e18cba87f3daa867139512858..1d03707e6718c16e9013f004fe936d68615c508c 100644 (file)
@@ -96,7 +96,7 @@ M: f pprint* drop \ f pprint-word ;
     [ effect>string ] [ effect-style ] bi styled-text ;
 
 ! Strings
-: ch>ascii-escape ( ch -- str )
+: ch>ascii-escape ( ch -- ch' ? )
     H{
         { CHAR: \a CHAR: a  }
         { CHAR: \e CHAR: e  }
@@ -106,10 +106,12 @@ M: f pprint* drop \ f pprint-word ;
         { CHAR: \0 CHAR: 0  }
         { CHAR: \\ CHAR: \\ }
         { CHAR: \" CHAR: \" }
-    } at ;
+    } ?at ; inline
 
 : unparse-ch ( ch -- )
-    dup ch>ascii-escape [ "\\" % ] [ ] ?if , ;
+    ch>ascii-escape [ "\\" % , ] [
+        dup 32 < [ dup 16 < "\\x0" "\\x" ? % >hex % ] [ , ] if
+    ] if ;
 
 : do-string-limit ( str -- trimmed )
     string-limit? get [
index 79d0a664c127276412a2fa614f69e8e4fa09b323..17cedcc7e499a7cf105946dd1b54fed4092f01fa 100644 (file)
@@ -40,6 +40,10 @@ unit-test
 [ "\e" unparse ]
 unit-test
 
+[ "\"\\x01\"" ]
+[ 1 1string unparse ]
+unit-test
+
 [ "f" ] [ f unparse ] unit-test
 [ "t" ] [ t unparse ] unit-test