1 /* SPDX-License-Identifier: LGPL-2.1+ */
8 #include "alloc-util.h"
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_
;
30 int strcasecmp_ptr(const char *a
, const char *b
) _pure_
;
32 static inline bool streq_ptr(const char *a
, const char *b
) {
33 return strcmp_ptr(a
, b
) == 0;
36 static inline const char* strempty(const char *s
) {
40 static inline const char* strnull(const char *s
) {
44 static inline const char *strna(const char *s
) {
48 static inline const char* yes_no(bool b
) {
49 return b
? "yes" : "no";
52 static inline const char* true_false(bool b
) {
53 return b
? "true" : "false";
56 static inline const char* one_zero(bool b
) {
60 static inline const char* enable_disable(bool b
) {
61 return b
? "enable" : "disable";
64 static inline bool isempty(const char *p
) {
68 static inline const char *empty_to_null(const char *p
) {
69 return isempty(p
) ? NULL
: p
;
72 static inline const char *empty_to_dash(const char *str
) {
73 return isempty(str
) ? "-" : str
;
76 static inline bool empty_or_dash(const char *str
) {
79 (str
[0] == '-' && str
[1] == 0);
82 static inline const char *empty_or_dash_to_null(const char *p
) {
83 return empty_or_dash(p
) ? NULL
: p
;
86 static inline char *startswith(const char *s
, const char *prefix
) {
90 if (strncmp(s
, prefix
, l
) == 0)
96 static inline char *startswith_no_case(const char *s
, const char *prefix
) {
100 if (strncasecmp(s
, prefix
, l
) == 0)
101 return (char*) s
+ l
;
106 char *endswith(const char *s
, const char *postfix
) _pure_
;
107 char *endswith_no_case(const char *s
, const char *postfix
) _pure_
;
109 char *first_word(const char *s
, const char *word
) _pure_
;
111 typedef enum SplitFlags
{
112 SPLIT_QUOTES
= 0x01 << 0,
113 SPLIT_RELAX
= 0x01 << 1,
116 /* Smelly. Do not use this anymore. Use extract_first_word() instead! */
117 const char* split(const char **state
, size_t *l
, const char *separator
, SplitFlags flags
);
119 /* Similar, don't use this anymore */
120 #define FOREACH_WORD(word, length, s, state) \
121 _FOREACH_WORD(word, length, s, WHITESPACE, 0, state)
123 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
124 _FOREACH_WORD(word, length, s, separator, 0, state)
126 #define _FOREACH_WORD(word, length, s, separator, flags, state) \
127 for ((state) = (s), (word) = split(&(state), &(length), (separator), (flags)); (word); (word) = split(&(state), &(length), (separator), (flags)))
129 char *strnappend(const char *s
, const char *suffix
, size_t length
);
131 char *strjoin_real(const char *x
, ...) _sentinel_
;
132 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
134 #define strjoina(a, ...) \
136 const char *_appendees_[] = { a, __VA_ARGS__ }; \
140 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
141 _len_ += strlen(_appendees_[_i_]); \
142 _p_ = _d_ = newa(char, _len_ + 1); \
143 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
144 _p_ = stpcpy(_p_, _appendees_[_i_]); \
149 char *strstrip(char *s
);
150 char *delete_chars(char *s
, const char *bad
);
151 char *delete_trailing_chars(char *s
, const char *bad
);
152 char *truncate_nl(char *s
);
154 static inline char *skip_leading_chars(const char *s
, const char *bad
) {
161 return (char*) s
+ strspn(s
, bad
);
164 char ascii_tolower(char x
);
165 char *ascii_strlower(char *s
);
166 char *ascii_strlower_n(char *s
, size_t n
);
168 char ascii_toupper(char x
);
169 char *ascii_strupper(char *s
);
171 int ascii_strcasecmp_n(const char *a
, const char *b
, size_t n
);
172 int ascii_strcasecmp_nn(const char *a
, size_t n
, const char *b
, size_t m
);
174 bool chars_intersect(const char *a
, const char *b
) _pure_
;
176 static inline bool _pure_
in_charset(const char *s
, const char* charset
) {
179 return s
[strspn(s
, charset
)] == '\0';
182 bool string_has_cc(const char *p
, const char *ok
) _pure_
;
184 char *ellipsize_mem(const char *s
, size_t old_length_bytes
, size_t new_length_columns
, unsigned percent
);
185 static inline char *ellipsize(const char *s
, size_t length
, unsigned percent
) {
186 return ellipsize_mem(s
, strlen(s
), length
, percent
);
189 char *cellescape(char *buf
, size_t len
, const char *s
);
191 /* This limit is arbitrary, enough to give some idea what the string contains */
192 #define CELLESCAPE_DEFAULT_LENGTH 64
194 char* strshorten(char *s
, size_t l
);
196 char *strreplace(const char *text
, const char *old_string
, const char *new_string
);
198 char *strip_tab_ansi(char **ibuf
, size_t *_isz
, size_t highlight
[2]);
200 char *strextend_with_separator(char **x
, const char *separator
, ...) _sentinel_
;
202 #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
204 char *strrep(const char *s
, unsigned n
);
206 int split_pair(const char *s
, const char *sep
, char **l
, char **r
);
208 int free_and_strdup(char **p
, const char *s
);
209 static inline int free_and_strdup_warn(char **p
, const char *s
) {
210 if (free_and_strdup(p
, s
) < 0)
214 int free_and_strndup(char **p
, const char *s
, size_t l
);
216 bool string_is_safe(const char *p
) _pure_
;
218 static inline size_t strlen_ptr(const char *s
) {
225 DISABLE_WARNING_STRINGOP_TRUNCATION
;
226 static inline void strncpy_exact(char *buf
, const char *src
, size_t buf_len
) {
227 strncpy(buf
, src
, buf_len
);
231 /* Like startswith(), but operates on arbitrary memory blocks */
232 static inline void *memory_startswith(const void *p
, size_t sz
, const char *token
) {
235 size_t n
= strlen(token
);
241 if (memcmp(p
, token
, n
) != 0)
244 return (uint8_t*) p
+ n
;
247 /* Like startswith_no_case(), but operates on arbitrary memory blocks.
248 * It works only for ASCII strings.
250 static inline void *memory_startswith_no_case(const void *p
, size_t sz
, const char *token
) {
253 size_t n
= strlen(token
);
259 for (size_t i
= 0; i
< n
; i
++)
260 if (ascii_tolower(((char *)p
)[i
]) != ascii_tolower(token
[i
]))
263 return (uint8_t*) p
+ n
;
266 static inline char* str_realloc(char **p
) {
267 /* Reallocate *p to actual size */
272 char *t
= realloc(*p
, strlen(*p
) + 1);
279 char* string_erase(char *x
);
281 int string_truncate_lines(const char *s
, size_t n_lines
, char **ret
);
282 int string_extract_line(const char *s
, size_t i
, char **ret
);
284 int string_contains_word_strv(const char *string
, const char *separators
, char **words
, const char **ret_word
);
285 static inline int string_contains_word(const char *string
, const char *separators
, const char *word
) {
286 return string_contains_word_strv(string
, separators
, STRV_MAKE(word
), NULL
);