Skip to content
volt
GitHub

std::text

Searching, trimming, splitting, replacing and parsing text (methods on str), UTF-8, and ASCII character classes (methods on u8). What makes new text returns a std::string; splitting returns views into the text it split, as a std::vec<str>.

Source: std/text.volt

Types

error std::text::parse_error

error parse_error

why parse_int, parse_uint, parse_float or parse_bool failed

Variant
EMPTYthere was nothing to parse
INVALIDit isn't a number (or true/false)
OVERFLOWthe number doesn't fit

Functions

std::text::join

<A: std::mem::allocator = std::mem::default_allocator> fn join(parts: str[..], sep: str, allocator: A = {}) -> std::string<A>

the parts with sep between them

std::text::join

<B: std::mem::allocator, A: std::mem::allocator = std::mem::default_allocator> fn join(parts: std::string<B>[..], sep: str, allocator: A = {}) -> std::string<A>

the parts with sep between them (owned strings, like std::fs::list_dir gives)

Methods on other types

Attached to built-in types, or to every type (T).

attach fn find(this: str&, needle: str) -> usize?

where needle first starts, as a byte index (an empty needle is at 0)

attach fn rfind(this: str&, needle: str) -> usize?

where needle last starts

attach fn contains(this: str&, needle: str) -> bool

is needle in it?

attach fn starts_with(this: str&, prefix: str) -> bool
attach fn ends_with(this: str&, suffix: str) -> bool
attach fn count(this: str&, needle: str) -> usize

how many times needle appears, not overlapping (an empty needle counts 0)

attach fn trim(this: str&) -> str

without the ASCII whitespace at either end

attach fn trim_start(this: str&) -> str
attach fn trim_end(this: str&) -> str
attach fn strip_prefix(this: str&, prefix: str) -> str?

the rest after prefix (null when it doesn't start with it)

attach fn strip_suffix(this: str&, suffix: str) -> str?

what comes before suffix (null when it doesn't end with it)

attach fn split_once(this: str&, sep: str) -> (str, str)?

the parts before and after the first sep (null when there's none)

<A: std::mem::allocator = std::mem::default_allocator> attach fn split(this: str&, sep: str, allocator: A = {}) -> std::vec<str, A>

the parts between each sep ("a,,b" has an empty one in the middle); an empty sep splits into characters

<A: std::mem::allocator = std::mem::default_allocator> attach fn lines(this: str&, allocator: A = {}) -> std::vec<str, A>

the lines, without their \n or \r\n (a final newline doesn't start another line)

<A: std::mem::allocator = std::mem::default_allocator> attach fn words(this: str&, allocator: A = {}) -> std::vec<str, A>

the words: the parts between runs of ASCII whitespace

<A: std::mem::allocator = std::mem::default_allocator> attach fn replace(this: str&, from: str, to: str, allocator: A = {}) -> std::string<A>

every from replaced by to, left to right (an empty from changes nothing)

<A: std::mem::allocator = std::mem::default_allocator> attach fn repeat(this: str&, n: usize, allocator: A = {}) -> std::string<A>

n copies, one after another

<A: std::mem::allocator = std::mem::default_allocator> attach fn to_upper(this: str&, allocator: A = {}) -> std::string<A>

ASCII letters in upper case (other bytes as they are)

<A: std::mem::allocator = std::mem::default_allocator> attach fn to_lower(this: str&, allocator: A = {}) -> std::string<A>

ASCII letters in lower case

attach fn eq_ignore_case(this: str&, other: str) -> bool

equal but for the case of ASCII letters?

attach fn cmp(this: str&, other: str) -> i32

-1, 0 or 1: how it sorts against other, byte by byte (a prefix sorts first)

attach fn parse_int(this: str&) -> std::text::parse_error!i64

a decimal integer with an optional sign: "42", "-7", "+3"

attach fn parse_uint(this: str&) -> std::text::parse_error!u64

a decimal integer that isn't negative: "42", "+3"

attach fn parse_float(this: str&) -> std::text::parse_error!f64

a decimal float: "3.5", "-1e3", ".5", "5.", "inf", "nan" (no spaces around it; too big is inf)

attach fn parse_bool(this: str&) -> std::text::parse_error!bool

"true" or "false"

attach fn char_at(this: str&, i: usize) -> (u32, usize)?

the character that starts at byte i, and its length in bytes; null when a valid one doesn't start there (i is inside one, or the bytes aren't UTF-8)

attach fn is_utf8(this: str&) -> bool

is it valid UTF-8?

attach fn utf8_error(this: str&) -> usize?

where the first byte that isn't part of a valid UTF-8 character is (a stray byte, an overlong or cut-off sequence, a surrogate), or null when all of it is valid

attach fn char_count(this: str&) -> usize

how many characters (a byte that isn't valid UTF-8 counts as one)

<A: std::mem::allocator = std::mem::default_allocator> attach fn chars(this: str&, allocator: A = {}) -> std::vec<u32, A>

the characters as code points (a byte that isn't valid UTF-8 is U+FFFD)

attach fn is_digit(this: u8&) -> bool
attach fn is_alpha(this: u8&) -> bool
attach fn is_alnum(this: u8&) -> bool
attach fn is_space(this: u8&) -> bool

space, tab, newline, carriage return, vertical tab or form feed

attach fn is_upper(this: u8&) -> bool
attach fn is_lower(this: u8&) -> bool
attach fn is_hex_digit(this: u8&) -> bool
attach fn to_upper(this: u8&) -> u8

an ASCII letter in upper case (anything else as it is)

attach fn to_lower(this: u8&) -> u8

an ASCII letter in lower case