MPD  0.20.15
StringAPI.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 STRING_API_HXX
31 #define STRING_API_HXX
32 
33 #include "Compiler.h"
34 
35 #include <string.h>
36 
37 #ifdef _UNICODE
38 #include "WStringAPI.hxx"
39 #endif
40 
42 static inline size_t
43 StringLength(const char *p) noexcept
44 {
45  return strlen(p);
46 }
47 
49 static inline const char *
50 StringFind(const char *haystack, const char *needle) noexcept
51 {
52  return strstr(haystack, needle);
53 }
54 
56 static inline char *
57 StringFind(char *haystack, char needle, size_t size) noexcept
58 {
59  return (char *)memchr(haystack, needle, size);
60 }
61 
63 static inline const char *
64 StringFind(const char *haystack, char needle, size_t size) noexcept
65 {
66  return (const char *)memchr(haystack, needle, size);
67 }
68 
70 static inline const char *
71 StringFind(const char *haystack, char needle) noexcept
72 {
73  return strchr(haystack, needle);
74 }
75 
77 static inline char *
78 StringFind(char *haystack, char needle) noexcept
79 {
80  return strchr(haystack, needle);
81 }
82 
84 static inline const char *
85 StringFindLast(const char *haystack, char needle) noexcept
86 {
87  return strrchr(haystack, needle);
88 }
89 
91 static inline char *
92 StringFindLast(char *haystack, char needle) noexcept
93 {
94  return strrchr(haystack, needle);
95 }
96 
98 static inline void
99 UnsafeCopyString(char *dest, const char *src) noexcept
100 {
101  strcpy(dest, src);
102 }
103 
105 static inline char *
106 UnsafeCopyStringP(char *dest, const char *src) noexcept
107 {
108 #if defined(_WIN32) || defined(__BIONIC__)
109  /* emulate stpcpy() */
110  UnsafeCopyString(dest, src);
111  return dest + StringLength(dest);
112 #else
113  return stpcpy(dest, src);
114 #endif
115 }
116 
121 static inline bool
122 StringIsEqual(const char *a, const char *b) noexcept
123 {
124  return strcmp(a, b) == 0;
125 }
126 
131 static inline bool
132 StringIsEqual(const char *a, const char *b, size_t length) noexcept
133 {
134  return strncmp(a, b, length) == 0;
135 }
136 
142 static inline char *
143 DuplicateString(const char *p)
144 {
145  return strdup(p);
146 }
147 
148 #endif
static gcc_nonnull_all char * UnsafeCopyStringP(char *dest, const char *src) noexcept
Definition: StringAPI.hxx:106
#define gcc_nonnull_all
Definition: Compiler.h:122
gcc_malloc static gcc_nonnull_all char * DuplicateString(const char *p)
Copy the string to a new allocation.
Definition: StringAPI.hxx:143
#define gcc_malloc
Definition: Compiler.h:112
static gcc_nonnull_all void UnsafeCopyString(char *dest, const char *src) noexcept
Definition: StringAPI.hxx:99
gcc_pure static gcc_nonnull_all const char * StringFindLast(const char *haystack, char needle) noexcept
Definition: StringAPI.hxx:85
gcc_pure static gcc_nonnull_all const char * StringFind(const char *haystack, const char *needle) noexcept
Definition: StringAPI.hxx:50
gcc_pure static gcc_nonnull_all size_t StringLength(const char *p) noexcept
Definition: StringAPI.hxx:43
#define gcc_pure
Definition: Compiler.h:116
gcc_pure static gcc_nonnull_all bool StringIsEqual(const char *a, const char *b) noexcept
Checks whether #a and #b are equal.
Definition: StringAPI.hxx:122