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