libstd/string

Strings are NUL-terminated byte sequences (see Strings). This module provides length, search, slicing, trimming, splitting and joining, and a line Reader. Lengths, positions and counts are long, so a text can exceed 4 GB. Results that are new strings are allocated in the arena and promoted to the caller's scope.

#include <libstd/string>

Includes libstd/stdio, libstd/type. Same functions and constant names on Linux and Windows: the system-specific part is in wrapper/linux/string and wrapper/windows/string (see Linux and Windows).

Example

#include <libstd/stdio>
#include <libstd/string>

s = "  key = value  "
t = strip(s, "")                       // "key = value"
pos = find_char(t, '=')                // 3
k = strip(get_chars_in_range(t, 0, pos), "")
v = strip(t + pos + 1, "")             // pointer arithmetic skips "= "
print(k, ":", v, "\n")                 // key:value

print(replace("a-b-c", "-", "+"), "\n") // a+b+c

Classes

class
Joiner

Accumulates pieces separated by sep; see joiner_new. Methods: j.add(piece), j.get_result().

    sep str
    result str
    count int
    add &joiner_add
    get_result &joiner_result
class
Reader

Iterates a text line by line without copying the whole text; see new_reader. Methods: r.next_row(), r.set_idx(n).

    all_text str
    actual_row str
    offset_start long
    num_line long
    total_num_nl long
    eof int
    row_slab str
    row_slab_cap long
    row_slab_used long
    slab_new &reader_slab_new
    slab_take_row &reader_slab_take_row
    next_row &reader_next_row
    set_idx &reader_set_idx

Functions

def str_from_c(p: ptr char) -> str

Copies a C string into a sealed str in the arena: the way to accept a char* from an external library.

def isupper(c: char) -> int

1 if the character is in A–Z.

def islower(c: char) -> int

1 if the character is in a–z.

def isnumeric(c: char) -> int

1 if the character is a decimal digit. Despite the parameter name it takes a single char.

def isalpha(c: char) -> int

1 if the character is a letter or a digit (alphanumeric, in this library). Takes a single char.

def is_all_lower(stringa: str) -> int

1 if every character of the string is lower case (or the string is empty).

def is_all_upper(stringa: str) -> int

1 if every character of the string is upper case (or the string is empty).

def strisalnum(stringa: str) -> int

1 if every character is a letter or a digit (or the string is empty).

def strisalalpha(stringa: str) -> int

Same as strisalnum: isalpha includes digits here.

def findsubstr(stringa: str, substr: str) -> long

Index of the first occurrence of substr, or -1.

def substrget(stringa: str, num_char_to_get: long) -> str

A new string with the first num_char_to_get bytes of stringa.

def char_in_set(c: char, chars: str) -> int

1 if c appears anywhere in chars.

def resolve_strip_chars(chars: str) -> str

Maps the "" sentinel of the strip functions to the generic whitespace set (space, tab, newline, CR).

def lstrip(stringa: str, chars: str) -> str

Removes leading characters that appear in chars ("" = whitespace). Returns a pointer into the original string (no allocation).

def tolower(s: str) -> str

A new string with the ASCII letters A-Z turned to lower case; every other byte, UTF-8 accented letters included, is copied as it is. tolower("Content-Length") is "content-length". libstd/http uses it for header names, which ignore case.

def toupper(s: str) -> str

A new string with the ASCII letters a-z turned to upper case; every other byte is copied as it is.

def rstrip(stringa: str, chars: str) -> str

Removes trailing characters that appear in chars ("" = whitespace). Returns a new string.

def strip(stringa: str, chars: str) -> str
lstrip then rstrip.
def is_blank(stringa: str, chars: str) -> int

1 if every character of the string is in chars ("" = whitespace), including the empty string; the non-allocating equivalent of strip(s, chars) == "".

def find_num_occurence(stringa: str, substr: str) -> long

Number of non-overlapping occurrences of substr.

def strcount(stringa: str, c: char) -> long

Number of occurrences of the character c.

def startswith(string: str, substring: str) -> int

1 if string starts with substring, 0 otherwise.

def endswith(string: str, substring: str) -> int

1 if string ends with substring, 0 otherwise.

def find_char(stringa: str, occurr: char) -> long

Index of the first occurrence of the character, or -1.

def rfind(stringa: str, occurr: char) -> long

Index of the last occurrence of the character, negative if absent.

def get_chars_in_range(stringa: str, start_pos: long, end_pos: long) -> str

A new string with the bytes in [start_pos, end_pos).

def strreadline(text: str, num_riga: long) -> str

The line number num_riga (0-based) of a multi-line text, as a new string.

def split_count(stringa: str, sep: str) -> long

How many pieces split_next would produce splitting on the whole separator sep.

def split_next(stringa: str, sep: str, offset: long) -> (long, str)

Returns the piece of stringa starting at offset and ending at the next occurrence of sep, together with the offset of the following piece (-1 after the last one). Loop until the offset is -1.

def replace(stringa: str, old: str, sostituto: str) -> str

A new string with every non-overlapping occurrence of old replaced by sostituto (which may be empty). old must not be empty.

def joiner_new(sep: str) -> Joiner

A new empty Joiner using sep between pieces.

def joiner_add(j: Joiner, pezzo: str) -> Joiner

Appends a piece. Mutates j in place and returns it, so calls can be chained.

def joiner_result(j: Joiner) -> str

The pieces joined so far, "" if none.

def reader_next_row(reader_o: Reader) -> str

Returns the next line (without the newline) and advances; sets eof after the last line. Lines are carved from a slab owned by the reader, so they stay valid until the reader is dropped.

def reader_set_idx(reader_o: Reader, new_idx: long) -> None

Repositions the reader as if reader_next_row had been called exactly new_idx times from the start.

def new_reader(all_text: str) -> Reader

A Reader positioned before the first line of all_text.

def strlen(stringa: str) -> long

Number of bytes before the terminator: read from the header when the string is sealed (a literal, a +, a reader row...), otherwise an SSE scan, 16 bytes per step.

def strlen_c(p: ptr char) -> long

Length of a C string (ptr char): always a scan, the header is never read.

def memcpy(dest: str, src: str, n: long) -> None

Copies n bytes with SSE2 (rt_simd_memcpy). Both buffers must have the 16-byte margin every arena allocation has.

Internal helpers

Used by the functions above; not meant to be called directly.