chiark / gitweb /
tree-wide: remove Emacs lines from all files
[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 <stdbool.h>
23 #include <string.h>
24
25 #include "macro.h"
26
27 /* What is interpreted as whitespace? */
28 #define WHITESPACE        " \t\n\r"
29 #define NEWLINE           "\n\r"
30 #define QUOTES            "\"\'"
31 #define COMMENTS          "#;"
32 #define GLOB_CHARS        "*?["
33 #define DIGITS            "0123456789"
34 #define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
35 #define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
36 #define LETTERS           LOWERCASE_LETTERS UPPERCASE_LETTERS
37 #define ALPHANUMERICAL    LETTERS DIGITS
38
39 #define streq(a,b) (strcmp((a),(b)) == 0)
40 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
41 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
42 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
43
44 int strcmp_ptr(const char *a, const char *b) _pure_;
45
46 static inline bool streq_ptr(const char *a, const char *b) {
47         return strcmp_ptr(a, b) == 0;
48 }
49
50 static inline const char* strempty(const char *s) {
51         return s ? s : "";
52 }
53
54 static inline const char* strnull(const char *s) {
55         return s ? s : "(null)";
56 }
57
58 static inline const char *strna(const char *s) {
59         return s ? s : "n/a";
60 }
61
62 static inline bool isempty(const char *p) {
63         return !p || !p[0];
64 }
65
66 static inline char *startswith(const char *s, const char *prefix) {
67         size_t l;
68
69         l = strlen(prefix);
70         if (strncmp(s, prefix, l) == 0)
71                 return (char*) s + l;
72
73         return NULL;
74 }
75
76 static inline char *startswith_no_case(const char *s, const char *prefix) {
77         size_t l;
78
79         l = strlen(prefix);
80         if (strncasecmp(s, prefix, l) == 0)
81                 return (char*) s + l;
82
83         return NULL;
84 }
85
86 char *endswith(const char *s, const char *postfix) _pure_;
87 char *endswith_no_case(const char *s, const char *postfix) _pure_;
88
89 char *first_word(const char *s, const char *word) _pure_;
90
91 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
92
93 #define FOREACH_WORD(word, length, s, state)                            \
94         _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
95
96 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state)       \
97         _FOREACH_WORD(word, length, s, separator, false, state)
98
99 #define FOREACH_WORD_QUOTED(word, length, s, state)                     \
100         _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
101
102 #define _FOREACH_WORD(word, length, s, separator, quoted, state)        \
103         for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
104
105 char *strappend(const char *s, const char *suffix);
106 char *strnappend(const char *s, const char *suffix, size_t length);
107
108 char *strjoin(const char *x, ...) _sentinel_;
109
110 #define strjoina(a, ...)                                                \
111         ({                                                              \
112                 const char *_appendees_[] = { a, __VA_ARGS__ };         \
113                 char *_d_, *_p_;                                        \
114                 int _len_ = 0;                                          \
115                 unsigned _i_;                                           \
116                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
117                         _len_ += strlen(_appendees_[_i_]);              \
118                 _p_ = _d_ = alloca(_len_ + 1);                          \
119                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
120                         _p_ = stpcpy(_p_, _appendees_[_i_]);            \
121                 *_p_ = 0;                                               \
122                 _d_;                                                    \
123         })
124
125 char *strstrip(char *s);
126 char *delete_chars(char *s, const char *bad);
127 char *truncate_nl(char *s);
128
129 char ascii_tolower(char x);
130 char *ascii_strlower(char *s);
131 char *ascii_strlower_n(char *s, size_t n);
132
133 int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
134 int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
135
136 bool chars_intersect(const char *a, const char *b) _pure_;
137
138 static inline bool _pure_ in_charset(const char *s, const char* charset) {
139         assert(s);
140         assert(charset);
141         return s[strspn(s, charset)] == '\0';
142 }
143
144 bool string_has_cc(const char *p, const char *ok) _pure_;
145
146 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
147 char *ellipsize(const char *s, size_t length, unsigned percent);
148
149 bool nulstr_contains(const char*nulstr, const char *needle);
150
151 char* strshorten(char *s, size_t l);
152
153 char *strreplace(const char *text, const char *old_string, const char *new_string);
154
155 char *strip_tab_ansi(char **p, size_t *l);
156
157 char *strextend(char **x, ...) _sentinel_;
158
159 char *strrep(const char *s, unsigned n);
160
161 int split_pair(const char *s, const char *sep, char **l, char **r);
162
163 int free_and_strdup(char **p, const char *s);
164
165 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
166 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
167
168         if (needlelen <= 0)
169                 return (void*) haystack;
170
171         if (haystacklen < needlelen)
172                 return NULL;
173
174         assert(haystack);
175         assert(needle);
176
177         return memmem(haystack, haystacklen, needle, needlelen);
178 }
179
180 void* memory_erase(void *p, size_t l);
181 char *string_erase(char *x);
182
183 char *string_free_erase(char *s);
184 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
185 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
186
187 bool string_is_safe(const char *p) _pure_;