Operations on ordinary null-terminated char * strings, not a dedicated string type - there's nothing to allocate or free beyond the buffer you already have.
More...
|
Functions | |
| size_t | sys_string_bytes (const char *str) |
| Return the length of a null-terminated string, in bytes. More... | |
| size_t | sys_string_runes (const char *str) |
| Count the runes in a null-terminated UTF-8 string. More... | |
| ptrdiff_t | sys_string_compare (const char *a, const char *b) |
| Compare two null-terminated strings byte by byte. More... | |
| char * | sys_string_to_upper (char *str) |
| Convert a string to uppercase in place. More... | |
| char * | sys_string_to_lower (char *str) |
| Convert a string to lowercase in place. More... | |
| char * | sys_string_trimspace (char *str) |
| Trim leading and trailing whitespace from a string in place. More... | |
| char * | sys_string_trimprefix (char *s, const char *prefix) |
| Remove prefix from the start of s, if present. More... | |
| char * | sys_string_trimsuffix (char *s, const char *suffix) |
| Remove suffix from the end of s, if present. More... | |
| bool | sys_string_hasprefix (const char *s, const char *prefix) |
| Reports whether s begins with prefix. More... | |
| bool | sys_string_hassuffix (const char *s, const char *suffix) |
| Reports whether s ends with suffix. More... | |
| ptrdiff_t | sys_string_contains (const char *s, const char *substr) |
| Find the first byte offset where substr occurs within s. More... | |
| sys_iostream_t * | sys_string_read (const char *str) |
| Wrap a string in a read-only stream. More... | |
| sys_iostream_t * | sys_string_open (char *buf, size_t cap) |
| Open a read/write stream backed by a caller-provided mutable buffer. More... | |
| bool | sys_string_parse_escape (const char *str, size_t len, rune_t *rune) |
| Decode a JSON-style backslash escape sequence into the rune it denotes. More... | |
| bool | sys_string_parse_bool (const char *str, bool *out) |
| Parse "true" or "false" into a bool. More... | |
| ptrdiff_t | sys_string_parse_quoted (const char *str, size_t len, char *out, size_t cap) |
| Decode a quoted string into its unescaped content. More... | |
| bool | sys_string_parse_int32 (const char *str, size_t len, int32_t *value) |
| Parse a signed 32-bit integer. More... | |
| bool | sys_string_parse_int64 (const char *str, size_t len, int64_t *value) |
| Parse a signed 64-bit integer. More... | |
| bool | sys_string_parse_uint32 (const char *str, size_t len, uint32_t *value) |
| Parse an unsigned 32-bit integer. More... | |
| bool | sys_string_parse_uint64 (const char *str, size_t len, uint64_t *value) |
| Parse an unsigned 64-bit integer. More... | |
| bool | sys_string_parse_float32 (const char *str, size_t len, float *value) |
| Parse a 32-bit floating-point number. More... | |
| bool | sys_string_parse_float64 (const char *str, size_t len, double *value) |
| Parse a 64-bit floating-point number. More... | |
Operations on ordinary null-terminated char * strings, not a dedicated string type - there's nothing to allocate or free beyond the buffer you already have.
The mutating functions (sys_string_to_upper(), sys_string_trimspace(), sys_string_trimprefix(), sys_string_trimsuffix()) are all destructive, writing through the pointer you pass in, and all return that same pointer back.
Example - trim, check, and uppercase:
| size_t sys_string_bytes | ( | const char * | str | ) |
Return the length of a null-terminated string, in bytes.
| str | Pointer to a null-terminated string, or NULL. |
| ptrdiff_t sys_string_compare | ( | const char * | a, |
| const char * | b | ||
| ) |
Compare two null-terminated strings byte by byte.
| a | First string, or NULL (treated as ""). |
| b | Second string, or NULL (treated as ""). |
| ptrdiff_t sys_string_contains | ( | const char * | s, |
| const char * | substr | ||
| ) |
Find the first byte offset where substr occurs within s.
| s | String to search, or NULL (treated as ""). |
| substr | Substring to look for, or NULL (treated as "", which is found at offset 0 in any string, including ""). |
| bool sys_string_hasprefix | ( | const char * | s, |
| const char * | prefix | ||
| ) |
Reports whether s begins with prefix.
| s | String to check, or NULL (treated as ""). |
| prefix | Prefix to look for, or NULL (treated as "", which every string has as a prefix). |
| bool sys_string_hassuffix | ( | const char * | s, |
| const char * | suffix | ||
| ) |
Reports whether s ends with suffix.
| s | String to check, or NULL (treated as ""). |
| suffix | Suffix to look for, or NULL (treated as "", which every string has as a suffix). |
| sys_iostream_t* sys_string_open | ( | char * | buf, |
| size_t | cap | ||
| ) |
Open a read/write stream backed by a caller-provided mutable buffer.
| buf | The buffer to read from and write into (no copy is made - buf must stay alive for the stream's lifetime). Always starts empty - buf[0] is set to '\0' regardless of whatever it previously contained. |
| cap | The buffer's total capacity in bytes, including room for a trailing NUL terminator. Must be at least 1. |
Reads and writes share one cursor, starting at position 0. Reads and seeks never go past the current content length; only a write can move that boundary, and only forward, up to (cap - 1) bytes - the last byte of cap is always reserved for a NUL terminator, kept up to date after every write, so buf is a valid, correctly terminated C string of whatever's been written so far at any point, not just once writing is done. Writing past the (cap - 1)-byte usable capacity doesn't grow the buffer; sys_iostream_write() truncates and reports the actual number of bytes written, the same convention as sys_sprintf().
| bool sys_string_parse_bool | ( | const char * | str, |
| bool * | out | ||
| ) |
Parse "true" or "false" into a bool.
| str | String to parse, or NULL. |
| out | Pointer to store the result. Left unchanged on a parse error - there's no reserved error value for bool the way RUNE_ERROR is for rune_t. |
| bool sys_string_parse_escape | ( | const char * | str, |
| size_t | len, | ||
| rune_t * | rune | ||
| ) |
Decode a JSON-style backslash escape sequence into the rune it denotes.
| str | Pointer to the escape sequence, starting at the backslash (e.g. as matched by sys_scanner_escapes - see sys/scanner.h). |
| len | The escape's exact length in bytes (2 for \" \\ \/ \b \f \n \r \t, 6 for \uXXXX), or 0 for str's length up to its own NUL terminator. Either way, str must contain exactly one escape and nothing else - anything past it (before len bytes, or before the terminator when len is 0) is a parse error, not silently ignored. With len set, str need not be NUL-terminated at all, and nothing past str[len - 1] is read. |
| rune | Pointer to store the decoded rune. Set to RUNE_ERROR on a parse error. |
| bool sys_string_parse_float32 | ( | const char * | str, |
| size_t | len, | ||
| float * | value | ||
| ) |
Parse a 32-bit floating-point number.
| str | Pointer to the number, optionally signed with a leading '+' or '-'. Recognizes ordinary decimal notation ("3.14", "-0.5", ".5", "5.", "1e10", "1.5e-3") and the exact literals "NaN" and "Inf" (optionally signed, e.g. "-Inf") - no hex/octal/binary floats, and no other spelling of infinity/not-a-number ("inf", "Infinity", "nan" are all parse errors, not accepted case-insensitively). |
| len | The number's exact length in bytes, or 0 for str's length up to its own NUL terminator. Either way, str must contain exactly one number and nothing else. With len set, str need not be NUL-terminated at all. |
| value | Pointer to store the result. Left unchanged on a parse error. |
| bool sys_string_parse_float64 | ( | const char * | str, |
| size_t | len, | ||
| double * | value | ||
| ) |
Parse a 64-bit floating-point number.
| str | Pointer to the number, optionally signed with a leading '+' or '-'. Same recognized forms as sys_string_parse_float32(). |
| len | The number's exact length in bytes, or 0 for str's length up to its own NUL terminator. Either way, str must contain exactly one number and nothing else. With len set, str need not be NUL-terminated at all. |
| value | Pointer to store the result. Left unchanged on a parse error. |
| bool sys_string_parse_int32 | ( | const char * | str, |
| size_t | len, | ||
| int32_t * | value | ||
| ) |
Parse a signed 32-bit integer.
| str | Pointer to the number, optionally signed with a leading '+' or '-'. |
| len | The number's exact length in bytes, or 0 for str's length up to its own NUL terminator. Either way, str must contain exactly one number and nothing else - trailing content is a parse error, not ignored. With len set, str need not be NUL-terminated at all. |
| value | Pointer to store the result. Left unchanged on a parse error - there's no reserved error value for int32_t the way RUNE_ERROR is for rune_t. |
| bool sys_string_parse_int64 | ( | const char * | str, |
| size_t | len, | ||
| int64_t * | value | ||
| ) |
Parse a signed 64-bit integer.
| str | Pointer to the number, optionally signed with a leading '+' or '-'. |
| len | The number's exact length in bytes, or 0 for str's length up to its own NUL terminator. Either way, str must contain exactly one number and nothing else. With len set, str need not be NUL-terminated at all. |
| value | Pointer to store the result. Left unchanged on a parse error. |
| ptrdiff_t sys_string_parse_quoted | ( | const char * | str, |
| size_t | len, | ||
| char * | out, | ||
| size_t | cap | ||
| ) |
Decode a quoted string into its unescaped content.
| str | Pointer to the quoted string, starting at the opening quote (' or ") - e.g. as matched by sys_scanner_quotes (see sys/scanner.h). Always NUL-terminated, regardless of len. |
| len | The quoted string's exact length in bytes, opening quote through closing quote inclusive, or 0 for str's length up to its own NUL terminator. Either way, str must contain exactly one complete, closed quoted string and nothing else - anything past the closing quote is a parse error, and so is never finding one. |
| out | Destination buffer for the unescaped content, or NULL to write nothing and just get the decoded length (cap is then ignored, as if it were 0, regardless of what's passed). May also be str itself, decoding in place - decoding never expands content (every escape's decoded form is no longer than the escape it came from), so writing into str as it's read never overtakes what's still being read. |
| cap | Capacity of out. |
| bool sys_string_parse_uint32 | ( | const char * | str, |
| size_t | len, | ||
| uint32_t * | value | ||
| ) |
Parse an unsigned 32-bit integer.
| str | Pointer to the number. A leading '+' is accepted; a leading '-' is always a parse error - even "-0" - there's no negative representation of an unsigned value. |
| len | The number's exact length in bytes, or 0 for str's length up to its own NUL terminator. Either way, str must contain exactly one number and nothing else. With len set, str need not be NUL-terminated at all. |
| value | Pointer to store the result. Left unchanged on a parse error. |
| bool sys_string_parse_uint64 | ( | const char * | str, |
| size_t | len, | ||
| uint64_t * | value | ||
| ) |
Parse an unsigned 64-bit integer.
| str | Pointer to the number. A leading '+' is accepted; a leading '-' is always a parse error - even "-0". |
| len | The number's exact length in bytes, or 0 for str's length up to its own NUL terminator. Either way, str must contain exactly one number and nothing else. With len set, str need not be NUL-terminated at all. |
| value | Pointer to store the result. Left unchanged on a parse error. |
| sys_iostream_t* sys_string_read | ( | const char * | str | ) |
Wrap a string in a read-only stream.
| str | Pointer to a null-terminated string, or NULL. |
| size_t sys_string_runes | ( | const char * | str | ) |
Count the runes in a null-terminated UTF-8 string.
| str | Pointer to a null-terminated UTF-8 string, or NULL. |
| char* sys_string_to_lower | ( | char * | str | ) |
Convert a string to lowercase in place.
| str | Pointer to a mutable, null-terminated UTF-8 string, or NULL. |
| char* sys_string_to_upper | ( | char * | str | ) |
Convert a string to uppercase in place.
| str | Pointer to a mutable, null-terminated UTF-8 string, or NULL. |
| char* sys_string_trimprefix | ( | char * | s, |
| const char * | prefix | ||
| ) |
Remove prefix from the start of s, if present.
| s | Pointer to a mutable, null-terminated string, or NULL. |
| prefix | Prefix to remove, or NULL (treated as ""). |
| char* sys_string_trimspace | ( | char * | str | ) |
Trim leading and trailing whitespace from a string in place.
| str | Pointer to a mutable, null-terminated UTF-8 string, or NULL. |
| char* sys_string_trimsuffix | ( | char * | s, |
| const char * | suffix | ||
| ) |
Remove suffix from the end of s, if present.
| s | Pointer to a mutable, null-terminated string, or NULL. |
| suffix | Suffix to remove, or NULL (treated as ""). |