MPD  0.20.18
WStringAPI.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2017 Max Kellermann <max.kellermann@gmail.com>
3  *
4  * Redistribution and use in source and binary forms, with or without
5  * modification, are permitted provided that the following conditions
6  * are met:
7  *
8  * - Redistributions of source code must retain the above copyright
9  * notice, this list of conditions and the following disclaimer.
10  *
11  * - Redistributions in binary form must reproduce the above copyright
12  * notice, this list of conditions and the following disclaimer in the
13  * documentation and/or other materials provided with the
14  * distribution.
15  *
16  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
17  * ``AS IS'' AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
18  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
19  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
20  * FOUNDATION OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
21  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES
22  * (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR
23  * SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION)
24  * HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
25  * STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
26  * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED
27  * OF THE POSSIBILITY OF SUCH DAMAGE.
28  */
29 
30 #ifndef WSTRING_API_HXX
31 #define WSTRING_API_HXX
32 
33 #include "Compiler.h"
34 
35 #include <wchar.h>
36 
38 static inline size_t
39 StringLength(const wchar_t *p) noexcept
40 {
41  return wcslen(p);
42 }
43 
45 static inline const wchar_t *
46 StringFind(const wchar_t *haystack, const wchar_t *needle) noexcept
47 {
48  return wcsstr(haystack, needle);
49 }
50 
52 static inline const wchar_t *
53 StringFind(const wchar_t *haystack, wchar_t needle, size_t size) noexcept
54 {
55  return wmemchr(haystack, needle, size);
56 }
57 
59 static inline wchar_t *
60 StringFind(wchar_t *haystack, wchar_t needle, size_t size) noexcept
61 {
62  return wmemchr(haystack, needle, size);
63 }
64 
66 static inline const wchar_t *
67 StringFind(const wchar_t *haystack, wchar_t needle) noexcept
68 {
69  return wcschr(haystack, needle);
70 }
71 
73 static inline wchar_t *
74 StringFind(wchar_t *haystack, wchar_t needle) noexcept
75 {
76  return wcschr(haystack, needle);
77 }
78 
80 static inline const wchar_t *
81 StringFindLast(const wchar_t *haystack, wchar_t needle) noexcept
82 {
83  return wcsrchr(haystack, needle);
84 }
85 
87 static inline wchar_t *
88 StringFindLast(wchar_t *haystack, wchar_t needle) noexcept
89 {
90  return wcsrchr(haystack, needle);
91 }
92 
94 static inline void
95 UnsafeCopyString(wchar_t *dest, const wchar_t *src) noexcept
96 {
97  wcscpy(dest, src);
98 }
99 
101 static inline wchar_t *
102 UnsafeCopyStringP(wchar_t *dest, const wchar_t *src) noexcept
103 {
104 #if defined(_WIN32) || defined(__BIONIC__) || defined(__OpenBSD__) || \
105  defined(__NetBSD__)
106  /* emulate wcpcpy() */
107  UnsafeCopyString(dest, src);
108  return dest + StringLength(dest);
109 #elif defined(__sun) && defined (__SVR4)
110  return std::wcpcpy(dest, src);
111 #else
112  return wcpcpy(dest, src);
113 #endif
114 }
115 
123 static inline bool
124 StringIsEqual(const wchar_t *str1, const wchar_t *str2) noexcept
125 {
126  return wcscmp(str1, str2) == 0;
127 }
128 
133 static inline bool
134 StringIsEqual(const wchar_t *a, const wchar_t *b, size_t length) noexcept
135 {
136  return wcsncmp(a, b, length) == 0;
137 }
138 
139 #ifndef __BIONIC__
140 
142 static inline wchar_t *
143 DuplicateString(const wchar_t *p)
144 {
145 #if defined(__sun) && defined (__SVR4)
146  return std::wcsdup(p);
147 #else
148  return wcsdup(p);
149 #endif
150 }
151 
152 #endif
153 
154 #endif
#define gcc_nonnull_all
Definition: Compiler.h:122
gcc_pure static gcc_nonnull_all bool StringIsEqual(const wchar_t *str1, const wchar_t *str2) noexcept
Checks whether str1 and str2 are equal.
Definition: WStringAPI.hxx:124
gcc_pure static gcc_nonnull_all const wchar_t * StringFind(const wchar_t *haystack, const wchar_t *needle) noexcept
Definition: WStringAPI.hxx:46
gcc_pure static gcc_nonnull_all const wchar_t * StringFindLast(const wchar_t *haystack, wchar_t needle) noexcept
Definition: WStringAPI.hxx:81
#define gcc_malloc
Definition: Compiler.h:112
static gcc_nonnull_all wchar_t * UnsafeCopyStringP(wchar_t *dest, const wchar_t *src) noexcept
Definition: WStringAPI.hxx:102
#define gcc_pure
Definition: Compiler.h:116
gcc_pure static gcc_nonnull_all size_t StringLength(const wchar_t *p) noexcept
Definition: WStringAPI.hxx:39
static gcc_nonnull_all void UnsafeCopyString(wchar_t *dest, const wchar_t *src) noexcept
Definition: WStringAPI.hxx:95
gcc_malloc static gcc_nonnull_all wchar_t * DuplicateString(const wchar_t *p)
Definition: WStringAPI.hxx:143