]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-util.h
Add SPDX license identifiers to source files under the LGPL
[thirdparty/systemd.git] / src / basic / string-util.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
07630cea
LP
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
11c3a366 23#include <alloca.h>
07630cea 24#include <stdbool.h>
11c3a366 25#include <stddef.h>
07630cea
LP
26#include <string.h>
27
28#include "macro.h"
29
b11d6a7b
LP
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
1a7906ae 41#define HEXDIGITS DIGITS "abcdefABCDEF"
b11d6a7b 42
07630cea
LP
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
48int strcmp_ptr(const char *a, const char *b) _pure_;
49
50static inline bool streq_ptr(const char *a, const char *b) {
51 return strcmp_ptr(a, b) == 0;
52}
53
54static inline const char* strempty(const char *s) {
55 return s ? s : "";
56}
57
58static inline const char* strnull(const char *s) {
59 return s ? s : "(null)";
60}
61
62static inline const char *strna(const char *s) {
63 return s ? s : "n/a";
64}
65
66static inline bool isempty(const char *p) {
67 return !p || !p[0];
68}
69
3c6f7c34
LP
70static inline const char *empty_to_null(const char *p) {
71 return isempty(p) ? NULL : p;
72}
73
07b0b339
SK
74static inline const char *strdash_if_empty(const char *str) {
75 return isempty(str) ? "-" : str;
76}
77
07630cea
LP
78static 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
88static 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
98char *endswith(const char *s, const char *postfix) _pure_;
99char *endswith_no_case(const char *s, const char *postfix) _pure_;
100
101char *first_word(const char *s, const char *word) _pure_;
102
103const 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
07630cea
LP
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
114char *strappend(const char *s, const char *suffix);
115char *strnappend(const char *s, const char *suffix, size_t length);
116
605405c6
ZJS
117char *strjoin_real(const char *x, ...) _sentinel_;
118#define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
07630cea
LP
119
120#define strjoina(a, ...) \
121 ({ \
122 const char *_appendees_[] = { a, __VA_ARGS__ }; \
123 char *_d_, *_p_; \
35207e25 124 size_t _len_ = 0; \
07630cea
LP
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
135char *strstrip(char *s);
136char *delete_chars(char *s, const char *bad);
7546145e 137char *delete_trailing_chars(char *s, const char *bad);
07630cea
LP
138char *truncate_nl(char *s);
139
7546145e
LP
140static 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
b577e3d5
LP
151char ascii_tolower(char x);
152char *ascii_strlower(char *s);
153char *ascii_strlower_n(char *s, size_t n);
07630cea 154
846b8fc3
LP
155char ascii_toupper(char x);
156char *ascii_strupper(char *s);
157
522d85ae 158int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
c1749834 159int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
522d85ae 160
07630cea
LP
161bool chars_intersect(const char *a, const char *b) _pure_;
162
163static 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
169bool string_has_cc(const char *p, const char *ok) _pure_;
170
171char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
172char *ellipsize(const char *s, size_t length, unsigned percent);
173
2d5dece8 174bool nulstr_contains(const char *nulstr, const char *needle);
07630cea
LP
175
176char* strshorten(char *s, size_t l);
177
178char *strreplace(const char *text, const char *old_string, const char *new_string);
179
180char *strip_tab_ansi(char **p, size_t *l);
181
182char *strextend(char **x, ...) _sentinel_;
183
184char *strrep(const char *s, unsigned n);
185
186int split_pair(const char *s, const char *sep, char **l, char **r);
187
188int free_and_strdup(char **p, const char *s);
189
190/* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
191static 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
4b9545f1 205#if !HAVE_EXPLICIT_BZERO
2d26d8e0
ZJS
206void explicit_bzero(void *p, size_t l);
207#endif
208
9fe4ea21 209char *string_erase(char *x);
07630cea
LP
210
211char *string_free_erase(char *s);
212DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
213#define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
f3e2e81d
LP
214
215bool string_is_safe(const char *p) _pure_;
7bf7ce28
LP
216
217static inline size_t strlen_ptr(const char *s) {
218 if (!s)
219 return 0;
220
221 return strlen(s);
222}