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
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_resultIterates 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_idxFunctions
Copies a C string into a sealed str in the arena: the way to accept a char* from an external library.
1 if the character is in A–Z.
1 if the character is in a–z.
1 if the character is a decimal digit. Despite the parameter name it takes a single char.
1 if the character is a letter or a digit (alphanumeric, in this library). Takes a single char.
1 if every character of the string is lower case (or the string is empty).
1 if every character of the string is upper case (or the string is empty).
1 if every character is a letter or a digit (or the string is empty).
Same as strisalnum: isalpha includes digits here.
Index of the first occurrence of substr, or -1.
A new string with the first num_char_to_get bytes of stringa.
1 if c appears anywhere in chars.
Maps the "" sentinel of the strip functions to the generic whitespace set (space, tab, newline, CR).
Removes leading characters that appear in chars ("" = whitespace). Returns a pointer into the original string (no allocation).
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.
A new string with the ASCII letters a-z turned to upper case; every other byte is copied as it is.
Removes trailing characters that appear in chars ("" = whitespace). Returns a new string.
lstrip then rstrip.1 if every character of the string is in chars ("" = whitespace), including the empty string; the non-allocating equivalent of strip(s, chars) == "".
Number of non-overlapping occurrences of substr.
Number of occurrences of the character c.
1 if string starts with substring, 0 otherwise.
1 if string ends with substring, 0 otherwise.
Index of the first occurrence of the character, or -1.
Index of the last occurrence of the character, negative if absent.
A new string with the bytes in [start_pos, end_pos).
The line number num_riga (0-based) of a multi-line text, as a new string.
How many pieces split_next would produce splitting on the whole separator sep.
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.
A new string with every non-overlapping occurrence of old replaced by sostituto (which may be empty). old must not be empty.
A new empty Joiner using sep between pieces.
Appends a piece. Mutates j in place and returns it, so calls can be chained.
The pieces joined so far, "" if none.
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.
Repositions the reader as if reader_next_row had been called exactly new_idx times from the start.
A Reader positioned before the first line of all_text.
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.
Length of a C string (ptr char): always a scan, the header is never read.
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.
- def substrget_nursery(stringa: str, num_char_to_get: long) -> str
- def get_chars_in_range_nursery(stringa: str, start_pos: long, end_pos: long) -> str
- def reader_slab_new(reader_o: Reader, min_cap: long) -> None
- def reader_slab_take_row(reader_o: Reader, src: str, len_no_nl: long) -> str