var ExFour = {};
(function(){ //var chars = '0123456789ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz .'; var chars = '0123456789abcdefghijklmnopqrstuvwxyz()!?@#%&*-;:\',./\n ', shift = 'ABCDEFGHIJKLMNOPQRSTUVWXYZß$^~_+={}|"<>[]`\\', shiftCode = chars.length, capsCode = shiftCode + 1;
var encodeChar = function(chr){ var doShift = false, idx = chars.indexOf(chr); // ordinary char
if(idx === -1){ idx = shift.indexOf(chr); // shift doShift = true; }
if(idx === -1){ // neither console.log(chr); return null; }
var ret = doShift ? DNA.padUInt(DNA.encodeUInt(shiftCode), 3) : ; return ret + DNA.padUInt(DNA.encodeUInt(idx), 3); };
var decChr = function(dna, doShift){ var code = DNA.decodeUInt(dna);
if(code === shiftCode || code === capsCode){ return code; }
if(code === null || code < 0 || code > chars.length){ return null; }
if(doShift){ ret = shift[code]; }else{ ret = chars[code]; }
return ret; };
var decodeChar = function(dna){ var code = decChr(dna.substr(0, 3), false), doShift = false;
if(code === shiftCode || code === capsCode){ // shift- or caps code found (note: caps is not memorized) doShift = true; if(dna.length < 6){ // no next (full) char return null; } dna = dna.substr(3); // decode next }
return decChr(dna, doShift); };
var encodeStr = function(str){ var ret = ;
for(var i = 0; i < str.length; i++){ var chr = encodeChar(str[i]); if(chr === null){ return null; } ret += chr; }
return ret; };
var decodeStr = function(dna){ var ret = , doShift = false, doCaps = false;
for(var i = 0; i < dna.length; i += 3){ var code = decChr(dna.substr(i, 3), doShift || doCaps);
if(code === capsCode){ doCaps = !doCaps; }else if(code === shiftCode){ doShift = true; }else{ ret += code; doShift = false; } }
return ret; };
ExFour = { 'encodeChar': encodeChar, 'decodeChar': decodeChar, 'encodeStr': encodeStr, 'decodeStr': decodeStr }; })();