diff --git a/basis/byte-arrays/hex/hex-docs.factor b/basis/byte-arrays/hex/hex-docs.factor index 8a2b842fc9..c2cac8d584 100644 --- a/basis/byte-arrays/hex/hex-docs.factor +++ b/basis/byte-arrays/hex/hex-docs.factor @@ -5,4 +5,4 @@ USING: byte-arrays help.markup help.syntax ; HELP: HEX{ { $syntax "HEX{ 0123 45 67 89abcdef }" } -{ $description "Constructs a " { $link byte-array } " from data specified in hexadecimal format. Whitespace between the curly braces is ignored." } ; +{ $description "Constructs a " { $link byte-array } " from data specified in hexadecimal format. Whitespace between the curly braces is ignored. There must be an even number of hex digits or an error is thrown." } ; diff --git a/basis/byte-arrays/hex/hex-tests.factor b/basis/byte-arrays/hex/hex-tests.factor new file mode 100644 index 0000000000..25f15405e9 --- /dev/null +++ b/basis/byte-arrays/hex/hex-tests.factor @@ -0,0 +1,11 @@ +! Copyright (C) 2011 Doug Coleman. +! See http://factorcode.org/license.txt for BSD license. +USING: tools.test byte-arrays.hex eval ; +IN: byte-arrays.hex.tests + +[ B{ 16 0 8 0 } ] [ HEX{ 10 00 08 00 } ] unit-test +[ B{ 255 255 15 255 255 255 } ] [ HEX{ ffff 0fff ffff } ] unit-test + +[ "HEX{ ffff fff ffff }" parse-string ] must-fail +[ "HEX{ 10 00 08 0 }" parse-string ] must-fail +[ "HEX{ 1 00 00 80 }" parse-string ] must-fail diff --git a/basis/byte-arrays/hex/hex.factor b/basis/byte-arrays/hex/hex.factor index ae9b9c8ba2..4912b0b98b 100644 --- a/basis/byte-arrays/hex/hex.factor +++ b/basis/byte-arrays/hex/hex.factor @@ -1,10 +1,14 @@ -! Copyright (C) 2009 Maxim Savchenko, Slava Pestov. +! Copyright (C) 2009,2011 Maxim Savchenko, Slava Pestov, Doug Coleman. ! See http://factorcode.org/license.txt for BSD license. -USING: grouping lexer ascii parser sequences kernel math.parser ; +USING: ascii grouping kernel math.parser sequences +strings.parser lexer math ; IN: byte-arrays.hex +ERROR: odd-length-hex-string string ; + SYNTAX: HEX{ "}" parse-tokens "" join [ blank? not ] filter + dup length even? [ odd-length-hex-string ] unless 2 group [ hex> ] B{ } map-as suffix! ;