| 1 |
/* SPDX-License-Identifier: LGPL-2.1-or-later */ |
| 2 |
#pragma once |
| 3 |
|
| 4 |
#include <string.h> |
| 5 |
|
| 6 |
#include "alloc-util.h" |
| 7 |
#include "basic-forward.h" |
| 8 |
|
| 9 |
#include "../fundamental/string-util.h" /* IWYU pragma: export */ |
| 10 |
|
| 11 |
static inline char* strstr_ptr_internal(const char *haystack, const char *needle) { |
| 12 |
if (!haystack || !needle) |
| 13 |
return NULL; |
| 14 |
return (char*) strstr(haystack, needle); |
| 15 |
} |
| 16 |
|
| 17 |
#define strstr_ptr(haystack, needle) \ |
| 18 |
const_generic(haystack, strstr_ptr_internal(haystack, needle)) |
| 19 |
|
| 20 |
static inline char* strstrafter_internal(const char *haystack, const char *needle) { |
| 21 |
/* Returns NULL if not found, or pointer to first character after needle if found */ |
| 22 |
|
| 23 |
char *p = (char*) strstr_ptr(haystack, needle); |
| 24 |
if (!p) |
| 25 |
return NULL; |
| 26 |
|
| 27 |
return p + strlen(needle); |
| 28 |
} |
| 29 |
|
| 30 |
#define strstrafter(haystack, needle) \ |
| 31 |
const_generic(haystack, strstrafter_internal(haystack, needle)) |
| 32 |
|
| 33 |
static inline const char* strnull(const char *s) { |
| 34 |
return s ?: "(null)"; |
| 35 |
} |
| 36 |
|
| 37 |
static inline const char* strna(const char *s) { |
| 38 |
return s ?: "n/a"; |
| 39 |
} |
| 40 |
|
| 41 |
static inline const char* true_false(bool b) { |
| 42 |
return b ? "true" : "false"; |
| 43 |
} |
| 44 |
|
| 45 |
static inline const char* plus_minus(bool b) { |
| 46 |
return b ? "+" : "-"; |
| 47 |
} |
| 48 |
|
| 49 |
static inline const char* one_zero(bool b) { |
| 50 |
return b ? "1" : "0"; |
| 51 |
} |
| 52 |
|
| 53 |
static inline const char* enable_disable(bool b) { |
| 54 |
return b ? "enable" : "disable"; |
| 55 |
} |
| 56 |
|
| 57 |
static inline const char* enabled_disabled(bool b) { |
| 58 |
return b ? "enabled" : "disabled"; |
| 59 |
} |
| 60 |
|
| 61 |
/* This macro's return pointer will have the "const" qualifier set or unset the same way as the input |
| 62 |
* pointer. */ |
| 63 |
#define empty_to_null(p) \ |
| 64 |
({ \ |
| 65 |
const char *_p = (p); \ |
| 66 |
(typeof(p)) (isempty(_p) ? NULL : _p); \ |
| 67 |
}) |
| 68 |
|
| 69 |
static inline const char* empty_to_na(const char *p) { |
| 70 |
return isempty(p) ? "n/a" : p; |
| 71 |
} |
| 72 |
|
| 73 |
static inline const char* empty_to_dash(const char *str) { |
| 74 |
return isempty(str) ? "-" : str; |
| 75 |
} |
| 76 |
|
| 77 |
static inline bool empty_or_dash(const char *str) { |
| 78 |
return !str || |
| 79 |
str[0] == 0 || |
| 80 |
(str[0] == '-' && str[1] == 0); |
| 81 |
} |
| 82 |
|
| 83 |
static inline const char* empty_or_dash_to_null(const char *p) { |
| 84 |
return empty_or_dash(p) ? NULL : p; |
| 85 |
} |
| 86 |
#define empty_or_dash_to_null(p) \ |
| 87 |
({ \ |
| 88 |
const char *_p = (p); \ |
| 89 |
(typeof(p)) (empty_or_dash(_p) ? NULL : _p); \ |
| 90 |
}) |
| 91 |
|
| 92 |
char* first_word(const char *s, const char *word) _pure_; |
| 93 |
|
| 94 |
char* strextendn(char **x, const char *s, size_t l) _nonnull_if_nonzero_(2, 3); |
| 95 |
|
| 96 |
#define strjoin(a, ...) strextend_with_separator_internal(NULL, NULL, a, __VA_ARGS__, NULL) |
| 97 |
|
| 98 |
#define strjoina(a, ...) \ |
| 99 |
({ \ |
| 100 |
const char *_appendees_[] = { a, __VA_ARGS__ }; \ |
| 101 |
char *_d_, *_p_; \ |
| 102 |
size_t _len_ = 0; \ |
| 103 |
size_t _i_; \ |
| 104 |
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
| 105 |
_len_ += strlen(_appendees_[_i_]); \ |
| 106 |
_p_ = _d_ = newa(char, _len_ + 1); \ |
| 107 |
for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \ |
| 108 |
_p_ = stpcpy(_p_, _appendees_[_i_]); \ |
| 109 |
*_p_ = 0; \ |
| 110 |
_d_; \ |
| 111 |
}) |
| 112 |
|
| 113 |
char* strstrip(char *s); |
| 114 |
char* delete_chars(char *s, const char *bad); |
| 115 |
char* delete_trailing_chars(char *s, const char *bad); |
| 116 |
char* truncate_nl_full(char *s, size_t *ret_len); |
| 117 |
static inline char* truncate_nl(char *s) { |
| 118 |
return truncate_nl_full(s, NULL); |
| 119 |
} |
| 120 |
|
| 121 |
static inline char* skip_leading_chars(const char *s, const char *bad) { |
| 122 |
if (!s) |
| 123 |
return NULL; |
| 124 |
|
| 125 |
if (!bad) |
| 126 |
bad = WHITESPACE; |
| 127 |
|
| 128 |
return (char*) s + strspn(s, bad); |
| 129 |
} |
| 130 |
|
| 131 |
char ascii_tolower(char x) _const_; |
| 132 |
char* ascii_strlower(char *s); |
| 133 |
char* ascii_strlower_n(char *s, size_t n); |
| 134 |
|
| 135 |
char ascii_toupper(char x) _const_; |
| 136 |
char* ascii_strupper(char *s); |
| 137 |
|
| 138 |
int ascii_strcasecmp_n(const char *a, const char *b, size_t n); |
| 139 |
int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m); |
| 140 |
|
| 141 |
bool chars_intersect(const char *a, const char *b) _pure_; |
| 142 |
|
| 143 |
static inline bool _pure_ in_charset(const char *s, const char *charset) { |
| 144 |
assert(s); |
| 145 |
assert(charset); |
| 146 |
return s[strspn(s, charset)] == '\0'; |
| 147 |
} |
| 148 |
|
| 149 |
static inline bool char_is_cc(char p) { |
| 150 |
/* char is unsigned on some architectures, e.g. aarch64. So, compiler may warn the condition |
| 151 |
* p >= 0 is always true. See #19543. Hence, let's cast to unsigned before the comparison. Note |
| 152 |
* that the cast in the right hand side is redundant, as according to the C standard, compilers |
| 153 |
* automatically cast a signed value to unsigned when comparing with an unsigned variable. Just |
| 154 |
* for safety and readability. */ |
| 155 |
return (uint8_t) p < (uint8_t) ' ' || p == 127; |
| 156 |
} |
| 157 |
bool string_has_cc(const char *p, const char *ok) _pure_; |
| 158 |
|
| 159 |
char* ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent); |
| 160 |
static inline char* ellipsize(const char *s, size_t length, unsigned percent) { |
| 161 |
return ellipsize_mem(s, strlen(s), length, percent); |
| 162 |
} |
| 163 |
|
| 164 |
char* cellescape(char *buf, size_t len, const char *s); |
| 165 |
|
| 166 |
/* This limit is arbitrary, enough to give some idea what the string contains */ |
| 167 |
#define CELLESCAPE_DEFAULT_LENGTH 64 |
| 168 |
|
| 169 |
char* strshorten(char *s, size_t l); |
| 170 |
|
| 171 |
int strgrowpad0(char **s, size_t l); |
| 172 |
|
| 173 |
char* strreplace(const char *text, const char *old_string, const char *new_string); |
| 174 |
|
| 175 |
char* strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]); |
| 176 |
|
| 177 |
char* strextendv_with_separator(char **x, const char *separator, va_list ap); |
| 178 |
char* strextend_with_separator_internal(char **x, const char *separator, ...) _sentinel_; |
| 179 |
#define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL) |
| 180 |
#define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL) |
| 181 |
|
| 182 |
int strextendf_with_separator(char **x, const char *separator, const char *format, ...) _printf_(3,4); |
| 183 |
#define strextendf(x, ...) strextendf_with_separator(x, NULL, __VA_ARGS__) |
| 184 |
|
| 185 |
#define strprepend_with_separator(x, separator, ...) \ |
| 186 |
({ \ |
| 187 |
char **_p_ = ASSERT_PTR(x), *_s_; \ |
| 188 |
_s_ = strextend_with_separator_internal(NULL, (separator), __VA_ARGS__, empty_to_null(*_p_), NULL); \ |
| 189 |
if (_s_) { \ |
| 190 |
free(*_p_); \ |
| 191 |
*_p_ = _s_; \ |
| 192 |
} \ |
| 193 |
_s_; \ |
| 194 |
}) |
| 195 |
#define strprepend(x, ...) strprepend_with_separator(x, NULL, __VA_ARGS__) |
| 196 |
|
| 197 |
char* strrep(const char *s, size_t n); |
| 198 |
|
| 199 |
#define strrepa(s, n) \ |
| 200 |
({ \ |
| 201 |
const char *_sss_ = (s); \ |
| 202 |
size_t _nnn_ = (n), _len_ = strlen(_sss_); \ |
| 203 |
assert_se(MUL_ASSIGN_SAFE(&_len_, _nnn_)); \ |
| 204 |
char *_d_, *_p_; \ |
| 205 |
_p_ = _d_ = newa(char, _len_ + 1); \ |
| 206 |
for (size_t _i_ = 0; _i_ < _nnn_; _i_++) \ |
| 207 |
_p_ = stpcpy(_p_, _sss_); \ |
| 208 |
*_p_ = 0; \ |
| 209 |
_d_; \ |
| 210 |
}) |
| 211 |
|
| 212 |
int split_pair(const char *s, const char *sep, char **ret_first, char **ret_second); |
| 213 |
|
| 214 |
int free_and_strdup(char **p, const char *s); |
| 215 |
int free_and_strdup_warn(char **p, const char *s); |
| 216 |
int free_and_strndup(char **p, const char *s, size_t l) _nonnull_if_nonzero_(2, 3); |
| 217 |
|
| 218 |
int strdup_to_full(char **ret, const char *src); |
| 219 |
static inline int strdup_to(char **ret, const char *src) { |
| 220 |
int r = strdup_to_full(ASSERT_PTR(ret), src); |
| 221 |
return r < 0 ? r : 0; /* Suppress return value of 1. */ |
| 222 |
} |
| 223 |
|
| 224 |
typedef enum StringSafeFlags { |
| 225 |
STRING_ASCII = 1 << 0, /* Verify string is 7-Bit ASCII (rather than just UTF-8) */ |
| 226 |
STRING_ALLOW_EMPTY = 1 << 1, /* Allow empty strings */ |
| 227 |
STRING_ALLOW_NEWLINES = 1 << 2, /* Allow newlines (\n) */ |
| 228 |
STRING_ALLOW_BACKSLASHES = 1 << 3, /* Allow backslashes (\) */ |
| 229 |
STRING_ALLOW_QUOTES = 1 << 4, /* Allow quotes (" or ') */ |
| 230 |
STRING_ALLOW_GLOBS = 1 << 5, /* Allow globs (?, * or [) */ |
| 231 |
STRING_FILENAME = 1 << 6, /* Verify the string is valid as regular filename */ |
| 232 |
} StringSafeFlags; |
| 233 |
|
| 234 |
bool string_is_safe(const char *p, StringSafeFlags flags) _pure_; |
| 235 |
|
| 236 |
DISABLE_WARNING_STRINGOP_TRUNCATION; |
| 237 |
static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) { |
| 238 |
strncpy(buf, src, buf_len); |
| 239 |
} |
| 240 |
REENABLE_WARNING; |
| 241 |
|
| 242 |
/* Like startswith_no_case(), but operates on arbitrary memory blocks. |
| 243 |
* It works only for ASCII strings. |
| 244 |
*/ |
| 245 |
static inline void* memory_startswith_no_case(const void *p, size_t sz, const char *token) { |
| 246 |
assert(token); |
| 247 |
|
| 248 |
size_t n = strlen(token); |
| 249 |
if (sz < n) |
| 250 |
return NULL; |
| 251 |
|
| 252 |
assert(p); |
| 253 |
|
| 254 |
for (size_t i = 0; i < n; i++) |
| 255 |
if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i])) |
| 256 |
return NULL; |
| 257 |
|
| 258 |
return (uint8_t*) p + n; |
| 259 |
} |
| 260 |
|
| 261 |
char* str_realloc(char *p); |
| 262 |
char* string_erase(char *x); |
| 263 |
|
| 264 |
int string_truncate_lines(const char *s, size_t n_lines, char **ret); |
| 265 |
int string_extract_line(const char *s, size_t i, char **ret); |
| 266 |
|
| 267 |
int string_contains_word_strv(const char *string, const char *separators, char * const *words, const char **ret_word); |
| 268 |
static inline int string_contains_word(const char *string, const char *separators, const char *word) { |
| 269 |
return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL); |
| 270 |
} |
| 271 |
|
| 272 |
bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok); |
| 273 |
|
| 274 |
char* string_replace_char(char *str, char old_char, char new_char); |
| 275 |
|
| 276 |
typedef enum MakeCStringMode { |
| 277 |
MAKE_CSTRING_REFUSE_TRAILING_NUL, |
| 278 |
MAKE_CSTRING_ALLOW_TRAILING_NUL, |
| 279 |
MAKE_CSTRING_REQUIRE_TRAILING_NUL, |
| 280 |
_MAKE_CSTRING_MODE_MAX, |
| 281 |
_MAKE_CSTRING_MODE_INVALID = -1, |
| 282 |
} MakeCStringMode; |
| 283 |
|
| 284 |
int make_cstring(const void *s, size_t n, MakeCStringMode mode, char **ret); |
| 285 |
|
| 286 |
size_t strspn_from_end(const char *str, const char *accept) _pure_; |
| 287 |
|
| 288 |
char* strdupspn(const char *a, const char *accept); |
| 289 |
char* strdupcspn(const char *a, const char *reject); |
| 290 |
|
| 291 |
/* These are like strdupa()/strndupa(), but honour ALLOCA_MAX */ |
| 292 |
#define strdupa_safe(s) \ |
| 293 |
({ \ |
| 294 |
const char *_t = (s); \ |
| 295 |
(char*) memdupa_suffix0(_t, strlen(_t)); \ |
| 296 |
}) |
| 297 |
|
| 298 |
#define strndupa_safe(s, n) \ |
| 299 |
({ \ |
| 300 |
const char *_t = (s); \ |
| 301 |
(char*) memdupa_suffix0(_t, strnlen(_t, n)); \ |
| 302 |
}) |
| 303 |
|
| 304 |
char* find_line_startswith_internal(const char *haystack, const char *needle); |
| 305 |
#define find_line_startswith(haystack, needle) \ |
| 306 |
const_generic(haystack, find_line_startswith_internal(haystack, needle)) |
| 307 |
|
| 308 |
char* find_line_internal(const char *haystack, const char *needle); |
| 309 |
#define find_line(haystack, needle) \ |
| 310 |
const_generic(haystack, find_line_internal(haystack, needle)) |
| 311 |
|
| 312 |
char* find_line_after_internal(const char *haystack, const char *needle); |
| 313 |
#define find_line_after(haystack, needle) \ |
| 314 |
const_generic(haystack, find_line_after_internal(haystack, needle)) |
| 315 |
|
| 316 |
bool version_is_valid(const char *s) _pure_; |
| 317 |
bool version_is_valid_versionspec(const char *s) _pure_; |
| 318 |
|
| 319 |
ssize_t strlevenshtein(const char *x, const char *y); |
| 320 |
|
| 321 |
char* strrstr_internal(const char *haystack, const char *needle) _pure_; |
| 322 |
#define strrstr(haystack, needle) \ |
| 323 |
const_generic(haystack, strrstr_internal(haystack, needle)) |
| 324 |
|
| 325 |
char* strrstr_no_case_internal(const char *haystack, const char *needle) _pure_; |
| 326 |
#define strrstr_no_case(haystack, needle) \ |
| 327 |
const_generic(haystack, strrstr_no_case_internal(haystack, needle)) |
| 328 |
|
| 329 |
size_t str_common_prefix(const char *a, const char *b) _pure_; |