]> gitweb.factorcode.org Git - factor.git/commitdiff
io.binary.fast: write generic be> and le> words.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sun, 24 Nov 2013 18:45:25 +0000 (10:45 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sun, 24 Nov 2013 18:45:25 +0000 (10:45 -0800)
extra/io/binary/fast/fast.factor

index 07ab1766d459c26417ea8040d42f6e160adfc44d..4773434608a305c2290365996fa20929ac493dcc 100644 (file)
@@ -3,6 +3,8 @@
 USING: combinators combinators.smart fry kernel macros math
 math.ranges sequences sequences.generalizations io.binary
 locals ;
+RENAME: be> io.binary => slow-be>
+RENAME: le> io.binary => slow-le>
 IN: io.binary.fast
 
 ERROR: bad-length bytes n ;
@@ -36,7 +38,22 @@ MACRO: reassemble-le ( n -- quot ) le-range reassemble-bytes ;
 : 4be> ( bytes -- x ) 4 n-be> ;
 : 8be> ( bytes -- x ) 8 n-be> ;
 
+: be> ( bytes -- x )
+    dup length {
+        { 2 [ 2be> ] }
+        { 4 [ 4be> ] }
+        { 8 [ 8be> ] }
+        [ drop slow-be> ]
+    } case ;
+
 : 2le> ( bytes -- x ) 2 n-le> ;
 : 4le> ( bytes -- x ) 4 n-le> ;
 : 8le> ( bytes -- x ) 8 n-le> ;
 
+: le> ( bytes -- x )
+    dup length {
+        { 2 [ 2le> ] }
+        { 4 [ 4le> ] }
+        { 8 [ 8le> ] }
+        [ drop slow-le> ]
+    } case ;