2 * UCW Library -- String Routines
4 * (c) 2006 Pavel Charvat <pchar@ucw.cz>
5 * (c) 2007--2012 Martin Mares <mj@ucw.cz>
7 * This software may be freely distributed and used according to the terms
8 * of the GNU Lesser General Public License.
17 uns strnlen(const char *str, uns n);
21 * Format a set of flag bits. When the i-th bit of @flags is 1,
22 * set the i-th character of @dest to @fmt[i], otherwise to '-'.
24 char *str_format_flags(char *dest, const char *fmt, uns flags);
26 /** Counts occurrences of @chr in @str. **/
27 uns str_count_char(const char *str, uns chr);
29 /** Returns a non-zero value if @haystack starts with @needle. **/
30 int str_starts_with(const char *haystack, const char *needle);
32 /** Returns a non-zero value if @haystack ends with @needle. **/
33 int str_ends_with(const char *haystack, const char *needle);
38 * Decode a string with backslash escape sequences as in C99 strings.
39 * It is safe to pass @dest equal to @src.
41 char *str_unesc(char *dest, const char *src);
46 * Split @str to at most @max fields separated by @sep. Occurrences of the
47 * separator are rewritten to string terminators, @rec[i] is set to point
48 * to the i-th field. The total number of fields is returned.
50 * When there are more than @max fields in @str, the first @max fields
51 * are processed and -1 is returned.
53 int str_sepsplit(char *str, uns sep, char **rec, uns max);
56 * Split @str to words separated by white-space characters. The spaces
57 * are replaced by string terminators, @rec[i] is set to point to the
58 * i-th field. The total number of fields is returned.
60 * When there are more than @max fields in @str, the first @max fields
61 * are processed and -1 is returned.
63 * Fields surrounded by double quotes are also recognized. They can contain
64 * spaces, but no mechanism for escaping embedded quotes is defined.
66 int str_wordsplit(char *str, char **rec, uns max);
68 /* str-(i)match.c: Matching of shell patterns */
71 * Test whether the string @str matches the shell-like pattern @patt. Only
72 * "*" and "?" meta-characters are supported.
74 int str_match_pattern(const char *patt, const char *str);
76 /** A case-insensitive version of @str_match_pattern(). **/
77 int str_match_pattern_nocase(const char *patt, const char *str);
82 * Create a hexdump of a memory block of @bytes bytes starting at @src.
83 * The @flags contain an optional separator of bytes (0 if bytes should
84 * not be separated), possibly OR-ed with `MEM_TO_HEX_UPCASE` when upper-case
85 * characters should be used.
87 void mem_to_hex(char *dest, const byte *src, uns bytes, uns flags);
90 * An inverse function to @mem_to_hex(). Takes a hexdump of at most @max_bytes
91 * bytes and stores the bytes to a buffer starting at @dest. Returns a pointer
92 * at the first character after the dump.
94 const char *hex_to_mem(byte *dest, const char *src, uns max_bytes, uns flags);
96 // Bottom 8 bits of flags are an optional separator of bytes, the rest is:
97 #define MEM_TO_HEX_UPCASE 0x100
101 int str_has_prefix(const char *str, const char *prefix); /** Tests if @str starts with @prefix. **/
102 int str_has_suffix(const char *str, const char *suffix); /** Tests if @str ends with @suffix. **/
105 * Let @str and @prefix be hierarchical names with components separated by
106 * a character @sep. Returns true if @str starts with @prefix, respecting
107 * component boundaries.
109 * For example, when @sep is '/' and @str is "/usr/local/bin", then:
111 * - "/usr/local" is a prefix
112 * - "/usr/local/" is a prefix, too
113 * - "/usr/loc" is not,
114 * - "/usr/local/bin" is a prefix,
115 * - "/usr/local/bin/" is not,
119 int str_hier_prefix(const char *str, const char *prefix, uns sep);
120 int str_hier_suffix(const char *str, const char *suffix, uns sep); /** Like @str_hier_prefix(), but for suffixes. **/