]> git.ipfire.org Git - thirdparty/systemd.git/blame - src/basic/string-util.h
tree-wide: drop 'This file is part of systemd' blurb
[thirdparty/systemd.git] / src / basic / string-util.h
CommitLineData
53e1b683 1/* SPDX-License-Identifier: LGPL-2.1+ */
07630cea
LP
2#pragma once
3
4/***
07630cea 5 Copyright 2010 Lennart Poettering
07630cea
LP
6***/
7
11c3a366 8#include <alloca.h>
07630cea 9#include <stdbool.h>
11c3a366 10#include <stddef.h>
07630cea
LP
11#include <string.h>
12
13#include "macro.h"
14
b11d6a7b
LP
15/* What is interpreted as whitespace? */
16#define WHITESPACE " \t\n\r"
17#define NEWLINE "\n\r"
18#define QUOTES "\"\'"
19#define COMMENTS "#;"
20#define GLOB_CHARS "*?["
21#define DIGITS "0123456789"
22#define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
23#define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
24#define LETTERS LOWERCASE_LETTERS UPPERCASE_LETTERS
25#define ALPHANUMERICAL LETTERS DIGITS
1a7906ae 26#define HEXDIGITS DIGITS "abcdefABCDEF"
b11d6a7b 27
07630cea
LP
28#define streq(a,b) (strcmp((a),(b)) == 0)
29#define strneq(a, b, n) (strncmp((a), (b), (n)) == 0)
30#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
31#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
32
33int strcmp_ptr(const char *a, const char *b) _pure_;
34
35static inline bool streq_ptr(const char *a, const char *b) {
36 return strcmp_ptr(a, b) == 0;
37}
38
39static inline const char* strempty(const char *s) {
ad5d4b17 40 return s ?: "";
07630cea
LP
41}
42
43static inline const char* strnull(const char *s) {
ad5d4b17 44 return s ?: "(null)";
07630cea
LP
45}
46
47static inline const char *strna(const char *s) {
ad5d4b17 48 return s ?: "n/a";
07630cea
LP
49}
50
51static inline bool isempty(const char *p) {
52 return !p || !p[0];
53}
54
3c6f7c34
LP
55static inline const char *empty_to_null(const char *p) {
56 return isempty(p) ? NULL : p;
57}
58
c5984fe1 59static inline const char *empty_to_dash(const char *str) {
07b0b339
SK
60 return isempty(str) ? "-" : str;
61}
62
07630cea
LP
63static inline char *startswith(const char *s, const char *prefix) {
64 size_t l;
65
66 l = strlen(prefix);
67 if (strncmp(s, prefix, l) == 0)
68 return (char*) s + l;
69
70 return NULL;
71}
72
73static inline char *startswith_no_case(const char *s, const char *prefix) {
74 size_t l;
75
76 l = strlen(prefix);
77 if (strncasecmp(s, prefix, l) == 0)
78 return (char*) s + l;
79
80 return NULL;
81}
82
83char *endswith(const char *s, const char *postfix) _pure_;
84char *endswith_no_case(const char *s, const char *postfix) _pure_;
85
86char *first_word(const char *s, const char *word) _pure_;
87
88const char* split(const char **state, size_t *l, const char *separator, bool quoted);
89
90#define FOREACH_WORD(word, length, s, state) \
91 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
92
93#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
94 _FOREACH_WORD(word, length, s, separator, false, state)
95
07630cea
LP
96#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
97 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
98
99char *strappend(const char *s, const char *suffix);
100char *strnappend(const char *s, const char *suffix, size_t length);
101
605405c6
ZJS
102char *strjoin_real(const char *x, ...) _sentinel_;
103#define strjoin(a, ...) strjoin_real((a), __VA_ARGS__, NULL)
07630cea
LP
104
105#define strjoina(a, ...) \
106 ({ \
107 const char *_appendees_[] = { a, __VA_ARGS__ }; \
108 char *_d_, *_p_; \
35207e25 109 size_t _len_ = 0; \
da6053d0 110 size_t _i_; \
07630cea
LP
111 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
112 _len_ += strlen(_appendees_[_i_]); \
113 _p_ = _d_ = alloca(_len_ + 1); \
114 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
115 _p_ = stpcpy(_p_, _appendees_[_i_]); \
116 *_p_ = 0; \
117 _d_; \
118 })
119
120char *strstrip(char *s);
121char *delete_chars(char *s, const char *bad);
7546145e 122char *delete_trailing_chars(char *s, const char *bad);
07630cea
LP
123char *truncate_nl(char *s);
124
7546145e
LP
125static inline char *skip_leading_chars(const char *s, const char *bad) {
126
127 if (!s)
128 return NULL;
129
130 if (!bad)
131 bad = WHITESPACE;
132
133 return (char*) s + strspn(s, bad);
134}
135
b577e3d5
LP
136char ascii_tolower(char x);
137char *ascii_strlower(char *s);
138char *ascii_strlower_n(char *s, size_t n);
07630cea 139
846b8fc3
LP
140char ascii_toupper(char x);
141char *ascii_strupper(char *s);
142
522d85ae 143int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
c1749834 144int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
522d85ae 145
07630cea
LP
146bool chars_intersect(const char *a, const char *b) _pure_;
147
148static inline bool _pure_ in_charset(const char *s, const char* charset) {
149 assert(s);
150 assert(charset);
151 return s[strspn(s, charset)] == '\0';
152}
153
154bool string_has_cc(const char *p, const char *ok) _pure_;
155
156char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
ae03775f
ZJS
157static inline char *ellipsize(const char *s, size_t length, unsigned percent) {
158 return ellipsize_mem(s, strlen(s), length, percent);
159}
160
8409f688 161char *cellescape(char *buf, size_t len, const char *s);
07630cea 162
cca24fc3
ZJS
163/* This limit is arbitrary, enough to give some idea what the string contains */
164#define CELLESCAPE_DEFAULT_LENGTH 64
165
2d5dece8 166bool nulstr_contains(const char *nulstr, const char *needle);
07630cea
LP
167
168char* strshorten(char *s, size_t l);
169
170char *strreplace(const char *text, const char *old_string, const char *new_string);
171
b4766d5f 172char *strip_tab_ansi(char **ibuf, size_t *_isz, size_t highlight[2]);
07630cea 173
bb8ad9ea
LP
174char *strextend_with_separator(char **x, const char *separator, ...) _sentinel_;
175
176#define strextend(x, ...) strextend_with_separator(x, NULL, __VA_ARGS__)
07630cea
LP
177
178char *strrep(const char *s, unsigned n);
179
180int split_pair(const char *s, const char *sep, char **l, char **r);
181
182int free_and_strdup(char **p, const char *s);
183
184/* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
185static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
186
187 if (needlelen <= 0)
188 return (void*) haystack;
189
190 if (haystacklen < needlelen)
191 return NULL;
192
193 assert(haystack);
194 assert(needle);
195
196 return memmem(haystack, haystacklen, needle, needlelen);
197}
198
4b9545f1 199#if !HAVE_EXPLICIT_BZERO
2d26d8e0
ZJS
200void explicit_bzero(void *p, size_t l);
201#endif
202
9fe4ea21 203char *string_erase(char *x);
07630cea
LP
204
205char *string_free_erase(char *s);
206DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
207#define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
f3e2e81d
LP
208
209bool string_is_safe(const char *p) _pure_;
7bf7ce28
LP
210
211static inline size_t strlen_ptr(const char *s) {
212 if (!s)
213 return 0;
214
215 return strlen(s);
216}
9b8ff183
LP
217
218/* Like startswith(), but operates on arbitrary memory blocks */
219static inline void *memory_startswith(const void *p, size_t sz, const char *token) {
220 size_t n;
221
222 assert(token);
223
224 n = strlen(token);
225 if (sz < n)
226 return NULL;
227
228 assert(p);
229
230 if (memcmp(p, token, n) != 0)
231 return NULL;
232
233 return (uint8_t*) p + n;
234}