]> git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / string-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
2 #pragma once
3
4 /***
5 This file is part of systemd.
6
7 Copyright 2010 Lennart Poettering
8
9 systemd is free software; you can redistribute it and/or modify it
10 under the terms of the GNU Lesser General Public License as published by
11 the Free Software Foundation; either version 2.1 of the License, or
12 (at your option) any later version.
13
14 systemd is distributed in the hope that it will be useful, but
15 WITHOUT ANY WARRANTY; without even the implied warranty of
16 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 Lesser General Public License for more details.
18
19 You should have received a copy of the GNU Lesser General Public License
20 along with systemd; If not, see <http://www.gnu.org/licenses/>.
21 ***/
22
23 #include <alloca.h>
24 #include <stdbool.h>
25 #include <stddef.h>
26 #include <string.h>
27
28 #include "macro.h"
29
30 /* What is interpreted as whitespace? */
31 #define WHITESPACE " \t\n\r"
32 #define NEWLINE "\n\r"
33 #define QUOTES "\"\'"
34 #define COMMENTS "#;"
35 #define GLOB_CHARS "*?["
36 #define DIGITS "0123456789"
37 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
38 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
39 #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
40 #define ALPHANUMERICAL LETTERS DIGITS
41 #define HEXDIGITS DIGITS "abcdefABCDEF"
42
43 #define streq(a,b) (strcmp((a),(b)) == 0)
44 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
45 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
46 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
47
48 int strcmp_ptr(const char *a, const char *b) _pure_;
49
50 static inline bool streq_ptr(const char *a, const char *b) {
51 return strcmp_ptr(a, b) == 0;
52 }
53
54 static inline const char* strempty(const char *s) {
55 return s ? s : "";
56 }
57
58 static inline const char* strnull(const char *s) {
59 return s ? s : "(null)";
60 }
61
62 static inline const char *strna(const char *s) {
63 return s ? s : "n/a";
64 }
65
66 static inline bool isempty(const char *p) {
67 return !p || !p[0];
68 }
69
70 static inline const char *empty_to_null(const char *p) {
71 return isempty(p) ? NULL : p;
72 }
73
74 static inline const char *strdash_if_empty(const char *str) {
75 return isempty(str) ? "-" : str;
76 }
77
78 static inline char *startswith(const char *s, const char *prefix) {
79 size_t l;
80
81 l = strlen(prefix);
82 if (strncmp(s, prefix, l) == 0)
83 return (char*) s + l;
84
85 return NULL;
86 }
87
88 static inline char *startswith_no_case(const char *s, const char *prefix) {
89 size_t l;
90
91 l = strlen(prefix);
92 if (strncasecmp(s, prefix, l) == 0)
93 return (char*) s + l;
94
95 return NULL;
96 }
97
98 char *endswith(const char *s, const char *postfix) _pure_;
99 char *endswith_no_case(const char *s, const char *postfix) _pure_;
100
101 char *first_word(const char *s, const char *word) _pure_;
102
103 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
104
105 #define FOREACH_WORD(word, length, s, state) \
106 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
107
108 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
109 _FOREACH_WORD(word, length, s, separator, false, state)
110
111 #define _FOREACH_WORD(word, length, s, separator, quoted, state) \
112 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
113
114 char *strappend(const char *s, const char *suffix);
115 char *strnappend(const char *s, const char *suffix, size_t length);
116
117 char *strjoin_real(const char *x, ...) _sentinel_;
118 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
119
120 #define strjoina(a, ...) \
121 ({ \
122 const char *_appendees_[] = { a, __VA_ARGS__ }; \
123 char *_d_, *_p_; \
124 size_t _len_ = 0; \
125 unsigned _i_; \
126 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
127 _len_ += strlen(_appendees_[_i_]); \
128 _p_ = _d_ = alloca(_len_ + 1); \
129 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
130 _p_ = stpcpy(_p_, _appendees_[_i_]); \
131 *_p_ = 0; \
132 _d_; \
133 })
134
135 char *strstrip(char *s);
136 char *delete_chars(char *s, const char *bad);
137 char *delete_trailing_chars(char *s, const char *bad);
138 char *truncate_nl(char *s);
139
140 static inline char *skip_leading_chars(const char *s, const char *bad) {
141
142 if (!s)
143 return NULL;
144
145 if (!bad)
146 bad = WHITESPACE;
147
148 return (char*) s + strspn(s, bad);
149 }
150
151 char ascii_tolower(char x);
152 char *ascii_strlower(char *s);
153 char *ascii_strlower_n(char *s, size_t n);
154
155 char ascii_toupper(char x);
156 char *ascii_strupper(char *s);
157
158 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
159 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
160
161 bool chars_intersect(const char *a, const char *b) _pure_;
162
163 static inline bool _pure_ in_charset(const char *s, const char* charset) {
164 assert(s);
165 assert(charset);
166 return s[strspn(s, charset)] == '\0';
167 }
168
169 bool string_has_cc(const char *p, const char *ok) _pure_;
170
171 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
172 char *ellipsize(const char *s, size_t length, unsigned percent);
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 **p, size_t *l);
181
182 char *strextend(char **x, ...) _sentinel_;
183
184 char *strrep(const char *s, unsigned n);
185
186 int split_pair(const char *s, const char *sep, char **l, char **r);
187
188 int free_and_strdup(char **p, const char *s);
189
190 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
191 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
192
193 if (needlelen <= 0)
194 return (void*) haystack;
195
196 if (haystacklen < needlelen)
197 return NULL;
198
199 assert(haystack);
200 assert(needle);
201
202 return memmem(haystack, haystacklen, needle, needlelen);
203 }
204
205 #if !HAVE_EXPLICIT_BZERO
206 void explicit_bzero(void *p, size_t l);
207 #endif
208
209 char *string_erase(char *x);
210
211 char *string_free_erase(char *s);
212 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
213 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
214
215 bool string_is_safe(const char *p) _pure_;
216
217 static inline size_t strlen_ptr(const char *s) {
218 if (!s)
219 return 0;
220
221 return strlen(s);
222 }