456de63d28844b4ec4d42dd577116103d697fb64
[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 #ifndef _GNU_SOURCE
54
55 #define strchrnul util_strchrnul
56 static inline char *
57 util_strchrnul(const char *s, char c)
58 {
59 for (; *s && *s != c; ++s);
60
61 return (char *)s;
62 }
63
64 #endif
65
66 #ifdef _WIN32
67
68 static inline int
69 util_vsnprintf(char *str, size_t size, const char *format, va_list ap)
70 {
71 /* We need to use _vscprintf to calculate the length as vsnprintf returns -1
72 * if the number of characters to write is greater than count.
73 */
74 va_list ap_copy;
75 int ret;
76 va_copy(ap_copy, ap);
77 ret = _vsnprintf(str, size, format, ap);
78 if (ret < 0) {
79 ret = _vscprintf(format, ap_copy);
80 }
81 va_end(ap_copy);
82 return ret;
83 }
84
85 #define snprintf util_snprintf
86 static inline int
87 PRINTFLIKE(3, 4)
88 util_snprintf(char *str, size_t size, const char *format, ...)
89 {
90 va_list ap;
91 int ret;
92 va_start(ap, format);
93 ret = util_vsnprintf(str, size, format, ap);
94 va_end(ap);
95 return ret;
96 }
97
98 #define sprintf util_sprintf
99 static inline void
100 PRINTFLIKE(2, 3)
101 util_sprintf(char *str, const char *format, ...)
102 {
103 va_list ap;
104 va_start(ap, format);
105 util_vsnprintf(str, (size_t)-1, format, ap);
106 va_end(ap);
107 }
108
109 #define vasprintf util_vasprintf
110 static inline int
111 util_vasprintf(char **ret, const char *format, va_list ap)
112 {
113 va_list ap_copy;
114
115 /* Compute length of output string first */
116 va_copy(ap_copy, ap);
117 int r = util_vsnprintf(NULL, 0, format, ap_copy);
118 va_end(ap_copy);
119
120 if (r < 0)
121 return -1;
122
123 *ret = (char *) malloc(r + 1);
124 if (!*ret)
125 return -1;
126
127 /* Print to buffer */
128 return util_vsnprintf(*ret, r + 1, format, ap);
129 }
130
131 #define strncat util_strncat
132 static inline char*
133 util_strncat(char *dst, const char *src, size_t n)
134 {
135 char *p = dst + strlen(dst);
136 const char *q = src;
137 size_t i;
138
139 for (i = 0; i < n && *q != '\0'; ++i)
140 *p++ = *q++;
141 *p = '\0';
142
143 return dst;
144 }
145
146 #define strcmp util_strcmp
147 static inline int
148 util_strcmp(const char *s1, const char *s2)
149 {
150 unsigned char u1, u2;
151
152 while (1) {
153 u1 = (unsigned char) *s1++;
154 u2 = (unsigned char) *s2++;
155 if (u1 != u2)
156 return u1 - u2;
157 if (u1 == '\0')
158 return 0;
159 }
160 return 0;
161 }
162
163 #define strncmp util_strncmp
164 static inline int
165 util_strncmp(const char *s1, const char *s2, size_t n)
166 {
167 unsigned char u1, u2;
168
169 while (n-- > 0) {
170 u1 = (unsigned char) *s1++;
171 u2 = (unsigned char) *s2++;
172 if (u1 != u2)
173 return u1 - u2;
174 if (u1 == '\0')
175 return 0;
176 }
177 return 0;
178 }
179
180
181
182 #define strcasecmp stricmp
183 #define strdup _strdup
184
185 #else
186
187 #define util_vsnprintf vsnprintf
188
189 #endif
190
191
192 #ifdef __cplusplus
193 }
194 #endif
195
196 #endif /* U_STRING_H_ */