Strings, Bytes, and Runes
Immutable UTF-8 strings, mutable byte buffers, Unicode scalar values, and C strings.
Contents
Haze separates three kinds of textual data. str is an immutable UTF-8 string, bytes is a mutable byte buffer, and cstr is a null-terminated C string used only at the FFI boundary. A rune is a single Unicode scalar value, represented as a distinct type rather than as a string of length one.
Strings#
A str is an immutable sequence of UTF-8 bytes. It is created from a string literal, by concatenation, by slicing, or by converting a byte buffer or C string. The len property is the number of bytes, not the number of Unicode scalar values.
const text = "aé日"
print(text.len) // 5 bytes
print(text[0]) // 97, the first byte
print(text + "!")
Indexing a string yields a u8 byte. The bounds are checked at runtime. A string cannot be mutated; assigning to text[0] is an error. Build a new string with str::slice, concatenation, or a byte buffer when modification is required.
const prefix = str::slice(text, 0, 1)
const rest = str::slice(text, 1, text.len)
print(prefix + rest)
str::slice(text, start, end) uses byte offsets, with start inclusive and end exclusive. Slicing in the middle of a multi-byte UTF-8 sequence produces a byte sequence that is not valid UTF-8. Use the rune-aware functions when the slice must respect character boundaries.
Runes#
A rune is a Unicode scalar value. It is a nominal type: it is not an integer and cannot be used where an integer is expected without an explicit conversion. There is no rune literal syntax; runes are produced by decoding strings or by converting an integer scalar value.
match rune::from_u32(0x263A) {
Success(smiley) => print(rune::to_u32(smiley))
Error(err) => print("invalid scalar value")
}
rune::from_u32 returns Result[rune, Utf8Error] and rejects surrogate code points and values above the Unicode maximum. rune::to_u32 returns the scalar value as a u32. print on a rune emits its numeric scalar value; interpolation renders the character, and the u format shows the decimal scalar value.
Rune-Aware String Operations#
The standard library exposes byte-oriented slices and rune-oriented decoding. The core language recognizes the qualified names and gives them their types.
| Operation | Result | Meaning |
|---|---|---|
str::rune_count(text) |
Result[u64, Utf8Error] |
Number of scalar values, rejecting invalid UTF-8 |
str::rune_count_lossy(text) |
u64 |
Number of scalar values, replacing invalid bytes |
str::rune_at(text, index) |
Result[rune, Utf8Error] |
Scalar value at a scalar index |
str::runes(text) |
Result[List[rune], Utf8Error] |
All scalar values, rejecting invalid UTF-8 |
str::runes_lossy(text) |
List[rune] |
All scalar values, replacing invalid bytes |
str::slice_runes(text, start, end) |
Result[str, Utf8Error] |
Substring by scalar index |
str::slice_runes_lossy(text, start, end) |
str |
Substring by scalar index, replacing invalid bytes |
The strict forms report Utf8Error.InvalidEncoding for malformed input and Utf8Error.OutOfBounds for an index outside the string. The lossy forms never fail; they substitute the replacement behavior defined by the runtime.
match str::runes("aé日") {
Success(values) => print(values.len)
Error(err) => print("invalid UTF-8")
}
Byte Buffers#
A bytes value is a mutable buffer of unsigned bytes. It has a length and a capacity; appending may grow the capacity. Indexing reads or writes a u8, and len is the current length as u64.
var buffer = bytes::new(4)
buffer[0] = 65
buffer[1] = 66
buffer[2] = 67
buffer[3] = 68
print(buffer.len)
buffer = bytes::append(buffer, 69)
print(bytes::to_str(buffer))
A byte buffer can be created from a string with bytes::from_str, converted back with bytes::to_str, sliced with bytes::slice, and exposed as a raw pointer with bytes::ptr. The conversion to str does not validate UTF-8; the result is a byte sequence with the str type, and rune operations may report an encoding error later.
var copy = bytes::from_str("hello")
copy[0] = 72
print(bytes::to_str(copy))
print(bytes::to_str(bytes::slice(copy, 1, 4)))
bytes is a managed value. The buffer is traced by the collector and does not need manual freeing. Mutating a buffer through one binding is visible through any other binding that refers to the same buffer.
C Strings#
A cstr is a pointer to a null-terminated byte sequence in unmanaged memory. It exists for calling C functions and for receiving strings from the operating system. It is not a general-purpose string type and it is not traced by the collector.
extern (C) puts(text cstr) i32 = "puts"
fn main() {
puts(cstr::from_str("hello from C"))
}
The built-in conversions below form the boundary between cstr and the managed world.
| Operation | Result |
|---|---|
cstr::from_str(text) |
A cstr view of a Haze string |
cstr::from_ptr(pointer) |
A cstr from a raw pointer |
cstr::to_str(value) |
Result[str, CStrError] |
cstr::to_bytes(value) |
Result[bytes, CStrError] |
cstr::is_null(value) |
bool |
cstr::null() |
The null cstr |
CStrError has two variants: NullPointer and InvalidUtf8. A null pointer cannot be converted to a string. Invalid UTF-8 is reported rather than replaced, so the caller decides whether to accept a lossy interpretation.
match cstr::to_str(cstr::from_str("world")) {
Success(text) => print(text)
Error(err) => print("conversion failed")
}
Raw Pointers#
ptr is an opaque raw pointer. It supports equality comparison and a small set of built-in operations for pointer arithmetic and conversion. It is managed as a root value but points to unmanaged memory; the collector does not trace through it.
const null_pointer = ptr::null()
print(null_pointer == ptr::null())
const advanced = ptr::add(null_pointer, 8)
Use ptr only when a C API requires an opaque handle or an address. Passing a Haze-managed object as a raw pointer is not a supported general operation; the boundary types are cstr, bytes::ptr, and extern records.
Choosing a Representation#
Use str for textual data that does not need mutation. Use bytes for binary data, incremental construction, or a buffer that will be converted to text at the end. Use rune when the program must reason about Unicode scalar values rather than bytes. Use cstr only in an extern declaration or at the immediate point of conversion from a C API. The Extern and C FFI chapter covers the boundary rules in more detail.