Throw an error if an odd number of hex digits gets parsed by H{.

db4
Doug Coleman 2011-10-06 10:05:27 -07:00
parent 7a4ffd92ed
commit e7e6191df9
3 changed files with 18 additions and 3 deletions

View File

@ -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." } ;

View File

@ -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

View File

@ -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! ;