]> gitweb.factorcode.org Git - factor.git/commitdiff
math.parser: adding bytes>hex-string and hex-string>bytes.
authorJohn Benediktsson <mrjbq7@gmail.com>
Wed, 16 Mar 2016 17:02:25 +0000 (10:02 -0700)
committerJohn Benediktsson <mrjbq7@gmail.com>
Wed, 16 Mar 2016 17:13:56 +0000 (10:13 -0700)
core/math/parser/parser-docs.factor
core/math/parser/parser-tests.factor
core/math/parser/parser.factor

index 66456d7bfde96345b038818c703f357e9fee9621..211a2f7f94726e1565dbee7c84b7f79cb56a2c51 100644 (file)
@@ -1,5 +1,7 @@
-USING: help.markup help.syntax math math.parser.private prettyprint
-namespaces make strings ;
+USING: byte-arrays help.markup help.syntax math
+math.parser.private prettyprint namespaces make sequences
+strings ;
+
 IN: math.parser
 
 ARTICLE: "number-strings" "Converting between numbers and strings"
@@ -114,3 +116,20 @@ HELP: number>string
 HELP: #
 { $values { "n" real } }
 { $description "Appends the string representation of a real number to the end of the sequence being constructed by " { $link make } "." } ;
+
+HELP: bytes>hex-string
+{ $values { "bytes" sequence } { "hex-string" string } }
+{ $description "Converts a sequence of bytes (integers in the range [0,255]) to a string of hex numbers in the range [00,ff]." }
+{ $examples
+    { $example "USING: math.parser prettyprint ;" "B{ 1 2 3 4 } bytes>hex-string ." "\"01020304\"" }
+}
+{ $notes "Numbers are zero-padded on the left." } ;
+
+HELP: hex-string>bytes
+{ $values { "hex-string" sequence } { "bytes" byte-array } }
+{ $description "Converts a sequence of hex numbers in the range [00,ff] to a sequence of bytes (integers in the range [0,255])." }
+{ $examples
+    { $example "USING: math.parser prettyprint ;" "\"cafebabe\" hex-string>bytes ." "B{ 202 254 186 190 }" }
+} ;
+
+{ bytes>hex-string hex-string>bytes } related-words
index 5486c52d362d412ba3e98d3c83a266509a827011..51e1172d39d9d0d15dd9595f837645f59f955821 100644 (file)
@@ -458,3 +458,6 @@ unit-test
 { 0.0  } [ "1e-100000" string>number ] unit-test
 { 1/0. } [ "0x1p300000" string>number ] unit-test
 { 0.0  } [ "0x1p-300000" string>number ] unit-test
+
+{ "deadbeef" } [ B{ 222 173 190 239 } bytes>hex-string ] unit-test
+{ B{ 222 173 190 239 } } [ "deADbeEF" hex-string>bytes ] unit-test
index 4664c8b2526f8a44bafbcd66cfd47dbd5a9623c4..fca71a2c337bf618e3fd131433a6f29039966046 100644 (file)
@@ -1,8 +1,8 @@
 ! Copyright (C) 2009 Joe Groff, 2013 John Benediktsson
 ! See http://factorcode.org/license.txt for BSD license.
 USING: accessors byte-arrays combinators kernel kernel.private
-layouts make math math.private namespaces sbufs sequences
-sequences.private splitting strings strings.private ;
+layouts make math math.private sbufs sequences sequences.private
+strings strings.private ;
 IN: math.parser
 
 <PRIVATE
@@ -577,3 +577,23 @@ M: float >base
     } cond ;
 
 : # ( n -- ) number>string % ; inline
+
+: hex-string>bytes ( hex-string -- bytes )
+    dup length 2/ <byte-array> [
+        [
+            [ digit> ] 2dip over even? [
+                [ 16 * ] [ 2/ ] [ set-nth ] tri*
+            ] [
+                [ 2/ ] [ [ + ] change-nth ] bi*
+            ] if
+        ] curry each-index
+    ] keep ;
+
+: bytes>hex-string ( bytes -- hex-string )
+    dup length 2 * CHAR: 0 <string> [
+        [
+            [ 16 /mod [ >digit ] bi@ ]
+            [ 2 * dup 1 + ]
+            [ [ set-nth ] curry bi-curry@ bi* ] tri*
+        ] curry each-index
+    ] keep ;