From f1e232c9812e570943f76bc9a1e6e3577659c2e9 Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Sun, 29 Jul 2012 10:14:54 -0700 Subject: [PATCH] prettyprint: print unprintables by hex escaping. --- basis/prettyprint/backend/backend.factor | 8 +++++--- basis/prettyprint/prettyprint-tests.factor | 4 ++++ 2 files changed, 9 insertions(+), 3 deletions(-) diff --git a/basis/prettyprint/backend/backend.factor b/basis/prettyprint/backend/backend.factor index b43387d93e..1d03707e67 100644 --- a/basis/prettyprint/backend/backend.factor +++ b/basis/prettyprint/backend/backend.factor @@ -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 [ diff --git a/basis/prettyprint/prettyprint-tests.factor b/basis/prettyprint/prettyprint-tests.factor index 79d0a664c1..17cedcc7e4 100644 --- a/basis/prettyprint/prettyprint-tests.factor +++ b/basis/prettyprint/prettyprint-tests.factor @@ -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