Keyboard shortcuts

Press ← or → to navigate between chapters

Press S or / to search in the book

Press ? to show this help

Press Esc to hide this help

string

std › string

Strings packed into one integer. A string literal like "hi" is a struct of code points; string_from_struct encodes it as UTF-8 bytes in a single int, with its length in bytes.

from std.string import *
from std.binary import Endian

macro show(value: int) {
    @emit value
}

macro demo() {
    const s = string_from_struct("héllo")
    show s.len
    show utf8_codepoint_count(s as Utf8String<6, Endian.Big>)
    show ascii_upper(string_from_struct("hi") as AsciiString<2, Endian.Big>).value
}

demo
6 5 18505

18505 is 0x4849, the bytes of HI. Converting to AsciiString or Utf8String with as checks the bytes are valid, and picks their Endian: whether byte 0 is the most (Big) or least (Little) significant byte of value. The byte order doesn’t change the UTF-8.

Macros

byte_at

Byte index of len bytes packed into value, counting from the endian end. index must be within the string.

SyntaxParametersResultDescription
byte_at(value, len, index, endian)value: int, len: int, index: int, endian: Endianreturns int

utf8_struct_byte_len

How many bytes a string literal (or any struct of code points) takes as UTF-8.

SyntaxParametersResultDescription
utf8_struct_byte_len(source)<S>, source: Sreturns int

string_from_struct

A string literal (or any struct of code points) as a String of its UTF-8 bytes, first character most significant.

SyntaxParametersResultDescription
string_from_struct source<S>, source: S

validate_ascii

1 if len bytes packed into value are all ASCII. Otherwise it’s a compile error.

SyntaxParametersResultDescription
validate_ascii(value, len, endian)value: int, len: int, endian: Endianreturns int

validate_utf8

1 if len bytes packed into value are valid UTF-8: no stray or missing continuation bytes, overlong forms, surrogates, or code points past U+10FFFF. Otherwise it’s a compile error.

SyntaxParametersResultDescription
validate_utf8(value, len, endian)value: int, len: int, endian: Endianreturns int

ascii_byte_at

Byte index of s.

SyntaxParametersResultDescription
ascii_byte_at(s, index)s: AsciiString, index: intreturns int

utf8_byte_at

Byte index of s. This is a byte, not a character.

SyntaxParametersResultDescription
utf8_byte_at(s, index)s: Utf8String, index: intreturns int

ascii_upper

s with a-z made uppercase.

SyntaxParametersResultDescription
ascii_upper(s)s: AsciiStringreturns AsciiString

ascii_lower

s with A-Z made lowercase.

SyntaxParametersResultDescription
ascii_lower(s)s: AsciiStringreturns AsciiString

ascii_title

s with its first byte made uppercase.

SyntaxParametersResultDescription
ascii_title(s)s: AsciiStringreturns AsciiString

utf8_codepoint_count

The number of characters (code points) in s.

SyntaxParametersResultDescription
utf8_codepoint_count(s)s: Utf8Stringreturns int

utf8_is_ascii

1 if every byte of s is ASCII, else 0.

SyntaxParametersResultDescription
utf8_is_ascii(s)s: Utf8Stringreturns int

Types

String

struct String<const len: int>

len bytes packed into value, first byte most significant. string_from_struct builds one. Convert it with as to AsciiString or Utf8String to check its bytes and choose their order.

FieldTypeDescription
valueintThe bytes, packed.
lenintThe number of bytes.

AsciiString

struct AsciiString<const len: int, const endian: Endian>

len ASCII bytes (each below 0x80) packed into value, with byte 0 at the endian end.

FieldTypeDescription
valueintThe bytes, packed.
lenintThe number of bytes.
endianEndianWhich end of value holds byte 0.

Utf8String

struct Utf8String<const len: int, const endian: Endian>

len bytes of valid UTF-8 packed into value, with byte 0 at the endian end.

FieldTypeDescription
valueintThe bytes, packed.
lenintThe number of bytes.
endianEndianWhich end of value holds byte 0.