|
1 | 1 | import * as js from './fallback/utf16.js' |
2 | | -import { nativeDecoder, isLE, E_STRING, E_STRICT_UNICODE } from './fallback/_utils.js' |
| 2 | +import { nativeDecoder, isLE } from './fallback/_utils.js' |
3 | 3 |
|
4 | 4 | function checkDecoders() { |
5 | 5 | // Not all barebone engines with TextDecoder support something except utf-8 |
@@ -28,24 +28,6 @@ const { E_STRICT } = js |
28 | 28 |
|
29 | 29 | // Unlike utf8, operates on Uint16Arrays by default |
30 | 30 |
|
31 | | -function encode(str, loose = false, format = 'uint16') { |
32 | | - if (typeof str !== 'string') throw new TypeError(E_STRING) |
33 | | - if (format !== 'uint16' && format !== 'uint8-le' && format !== 'uint8-be') { |
34 | | - throw new TypeError('Unknown format') |
35 | | - } |
36 | | - |
37 | | - // On v8 and SpiderMonkey, check via isWellFormed is faster than js |
38 | | - // On JSC, check during loop is faster than isWellFormed |
39 | | - // If isWellFormed is available, we skip check during decoding and recheck after |
40 | | - // If isWellFormed is unavailable, we check in js during decoding |
41 | | - if (!loose && isWellFormed && !isWellFormed.call(str)) throw new TypeError(E_STRICT_UNICODE) |
42 | | - const shouldSwap = (isLE && format === 'uint8-be') || (!isLE && format === 'uint8-le') |
43 | | - const u16 = js.encode(str, loose, !loose && isWellFormed, shouldSwap) |
44 | | - |
45 | | - // Bytes are already swapped and format is already checked, we need to just cast the view |
46 | | - return format === 'uint16' ? u16 : new Uint8Array(u16.buffer, u16.byteOffset, u16.byteLength) |
47 | | -} |
48 | | - |
49 | 31 | function decode(input, loose = false, format = 'uint16') { |
50 | 32 | let u16 |
51 | 33 | switch (format) { |
@@ -77,7 +59,7 @@ function decode(input, loose = false, format = 'uint16') { |
77 | 59 | return str |
78 | 60 | } |
79 | 61 |
|
80 | | -export const utf16fromString = (str, format = 'uint16') => encode(str, false, format) |
81 | | -export const utf16fromStringLoose = (str, format = 'uint16') => encode(str, true, format) |
| 62 | +export const utf16fromString = (str, format = 'uint16') => js.encodeApi(str, false, format) |
| 63 | +export const utf16fromStringLoose = (str, format = 'uint16') => js.encodeApi(str, true, format) |
82 | 64 | export const utf16toString = (arr, format = 'uint16') => decode(arr, false, format) |
83 | 65 | export const utf16toStringLoose = (arr, format = 'uint16') => decode(arr, true, format) |
0 commit comments