From 2316c0a73ea89cd4a4859633398aa1dfc1138d9e Mon Sep 17 00:00:00 2001 From: Dave Sann Date: Sat, 14 Jul 2012 14:49:18 +1000 Subject: [PATCH] adding support for \x.. characters http://dev.clojure.org/jira/browse/CLJ-1025 --- src/jvm/clojure/lang/LispReader.java | 13 +++++++++++++ test/clojure/test_clojure/reader.clj | 19 +++++++++++++++++-- 2 files changed, 30 insertions(+), 2 deletions(-) diff --git a/src/jvm/clojure/lang/LispReader.java b/src/jvm/clojure/lang/LispReader.java index 8f67b3b..2576248 100644 --- a/src/jvm/clojure/lang/LispReader.java +++ b/src/jvm/clojure/lang/LispReader.java @@ -485,6 +485,14 @@ public static class StringReader extends AFn{ ch = readUnicodeChar((PushbackReader) r, ch, 16, 4, true); break; } + case 'x': + { + ch = read1(r); + if (Character.digit(ch, 16) == -1) + throw Util.runtimeException("Invalid unicode escape: \\x" + (char) ch); + ch = readUnicodeChar((PushbackReader) r, ch, 16, 2, true); + break; + } default: { if(Character.isDigit(ch)) @@ -938,6 +946,11 @@ public static class CharacterReader extends AFn{ throw Util.runtimeException("Invalid character constant: \\u" + Integer.toString(c, 16)); return c; } + else if(token.startsWith("x")) + { + char c = (char) readUnicodeChar(token, 1, 2, 16); + return c; + } else if(token.startsWith("o")) { int len = token.length() - 1; diff --git a/test/clojure/test_clojure/reader.clj b/test/clojure/test_clojure/reader.clj index d6178dc..6186d67 100644 --- a/test/clojure/test_clojure/reader.clj +++ b/test/clojure/test_clojure/reader.clj @@ -94,7 +94,15 @@ [0xdfff] "\\udfff" [0xe000] "\\ue000" [0xffff] "\\uffff" - [4 49] "\\u00041")) + [4 49] "\\u00041" + + [0] "\\x00" + [0 51] "\\x003" + [0xe0] "\\xe0" + [0xe0 51] "\\xe03" + [0xff] "\\xff" + [4 49] "\\x041" + )) (testing (str "Errors reading string literals from " (name source)) (are [err msg form] (thrown-with-msg? err msg (read-from source f (str "\"" form "\""))) @@ -113,7 +121,14 @@ Exception #"Invalid unicode escape: \\ug" "\\ug000" Exception #"Invalid character length: 1, should be: 4" "\\u0" Exception #"Invalid character length: 3, should be: 4" "\\u004" - Exception #"Invalid digit: g" "\\u004g"))))) + Exception #"Invalid digit: g" "\\u004g" + + Exception #"Invalid unicode escape: \\x" "\\x" + Exception #"Invalid unicode escape: \\xg" "\\xg" + Exception #"Invalid unicode escape: \\xg" "\\xg000" + Exception #"Invalid character length: 1, should be: 2" "\\x0" + Exception #"Invalid digit: g" "\\x4g" + ))))) ;; Numbers -- 1.7.5.4