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