Encoding & Statics β
str matches String's static surface: the from* constructors, the MAX_LENGTH constant, and the UTF8 / UTF16 encoding namespaces.
Constructors β
ts
str.from(s); // wrap a whole string as a zero-copy view
str.fromRange(s, start, end); // a view of a code-unit [start, end) range
new str(data, start, end); // from explicit byte pointers
str.fromCharCode(0x41); // "A" (mirrors String.fromCharCode)
str.fromCharCodes([72, 105]); // "Hi"
str.fromCodePoint(0x1f600); // "π"str.MAX_LENGTH mirrors String.MAX_LENGTH.
UTF-8 / UTF-16 β
str.UTF8 and str.UTF16 are a drop-in for String.UTF8 / String.UTF16, powered by utf-as (SWAR/SIMD, pointer-based). They run straight off the view's range β no intermediate copy β and decode returns a str.
ts
const v = str.slice("xx hΓ©llo δΈη xx", 3, 11); // "hΓ©llo δΈη"
// UTF-16 β the view's bytes are already UTF-16
str.UTF16.byteLength(v); // bytes
const u16 = str.UTF16.encode(v); // ArrayBuffer
str.UTF16.decode(u16); // str round-trip
str.UTF16.validate(v); // well-formed UTF-16?
// UTF-8 β counted and encoded in place
str.UTF8.byteLength(v); // UTF-8 byte length of the view
const u8 = str.UTF8.encode(v); // ArrayBuffer of UTF-8 bytes
str.UTF8.decode(u8); // str round-tripFull surface β
Both namespaces expose the standard library functions plus the utf-as extras:
| Function | UTF8 | UTF16 |
|---|---|---|
byteLength(view, nullTerminated?) | β | β |
encode(view, β¦) | β | β |
encodeUnsafe(ptr, len, buf, β¦) | β | β |
decode(buf, β¦) β str | β | β |
decodeUnsafe(ptr, len, β¦) β str | β | β |
validate(buf) / validate(view) | β | β |
utf16Length(buf) | β |
The *Unsafe variants take raw pointers, identical to the stdlib signatures, so existing UTF code ports over unchanged.
