]> git.proxmox.com Git - systemd.git/blame - src/basic/string-util.h
Imported Upstream version 231
[systemd.git] / src / basic / string-util.h
CommitLineData
db2df898
MP
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
4c89c718 22#include <alloca.h>
db2df898 23#include <stdbool.h>
4c89c718 24#include <stddef.h>
db2df898
MP
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
aa27b158 40#define HEXDIGITS DIGITS "abcdefABCDEF"
db2df898
MP
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
47int strcmp_ptr(const char *a, const char *b) _pure_;
48
49static inline bool streq_ptr(const char *a, const char *b) {
50 return strcmp_ptr(a, b) == 0;
51}
52
53static inline const char* strempty(const char *s) {
54 return s ? s : "";
55}
56
57static inline const char* strnull(const char *s) {
58 return s ? s : "(null)";
59}
60
61static inline const char *strna(const char *s) {
62 return s ? s : "n/a";
63}
64
65static inline bool isempty(const char *p) {
66 return !p || !p[0];
67}
68
5a920b42
MP
69static inline const char *empty_to_null(const char *p) {
70 return isempty(p) ? NULL : p;
71}
72
db2df898
MP
73static inline char *startswith(const char *s, const char *prefix) {
74 size_t l;
75
76 l = strlen(prefix);
77 if (strncmp(s, prefix, l) == 0)
78 return (char*) s + l;
79
80 return NULL;
81}
82
83static inline char *startswith_no_case(const char *s, const char *prefix) {
84 size_t l;
85
86 l = strlen(prefix);
87 if (strncasecmp(s, prefix, l) == 0)
88 return (char*) s + l;
89
90 return NULL;
91}
92
93char *endswith(const char *s, const char *postfix) _pure_;
94char *endswith_no_case(const char *s, const char *postfix) _pure_;
95
96char *first_word(const char *s, const char *word) _pure_;
97
98const char* split(const char **state, size_t *l, const char *separator, bool quoted);
99
100#define FOREACH_WORD(word, length, s, state) \
101 _FOREACH_WORD(word, length, s, WHITESPACE, false, state)
102
103#define FOREACH_WORD_SEPARATOR(word, length, s, separator, state) \
104 _FOREACH_WORD(word, length, s, separator, false, state)
105
106#define FOREACH_WORD_QUOTED(word, length, s, state) \
107 _FOREACH_WORD(word, length, s, WHITESPACE, true, state)
108
109#define _FOREACH_WORD(word, length, s, separator, quoted, state) \
110 for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
111
112char *strappend(const char *s, const char *suffix);
113char *strnappend(const char *s, const char *suffix, size_t length);
114
115char *strjoin(const char *x, ...) _sentinel_;
116
117#define strjoina(a, ...) \
118 ({ \
119 const char *_appendees_[] = { a, __VA_ARGS__ }; \
120 char *_d_, *_p_; \
121 int _len_ = 0; \
122 unsigned _i_; \
123 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
124 _len_ += strlen(_appendees_[_i_]); \
125 _p_ = _d_ = alloca(_len_ + 1); \
126 for (_i_ = 0; _i_ < ELEMENTSOF(_appendees_) && _appendees_[_i_]; _i_++) \
127 _p_ = stpcpy(_p_, _appendees_[_i_]); \
128 *_p_ = 0; \
129 _d_; \
130 })
131
132char *strstrip(char *s);
133char *delete_chars(char *s, const char *bad);
134char *truncate_nl(char *s);
135
4c89c718
MP
136char ascii_tolower(char x);
137char *ascii_strlower(char *s);
138char *ascii_strlower_n(char *s, size_t n);
139
140int ascii_strcasecmp_n(const char *a, const char *b, size_t n);
141int ascii_strcasecmp_nn(const char *a, size_t n, const char *b, size_t m);
db2df898
MP
142
143bool chars_intersect(const char *a, const char *b) _pure_;
144
145static inline bool _pure_ in_charset(const char *s, const char* charset) {
146 assert(s);
147 assert(charset);
148 return s[strspn(s, charset)] == '\0';
149}
150
151bool string_has_cc(const char *p, const char *ok) _pure_;
152
153char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
154char *ellipsize(const char *s, size_t length, unsigned percent);
155
156bool nulstr_contains(const char*nulstr, const char *needle);
157
158char* strshorten(char *s, size_t l);
159
160char *strreplace(const char *text, const char *old_string, const char *new_string);
161
162char *strip_tab_ansi(char **p, size_t *l);
163
164char *strextend(char **x, ...) _sentinel_;
165
166char *strrep(const char *s, unsigned n);
167
168int split_pair(const char *s, const char *sep, char **l, char **r);
169
170int free_and_strdup(char **p, const char *s);
171
172/* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
173static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
174
175 if (needlelen <= 0)
176 return (void*) haystack;
177
178 if (haystacklen < needlelen)
179 return NULL;
180
181 assert(haystack);
182 assert(needle);
183
184 return memmem(haystack, haystacklen, needle, needlelen);
185}
186
187void* memory_erase(void *p, size_t l);
188char *string_erase(char *x);
189
190char *string_free_erase(char *s);
191DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
192#define _cleanup_string_free_erase_ _cleanup_(string_free_erasep)
193
194bool string_is_safe(const char *p) _pure_;