string-util.h revision 11c3a36649e5e5e77db499c92f3cdcbd619efd3a
/*-*- Mode: C; c-basic-offset: 8; indent-tabs-mode: nil -*-*/
#pragma once
/***
This file is part of systemd.
Copyright 2010 Lennart Poettering
under the terms of the GNU Lesser General Public License as published by
the Free Software Foundation; either version 2.1 of the License, or
(at your option) any later version.
systemd is distributed in the hope that it will be useful, but
WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Lesser General Public License for more details.
You should have received a copy of the GNU Lesser General Public License
along with systemd; If not, see <http://www.gnu.org/licenses/>.
***/
#include <alloca.h>
#include <stdbool.h>
#include <stddef.h>
#include <string.h>
#include "macro.h"
/* What is interpreted as whitespace? */
#define WHITESPACE " \t\n\r"
#define NEWLINE "\n\r"
#define QUOTES "\"\'"
#define COMMENTS "#;"
#define GLOB_CHARS "*?["
#define DIGITS "0123456789"
#define LOWERCASE_LETTERS "abcdefghijklmnopqrstuvwxyz"
#define UPPERCASE_LETTERS "ABCDEFGHIJKLMNOPQRSTUVWXYZ"
#define strcaseeq(a,b) (strcasecmp((a),(b)) == 0)
#define strncaseeq(a, b, n) (strncasecmp((a), (b), (n)) == 0)
int strcmp_ptr(const char *a, const char *b) _pure_;
static inline bool streq_ptr(const char *a, const char *b) {
return strcmp_ptr(a, b) == 0;
}
static inline const char* strempty(const char *s) {
return s ? s : "";
}
static inline const char* strnull(const char *s) {
return s ? s : "(null)";
}
static inline const char *strna(const char *s) {
return s ? s : "n/a";
}
static inline bool isempty(const char *p) {
return !p || !p[0];
}
static inline char *startswith(const char *s, const char *prefix) {
size_t l;
return (char*) s + l;
return NULL;
}
static inline char *startswith_no_case(const char *s, const char *prefix) {
size_t l;
if (strncasecmp(s, prefix, l) == 0)
return (char*) s + l;
return NULL;
}
for ((state) = (s), (word) = split(&(state), &(length), (separator), (quoted)); (word); (word) = split(&(state), &(length), (separator), (quoted)))
char *strjoin(const char *x, ...) _sentinel_;
#define strjoina(a, ...) \
({ \
const char *_appendees_[] = { a, __VA_ARGS__ }; \
int _len_ = 0; \
unsigned _i_; \
*_p_ = 0; \
_d_; \
})
char *strstrip(char *s);
char *delete_chars(char *s, const char *bad);
char *truncate_nl(char *s);
char *ascii_strlower(char *path);
bool chars_intersect(const char *a, const char *b) _pure_;
assert(s);
}
char *ellipsize_mem(const char *s, size_t old_length_bytes, size_t new_length_columns, unsigned percent);
char* strshorten(char *s, size_t l);
char *strip_tab_ansi(char **p, size_t *l);
char *strextend(char **x, ...) _sentinel_;
char *strrep(const char *s, unsigned n);
int split_pair(const char *s, const char *sep, char **l, char **r);
int free_and_strdup(char **p, const char *s);
/* Normal memmem() requires haystack to be nonnull, which is annoying for zero-length buffers */
static inline void *memmem_safe(const void *haystack, size_t haystacklen, const void *needle, size_t needlelen) {
if (needlelen <= 0)
return (void*) haystack;
if (haystacklen < needlelen)
return NULL;
}
void* memory_erase(void *p, size_t l);
char *string_erase(char *x);
char *string_free_erase(char *s);
DEFINE_TRIVIAL_CLEANUP_FUNC(char *, string_free_erase);
bool string_is_safe(const char *p) _pure_;