UNPKG

7.22 kBPlain TextView Raw
1// unescape(encodeURIComponent(str))
2export function utf8Encode(str) {
3 return unescape(encodeURIComponent(str));
4}
5/*
6export function utf8Encode(str) {
7 let x, y, output = '',
8 i = -1,
9 l;
10
11 if (str && str.length) {
12 l = str.length;
13 while ((i += 1) < l) {
14 /!* Decode utf-16 surrogate pairs *!/
15 x = str.charCodeAt(i);
16 y = i + 1 < l ? str.charCodeAt(i + 1) : 0;
17 if (0xD800 <= x && x <= 0xDBFF && 0xDC00 <= y && y <= 0xDFFF) {
18 x = 0x10000 + ((x & 0x03FF) << 10) + (y & 0x03FF);
19 i += 1;
20 }
21 /!* Encode output as utf-8 *!/
22 if (x <= 0x7F) {
23 output += String.fromCharCode(x);
24 } else if (x <= 0x7FF) {
25 output += String.fromCharCode(0xC0 | ((x >>> 6) & 0x1F),
26 0x80 | (x & 0x3F));
27 } else if (x <= 0xFFFF) {
28 output += String.fromCharCode(0xE0 | ((x >>> 12) & 0x0F),
29 0x80 | ((x >>> 6) & 0x3F),
30 0x80 | (x & 0x3F));
31 } else if (x <= 0x1FFFFF) {
32 output += String.fromCharCode(0xF0 | ((x >>> 18) & 0x07),
33 0x80 | ((x >>> 12) & 0x3F),
34 0x80 | ((x >>> 6) & 0x3F),
35 0x80 | (x & 0x3F));
36 }
37 }
38 }
39 return output;
40}
41*/
42
43// decodeURIComponent(escape(str))
44export function utf8Decode(str) {
45 return decodeURIComponent(escape(str));
46}
47/*
48export function utf8Decode(str) {
49 let i, ac, c1, c2, c3, arr = [],
50 l;
51 i = ac = c1 = c2 = c3 = 0;
52
53 if (str && str.length) {
54 l = str.length;
55 str += '';
56
57 while (i < l) {
58 c1 = str.charCodeAt(i);
59 ac += 1;
60 if (c1 < 128) {
61 arr[ac] = String.fromCharCode(c1);
62 i += 1;
63 } else if (c1 > 191 && c1 < 224) {
64 c2 = str.charCodeAt(i + 1);
65 arr[ac] = String.fromCharCode(((c1 & 31) << 6) | (c2 & 63));
66 i += 2;
67 } else {
68 c2 = str.charCodeAt(i + 1);
69 c3 = str.charCodeAt(i + 2);
70 arr[ac] = String.fromCharCode(((c1 & 15) << 12) | ((c2 & 63) << 6) | (c3 & 63));
71 i += 3;
72 }
73 }
74 }
75 return arr.join('');
76}
77*/
78
79/**
80 * Add integers, wrapping at 2^32. This uses 16-bit operations internally
81 * to work around bugs in some JS interpreters.
82 */
83export function safe_add(x, y) {
84 var lsw = (x & 0xFFFF) + (y & 0xFFFF),
85 msw = (x >> 16) + (y >> 16) + (lsw >> 16);
86 return (msw << 16) | (lsw & 0xFFFF);
87}
88
89/**
90 * Bitwise rotate a 32-bit number to the left.
91 */
92export function bit_rol(num, cnt) {
93 return (num << cnt) | (num >>> (32 - cnt));
94}
95
96/**
97 * Convert a raw string to a hex string
98 */
99export function rstr2hex(input, hexCase?) {
100 var hex_tab = hexCase ? '0123456789ABCDEF' : '0123456789abcdef',
101 output = '',
102 x, i = 0,
103 l = input.length;
104 for (; i < l; i += 1) {
105 x = input.charCodeAt(i);
106 output += hex_tab.charAt((x >>> 4) & 0x0F) + hex_tab.charAt(x & 0x0F);
107 }
108 return output;
109}
110
111/**
112 * Encode a string as utf-16
113 */
114export function str2rstr_utf16le(input) {
115 var i, l = input.length,
116 output = '';
117 for (i = 0; i < l; i += 1) {
118 output += String.fromCharCode(input.charCodeAt(i) & 0xFF, (input.charCodeAt(i) >>> 8) & 0xFF);
119 }
120 return output;
121}
122
123export function str2rstr_utf16be(input) {
124 var i, l = input.length,
125 output = '';
126 for (i = 0; i < l; i += 1) {
127 output += String.fromCharCode((input.charCodeAt(i) >>> 8) & 0xFF, input.charCodeAt(i) & 0xFF);
128 }
129 return output;
130}
131
132/**
133 * Convert an array of big-endian words to a string
134 */
135export function binb2rstr(input) {
136 var i, l = input.length * 32,
137 output = '';
138 for (i = 0; i < l; i += 8) {
139 output += String.fromCharCode((input[i >> 5] >>> (24 - i % 32)) & 0xFF);
140 }
141 return output;
142}
143
144/**
145 * Convert an array of little-endian words to a string
146 */
147export function binl2rstr(input) {
148 var i, l = input.length * 32,
149 output = '';
150 for (i = 0; i < l; i += 8) {
151 output += String.fromCharCode((input[i >> 5] >>> (i % 32)) & 0xFF);
152 }
153 return output;
154}
155
156/**
157 * Convert a raw string to an array of little-endian words
158 * Characters >255 have their high-byte silently ignored.
159 */
160export function rstr2binl(input) {
161 var i, l = input.length * 8,
162 output = Array(input.length >> 2),
163 lo = output.length;
164 for (i = 0; i < lo; i += 1) {
165 output[i] = 0;
166 }
167 for (i = 0; i < l; i += 8) {
168 output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (i % 32);
169 }
170 return output;
171}
172
173/**
174 * Convert a raw string to an array of big-endian words
175 * Characters >255 have their high-byte silently ignored.
176 */
177export function rstr2binb(input) {
178 var i, l = input.length * 8,
179 output = Array(input.length >> 2),
180 lo = output.length;
181 for (i = 0; i < lo; i += 1) {
182 output[i] = 0;
183 }
184 for (i = 0; i < l; i += 8) {
185 output[i >> 5] |= (input.charCodeAt(i / 8) & 0xFF) << (24 - i % 32);
186 }
187 return output;
188}
189
190/**
191 * Convert a raw string to an arbitrary string encoding
192 */
193export function rstr2any(input, encoding) {
194 var divisor = encoding.length,
195 remainders = Array(),
196 i, q, x, ld, quotient, dividend, output, full_length;
197
198 /* Convert to an array of 16-bit big-endian values, forming the dividend */
199 dividend = Array(Math.ceil(input.length / 2));
200 ld = dividend.length;
201 for (i = 0; i < ld; i += 1) {
202 dividend[i] = (input.charCodeAt(i * 2) << 8) | input.charCodeAt(i * 2 + 1);
203 }
204
205 /**
206 * Repeatedly perform a long division. The binary array forms the dividend,
207 * the length of the encoding is the divisor. Once computed, the quotient
208 * forms the dividend for the next step. We stop when the dividend is zerHashes.
209 * All remainders are stored for later use.
210 */
211 while (dividend.length > 0) {
212 quotient = Array();
213 x = 0;
214 for (i = 0; i < dividend.length; i += 1) {
215 x = (x << 16) + dividend[i];
216 q = Math.floor(x / divisor);
217 x -= q * divisor;
218 if (quotient.length > 0 || q > 0) {
219 quotient[quotient.length] = q;
220 }
221 }
222 remainders[remainders.length] = x;
223 dividend = quotient;
224 }
225
226 /* Convert the remainders to the output string */
227 output = '';
228 for (i = remainders.length - 1; i >= 0; i--) {
229 output += encoding.charAt(remainders[i]);
230 }
231
232 /* Append leading zero equivalents */
233 full_length = Math.ceil(input.length * 8 / (Math.log(encoding.length) / Math.log(2)));
234 for (i = output.length; i < full_length; i += 1) {
235 output = encoding[0] + output;
236 }
237 return output;
238}
239
240/**
241 * Convert a raw string to a base-64 string
242 */
243export function rstr2b64(input, b64pad='=') {
244 const tab = 'ABCDEFGHIJKLMNOPQRSTUVWXYZabcdefghijklmnopqrstuvwxyz0123456789+/';
245 let output = '',
246 len = input.length,
247 i, j, triplet;
248
249 for (i = 0; i < len; i += 3) {
250 triplet = (input.charCodeAt(i) << 16) | (i + 1 < len ? input.charCodeAt(i + 1) << 8 : 0) | (i + 2 < len ? input.charCodeAt(i + 2) : 0);
251 for (j = 0; j < 4; j += 1) {
252 if (i * 8 + j * 6 > input.length * 8) {
253 output += b64pad;
254 } else {
255 output += tab.charAt((triplet >>> 6 * (3 - j)) & 0x3F);
256 }
257 }
258 }
259 return output;
260}