Merge branch 'gallium-0.1' into gallium-tex-surfaces
[mesa.git] / src / gallium / auxiliary / util / u_string.h
1 /**************************************************************************
2 *
3 * Copyright 2008 Tungsten Graphics, Inc., Cedar Park, Texas.
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 TUNGSTEN GRAPHICS 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 <jrfonseca@tungstengraphics.com>
33 */
34
35 #ifndef U_STRING_H_
36 #define U_STRING_H_
37
38 #ifndef WIN32
39 #include <stdio.h>
40 #endif
41 #include <stddef.h>
42 #include <stdarg.h>
43
44 #include "pipe/p_compiler.h"
45
46
47 #ifdef __cplusplus
48 extern "C" {
49 #endif
50
51
52 #ifdef WIN32
53
54 int util_vsnprintf(char *, size_t, const char *, va_list);
55 int util_snprintf(char *str, size_t size, const char *format, ...);
56
57 static INLINE void
58 util_sprintf(char *str, const char *format, ...)
59 {
60 va_list ap;
61 va_start(ap, format);
62 util_vsnprintf(str, (size_t)-1, format, ap);
63 va_end(ap);
64 }
65
66 static INLINE char *
67 util_strchr(const char *s, char c)
68 {
69 while(*s) {
70 if(*s == c)
71 return (char *)s;
72 ++s;
73 }
74 return NULL;
75 }
76
77 static INLINE char*
78 util_strncat(char *dst, const char *src, size_t n)
79 {
80 char *p = dst + strlen(dst);
81 const char *q = src;
82 size_t i;
83
84 for (i = 0; i < n && *q != '\0'; ++i)
85 *p++ = *q++;
86 *p = '\0';
87
88 return dst;
89 }
90
91 static INLINE int
92 util_strcmp(const char *s1, const char *s2)
93 {
94 unsigned char u1, u2;
95
96 while (1) {
97 u1 = (unsigned char) *s1++;
98 u2 = (unsigned char) *s2++;
99 if (u1 != u2)
100 return u1 - u2;
101 if (u1 == '\0')
102 return 0;
103 }
104 return 0;
105 }
106
107 static INLINE int
108 util_strncmp(const char *s1, const char *s2, size_t n)
109 {
110 unsigned char u1, u2;
111
112 while (n-- > 0) {
113 u1 = (unsigned char) *s1++;
114 u2 = (unsigned char) *s2++;
115 if (u1 != u2)
116 return u1 - u2;
117 if (u1 == '\0')
118 return 0;
119 }
120 return 0;
121 }
122
123 static INLINE char *
124 util_strstr(const char *haystack, const char *needle)
125 {
126 const char *p = haystack;
127 int len = strlen(needle);
128
129 for (; (p = util_strchr(p, *needle)) != 0; p++) {
130 if (util_strncmp(p, needle, len) == 0) {
131 return (char *)p;
132 }
133 }
134 return NULL;
135 }
136
137 static INLINE void *
138 util_memmove(void *dest, const void *src, size_t n)
139 {
140 char *p = (char *)dest;
141 const char *q = (const char *)src;
142 if (dest < src) {
143 while (n--)
144 *p++ = *q++;
145 }
146 else
147 {
148 p += n;
149 q += n;
150 while (n--)
151 *--p = *--q;
152 }
153 return dest;
154 }
155
156
157 #else
158
159 #define util_vsnprintf vsnprintf
160 #define util_snprintf snprintf
161 #define util_sprintf sprintf
162 #define util_strchr strchr
163 #define util_strcmp strcmp
164 #define util_strncmp strncmp
165 #define util_strncat strncat
166 #define util_strstr strstr
167 #define util_memmove memmove
168
169 #endif
170
171
172 #ifdef __cplusplus
173 }
174 #endif
175
176 #endif /* U_STRING_H_ */