1 /* SPDX-License-Identifier: LGPL-2.1+ */
11 /* What is interpreted as whitespace? */
12 #define WHITESPACE " \t\n\r"
13 #define NEWLINE "\n\r"
16 #define GLOB_CHARS "*?["
17 #define DIGITS "0123456789"
18 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
19 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
20 #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
21 #define ALPHANUMERICAL LETTERS DIGITS
22 #define HEXDIGITS DIGITS "abcdefABCDEF"
24 #define streq(a,b) (strcmp((a),(b)) == 0)
25 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
26 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
27 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
29 int strcmp_ptr(const char *a, const char *b) _pure_;
31 static inline bool streq_ptr(const char *a, const char *b) {
32 return strcmp_ptr(a, b) == 0;
35 static inline const char* strempty(const char *s) {
39 static inline const char* strnull(const char *s) {
43 static inline const char *strna(const char *s) {
47 static inline bool isempty(const char *p) {
51 static inline const char *empty_to_null(const char *p) {
52 return isempty(p) ? NULL : p;
55 #if 0 /// UNNEEDED by elogind
56 static inline const char *empty_to_dash(const char *str) {
57 return isempty(str) ? "-" : str;
61 static inline char *startswith(const char *s, const char *prefix) {
65 if (strncmp(s, prefix, l) == 0)
71 #if 0 /// UNNEEDED by elogind
72 static inline char *startswith_no_case(const char *s, const char *prefix) {
76 if (strncasecmp(s, prefix, l) == 0)
83 char *endswith(const char *s, const char *postfix) _pure_;
84 char *endswith_no_case(const char *s, const char *postfix) _pure_;
86 char *first_word(const char *s, const char *word) _pure_;
88 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
90 #define FOREACH_WORD(word, length, s, state) \
91 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
93 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
94 _FOREACH_WORD(word, length, s, separator, false, state)
96 #define _FOREACH_WORD(word, length, s, separator, quoted, state) \
97 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
99 char *strappend(const char *s, const char *suffix);
100 char *strnappend(const char *s, const char *suffix, size_t length);
102 char *strjoin_real(const char *x, ...) _sentinel_;
103 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
105 #define strjoina(a, ...) \
107 const char *_appendees_[] = { a, __VA_ARGS__ }; \
111 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
112 _len_ += strlen(_appendees_[_i_]); \
113 _p_ = _d_ = alloca(_len_ + 1); \
114 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
115 _p_ = stpcpy(_p_, _appendees_[_i_]); \
120 char *strstrip(char *s);
121 char *delete_chars(char *s, const char *bad);
122 char *delete_trailing_chars(char *s, const char *bad);
123 char *truncate_nl(char *s);
125 static inline char *skip_leading_chars(const char *s, const char *bad) {
133 return (char*) s + strspn(s, bad);
136 #if 0 /// UNNEEDED by elogind
137 char ascii_tolower(char x);
138 char *ascii_strlower(char *s);
139 char *ascii_strlower_n(char *s, size_t n);
141 char ascii_toupper(char x);
142 char *ascii_strupper(char *s);
144 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
145 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
147 bool chars_intersect(const char *a, const char *b) _pure_;
150 static inline bool _pure_ in_charset(const char *s, const char* charset) {
153 return s[strspn(s, charset)] == '\0';
156 bool string_has_cc(const char *p, const char *ok) _pure_;
158 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
159 static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
160 return ellipsize_mem(s, strlen(s), length, percent);
163 char *cellescape(char *buf, size_t len, const char *s);
165 /* This limit is arbitrary, enough to give some idea what the string contains */
166 #define CELLESCAPE_DEFAULT_LENGTH 64
168 bool nulstr_contains(const char *nulstr, const char *needle);
170 char* strshorten(char *s, size_t l);
172 char *strreplace(const char *text, const char *old_string, const char *new_string);
174 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
176 char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_;
178 #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
180 char *strrep(const char *s, unsigned n);
182 int split_pair(const char *s, const char *sep, char **l, char **r);
184 int free_and_strdup(char **p, const char *s);
186 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
187 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
190 return (void*) haystack;
192 if (haystacklen < needlelen)
198 return memmem(haystack, haystacklen, needle, needlelen);
201 #if !HAVE_EXPLICIT_BZERO
202 void explicit_bzero(void *p, size_t l);
205 char *string_erase(char *x);
207 char *string_free_erase(char *s);
208 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
209 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
211 bool string_is_safe(const char *p) _pure_;
213 static inline size_t strlen_ptr(const char *s) {
220 /* Like startswith(), but operates on arbitrary memory blocks */
221 static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
232 if (memcmp(p, token, n) != 0)
235 return (uint8_t*) p + n;