std1.0.0-beta-7 SafeC docs

std :: collections::string

Struct std::String

#include <std/collections/string.h> source

struct String {
    &heap char data;
    unsigned long len;
    unsigned long cap;
}

Fields

§&heap char datasource

NUL-terminated heap buffer

§unsigned long lensource

byte length (not including NUL)

§unsigned long capsource

allocated buffer size (including NUL slot)

Methods

§static struct String new()source · definition

empty

§static struct String from(const &char s)source · definition

copy of a C string

§static struct String from_bytes([]const char bytes)source · definition

copy of the bytes (need not be NUL-terminated)

§static struct String with_cap(unsigned long cap)source · definition

empty, room for cap bytes

§static struct String repeat_str(const &char s, unsigned long n)source · definition

s repeated n times

§static struct String join(const ?&char sep, []String parts)source · definition

parts concatenated, sep between each (null sep: none)

§unsigned long length() constsource · definition
§int is_empty() constsource · definition
§const &char as_ptr() constsource · definition

NUL-terminated C string

§[]const char as_bytes() constsource · definition

the len bytes (no NUL); valid until the next change

§int char_at(unsigned long idx) constsource · definition

-1 if OOB

§void set_char(unsigned long idx, char c)source · definition
§int reserve(unsigned long additional)source · definition

ensure room for len + additional bytes

§void shrink_to_fit()source · definition

reduce cap to len + 1

§int push_char(char c)source · definition
§int push(const &char cstr)source · definition
§int push_bytes([]const char bytes)source · definition

append the bytes (need not be NUL-terminated)

§int push_str(&String other)source · definition
§int push_int(long long v)source · definition
§int push_uint(unsigned long long v)source · definition
§int push_float(double v, int decimals)source · definition
§int push_bool(int v)source · definition
§void clear()source · definition
§void truncate(unsigned long new_len)source · definition
§int insert(unsigned long idx, const &char cstr)source · definition
§int remove_range(unsigned long start, unsigned long end)source · definition
§void replace_char(char from, char to)source · definition
§int replace(const &char from, const &char to)source · definition

first occurrence

§int replace_all(const &char from, const &char to)source · definition

Single left-to-right pass into a freshly-sized buffer — O(n). The previous implementation called index_of() (a full O(n) scan from the start of the string, not from the current position) once per match, then shifted the whole remaining tail via remove_range()+insert() for each one — O(matches * n) overall, quadratic for a string with many matches.

§void reverse()source · definition

in-place byte reversal

§int pop_char()source · definition

remove last byte, return it (-1 if empty)

§long long index_of(const &char needle) constsource · definition
§long long last_index_of(const &char needle) constsource · definition
§int contains(const &char needle) constsource · definition
§int starts_with(const &char prefix) constsource · definition
§int ends_with(const &char suffix) constsource · definition
§int count(const &char needle) constsource · definition
§long long find_char(char c) constsource · definition

index of first c, or -1

§long long rfind_char(char c) constsource · definition

index of last c, or -1

§struct String substr(unsigned long start, unsigned long end) constsource · definition
§struct String to_upper() constsource · definition
§struct String to_lower() constsource · definition
§struct String trim() constsource · definition
§struct String trim_left() constsource · definition
§struct String trim_right() constsource · definition
§struct String pad_left(unsigned long width, char fill) constsource · definition
§struct String pad_right(unsigned long width, char fill) constsource · definition
§struct String strip_prefix(const &char prefix) constsource · definition

remove prefix if present, else clone

§struct String strip_suffix(const &char suffix) constsource · definition

remove suffix if present, else clone

§struct String repeat(unsigned long n) constsource · definition

repeat content n times

§struct String capitalize() constsource · definition

uppercase first byte, lowercase rest

§unsigned long split(const &char delim, []String out) constsource · definition

All split methods write into the caller's out slots (out.len of them). Return value = number of items written. If splits exceed out.len, the last slot receives the remainder.

§unsigned long split_lines([]String out) constsource · definition
§unsigned long split_whitespace([]String out) constsource · definition
§int eq(&String other) constsource · definition
§int eq_cstr(const &char other) constsource · definition
§int cmp(&String other) constsource · definition

<0, 0, >0

§int lt(&String other) constsource · definition
§int gt(&String other) constsource · definition
§int eq_ignore_case(&String other) constsource · definition
§int eq_cstr_ignore_case(const &char other) constsource · definition
§int is_ascii() constsource · definition
§int is_numeric() constsource · definition
§int is_alphanumeric() constsource · definition
§long long parse_int(?&int ok) constsource · definition
§double parse_float(?&int ok) constsource · definition
§struct String clone() constsource · definition
§void drop()source · definition

frees the buffer (Drop)

§void free() consumesource · definition

= drop()