Skip to content

std.strings

arena-backed StringBuilder (also a Writer) and byte-string helpers

pub StringBuilder :: struct {
arena: *Arena,
buf: []u8,
n: usize,
indent_level: usize,
}
  • buf: backing storage; cap == buf.len
  • n: bytes written; not len, that’s the method
  • indent_level: indentation depth (no indent/dedent fns exist)
pub builder :: fn(a: *Arena) -> StringBuilder

storage lives in a, outlives the builder

pub builder_cap :: fn(a: *Arena, cap: usize) -> StringBuilder

reserves at least cap bytes

pub from :: fn(a: *Arena, s: []const u8) -> StringBuilder

starts with a copy of s

pub sb_grow :: fn(self: *StringBuilder, extra: usize)

room for extra more bytes; doubles, or grows to fit

pub append :: fn(self: *StringBuilder, s: []const u8)
pub append_byte :: fn(self: *StringBuilder, c: u8)
pub append_repeat :: fn(self: *StringBuilder, c: u8, n: usize)

n copies of c

pub append_rune :: fn(self: *StringBuilder, cp: u32)

utf-8 encoded; U+FFFD for a surrogate or past U+10FFFF

pub append_u64 :: fn(self: *StringBuilder, v: u64)
pub append_i64 :: fn(self: *StringBuilder, v: i64)
pub append_base :: fn(self: *StringBuilder, v: u64, base: u8)

base 2..36

pub append_hex :: fn(self: *StringBuilder, v: u64)
pub append_bin :: fn(self: *StringBuilder, v: u64)
pub append_hex_byte :: fn(self: *StringBuilder, v: u8)

always two lowercase hex digits

pub append_u64_pad :: fn(self: *StringBuilder, v: u64, width: usize, pad: u8)

decimal, left-padded with pad to width

pub append_bool :: fn(self: *StringBuilder, b: bool)
pub str :: fn(self: *StringBuilder) -> []const u8

view into the buffer; invalid after the next grow

pub len :: fn(self: *StringBuilder) -> usize
pub cap :: fn(self: *StringBuilder) -> usize
pub is_empty :: fn(self: *StringBuilder) -> bool
pub byte_at :: fn(self: *StringBuilder, i: usize) -> u8
pub clear :: fn(self: *StringBuilder)

keeps capacity

pub truncate :: fn(self: *StringBuilder, n: usize)

keeps the first n bytes

pub pop :: fn(self: *StringBuilder) -> ?u8

null if empty

pub set_byte :: fn(self: *StringBuilder, i: usize, c: u8)
pub append_f64 :: fn(self: *StringBuilder, v: f64)

6 places, truncating; not shortest round-trip

pub append_f64_prec :: fn(self: *StringBuilder, v: f64, prec: usize)
pub append_f64_min :: fn(self: *StringBuilder, v: f64)

append_f64 without trailing zeros or bare dot (2.0 -> “2”); minimal json numbers

pub str_eq :: fn(a: []const u8, b: []const u8) -> bool
pub str_starts_with :: fn(s: []const u8, prefix: []const u8) -> bool
pub str_ends_with :: fn(s: []const u8, suffix: []const u8) -> bool
pub str_index_of :: fn(s: []const u8, needle: []const u8) -> ?usize

first index of needle, or null

pub str_contains :: fn(s: []const u8, needle: []const u8) -> bool
pub str_count_byte :: fn(s: []const u8, c: u8) -> usize
pub is_digit :: fn(c: u8) -> bool
pub is_upper :: fn(c: u8) -> bool
pub is_lower :: fn(c: u8) -> bool
pub is_alpha :: fn(c: u8) -> bool
pub is_alnum :: fn(c: u8) -> bool
pub is_space :: fn(c: u8) -> bool
pub to_upper_byte :: fn(c: u8) -> u8
pub to_lower_byte :: fn(c: u8) -> u8
pub str_trim_left :: fn(s: []const u8) -> []const u8
pub str_trim_right :: fn(s: []const u8) -> []const u8
pub str_trim :: fn(s: []const u8) -> []const u8
pub str_eq_ignore_case :: fn(a: []const u8, b: []const u8) -> bool
pub str_dup :: fn(a: *Arena, s: []const u8) -> []const u8
pub str_concat :: fn(a: *Arena, x: []const u8, y: []const u8) -> []const u8
pub str_repeat :: fn(a: *Arena, s: []const u8, count: usize) -> []const u8
pub str_upper :: fn(a: *Arena, s: []const u8) -> []const u8
pub str_lower :: fn(a: *Arena, s: []const u8) -> []const u8
pub str_replace :: fn(a: *Arena, s: []const u8, from: []const u8, to: []const u8) -> []const u8

left to right, non-overlapping

pub str_join :: fn(a: *Arena, parts: []const []const u8, sep: []const u8) -> []const u8
pub str_last_index_of :: fn(s: []const u8, needle: []const u8) -> ?usize
pub str_count :: fn(s: []const u8, needle: []const u8) -> usize

non-overlapping

pub split :: fn(s: []const u8, sep: []const u8) -> mem::Split(u8)

parts between seps, empty ones included

pub fields :: fn(s: []const u8) -> mem::Tokens(u8)

words between runs of spaces, tabs, line breaks

pub Lines :: struct {
rest: []const u8,
}

lines without \n or \r\n; no empty line after a final break

pub next :: fn(self: *Self) -> ?[]const u8
pub lines :: fn(s: []const u8) -> Lines