package quickjs
Install
dune-project
Dependency
Authors
Maintainers
Sources
sha256=24e60ef65737c2b8e762b98fbdbc836380425c35c5b3ffae657a14671e51d959
sha512=3868ca89255ebc03ea8a9ba6fe1604c8c5aad5060274d518be99a3300883ebeae864b8b68bf3782875c527a41828f40b3cb75d50179d6802ec0eafd59591af4f
doc/quickjs/Quickjs/String/index.html
Module Quickjs.StringSource
JavaScript String built-in object
This module mirrors the JavaScript String API with prototype methods for string manipulation. All methods use UTF-16 semantics for indices.
JavaScript vs OCaml Semantics
This library follows JavaScript semantics in most cases to ensure compatibility with the ECMA-262 specification:
- Indices: All string indices are UTF-16 code unit positions, not byte positions. A string containing an emoji has length 2 (surrogate pair). Indices returned by the RegExp-based methods use the same unit.
- Not-found values: Methods like
index_ofreturn-1when not found (JavaScript convention), notoption(OCaml convention). This matches JavaScript's String.prototype.indexOf().
- Character access: Methods like
char_code_atreturnint option(OCaml convention) for bounds checking, since OCaml does not have JavaScript's implicit NaN coercion.
- Negative indices: Methods like
slicesupport negative indices counting from the end, matching JavaScript behavior.
- Invalid patterns: RegExp-based methods raise
Invalid_argumenton invalid patterns, mirroring the SyntaxError JavaScript throws for invalid regular expression literals.
Unicode normalization forms
is_valid_utf8 s returns true if s contains only valid UTF-8 byte sequences. Use this for strict validation before processing untrusted input.
Note: All functions in this module handle invalid UTF-8 gracefully by replacing malformed sequences with U+FFFD (replacement character).
utf16_index_of_byte s i converts a UTF-8 byte offset in s into the corresponding UTF-16 code unit index (the unit used by every index in this module and in RegExp). Offsets falling inside a multi-byte sequence map to the index of that character; out-of-range offsets are clamped. Useful at the boundary with byte-oriented code.
byte_index_of_utf16 s i converts a UTF-16 code unit index into the corresponding UTF-8 byte offset in s. Indices falling inside a surrogate pair map to the byte offset of that character; out-of-range indices are clamped. Inverse of utf16_index_of_byte.
Static methods
from_char_code codes builds a string from UTF-16 code units. Every value is coerced with ToUint16 (kept modulo 216, so from_char_code [| 0x10041 |] is "A") and adjacent high/low surrogate values combine into one code point: from_char_code [| 0xD83D; 0xDE00 |] is "๐". Equivalent to JavaScript's String.fromCharCode().
Unpaired surrogates become U+FFFD, since UTF-8 cannot represent them (JavaScript builds a lone-surrogate string instead).
from_code_point code_points builds a string from Unicode code points: from_code_point [| 0x1F600 |] is "๐". Equivalent to JavaScript's String.fromCodePoint().
Surrogate halves given as separate code points pair up exactly like in JavaScript (from_code_point [| 0xD83D; 0xDE00 |] is also "๐"); unpaired surrogates become U+FFFD, since UTF-8 cannot represent them.
lowercase_char c converts a single character to lowercase. May return multiple characters for special cases.