|
16 | 16 | #define OMC_SORT_LEN_ASCENDING 1 << 2 |
17 | 17 | #define OMC_SORT_LEN_DESCENDING 1 << 3 |
18 | 18 |
|
| 19 | +/** |
| 20 | + * Determine how many times the character `ch` appears in `sptr` string |
| 21 | + * @param sptr string to scan |
| 22 | + * @param ch character to find |
| 23 | + * @return count of characters found |
| 24 | + */ |
19 | 25 | int num_chars(const char *sptr, int ch); |
| 26 | + |
| 27 | +/** |
| 28 | + * Scan for `pattern` string at the beginning of `sptr` |
| 29 | + * |
| 30 | + * @param sptr string to scan |
| 31 | + * @param pattern string to search for |
| 32 | + * @return 1 = found, 0 = not found, -1 = error |
| 33 | + */ |
20 | 34 | int startswith(const char *sptr, const char *pattern); |
| 35 | + |
| 36 | +/** |
| 37 | + * Scan for `pattern` string at the end of `sptr` |
| 38 | + * |
| 39 | + * @param sptr string to scan |
| 40 | + * @param pattern string to search for |
| 41 | + * @return 1 = found, 0 = not found, -1 = error |
| 42 | + */ |
21 | 43 | int endswith(const char *sptr, const char *pattern); |
22 | | -char *normpath(const char *path); |
| 44 | + |
| 45 | +/** |
| 46 | + * Deletes any characters matching `chars` from `sptr` string |
| 47 | + * |
| 48 | + * @param sptr string to be modified in-place |
| 49 | + * @param chars a string containing characters (e.g. " \n" would delete whitespace and line feeds) |
| 50 | + */ |
23 | 51 | void strchrdel(char *sptr, const char *chars); |
| 52 | + |
| 53 | +/** |
| 54 | + * Find the integer offset of the first occurrence of `ch` in `sptr` |
| 55 | + * |
| 56 | + * ~~~{.c} |
| 57 | + * char buffer[255]; |
| 58 | + * char string[] = "abc=123"; |
| 59 | + * long int separator_offset = strchroff(string, '='); |
| 60 | + * for (long int i = 0; i < separator_offset); i++) { |
| 61 | + * buffer[i] = string[i]; |
| 62 | + * } |
| 63 | + * ~~~ |
| 64 | + * |
| 65 | + * @param sptr string to scan |
| 66 | + * @param ch character to find |
| 67 | + * @return offset to character in string, or 0 on failure |
| 68 | + */ |
24 | 69 | long int strchroff(const char *sptr, int ch); |
| 70 | + |
| 71 | +/** |
| 72 | + * This function scans `sptr` from right to left removing any matches to `suffix` |
| 73 | + * from the string. |
| 74 | + * |
| 75 | + * @param sptr string to be modified |
| 76 | + * @param suffix string to be removed from `sptr` |
| 77 | + */ |
25 | 78 | void strdelsuffix(char *sptr, const char *suffix); |
| 79 | + |
| 80 | +/** |
| 81 | + * Split a string by every delimiter in `delim` string. |
| 82 | + * |
| 83 | + * Callee should free memory using `GENERIC_ARRAY_FREE()` |
| 84 | + * |
| 85 | + * @param sptr string to split |
| 86 | + * @param delim characters to split on |
| 87 | + * @return success=parts of string, failure=NULL |
| 88 | + */ |
26 | 89 | char** split(char *sptr, const char* delim, size_t max); |
| 90 | + |
| 91 | +/** |
| 92 | + * Create new a string from an array of strings |
| 93 | + * |
| 94 | + * ~~~{.c} |
| 95 | + * char *array[] = { |
| 96 | + * "this", |
| 97 | + * "is", |
| 98 | + * "a", |
| 99 | + * "test", |
| 100 | + * NULL, |
| 101 | + * } |
| 102 | + * |
| 103 | + * char *test = join(array, " "); // "this is a test" |
| 104 | + * char *test2 = join(array, "_"); // "this_is_a_test" |
| 105 | + * char *test3 = join(array, ", "); // "this, is, a, test" |
| 106 | + * |
| 107 | + * free(test); |
| 108 | + * free(test2); |
| 109 | + * free(test3); |
| 110 | + * ~~~ |
| 111 | + * |
| 112 | + * @param arr |
| 113 | + * @param separator characters to insert between elements in string |
| 114 | + * @return new joined string |
| 115 | + */ |
27 | 116 | char *join(char **arr, const char *separator); |
| 117 | + |
| 118 | +/** |
| 119 | + * Join two or more strings by a `separator` string |
| 120 | + * @param separator |
| 121 | + * @param ... |
| 122 | + * @return string |
| 123 | + */ |
28 | 124 | char *join_ex(char *separator, ...); |
| 125 | + |
| 126 | +/** |
| 127 | + * Extract the string encapsulated by characters listed in `delims` |
| 128 | + * |
| 129 | + * ~~~{.c} |
| 130 | + * char *str = "this is [some data] in a string"; |
| 131 | + * char *data = substring_between(string, "[]"); |
| 132 | + * // data = "some data"; |
| 133 | + * ~~~ |
| 134 | + * |
| 135 | + * @param sptr string to parse |
| 136 | + * @param delims two characters surrounding a string |
| 137 | + * @return success=text between delimiters, failure=NULL |
| 138 | + */ |
29 | 139 | char *substring_between(char *sptr, const char *delims); |
| 140 | + |
| 141 | +/** |
| 142 | + * Sort an array of strings |
| 143 | + * @param arr a NULL terminated array of strings |
| 144 | + * @param sort_mode |
| 145 | + * - OMC_SORT_LEN_DESCENDING |
| 146 | + * - OMC_SORT_LEN_ASCENDING |
| 147 | + * - OMC_SORT_ALPHA |
| 148 | + * - OMC_SORT_NUMERIC |
| 149 | + */ |
30 | 150 | void strsort(char **arr, unsigned int sort_mode); |
| 151 | + |
| 152 | +/** |
| 153 | + * Determine whether the input character is a relational operator |
| 154 | + * Note: `~` is non-standard |
| 155 | + * @param ch |
| 156 | + * @return 0=no, 1=yes |
| 157 | + */ |
31 | 158 | int isrelational(char ch); |
| 159 | + |
| 160 | +/** |
| 161 | + * Print characters in `s`, `len` times |
| 162 | + * @param s |
| 163 | + * @param len |
| 164 | + */ |
32 | 165 | void print_banner(const char *s, int len); |
| 166 | + |
| 167 | +/** |
| 168 | + * Search for string in an array of strings |
| 169 | + * @param arr array of strings |
| 170 | + * @param str string to search for |
| 171 | + * @return yes=`pointer to string`, no=`NULL`, failure=`NULL` |
| 172 | + */ |
33 | 173 | char *strstr_array(char **arr, const char *str); |
| 174 | + |
| 175 | +/** |
| 176 | + * Remove duplicate strings from an array of strings |
| 177 | + * @param arr |
| 178 | + * @return success=array of unique strings, failure=NULL |
| 179 | + */ |
34 | 180 | char **strdeldup(char **arr); |
| 181 | + |
| 182 | +/** Remove leading whitespace from a string |
| 183 | + * |
| 184 | + * ~~~{.c} |
| 185 | + * char input[100]; |
| 186 | + * |
| 187 | + * strcpy(input, " I had leading spaces"); |
| 188 | + * lstrip(input); |
| 189 | + * // input is now "I had leading spaces" |
| 190 | + * ~~~ |
| 191 | + * @param sptr pointer to string |
| 192 | + * @return pointer to first non-whitespace character in string |
| 193 | + */ |
35 | 194 | char *lstrip(char *sptr); |
| 195 | + |
| 196 | +/** |
| 197 | + * Strips trailing whitespace from a given string |
| 198 | + * |
| 199 | + * ~~~{.c} |
| 200 | + * char input[100]; |
| 201 | + * |
| 202 | + * strcpy(input, "I had trailing spaces "); |
| 203 | + * strip(input); |
| 204 | + * // input is now "I had trailing spaces" |
| 205 | + * ~~~ |
| 206 | + * |
| 207 | + * @param sptr input string |
| 208 | + * @return truncated string |
| 209 | + */ |
36 | 210 | char *strip(char *sptr); |
| 211 | + |
| 212 | +/** |
| 213 | + * Check if a given string is "visibly" empty |
| 214 | + * |
| 215 | + * ~~~{.c} |
| 216 | + * char visibly[100]; |
| 217 | + * |
| 218 | + * strcpy(visibly, "\t \t\n"); |
| 219 | + * if (isempty(visibly)) { |
| 220 | + * printf("string is 'empty'\n"); |
| 221 | + * } else { |
| 222 | + * printf("string is not 'empty'\n"); |
| 223 | + * } |
| 224 | + * ~~~ |
| 225 | + * |
| 226 | + * @param sptr pointer to string |
| 227 | + * @return 0=not empty, 1=empty |
| 228 | + */ |
37 | 229 | int isempty(char *sptr); |
| 230 | + |
| 231 | +/** |
| 232 | + * Determine if a string is encapsulated by quotes |
| 233 | + * @param sptr pointer to string |
| 234 | + * @return 0=not quoted, 1=quoted |
| 235 | + */ |
38 | 236 | int isquoted(char *sptr); |
| 237 | + |
| 238 | +/** |
| 239 | + * Collapse whitespace in `s`. The string is modified in place. |
| 240 | + * @param s |
| 241 | + * @return pointer to `s` |
| 242 | + */ |
39 | 243 | char *normalize_space(char *s); |
| 244 | + |
| 245 | +/** |
| 246 | + * Duplicate an array of strings |
| 247 | + * |
| 248 | + * ~~~{.c} |
| 249 | + * char **array_orig = calloc(10, sizeof(*orig)); |
| 250 | + * orig[0] = strdup("one"); |
| 251 | + * orig[1] = strdup("two"); |
| 252 | + * orig[2] = strdup("three"); |
| 253 | + * // ... |
| 254 | + * char **array_orig_copy = strdup_array(orig); |
| 255 | + * |
| 256 | + * for (size_t i = 0; array_orig_copy[i] != NULL; i++) { |
| 257 | + * printf("array_orig[%zu] = '%s'\narray_orig_copy[%zu] = '%s'\n\n", |
| 258 | + * i, array_orig[i], |
| 259 | + * i, array_orig_copy[i]); |
| 260 | + * free(array_orig_copy[i]); |
| 261 | + * free(array_orig[i]); |
| 262 | + * } |
| 263 | + * free(array_orig_copy); |
| 264 | + * free(array_orig); |
| 265 | + * |
| 266 | + * ~~~ |
| 267 | + * |
| 268 | + * @param array |
| 269 | + * @return |
| 270 | + */ |
40 | 271 | char **strdup_array(char **array); |
| 272 | + |
| 273 | +/** |
| 274 | + * Compare an array of strings |
| 275 | + * |
| 276 | + * ~~~{.c} |
| 277 | + * const char *a[] = { |
| 278 | + * "I", |
| 279 | + * "like", |
| 280 | + * "computers." |
| 281 | + * }; |
| 282 | + * const char *b[] = { |
| 283 | + * "I", |
| 284 | + * "like", |
| 285 | + * "cars." |
| 286 | + * }; |
| 287 | + * if (!strcmp_array(a, b)) { |
| 288 | + * printf("a and b are not equal\n"); |
| 289 | + * } else { |
| 290 | + * printf("a and b are equal\n"); |
| 291 | + * } |
| 292 | + * ~~~ |
| 293 | + * |
| 294 | + * @param a pointer to array |
| 295 | + * @param b poitner to array |
| 296 | + * @return 0 on identical, non-zero for different |
| 297 | + */ |
41 | 298 | int strcmp_array(const char **a, const char **b); |
| 299 | + |
| 300 | +/** |
| 301 | + * Determine whether a string is comprised of digits |
| 302 | + * @param s |
| 303 | + * @return 0=no, 1=yes |
| 304 | + */ |
42 | 305 | int isdigit_s(const char *s); |
| 306 | + |
| 307 | +/** |
| 308 | + * Convert input string to lowercase |
| 309 | + * |
| 310 | + * ~~~{.c} |
| 311 | + * char *str = strdup("HELLO WORLD!"); |
| 312 | + * tolower_s(str); |
| 313 | + * // str is "hello world!" |
| 314 | + * ~~~ |
| 315 | + * |
| 316 | + * @param s input string |
| 317 | + * @return pointer to input string |
| 318 | + */ |
43 | 319 | char *tolower_s(char *s); |
| 320 | + |
| 321 | +/** |
| 322 | + * Return a copy of the input string with "." characters removed |
| 323 | + * |
| 324 | + * ~~~{.c} |
| 325 | + * char *version = strdup("1.2.3"); |
| 326 | + * char *version_short = to_short_version(str); |
| 327 | + * // version_short is "123" |
| 328 | + * free(version_short); |
| 329 | + * |
| 330 | + * ~~~ |
| 331 | + * |
| 332 | + * @param s input string |
| 333 | + * @return pointer to new string |
| 334 | + */ |
44 | 335 | char *to_short_version(const char *s); |
45 | 336 |
|
46 | 337 | #endif //OMC_STR_H |
0 commit comments