cborg 4.0.0 → 4.0.1
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/CHANGELOG.md +2 -0
- package/lib/byte-utils.js +29 -86
- package/package.json +1 -1
package/CHANGELOG.md
CHANGED
package/lib/byte-utils.js
CHANGED
|
@@ -275,101 +275,44 @@ export function compare (b1, b2) {
|
|
|
275
275
|
return 0
|
|
276
276
|
}
|
|
277
277
|
|
|
278
|
-
// The below code is
|
|
279
|
-
// Licensed
|
|
278
|
+
// The below code is taken from https://github.com/google/closure-library/blob/8598d87242af59aac233270742c8984e2b2bdbe0/closure/goog/crypt/crypt.js#L117-L143
|
|
279
|
+
// Licensed Apache-2.0.
|
|
280
280
|
|
|
281
281
|
/**
|
|
282
|
-
* @param {string}
|
|
283
|
-
* @param {number} [units]
|
|
282
|
+
* @param {string} str
|
|
284
283
|
* @returns {number[]}
|
|
285
284
|
*/
|
|
286
|
-
function utf8ToBytes (
|
|
287
|
-
|
|
288
|
-
|
|
289
|
-
let
|
|
290
|
-
|
|
291
|
-
|
|
292
|
-
|
|
293
|
-
|
|
294
|
-
|
|
295
|
-
|
|
296
|
-
if (
|
|
297
|
-
|
|
298
|
-
|
|
299
|
-
|
|
300
|
-
|
|
301
|
-
|
|
302
|
-
|
|
303
|
-
|
|
304
|
-
|
|
305
|
-
} else if (i + 1 === length) {
|
|
306
|
-
// unpaired lead
|
|
307
|
-
if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd)
|
|
308
|
-
continue
|
|
309
|
-
}
|
|
310
|
-
|
|
311
|
-
// valid lead
|
|
312
|
-
leadSurrogate = codePoint
|
|
313
|
-
|
|
314
|
-
continue
|
|
315
|
-
}
|
|
316
|
-
|
|
317
|
-
// 2 leads in a row
|
|
318
|
-
/* c8 ignore next 5 */
|
|
319
|
-
if (codePoint < 0xdc00) {
|
|
320
|
-
if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd)
|
|
321
|
-
leadSurrogate = codePoint
|
|
322
|
-
continue
|
|
323
|
-
}
|
|
324
|
-
|
|
325
|
-
// valid surrogate pair
|
|
326
|
-
codePoint = (leadSurrogate - 0xd800 << 10 | codePoint - 0xdc00) + 0x10000
|
|
327
|
-
/* c8 ignore next 4 */
|
|
328
|
-
} else if (leadSurrogate) {
|
|
329
|
-
// valid bmp char, but last char was a lead
|
|
330
|
-
if ((units -= 3) > -1) bytes.push(0xef, 0xbf, 0xbd)
|
|
331
|
-
}
|
|
332
|
-
|
|
333
|
-
leadSurrogate = null
|
|
334
|
-
|
|
335
|
-
// encode utf8
|
|
336
|
-
if (codePoint < 0x80) {
|
|
337
|
-
/* c8 ignore next 1 */
|
|
338
|
-
if ((units -= 1) < 0) break
|
|
339
|
-
bytes.push(codePoint)
|
|
340
|
-
} else if (codePoint < 0x800) {
|
|
341
|
-
/* c8 ignore next 1 */
|
|
342
|
-
if ((units -= 2) < 0) break
|
|
343
|
-
bytes.push(
|
|
344
|
-
codePoint >> 0x6 | 0xc0,
|
|
345
|
-
codePoint & 0x3f | 0x80
|
|
346
|
-
)
|
|
347
|
-
} else if (codePoint < 0x10000) {
|
|
348
|
-
/* c8 ignore next 1 */
|
|
349
|
-
if ((units -= 3) < 0) break
|
|
350
|
-
bytes.push(
|
|
351
|
-
codePoint >> 0xc | 0xe0,
|
|
352
|
-
codePoint >> 0x6 & 0x3f | 0x80,
|
|
353
|
-
codePoint & 0x3f | 0x80
|
|
354
|
-
)
|
|
355
|
-
/* c8 ignore next 9 */
|
|
356
|
-
} else if (codePoint < 0x110000) {
|
|
357
|
-
if ((units -= 4) < 0) break
|
|
358
|
-
bytes.push(
|
|
359
|
-
codePoint >> 0x12 | 0xf0,
|
|
360
|
-
codePoint >> 0xc & 0x3f | 0x80,
|
|
361
|
-
codePoint >> 0x6 & 0x3f | 0x80,
|
|
362
|
-
codePoint & 0x3f | 0x80
|
|
363
|
-
)
|
|
285
|
+
function utf8ToBytes (str) {
|
|
286
|
+
const out = []
|
|
287
|
+
let p = 0
|
|
288
|
+
for (let i = 0; i < str.length; i++) {
|
|
289
|
+
let c = str.charCodeAt(i)
|
|
290
|
+
if (c < 128) {
|
|
291
|
+
out[p++] = c
|
|
292
|
+
} else if (c < 2048) {
|
|
293
|
+
out[p++] = (c >> 6) | 192
|
|
294
|
+
out[p++] = (c & 63) | 128
|
|
295
|
+
} else if (
|
|
296
|
+
((c & 0xFC00) === 0xD800) && (i + 1) < str.length &&
|
|
297
|
+
((str.charCodeAt(i + 1) & 0xFC00) === 0xDC00)) {
|
|
298
|
+
// Surrogate Pair
|
|
299
|
+
c = 0x10000 + ((c & 0x03FF) << 10) + (str.charCodeAt(++i) & 0x03FF)
|
|
300
|
+
out[p++] = (c >> 18) | 240
|
|
301
|
+
out[p++] = ((c >> 12) & 63) | 128
|
|
302
|
+
out[p++] = ((c >> 6) & 63) | 128
|
|
303
|
+
out[p++] = (c & 63) | 128
|
|
364
304
|
} else {
|
|
365
|
-
|
|
366
|
-
|
|
305
|
+
out[p++] = (c >> 12) | 224
|
|
306
|
+
out[p++] = ((c >> 6) & 63) | 128
|
|
307
|
+
out[p++] = (c & 63) | 128
|
|
367
308
|
}
|
|
368
309
|
}
|
|
369
|
-
|
|
370
|
-
return bytes
|
|
310
|
+
return out
|
|
371
311
|
}
|
|
372
312
|
|
|
313
|
+
// The below code is mostly taken from https://github.com/feross/buffer
|
|
314
|
+
// Licensed MIT. Copyright (c) Feross Aboukhadijeh
|
|
315
|
+
|
|
373
316
|
/**
|
|
374
317
|
* @param {Uint8Array} buf
|
|
375
318
|
* @param {number} offset
|