|
🧵 str
Painfully common string utilities for C
|
Utilities for string slices. More...
#include <stddef.h>#include <stdbool.h>Go to the source code of this file.
Data Structures | |
| struct | str |
| A string "slice", which is a pointer & length pair. More... | |
Macros | |
| #define | str_literal(SZ_LITERAL) |
| Constructs a str from a C-string literal. | |
| #define | str_strip_prefix(IT, PREFIX) |
| Returns a non-owning copy of a str with a given prefix removed, if it is present. | |
| #define | str_strip_postfix(IT, POSTFIX) |
| Returns a non-owning copy of a str with a given postfix removed, if it is present. | |
| #define | cstr_cmp_cstr strcmp |
| Compares two strings lexicographically. This is an alias to strncmp() for convenience. | |
| #define | str_cmp(LHS, RHS) |
| Compares two strings lexicographically. | |
| #define | str_eq(LHS, RHS) |
| Compares two strings to see if their contents are equal. | |
| #define | str_has_prefix(LHS, RHS) |
| Checks if a string begins with another string. | |
| #define | str_has_postfix(LHS, RHS) |
| Checks if a string ends with another string. | |
| #define | str_split(IT, ON) |
| Splits a str on the first occurence of something. | |
| #define | str_rsplit(IT, ON) |
| Splits a str on the last occurence of something. | |
Typedefs | |
| typedef struct str | str |
| A string "slice", which is a pointer & length pair. | |
Functions | |
| str | str_from_cstr (char *sz) |
| Constructs a non-owning str from a nil-terminated C-string. | |
| str | str_slice_cstr (char *sz, size_t len) |
| Constructs a non-owning str from part of a nil-terminated C-string. | |
| str | str_strip_whitespace_pre (str it) |
| Returns a non-owning copy of a str with all leading whitespace removed. | |
| str | str_strip_whitespace_post (str it) |
| Returns a non-owning copy of a str with all trailing whitespace removed. | |
| str | str_strip_whitespace (str it) |
| Returns a non-owning copy of a str with all leading and trailing whitespace removed. | |
| str | str_strip_prefix_str (str it, str prefix) |
| Returns a non-owning copy of a str with a given prefix removed, if it is present. | |
| str | str_strip_prefix_cstr (str it, const char *prefix) |
| Returns a non-owning copy of a str with a given prefix removed, if it is present. | |
| str | str_strip_prefix_if (str it, bool(*predicate)(char)) |
Returns a non-owning copy of a str with all leading characters satisfying predicate removed. | |
| str | str_strip_postfix_str (str it, str postfix) |
| Returns a non-owning copy of a str with a given postfix removed, if it is present. | |
| str | str_strip_postfix_cstr (str it, const char *postfix) |
| Returns a non-owning copy of a str with a given postfix removed, if it is present. | |
| str | str_strip_postfix_if (str it, bool(*predicate)(char)) |
Returns a non-owning copy of a str with all trailing characters satisfying predicate removed. | |
| int | str_cmp_str (str a, str b) |
| Compares two strings lexicographically. | |
| int | str_cmp_cstr (str a, const char *b) |
| Compares two strings lexicographically. | |
| int | cstr_cmp_str (const char *a, str b) |
| Compares two strings lexicographically. | |
| bool | str_eq_str (str a, str b) |
| Compares two strings to see if their contents are equal. | |
| bool | str_eq_cstr (str a, const char *b) |
| Compares two strings to see if their contents are equal. | |
| bool | cstr_eq_str (const char *a, str b) |
| Compares two strings to see if their contents are equal. | |
| bool | cstr_eq_cstr (const char *a, const char *b) |
| Compares two strings to see if their contents are equal. | |
| size_t | str_eq_str_any (str it, size_t count,...) |
| Compares a string to several others to see if its contents are equal to any of them. | |
| size_t | str_eq_strs_any (str it, size_t count, const str others[static count]) |
| Compares a string to several others to see if its contents are equal to any of them. | |
| size_t | str_eq_cstr_any (str it, size_t count,...) |
| Compares a string to several others to see if its contents are equal to any of them. | |
| size_t | str_eq_cstrs_any (str it, size_t count, const char *others[static count]) |
| Compares a string to several others to see if its contents are equal to any of them. | |
| size_t | cstr_eq_str_any (const char *it, size_t count,...) |
| Compares a string to several others to see if its contents are equal to any of them. | |
| size_t | cstr_eq_strs_any (const char *it, size_t count, const str others[static count]) |
| Compares a string to several others to see if its contents are equal to any of them. | |
| size_t | cstr_eq_cstr_any (const char *it, size_t count,...) |
| Compares a string to several others to see if its contents are equal to any of them. | |
| size_t | cstr_eq_cstrs_any (const char *it, size_t count, const char *others[static count]) |
| Compares a string to several others to see if its contents are equal to any of them. | |
| size_t | str_satisfies_any (str it, size_t count,...) |
| Checks if a string satisfies any of several predicates. | |
| size_t | str_satisfies_anys (str it, size_t count, bool(*predicates[static count])(str)) |
| Checks if a string satisfies any of several predicates. | |
| size_t | cstr_satisfies_any (const char *it, size_t count,...) |
| Checks if a string satisfies any of several predicates. | |
| size_t | cstr_satisfies_anys (const char *it, size_t count, bool(*predicates[static count])(const char *)) |
| Checks if a string satisfies any of several predicates. | |
| bool | str_satisfies_all (str it, size_t count,...) |
| Checks if a string satisfies all of several predicates. | |
| bool | str_satisfies_alls (str it, size_t count, bool(*predicates[static count])(str)) |
| Checks if a string satisfies all of several predicates. | |
| bool | cstr_satisfies_all (const char *it, size_t count,...) |
| Checks if a string satisfies all of several predicates. | |
| bool | cstr_satisfies_alls (const char *it, size_t count, bool(*predicates[static count])(const char *)) |
| Checks if a string satisfies all of several predicates. | |
| bool | str_chars_satisfies (str it, bool(*predicate)(char)) |
| Checks if all characters in a string satisfies a predicate. | |
| bool | cstr_chars_satisfies (const char *it, bool(*predicate)(char)) |
| Checks if all characters in a string satisfies a predicate. | |
| size_t | str_has_prefix_str (str it, str prefix) |
| Checks if a string begins with another string. | |
| size_t | str_has_prefix_cstr (str it, const char *prefix) |
| Checks if a string begins with another string. | |
| size_t | cstr_has_prefix_str (const char *it, str prefix) |
| Checks if a string begins with another string. | |
| size_t | cstr_has_prefix_cstr (const char *it, const char *prefix) |
| Checks if a string begins with another string. | |
| size_t | str_has_postfix_str (str it, str postfix) |
| Checks if a string ends with another string. | |
| size_t | str_has_postfix_cstr (str it, const char *postfix) |
| Checks if a string ends with another string. | |
| size_t | cstr_has_postfix_str (const char *it, str postfix) |
| Checks if a string ends with another string. | |
| size_t | cstr_has_postfix_cstr (const char *it, const char *postfix) |
| Checks if a string ends with another string. | |
| str | str_split_char (str *rest, char delim) |
Splits a str on the first occurence of the delimiter delim. | |
| str | str_rsplit_char (str *rest, char delim) |
Splits a str on the last occurence of the delimiter delim. | |
| str | str_split_if (str *rest, bool(*predicate)(char)) |
Splits a str on the first character satisfying predicate. | |
| str | str_rsplit_if (str *rest, bool(*predicate)(char)) |
Splits a str on the last character satisfying predicate. | |
Utilities for string slices.
| #define str_cmp | ( | LHS, | |
| RHS ) |
Compares two strings lexicographically.
| #define str_eq | ( | LHS, | |
| RHS ) |
Compares two strings to see if their contents are equal.
| #define str_has_postfix | ( | LHS, | |
| RHS ) |
Checks if a string ends with another string.
postfix if it is found; otherwise, 0 | #define str_has_prefix | ( | LHS, | |
| RHS ) |
Checks if a string begins with another string.
prefix if it is found; otherwise, 0 | #define str_literal | ( | SZ_LITERAL | ) |
| #define str_rsplit | ( | IT, | |
| ON ) |
Splits a str on the last occurence of something.
| #define str_split | ( | IT, | |
| ON ) |
Splits a str on the first occurence of something.
| #define str_strip_postfix | ( | IT, | |
| POSTFIX ) |
Returns a non-owning copy of a str with a given postfix removed, if it is present.
| #define str_strip_prefix | ( | IT, | |
| PREFIX ) |
Returns a non-owning copy of a str with a given prefix removed, if it is present.
| typedef struct str str |
A string "slice", which is a pointer & length pair.
A str can be printed using the "%.*s" format specifier:
| bool cstr_chars_satisfies | ( | const char * | it, |
| bool(* | predicate )(char) ) |
Checks if all characters in a string satisfies a predicate.
| it | The string whose characters to test |
| predicate | The predicate to test each character in it against |
it satisfied predicate | int cstr_cmp_str | ( | const char * | a, |
| str | b ) |
Compares two strings lexicographically.
| size_t cstr_eq_cstr_any | ( | const char * | it, |
| size_t | count, | ||
| ... ) |
Compares a string to several others to see if its contents are equal to any of them.
| it | The string to compare against many others. |
| count | The number of other strings to compare against. This must be no greater than SIZE_MAX - 1. |
| ... | The other strings to test it against. |
... that it was equal to, counting from 1, or 0 if it was equal to none of them. | size_t cstr_eq_cstrs_any | ( | const char * | it, |
| size_t | count, | ||
| const char * | others[static count] ) |
Compares a string to several others to see if its contents are equal to any of them.
| it | The string to compare against many others. |
| count | The number of other strings to compare against. This must be no greater than SIZE_MAX - 1. |
| others | The other strings to test it against. |
others that it was equal to, counting from 1 (or rather, 1 greater than the index), or 0 if it was equal to none of them. | size_t cstr_eq_str_any | ( | const char * | it, |
| size_t | count, | ||
| ... ) |
Compares a string to several others to see if its contents are equal to any of them.
| it | The string to compare against many others. |
| count | The number of other strings to compare against. This must be no greater than SIZE_MAX - 1. |
| ... | The other strings to test it against. |
... that it was equal to, counting from 1, or 0 if it was equal to none of them. | size_t cstr_eq_strs_any | ( | const char * | it, |
| size_t | count, | ||
| const str | others[static count] ) |
Compares a string to several others to see if its contents are equal to any of them.
| it | The string to compare against many others. |
| count | The number of other strings to compare against. This must be no greater than SIZE_MAX - 1. |
| others | The other strings to test it against. |
others that it was equal to, counting from 1 (or rather, 1 greater than the index), or 0 if it was equal to none of them. | size_t cstr_has_postfix_cstr | ( | const char * | it, |
| const char * | postfix ) |
Checks if a string ends with another string.
postfix if it is found; otherwise, 0 | size_t cstr_has_postfix_str | ( | const char * | it, |
| str | postfix ) |
Checks if a string ends with another string.
postfix if it is found; otherwise, 0 | size_t cstr_has_prefix_cstr | ( | const char * | it, |
| const char * | prefix ) |
Checks if a string begins with another string.
prefix if it is found; otherwise, 0 | size_t cstr_has_prefix_str | ( | const char * | it, |
| str | prefix ) |
Checks if a string begins with another string.
prefix if it is found; otherwise, 0 | bool cstr_satisfies_all | ( | const char * | it, |
| size_t | count, | ||
| ... ) |
Checks if a string satisfies all of several predicates.
| it | The string to test |
| count | The number of predicates to test against. This must be no greater than SIZE_MAX - 1. |
| ... | The predicates to test it against; must be of type bool (*)(const char*) |
it satisfied all predicates in ... | bool cstr_satisfies_alls | ( | const char * | it, |
| size_t | count, | ||
| bool(*[])(const char *) | count ) |
Checks if a string satisfies all of several predicates.
| it | The string to test |
| count | The number of predicates to test against. This must be no greater than SIZE_MAX - 1. |
| predicates | The predicates to test it against |
it satisfied all predicates in predicates | size_t cstr_satisfies_any | ( | const char * | it, |
| size_t | count, | ||
| ... ) |
Checks if a string satisfies any of several predicates.
| it | The string to test |
| count | The number of predicates to test against. This must be no greater than SIZE_MAX - 1. |
| ... | The predicates to test it against; must be of type bool (*)(const char*) |
... that was satisfied by it, counting from 1, or 0 if satisfied none of them. | size_t cstr_satisfies_anys | ( | const char * | it, |
| size_t | count, | ||
| bool(*[])(const char *) | count ) |
Checks if a string satisfies any of several predicates.
| it | The string to test |
| count | The number of predicates to test against. This must be no greater than SIZE_MAX - 1. |
| predicates | The predicates to test it against |
predicates that was satisfied by it, counting from 1 (or rather, 1 greater than the index), or 0 if satisfied none of them. | bool str_chars_satisfies | ( | str | it, |
| bool(* | predicate )(char) ) |
Checks if all characters in a string satisfies a predicate.
| it | The string whose characters to test |
| predicate | The predicate to test each character in it against |
it satisfied predicate | int str_cmp_cstr | ( | str | a, |
| const char * | b ) |
Compares two strings lexicographically.
| size_t str_eq_cstr_any | ( | str | it, |
| size_t | count, | ||
| ... ) |
Compares a string to several others to see if its contents are equal to any of them.
| it | The string to compare against many others. |
| count | The number of other strings to compare against. This must be no greater than SIZE_MAX - 1. |
| ... | The other strings to test it against. |
... that it was equal to, counting from 1, or 0 if it was equal to none of them. | size_t str_eq_cstrs_any | ( | str | it, |
| size_t | count, | ||
| const char * | others[static count] ) |
Compares a string to several others to see if its contents are equal to any of them.
| it | The string to compare against many others. |
| count | The number of other strings to compare against. This must be no greater than SIZE_MAX - 1. |
| others | The other strings to test it against. |
others that it was equal to, counting from 1 (or rather, 1 greater than the index), or 0 if it was equal to none of them. | size_t str_eq_str_any | ( | str | it, |
| size_t | count, | ||
| ... ) |
Compares a string to several others to see if its contents are equal to any of them.
| it | The string to compare against many others. |
| count | The number of other strings to compare against. This must be no greater than SIZE_MAX - 1. |
| ... | The other strings to test it against. |
... that it was equal to, counting from 1, or 0 if it was equal to none of them. Compares a string to several others to see if its contents are equal to any of them.
| it | The string to compare against many others. |
| count | The number of other strings to compare against. This must be no greater than SIZE_MAX - 1. |
| others | The other strings to test it against. |
others that it was equal to, counting from 1 (or rather, 1 greater than the index), or 0 if it was equal to none of them. | size_t str_has_postfix_cstr | ( | str | it, |
| const char * | postfix ) |
Checks if a string ends with another string.
postfix if it is found; otherwise, 0 Checks if a string ends with another string.
postfix if it is found; otherwise, 0 | size_t str_has_prefix_cstr | ( | str | it, |
| const char * | prefix ) |
Checks if a string begins with another string.
prefix if it is found; otherwise, 0 Checks if a string begins with another string.
prefix if it is found; otherwise, 0 Splits a str on the last occurence of the delimiter delim.
If the delimiter delim occurs in rest, constructs a str of all characters after the last occurence of delim, then modifies rest to contain only characters before that occurence of delim.
If the delimiter delim does not occur in rest, returns an empty str.
Splits a str on the last character satisfying predicate.
If a character satisfying predicate occurs in rest, constructs a str of all characters after that character, then modifies rest to contain only characters before that character.
If no character in rest satisfies predicate, returns an empty str.
| bool str_satisfies_all | ( | str | it, |
| size_t | count, | ||
| ... ) |
Checks if a string satisfies all of several predicates.
| it | The string to test |
| count | The number of predicates to test against. This must be no greater than SIZE_MAX - 1. |
| ... | The predicates to test it against; must be of type bool (*)(str) |
it satisfied all predicates in ... Checks if a string satisfies all of several predicates.
| it | The string to test |
| count | The number of predicates to test against. This must be no greater than SIZE_MAX - 1. |
| predicates | The predicates to test it against |
it satisfied all predicates in predicates | size_t str_satisfies_any | ( | str | it, |
| size_t | count, | ||
| ... ) |
Checks if a string satisfies any of several predicates.
| it | The string to test |
| count | The number of predicates to test against. This must be no greater than SIZE_MAX - 1. |
| ... | The predicates to test it against; must be of type bool (*)(str) |
... that was satisfied by it, counting from 1, or 0 if satisfied none of them. Checks if a string satisfies any of several predicates.
| it | The string to test |
| count | The number of predicates to test against. This must be no greater than SIZE_MAX - 1. |
| predicates | The predicates to test it against |
predicates that was satisfied by it, counting from 1 (or rather, 1 greater than the index), or 0 if satisfied none of them. Splits a str on the first occurence of the delimiter delim.
If the delimiter delim occurs in rest, constructs a str of all characters before the first occurence of delim, then modifies rest to contain only characters after that occurence of delim.
If the delimiter delim does not occur in rest, returns an empty str.
Splits a str on the first character satisfying predicate.
If a character satisfying predicate occurs in rest, constructs a str of all characters before that character, then modifies rest to contain only characters after that character.
If no character in rest satisfies predicate, returns an empty str.