chiark / gitweb /
4b5cb8c7011dfd534fef49bf9e8eb180ae6847bc
[elogind.git] / src / basic / string-util.h
1 #pragma once
2
3 /***
4   This file is part of systemd.
5
6   Copyright 2010 Lennart Poettering
7
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.
12
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.
17
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/>.
20 ***/
21
22 #include <alloca.h>
23 #include <stdbool.h>
24 #include <stddef.h>
25 #include <string.h>
26
27 #include "macro.h"
28
29 /* What is interpreted as whitespace? */
30 #define WHITESPACE        " \t\n\r"
31 #define NEWLINE           "\n\r"
32 #define QUOTES            "\"\'"
33 #define COMMENTS          "#;"
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"
41
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)
46
47 int strcmp_ptr(const char *a, const char *b) _pure_;
48
49 static inline bool streq_ptr(const char *a, const char *b) {
50         return strcmp_ptr(a, b) == 0;
51 }
52
53 static inline const char* strempty(const char *s) {
54         return s ? s : "";
55 }
56
57 static inline const char* strnull(const char *s) {
58         return s ? s : "(null)";
59 }
60
61 static inline const char *strna(const char *s) {
62         return s ? s : "n/a";
63 }
64
65 static inline bool isempty(const char *p) {
66         return !p || !p[0];
67 }
68
69 #if 0 /// UNNEEDED by elogind
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 #endif // 0
78
79 static inline char *startswith(const char *s, const char *prefix) {
80         size_t l;
81
82         l = strlen(prefix);
83         if (strncmp(s, prefix, l) == 0)
84                 return (char*) s + l;
85
86         return NULL;
87 }
88
89 #if 0 /// UNNEEDED by elogind
90 static inline char *startswith_no_case(const char *s, const char *prefix) {
91         size_t l;
92
93         l = strlen(prefix);
94         if (strncasecmp(s, prefix, l) == 0)
95                 return (char*) s + l;
96
97         return NULL;
98 }
99 #endif // 0
100
101 char *endswith(const char *s, const char *postfix) _pure_;
102 char *endswith_no_case(const char *s, const char *postfix) _pure_;
103
104 char *first_word(const char *s, const char *word) _pure_;
105
106 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
107
108 #define FOREACH_WORD(word, length, s, state)                            \
109         _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
110
111 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state)       \
112         _FOREACH_WORD(word, length, s, separator, false, state)
113
114 #define FOREACH_WORD_QUOTED(word, length, s, state)                     \
115         _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
116
117 #define _FOREACH_WORD(word, length, s, separator, quoted, state)        \
118         for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
119
120 char *strappend(const char *s, const char *suffix);
121 char *strnappend(const char *s, const char *suffix, size_t length);
122
123 char *strjoin_real(const char *x, ...) _sentinel_;
124 #define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
125
126 #define strjoina(a, ...)                                                \
127         ({                                                              \
128                 const char *_appendees_[] = { a, __VA_ARGS__ };         \
129                 char *_d_, *_p_;                                        \
130                 int _len_ = 0;                                          \
131                 unsigned _i_;                                           \
132                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
133                         _len_ += strlen(_appendees_[_i_]);              \
134                 _p_ = _d_ = alloca(_len_ + 1);                          \
135                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
136                         _p_ = stpcpy(_p_, _appendees_[_i_]);            \
137                 *_p_ = 0;                                               \
138                 _d_;                                                    \
139         })
140
141 char *strstrip(char *s);
142 #if 0 /// UNNEEDED by elogind
143 char *delete_chars(char *s, const char *bad);
144 #endif // 0
145 char *truncate_nl(char *s);
146
147 #if 0 /// UNNEEDED by elogind
148 char ascii_tolower(char x);
149 char *ascii_strlower(char *s);
150 char *ascii_strlower_n(char *s, size_t n);
151
152 char ascii_toupper(char x);
153 char *ascii_strupper(char *s);
154
155 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
156 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
157
158 bool chars_intersect(const char *a, const char *b) _pure_;
159 #endif // 0
160
161 static inline bool _pure_ in_charset(const char *s, const char* charset) {
162         assert(s);
163         assert(charset);
164         return s[strspn(s, charset)] == '\0';
165 }
166
167 bool string_has_cc(const char *p, const char *ok) _pure_;
168
169 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
170 char *ellipsize(const char *s, size_t length, unsigned percent);
171
172 bool nulstr_contains(const char*nulstr, const char *needle);
173
174 char* strshorten(char *s, size_t l);
175
176 char *strreplace(const char *text, const char *old_string, const char *new_string);
177
178 char *strip_tab_ansi(char **p, size_t *l);
179
180 char *strextend(char **x, ...) _sentinel_;
181
182 char *strrep(const char *s, unsigned n);
183
184 int split_pair(const char *s, const char *sep, char **l, char **r);
185
186 int free_and_strdup(char **p, const char *s);
187
188 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
189 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
190
191         if (needlelen <= 0)
192                 return (void*) haystack;
193
194         if (haystacklen < needlelen)
195                 return NULL;
196
197         assert(haystack);
198         assert(needle);
199
200         return memmem(haystack, haystacklen, needle, needlelen);
201 }
202
203 void* memory_erase(void *p, size_t l);
204 char *string_erase(char *x);
205
206 char *string_free_erase(char *s);
207 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
208 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
209
210 bool string_is_safe(const char *p) _pure_;