comunicationLeap/node_modules/utf8/utf8.js
node_modules/utf8/utf8.jsRaw
1/*! http://mths.be/utf8js v2.0.0 by @mathias */
2;(function(root) {
3
4 // Detect free variables `exports`
5 var freeExports = typeof exports == 'object' && exports;
6
7 // Detect free variable `module`
8 var freeModule = typeof module == 'object' && module &&
9 module.exports == freeExports && module;
10
11 // Detect free variable `global`, from Node.js or Browserified code,
12 // and use it as `root`
13 var freeGlobal = typeof global == 'object' && global;
14 if (freeGlobal.global === freeGlobal || freeGlobal.window === freeGlobal) {
15 root = freeGlobal;
16 }
17
18 /*--------------------------------------------------------------------------*/
19
20 var stringFromCharCode = String.fromCharCode;
21
22 // Taken from http://mths.be/punycode
23 function ucs2decode(string) {
24 var output = [];
25 var counter = 0;
26 var length = string.length;
27 var value;
28 var extra;
29 while (counter < length) {
30 value = string.charCodeAt(counter++);
31 if (value >= 0xD800 && value <= 0xDBFF && counter < length) {
32 // high surrogate, and there is a next character
33 extra = string.charCodeAt(counter++);
34 if ((extra & 0xFC00) == 0xDC00) { // low surrogate
35 output.push(((value & 0x3FF) << 10) + (extra & 0x3FF) + 0x10000);
36 } else {
37 // unmatched surrogate; only append this code unit, in case the next
38 // code unit is the high surrogate of a surrogate pair
39 output.push(value);
40 counter--;
41 }
42 } else {
43 output.push(value);
44 }
45 }
46 return output;
47 }
48
49 // Taken from http://mths.be/punycode
50 function ucs2encode(array) {
51 var length = array.length;
52 var index = -1;
53 var value;
54 var output = '';
55 while (++index < length) {
56 value = array[index];
57 if (value > 0xFFFF) {
58 value -= 0x10000;
59 output += stringFromCharCode(value >>> 10 & 0x3FF | 0xD800);
60 value = 0xDC00 | value & 0x3FF;
61 }
62 output += stringFromCharCode(value);
63 }
64 return output;
65 }
66
67 /*--------------------------------------------------------------------------*/
68
69 function createByte(codePoint, shift) {
70 return stringFromCharCode(((codePoint >> shift) & 0x3F) | 0x80);
71 }
72
73 function encodeCodePoint(codePoint) {
74 if ((codePoint & 0xFFFFFF80) == 0) { // 1-byte sequence
75 return stringFromCharCode(codePoint);
76 }
77 var symbol = '';
78 if ((codePoint & 0xFFFFF800) == 0) { // 2-byte sequence
79 symbol = stringFromCharCode(((codePoint >> 6) & 0x1F) | 0xC0);
80 }
81 else if ((codePoint & 0xFFFF0000) == 0) { // 3-byte sequence
82 symbol = stringFromCharCode(((codePoint >> 12) & 0x0F) | 0xE0);
83 symbol += createByte(codePoint, 6);
84 }
85 else if ((codePoint & 0xFFE00000) == 0) { // 4-byte sequence
86 symbol = stringFromCharCode(((codePoint >> 18) & 0x07) | 0xF0);
87 symbol += createByte(codePoint, 12);
88 symbol += createByte(codePoint, 6);
89 }
90 symbol += stringFromCharCode((codePoint & 0x3F) | 0x80);
91 return symbol;
92 }
93
94 function utf8encode(string) {
95 var codePoints = ucs2decode(string);
96
97 // console.log(JSON.stringify(codePoints.map(function(x) {
98 // return 'U+' + x.toString(16).toUpperCase();
99 // })));
100
101 var length = codePoints.length;
102 var index = -1;
103 var codePoint;
104 var byteString = '';
105 while (++index < length) {
106 codePoint = codePoints[index];
107 byteString += encodeCodePoint(codePoint);
108 }
109 return byteString;
110 }
111
112 /*--------------------------------------------------------------------------*/
113
114 function readContinuationByte() {
115 if (byteIndex >= byteCount) {
116 throw Error('Invalid byte index');
117 }
118
119 var continuationByte = byteArray[byteIndex] & 0xFF;
120 byteIndex++;
121
122 if ((continuationByte & 0xC0) == 0x80) {
123 return continuationByte & 0x3F;
124 }
125
126 // If we end up here, it’s not a continuation byte
127 throw Error('Invalid continuation byte');
128 }
129
130 function decodeSymbol() {
131 var byte1;
132 var byte2;
133 var byte3;
134 var byte4;
135 var codePoint;
136
137 if (byteIndex > byteCount) {
138 throw Error('Invalid byte index');
139 }
140
141 if (byteIndex == byteCount) {
142 return false;
143 }
144
145 // Read first byte
146 byte1 = byteArray[byteIndex] & 0xFF;
147 byteIndex++;
148
149 // 1-byte sequence (no continuation bytes)
150 if ((byte1 & 0x80) == 0) {
151 return byte1;
152 }
153
154 // 2-byte sequence
155 if ((byte1 & 0xE0) == 0xC0) {
156 var byte2 = readContinuationByte();
157 codePoint = ((byte1 & 0x1F) << 6) | byte2;
158 if (codePoint >= 0x80) {
159 return codePoint;
160 } else {
161 throw Error('Invalid continuation byte');
162 }
163 }
164
165 // 3-byte sequence (may include unpaired surrogates)
166 if ((byte1 & 0xF0) == 0xE0) {
167 byte2 = readContinuationByte();
168 byte3 = readContinuationByte();
169 codePoint = ((byte1 & 0x0F) << 12) | (byte2 << 6) | byte3;
170 if (codePoint >= 0x0800) {
171 return codePoint;
172 } else {
173 throw Error('Invalid continuation byte');
174 }
175 }
176
177 // 4-byte sequence
178 if ((byte1 & 0xF8) == 0xF0) {
179 byte2 = readContinuationByte();
180 byte3 = readContinuationByte();
181 byte4 = readContinuationByte();
182 codePoint = ((byte1 & 0x0F) << 0x12) | (byte2 << 0x0C) |
183 (byte3 << 0x06) | byte4;
184 if (codePoint >= 0x010000 && codePoint <= 0x10FFFF) {
185 return codePoint;
186 }
187 }
188
189 throw Error('Invalid UTF-8 detected');
190 }
191
192 var byteArray;
193 var byteCount;
194 var byteIndex;
195 function utf8decode(byteString) {
196 byteArray = ucs2decode(byteString);
197 byteCount = byteArray.length;
198 byteIndex = 0;
199 var codePoints = [];
200 var tmp;
201 while ((tmp = decodeSymbol()) !== false) {
202 codePoints.push(tmp);
203 }
204 return ucs2encode(codePoints);
205 }
206
207 /*--------------------------------------------------------------------------*/
208
209 var utf8 = {
210 'version': '2.0.0',
211 'encode': utf8encode,
212 'decode': utf8decode
213 };
214
215 // Some AMD build optimizers, like r.js, check for specific condition patterns
216 // like the following:
217 if (
218 typeof define == 'function' &&
219 typeof define.amd == 'object' &&
220 define.amd
221 ) {
222 define(function() {
223 return utf8;
224 });
225 } else if (freeExports && !freeExports.nodeType) {
226 if (freeModule) { // in Node.js or RingoJS v0.8.0+
227 freeModule.exports = utf8;
228 } else { // in Narwhal or RingoJS v0.7.0-
229 var object = {};
230 var hasOwnProperty = object.hasOwnProperty;
231 for (var key in utf8) {
232 hasOwnProperty.call(utf8, key) && (freeExports[key] = utf8[key]);
233 }
234 }
235 } else { // in Rhino or a web browser
236 root.utf8 = utf8;
237 }
238
239}(this));