StringAPI.hxx 5.11 KB
Newer Older
1
/*
2
 * Copyright 2010-2021 Max Kellermann <max.kellermann@gmail.com>
3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29
 *
 * Redistribution and use in source and binary forms, with or without
 * modification, are permitted provided that the following conditions
 * are met:
 *
 * - Redistributions of source code must retain the above copyright
 * notice, this list of conditions and the following disclaimer.
 *
 * - Redistributions in binary form must reproduce the above copyright
 * notice, this list of conditions and the following disclaimer in the
 * documentation and/or other materials provided with the
 * distribution.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
 * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED.  IN NO EVENT SHALL THE
 * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
 * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
 * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
 * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
 * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
 * OF THE POSSIBILITY OF SUCH DAMAGE.
 */

30 31
#ifndef STRING_API_HXX
#define STRING_API_HXX
32

Rosen Penev's avatar
Rosen Penev committed
33
#include <cstring>
34

35 36 37 38
#ifdef _UNICODE
#include "WStringAPI.hxx"
#endif

39
[[gnu::pure]] [[gnu::nonnull]]
40
static inline size_t
41
StringLength(const char *p) noexcept
42 43 44 45
{
	return strlen(p);
}

46
[[gnu::pure]] [[gnu::nonnull]]
47
static inline const char *
48
StringFind(const char *haystack, const char *needle) noexcept
49 50 51 52
{
	return strstr(haystack, needle);
}

53
[[gnu::pure]] [[gnu::nonnull]]
54
static inline char *
55
StringFind(char *haystack, char needle, size_t size) noexcept
56
{
Rosen Penev's avatar
Rosen Penev committed
57
	return (char *)std::memchr(haystack, needle, size);
58 59
}

60
[[gnu::pure]] [[gnu::nonnull]]
61
static inline const char *
62
StringFind(const char *haystack, char needle, size_t size) noexcept
63
{
Rosen Penev's avatar
Rosen Penev committed
64
	return (const char *)std::memchr(haystack, needle, size);
65 66
}

67
[[gnu::pure]] [[gnu::nonnull]]
68
static inline const char *
69
StringFind(const char *haystack, char needle) noexcept
70
{
Rosen Penev's avatar
Rosen Penev committed
71
	return std::strchr(haystack, needle);
72 73
}

74
[[gnu::pure]] [[gnu::nonnull]]
75
static inline char *
76
StringFind(char *haystack, char needle) noexcept
77
{
Rosen Penev's avatar
Rosen Penev committed
78
	return std::strchr(haystack, needle);
79 80
}

81
[[gnu::pure]] [[gnu::nonnull]]
82
static inline const char *
83
StringFindLast(const char *haystack, char needle) noexcept
84
{
Rosen Penev's avatar
Rosen Penev committed
85
	return std::strrchr(haystack, needle);
86 87
}

88
[[gnu::pure]] [[gnu::nonnull]]
89
static inline char *
90
StringFindLast(char *haystack, char needle) noexcept
91
{
Rosen Penev's avatar
Rosen Penev committed
92
	return std::strrchr(haystack, needle);
93 94
}

95
[[gnu::pure]] [[gnu::nonnull]]
96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115
static inline const char *
StringFindLast(const char *haystack, char needle, size_t size) noexcept
{
#if defined(__GLIBC__) || defined(__BIONIC__)
	/* memrchr() is a GNU extension (and also available on
	   Android) */
	return (const char *)memrchr(haystack, needle, size);
#else
	/* emulate for everybody else */
	const auto *p = haystack + size;
	while (p > haystack) {
		--p;
		if (*p == needle)
			return p;
	}

	return nullptr;
#endif
}

116
[[gnu::pure]] [[gnu::nonnull]]
117 118 119 120 121 122
static inline const char *
StringFindAny(const char *haystack, const char *accept) noexcept
{
	return strpbrk(haystack, accept);
}

123 124 125 126 127 128
static inline char *
StringToken(char *str, const char *delim) noexcept
{
	return strtok(str, delim);
}

129
[[gnu::nonnull]]
130
static inline void
131
UnsafeCopyString(char *dest, const char *src) noexcept
132 133 134 135
{
	strcpy(dest, src);
}

136
[[gnu::returns_nonnull]] [[gnu::nonnull]]
137
static inline char *
138
UnsafeCopyStringP(char *dest, const char *src) noexcept
139
{
140
#if defined(_WIN32)
141 142 143
	/* emulate stpcpy() */
	UnsafeCopyString(dest, src);
	return dest + StringLength(dest);
144
#else
145
	return stpcpy(dest, src);
146 147 148
#endif
}

149
[[gnu::pure]] [[gnu::nonnull]]
150 151 152 153 154 155
static inline int
StringCompare(const char *a, const char *b) noexcept
{
	return strcmp(a, b);
}

156
[[gnu::pure]] [[gnu::nonnull]]
157 158 159 160 161 162
static inline int
StringCompare(const char *a, const char *b, size_t n) noexcept
{
	return strncmp(a, b, n);
}

163 164 165
/**
 * Checks whether #a and #b are equal.
 */
166
[[gnu::pure]] [[gnu::nonnull]]
167
static inline bool
168
StringIsEqual(const char *a, const char *b) noexcept
169
{
170
	return StringCompare(a, b) == 0;
171 172 173 174 175
}

/**
 * Checks whether #a and #b are equal.
 */
176
[[gnu::pure]] [[gnu::nonnull]]
177
static inline bool
178
StringIsEqual(const char *a, const char *b, size_t length) noexcept
179 180 181 182
{
	return strncmp(a, b, length) == 0;
}

183
[[gnu::pure]] [[gnu::nonnull]]
184 185 186 187 188 189
static inline bool
StringIsEqualIgnoreCase(const char *a, const char *b) noexcept
{
	return strcasecmp(a, b) == 0;
}

190
[[gnu::pure]] [[gnu::nonnull]]
191 192 193 194 195 196
static inline bool
StringIsEqualIgnoreCase(const char *a, const char *b, size_t size) noexcept
{
	return strncasecmp(a, b, size) == 0;
}

197
[[gnu::pure]] [[gnu::nonnull]]
198 199 200 201 202 203
static inline int
StringCollate(const char *a, const char *b) noexcept
{
	return strcoll(a, b);
}

204 205 206 207
/**
 * Copy the string to a new allocation.  The return value must be
 * freed with free().
 */
208
[[gnu::malloc]] [[gnu::returns_nonnull]] [[gnu::nonnull]]
209
static inline char *
210
DuplicateString(const char *p) noexcept
211 212 213 214 215
{
	return strdup(p);
}

#endif