]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.h
4 This file is part of systemd.
6 Copyright 2010 Lennart Poettering
8 systemd is free software; you can redistribute it and/or modify it
9 under the terms of the GNU Lesser General Public License as published by
10 the Free Software Foundation; either version 2.1 of the License, or
11 (at your option) any later version.
13 systemd is distributed in the hope that it will be useful, but
14 WITHOUT ANY WARRANTY; without even the implied warranty of
15 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 Lesser General Public License for more details.
18 You should have received a copy of the GNU Lesser General Public License
19 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 /* What is interpreted as whitespace? */
30 #define WHITESPACE " \t\n\r"
31 #define NEWLINE "\n\r"
34 #define GLOB_CHARS "*?["
35 #define DIGITS "0123456789"
36 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
37 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
38 #define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
39 #define ALPHANUMERICAL LETTERS DIGITS
40 #define HEXDIGITS DIGITS "abcdefABCDEF"
42 #define streq(a,b) (strcmp((a),(b)) == 0)
43 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
44 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
45 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
47 int strcmp_ptr(const char *a
, const char *b
) _pure_
;
49 static inline bool streq_ptr(const char *a
, const char *b
) {
50 return strcmp_ptr(a
, b
) == 0;
53 static inline const char* strempty(const char *s
) {
57 static inline const char* strnull(const char *s
) {
58 return s
? s
: "(null)";
61 static inline const char *strna(const char *s
) {
65 static inline bool isempty(const char *p
) {
69 static inline const char *empty_to_null(const char *p
) {
70 return isempty(p
) ? NULL
: p
;
73 static inline const char *strdash_if_empty(const char *str
) {
74 return isempty(str
) ? "-" : str
;
77 static inline char *startswith(const char *s
, const char *prefix
) {
81 if (strncmp(s
, prefix
, l
) == 0)
87 static inline char *startswith_no_case(const char *s
, const char *prefix
) {
91 if (strncasecmp(s
, prefix
, l
) == 0)
97 char *endswith(const char *s
, const char *postfix
) _pure_
;
98 char *endswith_no_case(const char *s
, const char *postfix
) _pure_
;
100 char *first_word(const char *s
, const char *word
) _pure_
;
102 const char* split(const char **state
, size_t *l
, const char *separator
, bool quoted
);
104 #define FOREACH_WORD(word, length, s, state) \
105 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
107 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
108 _FOREACH_WORD(word, length, s, separator, false, state)
110 #define _FOREACH_WORD(word, length, s, separator, quoted, state) \
111 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
113 char *strappend(const char *s
, const char *suffix
);
114 char *strnappend(const char *s
, const char *suffix
, size_t length
);
116 char *strjoin_real(const char *x
, ...) _sentinel_
;
117 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
119 #define strjoina(a, ...) \
121 const char *_appendees_[] = { a, __VA_ARGS__ }; \
125 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
126 _len_ += strlen(_appendees_[_i_]); \
127 _p_ = _d_ = alloca(_len_ + 1); \
128 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
129 _p_ = stpcpy(_p_, _appendees_[_i_]); \
134 char *strstrip(char *s
);
135 char *delete_chars(char *s
, const char *bad
);
136 char *truncate_nl(char *s
);
138 char ascii_tolower(char x
);
139 char *ascii_strlower(char *s
);
140 char *ascii_strlower_n(char *s
, size_t n
);
142 char ascii_toupper(char x
);
143 char *ascii_strupper(char *s
);
145 int ascii_strcasecmp_n(const char *a
, const char *b
, size_t n
);
146 int ascii_strcasecmp_nn(const char *a
, size_t n
, const char *b
, size_t m
);
148 bool chars_intersect(const char *a
, const char *b
) _pure_
;
150 static inline bool _pure_
in_charset(const char *s
, const char* charset
) {
153 return s
[strspn(s
, charset
)] == '\0';
156 bool string_has_cc(const char *p
, const char *ok
) _pure_
;
158 char *ellipsize_mem(const char *s
, size_t old_length_bytes
, size_t new_length_columns
, unsigned percent
);
159 char *ellipsize(const char *s
, size_t length
, unsigned percent
);
161 bool nulstr_contains(const char *nulstr
, const char *needle
);
163 char* strshorten(char *s
, size_t l
);
165 char *strreplace(const char *text
, const char *old_string
, const char *new_string
);
167 char *strip_tab_ansi(char **p
, size_t *l
);
169 char *strextend(char **x
, ...) _sentinel_
;
171 char *strrep(const char *s
, unsigned n
);
173 int split_pair(const char *s
, const char *sep
, char **l
, char **r
);
175 int free_and_strdup(char **p
, const char *s
);
177 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
178 static inline void *memmem_safe(const void *haystack
, size_t haystacklen
, const void *needle
, size_t needlelen
) {
181 return (void*) haystack
;
183 if (haystacklen
< needlelen
)
189 return memmem(haystack
, haystacklen
, needle
, needlelen
);
192 #if !HAVE_DECL_EXPLICIT_BZERO
193 void explicit_bzero(void *p
, size_t l
);
196 char *string_erase(char *x
);
198 char *string_free_erase(char *s
);
199 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase
);
200 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
202 bool string_is_safe(const char *p
) _pure_
;
204 static inline size_t strlen_ptr(const char *s
) {