]>
git.ipfire.org Git - thirdparty/systemd.git/blob - src/basic/string-util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
6 This file is part of systemd.
8 Copyright 2010 Lennart Poettering
10 systemd is free software; you can redistribute it and/or modify it
11 under the terms of the GNU Lesser General Public License as published by
12 the Free Software Foundation; either version 2.1 of the License, or
13 (at your option) any later version.
15 systemd is distributed in the hope that it will be useful, but
16 WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
18 Lesser General Public License for more details.
20 You should have received a copy of the GNU Lesser General Public License
21 along with systemd; If not, see <http://www.gnu.org/licenses/>.
29 #define streq(a,b) (strcmp((a),(b)) == 0)
30 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
31 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
32 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
34 int strcmp_ptr(const char *a
, const char *b
) _pure_
;
36 static inline bool streq_ptr(const char *a
, const char *b
) {
37 return strcmp_ptr(a
, b
) == 0;
40 static inline const char* strempty(const char *s
) {
44 static inline const char* strnull(const char *s
) {
45 return s
? s
: "(null)";
48 static inline const char *strna(const char *s
) {
52 static inline bool isempty(const char *p
) {
56 static inline char *startswith(const char *s
, const char *prefix
) {
60 if (strncmp(s
, prefix
, l
) == 0)
66 static inline char *startswith_no_case(const char *s
, const char *prefix
) {
70 if (strncasecmp(s
, prefix
, l
) == 0)
76 char *endswith(const char *s
, const char *postfix
) _pure_
;
77 char *endswith_no_case(const char *s
, const char *postfix
) _pure_
;
79 char *first_word(const char *s
, const char *word
) _pure_
;
81 const char* split(const char **state
, size_t *l
, const char *separator
, bool quoted
);
83 #define FOREACH_WORD(word, length, s, state) \
84 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
86 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
87 _FOREACH_WORD(word, length, s, separator, false, state)
89 #define FOREACH_WORD_QUOTED(word, length, s, state) \
90 _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
92 #define _FOREACH_WORD(word, length, s, separator, quoted, state) \
93 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
95 char *strappend(const char *s
, const char *suffix
);
96 char *strnappend(const char *s
, const char *suffix
, size_t length
);
98 char *strjoin(const char *x
, ...) _sentinel_
;
100 #define strjoina(a, ...) \
102 const char *_appendees_[] = { a, __VA_ARGS__ }; \
106 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
107 _len_ += strlen(_appendees_[_i_]); \
108 _p_ = _d_ = alloca(_len_ + 1); \
109 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
110 _p_ = stpcpy(_p_, _appendees_[_i_]); \
115 char *strstrip(char *s
);
116 char *delete_chars(char *s
, const char *bad
);
117 char *truncate_nl(char *s
);
119 char *ascii_strlower(char *path
);
121 bool chars_intersect(const char *a
, const char *b
) _pure_
;
123 static inline bool _pure_
in_charset(const char *s
, const char* charset
) {
126 return s
[strspn(s
, charset
)] == '\0';
129 bool string_has_cc(const char *p
, const char *ok
) _pure_
;
131 char *ellipsize_mem(const char *s
, size_t old_length_bytes
, size_t new_length_columns
, unsigned percent
);
132 char *ellipsize(const char *s
, size_t length
, unsigned percent
);
134 bool nulstr_contains(const char*nulstr
, const char *needle
);
136 char* strshorten(char *s
, size_t l
);
138 char *strreplace(const char *text
, const char *old_string
, const char *new_string
);
140 char *strip_tab_ansi(char **p
, size_t *l
);
142 char *strextend(char **x
, ...) _sentinel_
;
144 char *strrep(const char *s
, unsigned n
);
146 int split_pair(const char *s
, const char *sep
, char **l
, char **r
);
148 int free_and_strdup(char **p
, const char *s
);
150 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
151 static inline void *memmem_safe(const void *haystack
, size_t haystacklen
, const void *needle
, size_t needlelen
) {
154 return (void*) haystack
;
156 if (haystacklen
< needlelen
)
162 return memmem(haystack
, haystacklen
, needle
, needlelen
);
165 #define memory_erase(p, l) memset((p), 'x', (l))
166 void string_erase(char *x
);
168 char *string_free_erase(char *s
);
169 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase
);
170 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)