EXPORTER = (exports ? this)
BASE52 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
BASE57 = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
EXPORTER = (exports ? this)
BASE52 = "ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
BASE57 = "23456789ABCDEFGHJKLMNPQRSTUVWXYZabcdefghijkmnopqrstuvwxyz"
Our base57 digit set is A-Za-z0-9 less the five ambiguous ones: zero, one, capital I, capital O and lower case ell: 01OIl so 52+10-5 = 57
BASE62 = "0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz"
BASE10 = "0123456789"
BASE2 = "01"
BASE8 = "01234567"
BASE10 = "0123456789"
BASE16 = "0123456789abcdef"
BASE75 = "0123456789abcdefghijklmnopqrstuvwxyzABCDEFGHIJKLMNOPQRSTUVWXYZ_.,!=-*(){}[]"
convert.js
http://rot47.net
Dr Zhihua Lai
convert = (src, srctable, desttable) ->
srclen = srctable.length
destlen = desttable.length
first convert to base 10
val = 0
numlen = src.length
i = 0
while i < numlen
val = val * srclen + srctable.indexOf(src.charAt(i))
i++
return 0 if val < 0
then covert to any base
r = val % destlen
res = desttable.charAt(r)
q = Math.floor(val / destlen)
while q
r = q % destlen
q = Math.floor(q / destlen)
res = desttable.charAt(r) + res
res
EXPORTER.DEFAULT_BASE = BASE57
base_to_int = (base) ->
parseInt(convert(base, EXPORTER.DEFAULT_BASE, BASE10))
EXPORTER.base_to_int = base_to_int
int_to_base = (intgr) ->
convert(""+intgr, BASE10, EXPORTER.DEFAULT_BASE)
EXPORTER.int_to_base = int_to_base
modulo_char_at = (base, byte) ->
return base[byte % base.length]
symbol_from_bytes = (bytes, base) ->
Purpose Return a symbol (ie does not start with a digit) from an array Accept bytes: an array of bytes base: an array of characters used as a base (optional or DEFAULT_BASE used)
if not base?
base = EXPORTER.DEFAULT_BASE
if not EXPORTER.DEFAULT_BASE_nondig
cache nondig for reuse
EXPORTER.DEFAULT_BASE_nondig = base.replace(/\d/g,'')
nondig = EXPORTER.DEFAULT_BASE_nondig
else
nondig = base.replace(/\d/g,'')
out = [modulo_char_at(nondig,bytes[0])]
for b in bytes[1...bytes.length] by 1
out.push(modulo_char_at(base, b))
return out.join('')
logBASE = Math.log(EXPORTER.DEFAULT_BASE.length)
We count the number of digits in the base. WARNING this assumes that the digits are all at the beginning of the BASE
numDigitsBASE = EXPORTER.DEFAULT_BASE.match(/^\d*/g)[0].length
int_to_base_symbol = (intgr) ->
FIXME(smurp) bugged!!!! because several inputs will produce the same output Return a value which does not start with a digit
If the intgr is going to cause an response which starts with a digit \d then increase intgr to the first value which will not do so.
This line is a presumably slow workaround if (“”+(Math.log(intgr)/logBASE)).indexOf(“.”) is -1 for this line, which is legal only in more recent coffeescript if ((Math.log(intgr)/logBASE) %% 1) is 0 so we will gear down to javascript native code
`if (((Math.log(intgr)/logBASE) % 1) === 0) {
intgr = intgr * numDigitsBASE;
}`
return [int_to_base(intgr), intgr]
EXPORTER.make_symbol = int_to_base_symbol
EXPORTER.int_to_base = int_to_base
EXPORTER.symbol_from_bytes = symbol_from_bytes
EXPORTER.rebase = convert