]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.h
1 /* SPDX-License-Identifier: LGPL-2.1+ */
5 This file is part of systemd.
7 Copyright 2010 Lennart Poettering
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.
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.
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/>.
30 /* What is interpreted as whitespace? */
31 #define WHITESPACE " \t\n\r"
32 #define NEWLINE "\n\r"
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"
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)
48 int strcmp_ptr(const char *a
, const char *b
) _pure_
;
50 static inline bool streq_ptr(const char *a
, const char *b
) {
51 return strcmp_ptr(a
, b
) == 0;
54 static inline const char* strempty(const char *s
) {
58 static inline const char* strnull(const char *s
) {
59 return s
? s
: "(null)";
62 static inline const char *strna(const char *s
) {
66 static inline bool isempty(const char *p
) {
70 static inline const char *empty_to_null(const char *p
) {
71 return isempty(p
) ? NULL
: p
;
74 static inline const char *strdash_if_empty(const char *str
) {
75 return isempty(str
) ? "-" : str
;
78 static inline char *startswith(const char *s
, const char *prefix
) {
82 if (strncmp(s
, prefix
, l
) == 0)
88 static inline char *startswith_no_case(const char *s
, const char *prefix
) {
92 if (strncasecmp(s
, prefix
, l
) == 0)
98 char *endswith(const char *s
, const char *postfix
) _pure_
;
99 char *endswith_no_case(const char *s
, const char *postfix
) _pure_
;
101 char *first_word(const char *s
, const char *word
) _pure_
;
103 const char* split(const char **state
, size_t *l
, const char *separator
, bool quoted
);
105 #define FOREACH_WORD(word, length, s, state) \
106 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
108 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
109 _FOREACH_WORD(word, length, s, separator, false, state)
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)))
114 char *strappend(const char *s
, const char *suffix
);
115 char *strnappend(const char *s
, const char *suffix
, size_t length
);
117 char *strjoin_real(const char *x
, ...) _sentinel_
;
118 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
120 #define strjoina(a, ...) \
122 const char *_appendees_[] = { a, __VA_ARGS__ }; \
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_]); \
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
);
140 static inline char *skip_leading_chars(const char *s
, const char *bad
) {
148 return (char*) s
+ strspn(s
, bad
);
151 char ascii_tolower(char x
);
152 char *ascii_strlower(char *s
);
153 char *ascii_strlower_n(char *s
, size_t n
);
155 char ascii_toupper(char x
);
156 char *ascii_strupper(char *s
);
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
);
161 bool chars_intersect(const char *a
, const char *b
) _pure_
;
163 static inline bool _pure_
in_charset(const char *s
, const char* charset
) {
166 return s
[strspn(s
, charset
)] == '\0';
169 bool string_has_cc(const char *p
, const char *ok
) _pure_
;
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
);
174 bool nulstr_contains(const char *nulstr
, const char *needle
);
176 char* strshorten(char *s
, size_t l
);
178 char *strreplace(const char *text
, const char *old_string
, const char *new_string
);
180 char *strip_tab_ansi(char **p
, size_t *l
);
182 char *strextend(char **x
, ...) _sentinel_
;
184 char *strrep(const char *s
, unsigned n
);
186 int split_pair(const char *s
, const char *sep
, char **l
, char **r
);
188 int free_and_strdup(char **p
, const char *s
);
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
) {
194 return (void*) haystack
;
196 if (haystacklen
< needlelen
)
202 return memmem(haystack
, haystacklen
, needle
, needlelen
);
205 #if !HAVE_EXPLICIT_BZERO
206 void explicit_bzero(void *p
, size_t l
);
209 char *string_erase(char *x
);
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)
215 bool string_is_safe(const char *p
) _pure_
;
217 static inline size_t strlen_ptr(const char *s
) {