zena:string
import {…} from 'zena:string';
Classes
String
final class String implements Hashable
new(data: ByteArray, start: i32, end: i32, encoding: Encoding)
Create a new array from a ByteArray.
Important: This should be considered a private constructor - strings are created by the compiler for literals or by StringBuilder/transcoding functions. When Zena supports private constructors, this will be marked as private to prevent misuse.
encoding: Encoding { get; }
The encoding of the backing bytes. Encoding is deliberately an internal detail almost everywhere; this exists for boundary code — canonical-ABI marshaling, host interop — that copies the bytes verbatim and must assert what it is copying rather than assume.
length: i32 { get; }
The length of the string in bytes.
Since string operations like slicing and getByteAt operate on bytes,
this consistently returns the byte capacity of the view regardless of
the underlying encoding (WTF-8 or WTF-16).
TODO: Rename to byteLength or remove entirely.
static fromByteArray(data: ByteArray, start: i32, end: i32, encoding: Encoding): String
Creates a String from a ByteArray with given bounds and encoding. This is used by StringBuilder and other internal operations.
static fromParts<A extends Array<String>>(parts: A): String
Creates a new string by concatenating all parts. This is more efficient than chained + operators for multiple strings, as it allocates the result array only once.
Generic over the array representation: element access resolves through the bound, so each specialization reads its receiver directly with no interface dispatch.
static fromRawParts(parts: array<String>): String
The raw-array entry the compiler's synthesized string joins call —
template-literal lowering resolves it by name against a
one-parameter raw-array signature. Its body is also the call site
that keeps the FixedArray
getByteAt(index: i32): i32
Returns the byte at the given index.
For WTF-8 strings, this is the UTF-8 byte at the given index. For WTF-16 strings, this is the byte (not code unit) at the given index.
sliceBytes(start: i32, end: i32): String
O(1) zero-copy slice by byte indices. Returns a new String sharing the backing array.
SAFETY: Indices are byte offsets, not code point offsets. Slicing in the middle of a multi-byte UTF-8 sequence produces an invalid string. Use StringReader.mark() to get safe positions.
copy(): String
Force a copy - creates a new String with its own backing array. Use this when you need to release the parent string's memory.
copyBytesTo(target: ByteArray, targetOffset: i32, start: i32 = 0, length: i32 = -1): void
Copies bytes from this string to a target ByteArray.
startsWith(prefix: String): boolean
Returns true if this string starts with the given prefix.
endsWith(suffix: String): boolean
Returns true if this string ends with the given suffix.
split(separator: String): FixedArray<String>
Splits the string into an array of substrings separated by the given separator.
contains(needle: String): boolean
getBackingArrayIfFull(): ByteArray | null
Returns the backing ByteArray if the String spans the entire array (i.e. not a slice). Used for optimization by StringBuilder.
asciiLowerCase(): String
FNV-1a hash of the string's bytes, cached in #hashCode after the first computation. Delegates to the compiler's shared string hash helper (generateStringHashFunction), which reads and updates the cache field.
Returns this string with every ASCII letter lowercased; every other byte
is left alone. See asciiLowerByte for why this is deliberately not
Unicode-aware.
Byte-wise, which is exactly right for WTF-8: an ASCII byte is always a
whole code point, and no continuation byte falls in 0x41..0x5A, so no
multi-byte sequence can be corrupted. A WTF-16 string would instead need
code-unit stepping — and that branch belongs here, on #encoding, rather
than in every caller, which is the reason this lives on String at all. No
String method supports WTF-16 yet and nothing constructs one today.
Returns this when there is nothing to change, so the common
already-lowercase case does not allocate.
asciiUpperCase(): String
Returns this string with every ASCII letter uppercased.
hashCode(): i32
Enums
Encoding
Functions
asciiLowerByte
function asciiLowerByte(b: i32): i32
An sequence of unicode code points, stored as a view into a ByteArray with a specified encoding.
Strings are immutable and comparable by value. They are created by the compiler for literals, by string operations like concatenation and slicing, StringBuilders, template literals, etc. The internal design allows for efficient slicing and concatenation without copying, while still providing value semantics for equality and immutability.
Strings take care to mostly hide the underlying encoding details, in order to be portable across platforms with different native string representations. Zena can be compiled to platforms with either UTF-8 or UTF-16 native strings, and the String class abstracts over these differences, to zupport efficient string sharing with host platforms. Encoding is mostly an internal detail, but it can be observed in some cases, like the getByteAt() and sliceBytes() methods.
Use StringReader for parsing strings, which provides safe methods for slicing at code point boundaries. Use StringBuilder for efficient construction of strings from parts.
Returns the ASCII lowercase of b, leaving every other byte or code point
unchanged.
ASCII-only is the point, not a shortcut. It is the operation specifications
such as the WHATWG URL Standard mandate, and it is locale-independent and
table-free. Unicode case mapping is a genuinely different function: it is
locale-sensitive (under Turkish rules I lowercases to a dotless i), it can
change length (uppercasing the sharp s yields SS), and it needs
case-mapping tables that would then be reachable from every caller. It must
therefore stay a separate function rather than being folded into these.
asciiUpperByte
function asciiUpperByte(b: i32): i32
Returns the ASCII uppercase of b. See asciiLowerByte.