chiark / gitweb /
tree-wide: drop 'This file is part of systemd' blurb
[elogind.git] / src / basic / string-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5   Copyright 2010 Lennart Poettering
6 ***/
7
8 #include <alloca.h>
9 #include <stdbool.h>
10 #include <stddef.h>
11 #include <string.h>
12
13 #include "macro.h"
14
15 /* What is interpreted as whitespace? */
16 #define WHITESPACE        " \t\n\r"
17 #define NEWLINE           "\n\r"
18 #define QUOTES            "\"\'"
19 #define COMMENTS          "#;"
20 #define GLOB_CHARS        "*?["
21 #define DIGITS            "0123456789"
22 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
23 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24 #define LETTERS           LOWERCASE_LETTERS UPPERCASE_LETTERS
25 #define ALPHANUMERICAL    LETTERS DIGITS
26 #define HEXDIGITS         DIGITS "abcdefABCDEF"
27
28 #define streq(a,b) (strcmp((a),(b)) == 0)
29 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
30 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
31 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
32
33 int strcmp_ptr(const char *a, const char *b) _pure_;
34
35 static inline bool streq_ptr(const char *a, const char *b) {
36         return strcmp_ptr(a, b) == 0;
37 }
38
39 static inline const char* strempty(const char *s) {
40         return s ?: "";
41 }
42
43 static inline const char* strnull(const char *s) {
44         return s ?: "(null)";
45 }
46
47 static inline const char *strna(const char *s) {
48         return s ?: "n/a";
49 }
50
51 static inline bool isempty(const char *p) {
52         return !p || !p[0];
53 }
54
55 static inline const char *empty_to_null(const char *p) {
56         return isempty(p) ? NULL : p;
57 }
58
59 #if 0 /// UNNEEDED by elogind
60 static inline const char *empty_to_dash(const char *str) {
61         return isempty(str) ? "-" : str;
62 }
63 #endif // 0
64
65 static inline char *startswith(const char *s, const char *prefix) {
66         size_t l;
67
68         l = strlen(prefix);
69         if (strncmp(s, prefix, l) == 0)
70                 return (char*) s + l;
71
72         return NULL;
73 }
74
75 #if 0 /// UNNEEDED by elogind
76 static inline char *startswith_no_case(const char *s, const char *prefix) {
77         size_t l;
78
79         l = strlen(prefix);
80         if (strncasecmp(s, prefix, l) == 0)
81                 return (char*) s + l;
82
83         return NULL;
84 }
85 #endif // 0
86
87 char *endswith(const char *s, const char *postfix) _pure_;
88 char *endswith_no_case(const char *s, const char *postfix) _pure_;
89
90 char *first_word(const char *s, const char *word) _pure_;
91
92 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
93
94 #define FOREACH_WORD(word, length, s, state)                            \
95         _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
96
97 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state)       \
98         _FOREACH_WORD(word, length, s, separator, false, state)
99
100 #define _FOREACH_WORD(word, length, s, separator, quoted, state)        \
101         for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
102
103 char *strappend(const char *s, const char *suffix);
104 char *strnappend(const char *s, const char *suffix, size_t length);
105
106 char *strjoin_real(const char *x, ...) _sentinel_;
107 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
108
109 #define strjoina(a, ...)                                                \
110         ({                                                              \
111                 const char *_appendees_[] = { a, __VA_ARGS__ };         \
112                 char *_d_, *_p_;                                        \
113                 size_t _len_ = 0;                                          \
114                 size_t _i_;                                           \
115                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
116                         _len_ += strlen(_appendees_[_i_]);              \
117                 _p_ = _d_ = alloca(_len_ + 1);                          \
118                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
119                         _p_ = stpcpy(_p_, _appendees_[_i_]);            \
120                 *_p_ = 0;                                               \
121                 _d_;                                                    \
122         })
123
124 char *strstrip(char *s);
125 #if 0 /// UNNEEDED by elogind
126 char *delete_chars(char *s, const char *bad);
127 #endif // 0
128 char *delete_trailing_chars(char *s, const char *bad);
129 char *truncate_nl(char *s);
130
131 #if 0 /// UNNEEDED by elogind
132 static inline char *skip_leading_chars(const char *s, const char *bad) {
133
134         if (!s)
135                 return NULL;
136
137         if (!bad)
138                 bad = WHITESPACE;
139
140         return (char*) s + strspn(s, bad);
141 }
142
143 char ascii_tolower(char x);
144 char *ascii_strlower(char *s);
145 char *ascii_strlower_n(char *s, size_t n);
146
147 char ascii_toupper(char x);
148 char *ascii_strupper(char *s);
149
150 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
151 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
152
153 bool chars_intersect(const char *a, const char *b) _pure_;
154 #endif // 0
155
156 static inline bool _pure_ in_charset(const char *s, const char* charset) {
157         assert(s);
158         assert(charset);
159         return s[strspn(s, charset)] == '\0';
160 }
161
162 bool string_has_cc(const char *p, const char *ok) _pure_;
163
164 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
165 static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
166         return ellipsize_mem(s, strlen(s), length, percent);
167 }
168
169 char *cellescape(char *buf, size_t len, const char *s);
170
171 /* This limit is arbitrary, enough to give some idea what the string contains */
172 #define CELLESCAPE_DEFAULT_LENGTH 64
173
174 bool nulstr_contains(const char *nulstr, const char *needle);
175
176 char* strshorten(char *s, size_t l);
177
178 char *strreplace(const char *text, const char *old_string, const char *new_string);
179
180 char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
181
182 char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_;
183
184 #define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
185
186 char *strrep(const char *s, unsigned n);
187
188 int split_pair(const char *s, const char *sep, char **l, char **r);
189
190 int free_and_strdup(char **p, const char *s);
191
192 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
193 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
194
195         if (needlelen <= 0)
196                 return (void*) haystack;
197
198         if (haystacklen < needlelen)
199                 return NULL;
200
201         assert(haystack);
202         assert(needle);
203
204         return memmem(haystack, haystacklen, needle, needlelen);
205 }
206
207 #if !HAVE_EXPLICIT_BZERO
208 void explicit_bzero(void *p, size_t l);
209 #endif
210
211 char *string_erase(char *x);
212
213 char *string_free_erase(char *s);
214 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
215 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
216
217 bool string_is_safe(const char *p) _pure_;
218
219 static inline size_t strlen_ptr(const char *s) {
220         if (!s)
221                 return 0;
222
223         return strlen(s);
224 }
225
226 /* Like startswith(), but operates on arbitrary memory blocks */
227 static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
228         size_t n;
229
230         assert(token);
231
232         n = strlen(token);
233         if (sz < n)
234                 return NULL;
235
236         assert(p);
237
238         if (memcmp(p, token, n) != 0)
239                 return NULL;
240
241         return (uint8_t*) p + n;
242 }