WStringAPI.hxx 5.21 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 30 31 32
 *
 * 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.
 */

#ifndef WSTRING_API_HXX
#define WSTRING_API_HXX

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

35
[[gnu::pure]] [[gnu::nonnull]]
36
static inline size_t
37
StringLength(const wchar_t *p) noexcept
38 39 40 41
{
	return wcslen(p);
}

42
[[gnu::pure]] [[gnu::nonnull]]
43
static inline const wchar_t *
44
StringFind(const wchar_t *haystack, const wchar_t *needle) noexcept
45 46 47 48
{
	return wcsstr(haystack, needle);
}

49
[[gnu::pure]] [[gnu::nonnull]]
50
static inline const wchar_t *
51
StringFind(const wchar_t *haystack, wchar_t needle, size_t size) noexcept
52
{
Rosen Penev's avatar
Rosen Penev committed
53
	return std::wmemchr(haystack, needle, size);
54 55
}

56
[[gnu::pure]] [[gnu::nonnull]]
57
static inline wchar_t *
58
StringFind(wchar_t *haystack, wchar_t needle, size_t size) noexcept
59
{
Rosen Penev's avatar
Rosen Penev committed
60
	return std::wmemchr(haystack, needle, size);
61 62
}

63
[[gnu::pure]] [[gnu::nonnull]]
64
static inline const wchar_t *
65
StringFind(const wchar_t *haystack, wchar_t needle) noexcept
66 67 68 69
{
	return wcschr(haystack, needle);
}

70
[[gnu::pure]] [[gnu::nonnull]]
71
static inline wchar_t *
72
StringFind(wchar_t *haystack, wchar_t needle) noexcept
73 74 75 76
{
	return wcschr(haystack, needle);
}

77
[[gnu::pure]] [[gnu::nonnull]]
78
static inline const wchar_t *
79
StringFindLast(const wchar_t *haystack, wchar_t needle) noexcept
80 81 82 83
{
	return wcsrchr(haystack, needle);
}

84
[[gnu::pure]] [[gnu::nonnull]]
85
static inline wchar_t *
86
StringFindLast(wchar_t *haystack, wchar_t needle) noexcept
87 88 89 90
{
	return wcsrchr(haystack, needle);
}

91
[[gnu::pure]] [[gnu::nonnull]]
92 93 94 95 96 97 98 99 100 101 102 103 104 105
static inline const wchar_t *
StringFindLast(const wchar_t *haystack, wchar_t needle, size_t size) noexcept
{
	/* there's no wmemrchr() unfortunately */
	const auto *p = haystack + size;
	while (p > haystack) {
		--p;
		if (*p == needle)
			return p;
	}

	return nullptr;
}

106
[[gnu::pure]] [[gnu::nonnull]]
107 108 109 110 111 112
static inline const wchar_t *
StringFindAny(const wchar_t *haystack, const wchar_t *accept) noexcept
{
	return wcspbrk(haystack, accept);
}

113
[[gnu::nonnull]]
114
static inline void
115
UnsafeCopyString(wchar_t *dest, const wchar_t *src) noexcept
116 117 118 119
{
	wcscpy(dest, src);
}

120
[[gnu::returns_nonnull]] [[gnu::nonnull]]
121
static inline wchar_t *
122
UnsafeCopyStringP(wchar_t *dest, const wchar_t *src) noexcept
123
{
124
#if defined(_WIN32) || defined(__OpenBSD__) || defined(__NetBSD__)
125 126 127
	/* emulate wcpcpy() */
	UnsafeCopyString(dest, src);
	return dest + StringLength(dest);
128 129
#elif defined(__sun) && defined (__SVR4)
	return std::wcpcpy(dest, src);
130
#else
131
	return wcpcpy(dest, src);
132 133 134
#endif
}

135
[[gnu::pure]] [[gnu::nonnull]]
136 137 138 139 140 141
static inline int
StringCompare(const wchar_t *a, const wchar_t *b) noexcept
{
	return wcscmp(a, b);
}

142
[[gnu::pure]] [[gnu::nonnull]]
143 144 145 146 147 148
static inline int
StringCompare(const wchar_t *a, const wchar_t *b, size_t n) noexcept
{
	return wcsncmp(a, b, n);
}

149 150 151 152 153 154
/**
 * Checks whether str1 and str2 are equal.
 * @param str1 String 1
 * @param str2 String 2
 * @return True if equal, False otherwise
 */
155
[[gnu::pure]] [[gnu::nonnull]]
156
static inline bool
157
StringIsEqual(const wchar_t *str1, const wchar_t *str2) noexcept
158
{
159
	return StringCompare(str1, str2) == 0;
160 161 162 163 164
}

/**
 * Checks whether #a and #b are equal.
 */
165
[[gnu::pure]] [[gnu::nonnull]]
166
static inline bool
167
StringIsEqual(const wchar_t *a, const wchar_t *b, size_t length) noexcept
168 169 170 171
{
	return wcsncmp(a, b, length) == 0;
}

172
[[gnu::pure]] [[gnu::nonnull]]
173 174 175
static inline bool
StringIsEqualIgnoreCase(const wchar_t *a, const wchar_t *b) noexcept
{
176
#ifdef _WIN32
177
	return _wcsicmp(a, b) == 0;
178 179 180
#else
	return wcscasecmp(a, b) == 0;
#endif
181 182
}

183
[[gnu::pure]] [[gnu::nonnull]]
184 185 186 187
static inline bool
StringIsEqualIgnoreCase(const wchar_t *a, const wchar_t *b,
			size_t size) noexcept
{
188
#ifdef _WIN32
189
	return _wcsnicmp(a, b, size) == 0;
190 191
#else
	return wcsncasecmp(a, b, size) == 0;
192
#endif
193
}
194

195
[[gnu::pure]] [[gnu::nonnull]]
196 197 198 199 200 201
static inline int
StringCollate(const wchar_t *a, const wchar_t *b) noexcept
{
	return wcscoll(a, b);
}

202
[[gnu::malloc]] [[gnu::returns_nonnull]] [[gnu::nonnull]]
203
static inline wchar_t *
204
DuplicateString(const wchar_t *p) noexcept
205
{
206 207 208
#if defined(__sun) && defined (__SVR4)
	return std::wcsdup(p);
#else
209
	return wcsdup(p);
210
#endif
211 212 213
}

#endif