chiark / gitweb /
Merge pull request #7 from elogind/dev_v228-r1
[elogind.git] / src / basic / string-util.h
1 /*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
2
3 #pragma once
4
5 /***
6   This file is part of systemd.
7
8   Copyright 2010 Lennart Poettering
9
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.
14
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.
19
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/>.
22 ***/
23
24 #include <stdbool.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
41 #define streq(a,b) (strcmp((a),(b)) == 0)
42 #define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
43 #define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
44 #define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
45
46 int strcmp_ptr(const char *a, const char *b) _pure_;
47
48 static inline bool streq_ptr(const char *a, const char *b) {
49         return strcmp_ptr(a, b) == 0;
50 }
51
52 static inline const char* strempty(const char *s) {
53         return s ? s : "";
54 }
55
56 static inline const char* strnull(const char *s) {
57         return s ? s : "(null)";
58 }
59
60 static inline const char *strna(const char *s) {
61         return s ? s : "n/a";
62 }
63
64 static inline bool isempty(const char *p) {
65         return !p || !p[0];
66 }
67
68 static inline char *startswith(const char *s, const char *prefix) {
69         size_t l;
70
71         l = strlen(prefix);
72         if (strncmp(s, prefix, l) == 0)
73                 return (char*) s + l;
74
75         return NULL;
76 }
77
78 static inline char *startswith_no_case(const char *s, const char *prefix) {
79         size_t l;
80
81         l = strlen(prefix);
82         if (strncasecmp(s, prefix, l) == 0)
83                 return (char*) s + l;
84
85         return NULL;
86 }
87
88 char *endswith(const char *s, const char *postfix) _pure_;
89 char *endswith_no_case(const char *s, const char *postfix) _pure_;
90
91 char *first_word(const char *s, const char *word) _pure_;
92
93 const char* split(const char **state, size_t *l, const char *separator, bool quoted);
94
95 #define FOREACH_WORD(word, length, s, state)                            \
96         _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
97
98 #define FOREACH_WORD_SEPARATOR(word, length, s, separator, state)       \
99         _FOREACH_WORD(word, length, s, separator, false, state)
100
101 #define FOREACH_WORD_QUOTED(word, length, s, state)                     \
102         _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
103
104 #define _FOREACH_WORD(word, length, s, separator, quoted, state)        \
105         for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
106
107 char *strappend(const char *s, const char *suffix);
108 char *strnappend(const char *s, const char *suffix, size_t length);
109
110 char *strjoin(const char *x, ...) _sentinel_;
111
112 #define strjoina(a, ...)                                                \
113         ({                                                              \
114                 const char *_appendees_[] = { a, __VA_ARGS__ };         \
115                 char *_d_, *_p_;                                        \
116                 int _len_ = 0;                                          \
117                 unsigned _i_;                                           \
118                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
119                         _len_ += strlen(_appendees_[_i_]);              \
120                 _p_ = _d_ = alloca(_len_ + 1);                          \
121                 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
122                         _p_ = stpcpy(_p_, _appendees_[_i_]);            \
123                 *_p_ = 0;                                               \
124                 _d_;                                                    \
125         })
126
127 char *strstrip(char *s);
128 char *delete_chars(char *s, const char *bad);
129 char *truncate_nl(char *s);
130
131 char *ascii_strlower(char *path);
132
133 bool chars_intersect(const char *a, const char *b) _pure_;
134
135 static inline bool _pure_ in_charset(const char *s, const char* charset) {
136         assert(s);
137         assert(charset);
138         return s[strspn(s, charset)] == '\0';
139 }
140
141 bool string_has_cc(const char *p, const char *ok) _pure_;
142
143 char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
144 char *ellipsize(const char *s, size_t length, unsigned percent);
145
146 bool nulstr_contains(const char*nulstr, const char *needle);
147
148 char* strshorten(char *s, size_t l);
149
150 char *strreplace(const char *text, const char *old_string, const char *new_string);
151
152 char *strip_tab_ansi(char **p, size_t *l);
153
154 char *strextend(char **x, ...) _sentinel_;
155
156 char *strrep(const char *s, unsigned n);
157
158 int split_pair(const char *s, const char *sep, char **l, char **r);
159
160 int free_and_strdup(char **p, const char *s);
161
162 /* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
163 static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
164
165         if (needlelen <= 0)
166                 return (void*) haystack;
167
168         if (haystacklen < needlelen)
169                 return NULL;
170
171         assert(haystack);
172         assert(needle);
173
174         return memmem(haystack, haystacklen, needle, needlelen);
175 }
176
177 void* memory_erase(void *p, size_t l);
178 char *string_erase(char *x);
179
180 char *string_free_erase(char *s);
181 DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
182 #define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
183
184 bool string_is_safe(const char *p) _pure_;