|
| 1 | +/** |
| 2 | + * UTF-16 encoding/decoding |
| 3 | + * |
| 4 | + * ```js |
| 5 | + * import { utf16fromString, utf16toString } from '@exodus/bytes/utf16.js' |
| 6 | + * |
| 7 | + * // loose |
| 8 | + * import { utf16fromStringLoose, utf16toStringLoose } from '@exodus/bytes/utf16.js' |
| 9 | + * ``` |
| 10 | + * |
| 11 | + * _These methods by design encode/decode BOM (codepoint `U+FEFF` Byte Order Mark) as-is._\ |
| 12 | + * _If you need BOM handling or detection, use `@exodus/bytes/encoding.js`_ |
| 13 | + * |
| 14 | + * @module @exodus/bytes/utf16.js |
| 15 | + */ |
| 16 | + |
| 17 | +/// <reference types="node" /> |
| 18 | + |
| 19 | +import type { Uint8ArrayBuffer, Uint16ArrayBuffer } from './array.js'; |
| 20 | + |
| 21 | +/** |
| 22 | + * Output format for UTF-16 encoding |
| 23 | + */ |
| 24 | +export type Utf16Format = 'uint16' | 'uint8-le' | 'uint8-be'; |
| 25 | + |
| 26 | +/** |
| 27 | + * Encode a string to UTF-16 bytes (strict mode) |
| 28 | + * |
| 29 | + * Throws on invalid Unicode (unpaired surrogates) |
| 30 | + * |
| 31 | + * @param string - The string to encode |
| 32 | + * @param format - Output format (default: 'uint16') |
| 33 | + * @returns The encoded bytes |
| 34 | + */ |
| 35 | +export function utf16fromString(string: string, format?: 'uint16'): Uint16ArrayBuffer; |
| 36 | +export function utf16fromString(string: string, format: 'uint8-le'): Uint8ArrayBuffer; |
| 37 | +export function utf16fromString(string: string, format: 'uint8-be'): Uint8ArrayBuffer; |
| 38 | +export function utf16fromString(string: string, format?: Utf16Format): Uint16ArrayBuffer | Uint8ArrayBuffer; |
| 39 | + |
| 40 | +/** |
| 41 | + * Encode a string to UTF-16 bytes (loose mode) |
| 42 | + * |
| 43 | + * Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD` |
| 44 | + * per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification. |
| 45 | + * |
| 46 | + * _Such replacement is a non-injective function, is irreversible and causes collisions.\ |
| 47 | + * Prefer using strict throwing methods for cryptography applications._ |
| 48 | + * |
| 49 | + * @param string - The string to encode |
| 50 | + * @param format - Output format (default: 'uint16') |
| 51 | + * @returns The encoded bytes |
| 52 | + */ |
| 53 | +export function utf16fromStringLoose(string: string, format?: 'uint16'): Uint16ArrayBuffer; |
| 54 | +export function utf16fromStringLoose(string: string, format: 'uint8-le'): Uint8ArrayBuffer; |
| 55 | +export function utf16fromStringLoose(string: string, format: 'uint8-be'): Uint8ArrayBuffer; |
| 56 | +export function utf16fromStringLoose(string: string, format?: Utf16Format): Uint16ArrayBuffer | Uint8ArrayBuffer; |
| 57 | + |
| 58 | +/** |
| 59 | + * Decode UTF-16 bytes to a string (strict mode) |
| 60 | + * |
| 61 | + * Throws on invalid UTF-16 byte sequences |
| 62 | + * |
| 63 | + * Throws on non-even byte length. |
| 64 | + * |
| 65 | + * @param arr - The bytes to decode |
| 66 | + * @param format - Input format (default: 'uint16') |
| 67 | + * @returns The decoded string |
| 68 | + */ |
| 69 | +export function utf16toString(arr: Uint16ArrayBuffer, format?: 'uint16'): string; |
| 70 | +export function utf16toString(arr: Uint8ArrayBuffer, format: 'uint8-le'): string; |
| 71 | +export function utf16toString(arr: Uint8ArrayBuffer, format: 'uint8-be'): string; |
| 72 | +export function utf16toString(arr: Uint16ArrayBuffer | Uint8ArrayBuffer, format?: Utf16Format): string; |
| 73 | + |
| 74 | +/** |
| 75 | + * Decode UTF-16 bytes to a string (loose mode) |
| 76 | + * |
| 77 | + * Replaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD` |
| 78 | + * per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification. |
| 79 | + * |
| 80 | + * _Such replacement is a non-injective function, is irreversible and causes collisions.\ |
| 81 | + * Prefer using strict throwing methods for cryptography applications._ |
| 82 | + * |
| 83 | + * Throws on non-even byte length. |
| 84 | + * |
| 85 | + * @param arr - The bytes to decode |
| 86 | + * @param format - Input format (default: 'uint16') |
| 87 | + * @returns The decoded string |
| 88 | + */ |
| 89 | +export function utf16toStringLoose(arr: Uint16ArrayBuffer, format?: 'uint16'): string; |
| 90 | +export function utf16toStringLoose(arr: Uint8ArrayBuffer, format: 'uint8-le'): string; |
| 91 | +export function utf16toStringLoose(arr: Uint8ArrayBuffer, format: 'uint8-be'): string; |
| 92 | +export function utf16toStringLoose(arr: Uint16ArrayBuffer | Uint8ArrayBuffer, format?: Utf16Format): string; |
0 commit comments