clean up count-end

db4
Doug Coleman 2008-09-10 19:16:18 -05:00
parent d2f0ee3d1c
commit d97ff8d94d
1 changed files with 10 additions and 6 deletions

View File

@ -1,12 +1,13 @@
! Copyright (C) 2008 Doug Coleman. ! Copyright (C) 2008 Doug Coleman.
! See http://factorcode.org/license.txt for BSD license. ! See http://factorcode.org/license.txt for BSD license.
USING: kernel math sequences io.binary splitting grouping ; USING: kernel math sequences io.binary splitting grouping
accessors ;
IN: base64 IN: base64
<PRIVATE <PRIVATE
: count-end ( seq quot -- count ) : count-end ( seq quot -- n )
>r [ length ] keep r> find-last drop dup [ - 1- ] [ 2drop 0 ] if ; inline trim-right-slice [ seq>> length ] [ to>> ] bi - ; inline
: ch>base64 ( ch -- ch ) : ch>base64 ( ch -- ch )
"ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" nth ; "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/" nth ;
@ -21,13 +22,16 @@ IN: base64
} nth ; } nth ;
: encode3 ( seq -- seq ) : encode3 ( seq -- seq )
be> 4 <reversed> [ -6 * shift HEX: 3f bitand ch>base64 ] with B{ } map-as ; be> 4 <reversed> [
-6 * shift HEX: 3f bitand ch>base64
] with B{ } map-as ;
: decode4 ( str -- str ) : decode4 ( str -- str )
0 [ base64>ch swap 6 shift bitor ] reduce 3 >be ; 0 [ base64>ch swap 6 shift bitor ] reduce 3 >be ;
: >base64-rem ( str -- str ) : >base64-rem ( str -- str )
[ 3 0 pad-right encode3 ] [ length 1+ ] bi head 4 CHAR: = pad-right ; [ 3 0 pad-right encode3 ] [ length 1+ ] bi
head-slice 4 CHAR: = pad-right ;
PRIVATE> PRIVATE>
@ -42,5 +46,5 @@ PRIVATE>
: base64> ( base64 -- str ) : base64> ( base64 -- str )
#! input length must be a multiple of 4 #! input length must be a multiple of 4
[ 4 <groups> [ decode4 ] map concat ] [ 4 <groups> [ decode4 ] map concat ]
[ [ CHAR: = = not ] count-end ] [ [ CHAR: = = ] count-end ]
bi head* ; bi head* ;