utf16.d.ts 3.5 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192
  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. /// <reference types="node" />
  17. import type { Uint8ArrayBuffer, Uint16ArrayBuffer } from './array.js';
  18. /**
  19. * Output format for UTF-16 encoding
  20. */
  21. export type Utf16Format = 'uint16' | 'uint8-le' | 'uint8-be';
  22. /**
  23. * Encode a string to UTF-16 bytes (strict mode)
  24. *
  25. * Throws on invalid Unicode (unpaired surrogates)
  26. *
  27. * @param string - The string to encode
  28. * @param format - Output format (default: 'uint16')
  29. * @returns The encoded bytes
  30. */
  31. export function utf16fromString(string: string, format?: 'uint16'): Uint16ArrayBuffer;
  32. export function utf16fromString(string: string, format: 'uint8-le'): Uint8ArrayBuffer;
  33. export function utf16fromString(string: string, format: 'uint8-be'): Uint8ArrayBuffer;
  34. export function utf16fromString(string: string, format?: Utf16Format): Uint16ArrayBuffer | Uint8ArrayBuffer;
  35. /**
  36. * Encode a string to UTF-16 bytes (loose mode)
  37. *
  38. * Replaces invalid Unicode (unpaired surrogates) with replacement codepoints `U+FFFD`
  39. * per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
  40. *
  41. * _Such replacement is a non-injective function, is irreversible and causes collisions.\
  42. * Prefer using strict throwing methods for cryptography applications._
  43. *
  44. * @param string - The string to encode
  45. * @param format - Output format (default: 'uint16')
  46. * @returns The encoded bytes
  47. */
  48. export function utf16fromStringLoose(string: string, format?: 'uint16'): Uint16ArrayBuffer;
  49. export function utf16fromStringLoose(string: string, format: 'uint8-le'): Uint8ArrayBuffer;
  50. export function utf16fromStringLoose(string: string, format: 'uint8-be'): Uint8ArrayBuffer;
  51. export function utf16fromStringLoose(string: string, format?: Utf16Format): Uint16ArrayBuffer | Uint8ArrayBuffer;
  52. /**
  53. * Decode UTF-16 bytes to a string (strict mode)
  54. *
  55. * Throws on invalid UTF-16 byte sequences
  56. *
  57. * Throws on non-even byte length.
  58. *
  59. * @param arr - The bytes to decode
  60. * @param format - Input format (default: 'uint16')
  61. * @returns The decoded string
  62. */
  63. export function utf16toString(arr: Uint16Array, format?: 'uint16'): string;
  64. export function utf16toString(arr: Uint8Array, format: 'uint8-le'): string;
  65. export function utf16toString(arr: Uint8Array, format: 'uint8-be'): string;
  66. export function utf16toString(arr: Uint16Array | Uint8Array, format?: Utf16Format): string;
  67. /**
  68. * Decode UTF-16 bytes to a string (loose mode)
  69. *
  70. * Replaces invalid UTF-16 byte sequences with replacement codepoints `U+FFFD`
  71. * per [WHATWG Encoding](https://encoding.spec.whatwg.org/) specification.
  72. *
  73. * _Such replacement is a non-injective function, is irreversible and causes collisions.\
  74. * Prefer using strict throwing methods for cryptography applications._
  75. *
  76. * Throws on non-even byte length.
  77. *
  78. * @param arr - The bytes to decode
  79. * @param format - Input format (default: 'uint16')
  80. * @returns The decoded string
  81. */
  82. export function utf16toStringLoose(arr: Uint16Array, format?: 'uint16'): string;
  83. export function utf16toStringLoose(arr: Uint8Array, format: 'uint8-le'): string;
  84. export function utf16toStringLoose(arr: Uint8Array, format: 'uint8-be'): string;
  85. export function utf16toStringLoose(arr: Uint16Array | Uint8Array, format?: Utf16Format): string;