1 ! Copyright (C) 2019 John Benediktsson.
2 ! See http://factorcode.org/license.txt for BSD license.
3 USING: base64.private byte-arrays kernel kernel.private
4 literals math sequences ;
7 ERROR: malformed-base91 ;
13 "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789!#$%&()*+,./:;<=>?@[]^_`{|}~\""
18 : ch>base91 ( ch -- ch )
21 : base91>ch ( ch -- ch )
22 $[ alphabet alphabet-inverse ] nth
23 [ malformed-base91 ] unless* { fixnum } declare ; inline
27 :: >base91 ( seq -- base91 )
36 b 0x1fff bitand dup 88 > [
43 ] if 91 /mod swap [ ch>base91 accum push ] bi@
48 b 91 mod ch>base91 accum push
50 b 91 /i ch>base91 accum push
56 :: base91> ( base91 -- seq )
67 v 0x1fff bitand 88 > 13 14 ? n + n!
69 b 0xff bitand accum push
80 b v n shift bitor 0xff bitand accum push