]> gitweb.factorcode.org Git - factor.git/commitdiff
base85: change to not pad encoding, fix output bug.
authorJohn Benediktsson <mrjbq7@gmail.com>
Sat, 26 Jan 2019 05:14:11 +0000 (21:14 -0800)
committerJohn Benediktsson <mrjbq7@gmail.com>
Sat, 26 Jan 2019 05:14:11 +0000 (21:14 -0800)
extra/base85/base85-tests.factor
extra/base85/base85.factor

index a55b8292a0fbfd32251b70c875e5382759352f73..4f1ebd6193245affb209eefece42b7a5f8b5b34c 100644 (file)
@@ -3,5 +3,8 @@ USING: base85 kernel strings tools.test ;
 { t } [ "Hello, world" dup >base85 base85> >string = ] unit-test
 { t } [ "ready" dup >base85 base85> >string = ] unit-test
 
-{ "NM!&3" } [ "He" >base85 >string ] unit-test
+{ "NM!" } [ "He" >base85 >string ] unit-test
 { t } [ "He" dup >base85 base85> >string = ] unit-test
+
+{ "00" } [ B{ 0 } >base85 >string ] unit-test
+{ "\0" } [ "00" base85> >string ] unit-test
index 3ddfcc8326b312a41b8bb87ffbb1b6de9225be38..22fcf752b7e5582fd50c9191480008684c9b9aa8 100644 (file)
@@ -27,7 +27,12 @@ CONSTANT: alphabet
     4 pick stream-read dup length {
         { 0 [ 3drop ] }
         { 4 [ encode4 write-lines (encode-base85) ] }
-        [ drop 4 0 pad-tail encode4 write-lines (encode-base85) ]
+        [
+            drop
+            [ 4 0 pad-tail encode4 ]
+            [ length neg 4 rem head-slice* write-lines ] bi
+            (encode-base85)
+        ]
     } case ;
 
 PRIVATE>
@@ -40,15 +45,19 @@ PRIVATE>
 
 <PRIVATE
 
-: decode5 ( seq -- )
-    0 [ [ 85 * ] [ base85>ch ] bi* + ] reduce 4 >be
-    [ zero? ] trim-tail-slice write ; inline
+: decode5 ( seq -- seq' )
+    0 [ [ 85 * ] [ base85>ch ] bi* + ] reduce 4 >be ; inline
 
 : (decode-base85) ( stream -- )
     5 "\n\r" pick read-ignoring dup length {
         { 0 [ 2drop ] }
-        { 5 [ decode5 (decode-base85) ] }
-        [ malformed-base85 ]
+        { 5 [ decode5 write (decode-base85) ] }
+        [
+            drop
+            [ 5 CHAR: ~ pad-tail decode5 ]
+            [ length neg 5 rem head-slice* write ] bi
+            (decode-base85)
+        ]
     } case ;
 
 PRIVATE>