1 /* SPDX-License-Identifier: LGPL-2.1-or-later */
2 #pragma once
3
4 #include <stdbool.h>
5 #include <stddef.h>
6 #include <string.h>
7
8 #include "alloc-util.h"
9 #include "macro.h"
10 #include "string-util-fundamental.h"
11
12 /* What is interpreted as whitespace? */
13 #define WHITESPACE " \t\n\r"
14 #define NEWLINE "\n\r"
15 #define QUOTES "\"\'"
16 #define COMMENTS "#;"
17 #define GLOB_CHARS "*?["
18 #define DIGITS "0123456789"
19 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
20 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
21 #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
22 #define ALPHANUMERICAL LETTERS DIGITS
23 #define HEXDIGITS DIGITS "abcdefABCDEF"
24 #define LOWERCASE_HEXDIGITS DIGITS "abcdef"
25
strstr_ptr(const char * haystack,const char * needle)26 static inline char* strstr_ptr(const char *haystack, const char *needle) {
27 if (!haystack || !needle)
28 return NULL;
29 return strstr(haystack, needle);
30 }
31
strempty(const char * s)32 static inline const char* strempty(const char *s) {
33 return s ?: "";
34 }
35
strnull(const char * s)36 static inline const char* strnull(const char *s) {
37 return s ?: "(null)";
38 }
39
strna(const char * s)40 static inline const char *strna(const char *s) {
41 return s ?: "n/a";
42 }
43
true_false(bool b)44 static inline const char* true_false(bool b) {
45 return b ? "true" : "false";
46 }
47
plus_minus(bool b)48 static inline const char* plus_minus(bool b) {
49 return b ? "+" : "-";
50 }
51
one_zero(bool b)52 static inline const char* one_zero(bool b) {
53 return b ? "1" : "0";
54 }
55
enable_disable(bool b)56 static inline const char* enable_disable(bool b) {
57 return b ? "enable" : "disable";
58 }
59
empty_to_null(const char * p)60 static inline const char *empty_to_null(const char *p) {
61 return isempty(p) ? NULL : p;
62 }
63
empty_to_na(const char * p)64 static inline const char *empty_to_na(const char *p) {
65 return isempty(p) ? "n/a" : p;
66 }
67
empty_to_dash(const char * str)68 static inline const char *empty_to_dash(const char *str) {
69 return isempty(str) ? "-" : str;
70 }
71
empty_or_dash(const char * str)72 static inline bool empty_or_dash(const char *str) {
73 return !str ||
74 str[0] == 0 ||
75 (str[0] == '-' && str[1] == 0);
76 }
77
empty_or_dash_to_null(const char * p)78 static inline const char *empty_or_dash_to_null(const char *p) {
79 return empty_or_dash(p) ? NULL : p;
80 }
81
82 char *first_word(const char *s, const char *word) _pure_;
83
84 char *strnappend(const char *s, const char *suffix, size_t length);
85
86 char *strjoin_real(const char *x, ...) _sentinel_;
87 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
88
89 #define strjoina(a, ...) \
90 ({ \
91 const char *_appendees_[] = { a, __VA_ARGS__ }; \
92 char *_d_, *_p_; \
93 size_t _len_ = 0; \
94 size_t _i_; \
95 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
96 _len_ += strlen(_appendees_[_i_]); \
97 _p_ = _d_ = newa(char, _len_ + 1); \
98 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
99 _p_ = stpcpy(_p_, _appendees_[_i_]); \
100 *_p_ = 0; \
101 _d_; \
102 })
103
104 char *strstrip(char *s);
105 char *delete_chars(char *s, const char *bad);
106 char *delete_trailing_chars(char *s, const char *bad);
107 char *truncate_nl(char *s);
108
skip_leading_chars(const char * s,const char * bad)109 static inline char *skip_leading_chars(const char *s, const char *bad) {
110 if (!s)
111 return NULL;
112
113 if (!bad)
114 bad = WHITESPACE;
115
116 return (char*) s + strspn(s, bad);
117 }
118
119 char ascii_tolower(char x);
120 char *ascii_strlower(char *s);
121 char *ascii_strlower_n(char *s, size_t n);
122
123 char ascii_toupper(char x);
124 char *ascii_strupper(char *s);
125
126 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
127 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
128
129 bool chars_intersect(const char *a, const char *b) _pure_;
130
in_charset(const char * s,const char * charset)131 static inline bool _pure_ in_charset(const char *s, const char* charset) {
132 assert(s);
133 assert(charset);
134 return s[strspn(s, charset)] == '\0';
135 }
136
char_is_cc(char p)137 static inline bool char_is_cc(char p) {
138 /* char is unsigned on some architectures, e.g. aarch64. So, compiler may warn the condition
139 * p >= 0 is always true. See #19543. Hence, let's cast to unsigned before the comparison. Note
140 * that the cast in the right hand side is redundant, as according to the C standard, compilers
141 * automatically cast a signed value to unsigned when comparing with an unsigned variable. Just
142 * for safety and readability. */
143 return (uint8_t) p < (uint8_t) ' ' || p == 127;
144 }
145 bool string_has_cc(const char *p, const char *ok) _pure_;
146
147 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
ellipsize(const char * s,size_t length,unsigned percent)148 static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
149 return ellipsize_mem(s, strlen(s), length, percent);
150 }
151
152 char *cellescape(char *buf, size_t len, const char *s);
153
154 /* This limit is arbitrary, enough to give some idea what the string contains */
155 #define CELLESCAPE_DEFAULT_LENGTH 64
156
157 char* strshorten(char *s, size_t l);
158
159 char *strreplace(const char *text, const char *old_string, const char *new_string);
160
161 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
162
163 char *strextend_with_separator_internal(char **x, const char *separator, ...) _sentinel_;
164 #define strextend_with_separator(x, separator, ...) strextend_with_separator_internal(x, separator, __VA_ARGS__, NULL)
165 #define strextend(x, ...) strextend_with_separator_internal(x, NULL, __VA_ARGS__, NULL)
166
167 int strextendf_with_separator(char **x, const char *separator, const char *format, ...) _printf_(3,4);
168 #define strextendf(x, ...) strextendf_with_separator(x, NULL, __VA_ARGS__)
169
170 char *strrep(const char *s, unsigned n);
171
172 int split_pair(const char *s, const char *sep, char **l, char **r);
173
174 int free_and_strdup(char **p, const char *s);
free_and_strdup_warn(char ** p,const char * s)175 static inline int free_and_strdup_warn(char **p, const char *s) {
176 if (free_and_strdup(p, s) < 0)
177 return log_oom();
178 return 0;
179 }
180 int free_and_strndup(char **p, const char *s, size_t l);
181
182 bool string_is_safe(const char *p) _pure_;
183
strlen_ptr(const char * s)184 static inline size_t strlen_ptr(const char *s) {
185 if (!s)
186 return 0;
187
188 return strlen(s);
189 }
190
191 DISABLE_WARNING_STRINGOP_TRUNCATION;
strncpy_exact(char * buf,const char * src,size_t buf_len)192 static inline void strncpy_exact(char *buf, const char *src, size_t buf_len) {
193 strncpy(buf, src, buf_len);
194 }
195 REENABLE_WARNING;
196
197 /* Like startswith_no_case(), but operates on arbitrary memory blocks.
198 * It works only for ASCII strings.
199 */
memory_startswith_no_case(const void * p,size_t sz,const char * token)200 static inline void *memory_startswith_no_case(const void *p, size_t sz, const char *token) {
201 assert(token);
202
203 size_t n = strlen(token);
204 if (sz < n)
205 return NULL;
206
207 assert(p);
208
209 for (size_t i = 0; i < n; i++)
210 if (ascii_tolower(((char *)p)[i]) != ascii_tolower(token[i]))
211 return NULL;
212
213 return (uint8_t*) p + n;
214 }
215
str_realloc(char * p)216 static inline char* str_realloc(char *p) {
217 /* Reallocate *p to actual size. Ignore failure, and return the original string on error. */
218
219 if (!p)
220 return NULL;
221
222 return realloc(p, strlen(p) + 1) ?: p;
223 }
224
225 char* string_erase(char *x);
226
227 int string_truncate_lines(const char *s, size_t n_lines, char **ret);
228 int string_extract_line(const char *s, size_t i, char **ret);
229
230 int string_contains_word_strv(const char *string, const char *separators, char **words, const char **ret_word);
string_contains_word(const char * string,const char * separators,const char * word)231 static inline int string_contains_word(const char *string, const char *separators, const char *word) {
232 return string_contains_word_strv(string, separators, STRV_MAKE(word), NULL);
233 }
234
235 bool streq_skip_trailing_chars(const char *s1, const char *s2, const char *ok);
236
237 char *string_replace_char(char *str, char old_char, char new_char);
238
239 size_t strspn_from_end(const char *str, const char *accept);
240