base32-crockford: swap stack effects and update tests.

clean-macosx-x86-32
John Benediktsson 2019-05-20 10:20:47 -07:00
parent 414a426880
commit 1b10b8dbf4
2 changed files with 21 additions and 21 deletions

View File

@ -3,18 +3,18 @@
USING: base32-crockford tools.test ;
{ "16J" } [ 1234 base32-crockford> ] unit-test
{ "16JD" } [ 1234 base32-crockford-checksum> ] unit-test
{ "0" } [ 0 base32-crockford> ] unit-test
{ "00" } [ 0 base32-crockford-checksum> ] unit-test
[ -1 base32-crockford> ] must-fail
[ 1.0 base32-crockford> ] must-fail
{ "16J" } [ 1234 >base32-crockford ] unit-test
{ "16JD" } [ 1234 >base32-crockford-checksum ] unit-test
{ "0" } [ 0 >base32-crockford ] unit-test
{ "00" } [ 0 >base32-crockford-checksum ] unit-test
[ -1 >base32-crockford ] must-fail
[ 1.0 >base32-crockford ] must-fail
{ 1234 } [ "16J" >base32-crockford ] unit-test
{ 1234 } [ "I6J" >base32-crockford ] unit-test
{ 1234 } [ "i6J" >base32-crockford ] unit-test
{ 1234 } [ "16JD" >base32-crockford-checksum ] unit-test
{ 1234 } [ "I6JD" >base32-crockford-checksum ] unit-test
{ 1234 } [ "i6JD" >base32-crockford-checksum ] unit-test
{ 0 } [ "0" >base32-crockford ] unit-test
{ 0 } [ "00" >base32-crockford-checksum ] unit-test
{ 1234 } [ "16J" base32-crockford> ] unit-test
{ 1234 } [ "I6J" base32-crockford> ] unit-test
{ 1234 } [ "i6J" base32-crockford> ] unit-test
{ 1234 } [ "16JD" base32-crockford-checksum> ] unit-test
{ 1234 } [ "I6JD" base32-crockford-checksum> ] unit-test
{ 1234 } [ "i6JD" base32-crockford-checksum> ] unit-test
{ 0 } [ "0" base32-crockford> ] unit-test
{ 0 } [ "00" base32-crockford-checksum> ] unit-test

View File

@ -15,29 +15,29 @@ CONSTANT: INVERSE $[ 256 [ ALPHABET index 0xff or ] B{ } map-integers ]
CONSTANT: CHECKSUM $[ ALPHABET "*~$=U" append ]
: normalize-base32 ( seq -- seq' )
: normalize-base32 ( base32 -- base32' )
CHAR: - swap remove >upper H{
{ CHAR: I CHAR: 1 }
{ CHAR: L CHAR: 1 }
{ CHAR: O CHAR: 0 }
} substitute ;
: parse-base32 ( seq -- base32 )
: parse-base32 ( base32 -- n )
0 swap [ [ 32 * ] [ INVERSE nth + ] bi* ] each ;
PRIVATE>
: >base32-crockford ( seq -- base32 )
: base32-crockford> ( base32 -- n )
normalize-base32 parse-base32 ;
: base32-crockford> ( base32 -- seq )
: >base32-crockford ( n -- base32 )
dup 0 < [ non-negative-integer-expected ] when
[ dup 0 > ] [ 32 /mod ALPHABET nth ] "" produce-as nip
[ "0" ] when-empty reverse! ;
: >base32-crockford-checksum ( seq -- base32 )
: base32-crockford-checksum> ( base32 -- n )
normalize-base32 unclip-last [ parse-base32 ] dip
CHECKSUM index over 37 mod assert= ;
: base32-crockford-checksum> ( base32 -- seq )
[ base32-crockford> ] keep 37 mod CHECKSUM nth suffix ;
: >base32-crockford-checksum ( n -- base32 )
[ >base32-crockford ] keep 37 mod CHECKSUM nth suffix ;