MPD  0.20.6
WStringAPI.hxx
Go to the documentation of this file.
1 /*
2  * Copyright (C) 2010-2015 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)
40 {
41  return wcslen(p);
42 }
43 
45 static inline const wchar_t *
46 StringFind(const wchar_t *haystack, const wchar_t *needle)
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)
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)
61 {
62  return wmemchr(haystack, needle, size);
63 }
64 
66 static inline const wchar_t *
67 StringFind(const wchar_t *haystack, wchar_t needle)
68 {
69  return wcschr(haystack, needle);
70 }
71 
73 static inline wchar_t *
74 StringFind(wchar_t *haystack, wchar_t needle)
75 {
76  return wcschr(haystack, needle);
77 }
78 
80 static inline const wchar_t *
81 StringFindLast(const wchar_t *haystack, wchar_t needle)
82 {
83  return wcsrchr(haystack, needle);
84 }
85 
87 static inline wchar_t *
88 StringFindLast(wchar_t *haystack, wchar_t needle)
89 {
90  return wcsrchr(haystack, needle);
91 }
92 
94 static inline void
95 UnsafeCopyString(wchar_t *dest, const wchar_t *src)
96 {
97  wcscpy(dest, src);
98 }
99 
101 static inline wchar_t *
102 UnsafeCopyStringP(wchar_t *dest, const wchar_t *src)
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 #else
110  return wcpcpy(dest, src);
111 #endif
112 }
113 
121 static inline bool
122 StringIsEqual(const wchar_t *str1, const wchar_t *str2)
123 {
124  return wcscmp(str1, str2) == 0;
125 }
126 
131 static inline bool
132 StringIsEqual(const wchar_t *a, const wchar_t *b, size_t length)
133 {
134  return wcsncmp(a, b, length) == 0;
135 }
136 
137 #ifndef __BIONIC__
138 
140 static inline wchar_t *
141 DuplicateString(const wchar_t *p)
142 {
143  return wcsdup(p);
144 }
145 
146 #endif
147 
148 #endif
gcc_pure static gcc_nonnull_all size_t StringLength(const wchar_t *p)
Definition: WStringAPI.hxx:39
static gcc_nonnull_all wchar_t * UnsafeCopyStringP(wchar_t *dest, const wchar_t *src)
Definition: WStringAPI.hxx:102
#define gcc_nonnull_all
Definition: Compiler.h:122
#define gcc_malloc
Definition: Compiler.h:112
gcc_pure static gcc_nonnull_all const wchar_t * StringFind(const wchar_t *haystack, const wchar_t *needle)
Definition: WStringAPI.hxx:46
gcc_pure static gcc_nonnull_all const wchar_t * StringFindLast(const wchar_t *haystack, wchar_t needle)
Definition: WStringAPI.hxx:81
static gcc_nonnull_all void UnsafeCopyString(wchar_t *dest, const wchar_t *src)
Definition: WStringAPI.hxx:95
#define gcc_pure
Definition: Compiler.h:116
gcc_pure static gcc_nonnull_all bool StringIsEqual(const wchar_t *str1, const wchar_t *str2)
Checks whether str1 and str2 are equal.
Definition: WStringAPI.hxx:122
gcc_malloc static gcc_nonnull_all wchar_t * DuplicateString(const wchar_t *p)
Definition: WStringAPI.hxx:141