asciiblaster- draw irc art in your web browser |
git clone git://git.acid.vegas/asciiblaster.git |
Log | Files | Refs | Archive | README |
text-encoder-lite.js (3463B)
1 // taken from https://github.com/coolaj86/TextEncoderLite/blob/master/index.js 2 // added polyfill at bottom 3 4 function TextEncoderLite() { 5 } 6 function TextDecoderLite() { 7 } 8 9 (function () { 10 'use strict'; 11 12 // Taken from https://github.com/feross/buffer/blob/master/index.js 13 // Thanks Feross et al! :-) 14 15 function utf8ToBytes (string, units) { 16 units = units || Infinity 17 var codePoint 18 var length = string.length 19 var leadSurrogate = null 20 var bytes = [] 21 var i = 0 22 23 for (; i < length; i++) { 24 codePoint = string.charCodeAt(i) 25 26 // is surrogate component 27 if (codePoint > 0xD7FF && codePoint < 0xE000) { 28 // last char was a lead 29 if (leadSurrogate) { 30 // 2 leads in a row 31 if (codePoint < 0xDC00) { 32 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 33 leadSurrogate = codePoint 34 continue 35 } else { 36 // valid surrogate pair 37 codePoint = leadSurrogate - 0xD800 << 10 | codePoint - 0xDC00 | 0x10000 38 leadSurrogate = null 39 } 40 } else { 41 // no lead yet 42 43 if (codePoint > 0xDBFF) { 44 // unexpected trail 45 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 46 continue 47 } else if (i + 1 === length) { 48 // unpaired lead 49 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 50 continue 51 } else { 52 // valid lead 53 leadSurrogate = codePoint 54 continue 55 } 56 } 57 } else if (leadSurrogate) { 58 // valid bmp char, but last char was a lead 59 if ((units -= 3) > -1) bytes.push(0xEF, 0xBF, 0xBD) 60 leadSurrogate = null 61 } 62 63 // encode utf8 64 if (codePoint < 0x80) { 65 if ((units -= 1) < 0) break 66 bytes.push(codePoint) 67 } else if (codePoint < 0x800) { 68 if ((units -= 2) < 0) break 69 bytes.push( 70 codePoint >> 0x6 | 0xC0, 71 codePoint & 0x3F | 0x80 72 ) 73 } else if (codePoint < 0x10000) { 74 if ((units -= 3) < 0) break 75 bytes.push( 76 codePoint >> 0xC | 0xE0, 77 codePoint >> 0x6 & 0x3F | 0x80, 78 codePoint & 0x3F | 0x80 79 ) 80 } else if (codePoint < 0x200000) { 81 if ((units -= 4) < 0) break 82 bytes.push( 83 codePoint >> 0x12 | 0xF0, 84 codePoint >> 0xC & 0x3F | 0x80, 85 codePoint >> 0x6 & 0x3F | 0x80, 86 codePoint & 0x3F | 0x80 87 ) 88 } else { 89 throw new Error('Invalid code point') 90 } 91 } 92 93 return bytes 94 } 95 96 function utf8Slice (buf, start, end) { 97 var res = '' 98 var tmp = '' 99 end = Math.min(buf.length, end || Infinity) 100 start = start || 0; 101 102 for (var i = start; i < end; i++) { 103 if (buf[i] <= 0x7F) { 104 res += decodeUtf8Char(tmp) + String.fromCharCode(buf[i]) 105 tmp = '' 106 } else { 107 tmp += '%' + buf[i].toString(16) 108 } 109 } 110 111 return res + decodeUtf8Char(tmp) 112 } 113 114 function decodeUtf8Char (str) { 115 try { 116 return decodeURIComponent(str) 117 } catch (err) { 118 return String.fromCharCode(0xFFFD) // UTF 8 invalid char 119 } 120 } 121 122 TextEncoderLite.prototype.encode = function (str) { 123 var result; 124 125 if ('undefined' === typeof Uint8Array) { 126 result = utf8ToBytes(str); 127 } else { 128 result = new Uint8Array(utf8ToBytes(str)); 129 } 130 131 return result; 132 }; 133 134 TextDecoderLite.prototype.decode = function (bytes) { 135 return utf8Slice(bytes, 0, bytes.length); 136 } 137 138 }()); 139 140 if (typeof TextEncoder === 'undefined') TextEncoder = TextEncoderLite 141 if (typeof TextDecoder === 'undefined') TextDecoder = TextDecoderLite