LibUCW contains a rich set of functions for manipulating strings.
So far, only a small subset is documented.
ucw/string.h
char *str_format_flags(char *dest, const char *fmt, uint flags);
Format a set of flag bits. When the i-th bit of flags is 1, set the i-th character of dest to fmt[i], otherwise to -.
size_t str_count_char(const char *str, uint chr);
Counts occurrences of chr in str.
char *str_unesc(char *dest, const char *src);
Decode a string with backslash escape sequences as in C99 strings. It is safe to pass dest equal to src.
int str_sepsplit(char *str, uint sep, char **rec, uint max);
Split str to at most max fields separated by sep. Occurrences of the separator are rewritten to string terminators, rec[i] is set to point to the i-th field. The total number of fields is returned.
When there are more than max fields in str, the first max fields are processed and -1 is returned.
int str_wordsplit(char *str, char **rec, uint max);
Split str to words separated by white-space characters. The spaces are replaced by string terminators, rec[i] is set to point to the i-th field. The total number of fields is returned.
When there are more than max fields in str, the first max fields are processed and -1 is returned.
Fields surrounded by double quotes are also recognized. They can contain spaces, but no mechanism for escaping embedded quotes is defined.
int str_match_pattern(const char *patt, const char *str);
Test whether the string str matches the shell-like pattern patt. Only "*" and "?" meta-characters are supported.
int str_match_pattern_nocase(const char *patt, const char *str);
A case-insensitive version of str_match_pattern().
void mem_to_hex(char *dest, const byte *src, size_t bytes, uint flags);
Create a hexdump of a memory block of bytes bytes starting at src. The flags contain an optional separator of bytes (0 if bytes should not be separated), possibly OR-ed with MEM_TO_HEX_UPCASE when upper-case characters should be used.
const char *hex_to_mem(byte *dest, const char *src, size_t max_bytes, uint flags);
An inverse function to mem_to_hex(). Takes a hexdump of at most max_bytes bytes and stores the bytes to a buffer starting at dest. Returns a pointer at the first character after the dump.
int str_has_prefix(const char *str, const char *prefix);
Tests if str starts with prefix.
int str_has_suffix(const char *str, const char *suffix);
Tests if str ends with suffix.
int str_hier_prefix(const char *str, const char *prefix, uint sep);
Let str and prefix be hierarchical names with components separated by a character sep. Returns true if str starts with prefix, respecting component boundaries.
For example, when sep is / and str is "/usr/local/bin", then:
-
"/usr/local" is a prefix
-
"/usr/local/" is a prefix, too
-
"/usr/loc" is not,
-
"/usr/local/bin" is a prefix,
-
"/usr/local/bin/" is not,
-
"/" is a prefix,
-
"" is a prefix.
int str_hier_suffix(const char *str, const char *suffix, uint sep);
Like str_hier_prefix(), but for suffixes.