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