util: include string.h in u_string.h
[mesa.git] / src / util / u_string.h
1 /**************************************************************************
2 *
3 * Copyright 2008 VMware, Inc.
4 * All Rights Reserved.
5 *
6 * Permission is hereby granted, free of charge, to any person obtaining a
7 * copy of this software and associated documentation files (the
8 * "Software"), to deal in the Software without restriction, including
9 * without limitation the rights to use, copy, modify, merge, publish,
10 * distribute, sub license, and/or sell copies of the Software, and to
11 * permit persons to whom the Software is furnished to do so, subject to
12 * the following conditions:
13 *
14 * The above copyright notice and this permission notice (including the
15 * next paragraph) shall be included in all copies or substantial portions
16 * of the Software.
17 *
18 * THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS
19 * OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
20 * MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NON-INFRINGEMENT.
21 * IN NO EVENT SHALL VMWARE AND/OR ITS SUPPLIERS BE LIABLE FOR
22 * ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT,
23 * TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE
24 * SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
25 *
26 **************************************************************************/
27
28 /**
29 * @file
30 * Platform independent functions for string manipulation.
31 *
32 * @author Jose Fonseca <jfonseca@vmware.com>
33 */
34
35 #ifndef U_STRING_H_
36 #define U_STRING_H_
37
38 #if !defined(XF86_LIBC_H)
39 #include <stdio.h>
40 #endif
41 #include <stdlib.h>
42 #include <stddef.h>
43 #include <stdarg.h>
44 #include <string.h>
45
46 #include "util/macros.h" // PRINTFLIKE
47
48
49 #ifdef __cplusplus
50 extern "C" {
51 #endif
52
53 #ifdef _GNU_SOURCE
54
55 #define util_strchrnul strchrnul
56
57 #else
58
59 static inline char *
60 util_strchrnul(const char *s, char c)
61 {
62 for (; *s && *s != c; ++s);
63
64 return (char *)s;
65 }
66
67 #endif
68
69 #ifdef _WIN32
70
71 static inline int
72 util_vsnprintf(char *str, size_t size, const char *format, va_list ap)
73 {
74 /* We need to use _vscprintf to calculate the length as vsnprintf returns -1
75 * if the number of characters to write is greater than count.
76 */
77 va_list ap_copy;
78 int ret;
79 va_copy(ap_copy, ap);
80 ret = _vsnprintf(str, size, format, ap);
81 if (ret < 0) {
82 ret = _vscprintf(format, ap_copy);
83 }
84 return ret;
85 }
86
87 static inline int
88 PRINTFLIKE(3, 4)
89 util_snprintf(char *str, size_t size, const char *format, ...)
90 {
91 va_list ap;
92 int ret;
93 va_start(ap, format);
94 ret = util_vsnprintf(str, size, format, ap);
95 va_end(ap);
96 return ret;
97 }
98
99 static inline void
100 util_vsprintf(char *str, const char *format, va_list ap)
101 {
102 util_vsnprintf(str, (size_t)-1, format, ap);
103 }
104
105 static inline void
106 PRINTFLIKE(2, 3)
107 util_sprintf(char *str, const char *format, ...)
108 {
109 va_list ap;
110 va_start(ap, format);
111 util_vsnprintf(str, (size_t)-1, format, ap);
112 va_end(ap);
113 }
114
115 static inline int
116 util_vasprintf(char **ret, const char *format, va_list ap)
117 {
118 va_list ap_copy;
119
120 /* Compute length of output string first */
121 va_copy(ap_copy, ap);
122 int r = util_vsnprintf(NULL, 0, format, ap);
123 va_end(ap_copy);
124
125 if (r < 0)
126 return -1;
127
128 *ret = (char *) malloc(r + 1);
129 if (!ret)
130 return -1;
131
132 /* Print to buffer */
133 return util_vsnprintf(*ret, r + 1, format, ap);
134 }
135
136 static inline char *
137 util_strchr(const char *s, char c)
138 {
139 char *p = util_strchrnul(s, c);
140
141 return *p ? p : NULL;
142 }
143
144 static inline char*
145 util_strncat(char *dst, const char *src, size_t n)
146 {
147 char *p = dst + strlen(dst);
148 const char *q = src;
149 size_t i;
150
151 for (i = 0; i < n && *q != '\0'; ++i)
152 *p++ = *q++;
153 *p = '\0';
154
155 return dst;
156 }
157
158 static inline int
159 util_strcmp(const char *s1, const char *s2)
160 {
161 unsigned char u1, u2;
162
163 while (1) {
164 u1 = (unsigned char) *s1++;
165 u2 = (unsigned char) *s2++;
166 if (u1 != u2)
167 return u1 - u2;
168 if (u1 == '\0')
169 return 0;
170 }
171 return 0;
172 }
173
174 static inline int
175 util_strncmp(const char *s1, const char *s2, size_t n)
176 {
177 unsigned char u1, u2;
178
179 while (n-- > 0) {
180 u1 = (unsigned char) *s1++;
181 u2 = (unsigned char) *s2++;
182 if (u1 != u2)
183 return u1 - u2;
184 if (u1 == '\0')
185 return 0;
186 }
187 return 0;
188 }
189
190 static inline char *
191 util_strstr(const char *haystack, const char *needle)
192 {
193 const char *p = haystack;
194 size_t len = strlen(needle);
195
196 for (; (p = util_strchr(p, *needle)) != 0; p++) {
197 if (util_strncmp(p, needle, len) == 0) {
198 return (char *)p;
199 }
200 }
201 return NULL;
202 }
203
204
205 #define util_strcasecmp stricmp
206
207 #else
208
209 #define util_vsnprintf vsnprintf
210 #define util_snprintf snprintf
211 #define util_vsprintf vsprintf
212 #define util_vasprintf vasprintf
213 #define util_sprintf sprintf
214 #define util_strchr strchr
215 #define util_strcmp strcmp
216 #define util_strncmp strncmp
217 #define util_strncat strncat
218 #define util_strstr strstr
219 #define util_strcasecmp strcasecmp
220
221 #endif
222
223
224 #ifdef __cplusplus
225 }
226 #endif
227
228 #endif /* U_STRING_H_ */