String
Stock Wren strings plus a few Wrenlift extras for code-point construction, byte-level inspection, and interop with typed arrays.
Stock surface
Wrenlift implements the
full
stock Wren String API — concatenation,
count, indexOf,
contains, startsWith,
endsWith, slice (s[a..b]),
codePointAt, byteAt,
split, trim,
replace, and the iteration protocol.
Strings are immutable; every "mutator" returns a new
string.
This page covers what Wrenlift adds on top of that surface. Everything else is exactly as Wren spec documents it.
fromCodePoint
Construct a one-character string from a Unicode code-point integer:
System.print(String.fromCodePoint(65)) // "A"
System.print(String.fromCodePoint(0x1F44B)) // "👋"
Combine with 0..n ranges to build small
UTF-8 buffers without
ByteArray:
// Build a 4-byte UTF-8 payload of 0x41 0x42 0x43 0x44 (ABCD):
var s = ""
for (cp in [0x41, 0x42, 0x43, 0x44]) {
s = s + String.fromCodePoint(cp)
}
Throws Fiber.abort on invalid code points
(negative, > 0x10FFFF, or in the surrogate
gap 0xD800..=0xDFFF).
Byte iteration
Stock Wren iterates a string by code-point. Wrenlift
keeps that, and adds a byte-level view through the
.bytes accessor:
var s = "héllo"
System.print(s.count) // 5 — code points
System.print(s.bytes.count) // 6 — bytes (é is 2 bytes in UTF-8)
for (b in s.bytes) {
System.print(b) // 104, 195, 169, 108, 108, 111
}
s.bytes is a lightweight view, not a copy —
constructing it doesn't allocate a backing buffer. For
a real, mutable buffer, copy through
ByteArray.fromString(s).
Typed-array interop
Strings and typed arrays bridge cleanly in both directions:
| Direction | Call |
|---|---|
| String → bytes | ByteArray.fromString(s) (UTF-8 snapshot) |
| Bytes → String | String.fromBytes(byteArray) (decode UTF-8 — aborts on invalid sequences) |
var b = ByteArray.fromString("hi 👋")
System.print(b.count) // 6
var s = String.fromBytes(b)
System.print(s) // "hi 👋"