From af4dd22fe908bc78a1e700ef5466cbfd224374eb Mon Sep 17 00:00:00 2001 From: John Benediktsson Date: Thu, 4 Apr 2013 11:43:17 -0700 Subject: [PATCH] base85: fix handling of pad characters. --- extra/base85/base85-tests.factor | 3 +++ extra/base85/base85.factor | 12 ++++-------- 2 files changed, 7 insertions(+), 8 deletions(-) diff --git a/extra/base85/base85-tests.factor b/extra/base85/base85-tests.factor index 57cc34fe86..2b3171fd7b 100644 --- a/extra/base85/base85-tests.factor +++ b/extra/base85/base85-tests.factor @@ -2,3 +2,6 @@ USING: kernel strings tools.test ; IN: base85 { t } [ "Hello, world" dup >base85 base85> >string = ] unit-test + +{ "NM!&3" } [ "He" >base85 >string ] unit-test +{ t } [ "He" dup >base85 base85> >string = ] unit-test diff --git a/extra/base85/base85.factor b/extra/base85/base85.factor index f32248b353..51c3a0338d 100644 --- a/extra/base85/base85.factor +++ b/extra/base85/base85.factor @@ -29,15 +29,11 @@ ERROR: malformed-base85 ; reverse! nip [ _ write1-lines ] each ] change ; inline -: encode-pad ( seq n -- ) - [ 4 0 pad-tail binary [ encode4 ] with-byte-writer ] - [ 1 + ] bi* head-slice 5 CHAR: = pad-tail write-lines ; inline - : (encode-base85) ( stream -- ) 4 over stream-read dup length { { 0 [ 2drop ] } { 4 [ encode4 (encode-base85) ] } - [ encode-pad (encode-base85) ] + [ drop 4 0 pad-tail encode4 (encode-base85) ] } case ; PRIVATE> @@ -51,9 +47,9 @@ PRIVATE> ch ] bi* + ] reduce 4 >be ] - [ [ CHAR: = = ] count ] bi head-slice* - output-stream get '[ _ stream-write1 ] each ; inline + 0 [ [ 85 * ] [ base85>ch ] bi* + ] reduce 4 >be + [ zero? ] trim-tail-slice output-stream get + '[ _ stream-write1 ] each ; inline : (decode-base85) ( stream -- ) 5 "\n\r" pick read-ignoring dup length {