Removed support for 'rsrc' spec file statement.
[wine/multimedia.git] / unicode / string.c
blob7b69204c4c25ba0dbdaa2012506c4f7e10871f4f
1 /*
2 * Unicode string manipulation functions
4 * Copyright 2000 Alexandre Julliard
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, write to the Free Software
18 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
21 #include <limits.h>
23 #include "wine/unicode.h"
25 int strcmpiW( const WCHAR *str1, const WCHAR *str2 )
27 for (;;)
29 int ret = toupperW(*str1) - toupperW(*str2);
30 if (ret || !*str1) return ret;
31 str1++;
32 str2++;
36 int strncmpiW( const WCHAR *str1, const WCHAR *str2, int n )
38 int ret = 0;
39 for ( ; n > 0; n--, str1++, str2++)
40 if ((ret = toupperW(*str1) - toupperW(*str2)) || !*str1) break;
41 return ret;
44 WCHAR *strstrW( const WCHAR *str, const WCHAR *sub )
46 while (*str)
48 const WCHAR *p1 = str, *p2 = sub;
49 while (*p1 && *p2 && *p1 == *p2) { p1++; p2++; }
50 if (!*p2) return (WCHAR *)str;
51 str++;
53 return NULL;
56 /* strtolW and strtoulW implementation based on the GNU C library code */
57 /* Copyright (C) 1991,92,94,95,96,97,98,99,2000,2001 Free Software Foundation, Inc. */
59 long int strtolW( const WCHAR *nptr, WCHAR **endptr, int base )
61 int negative;
62 register unsigned long int cutoff;
63 register unsigned int cutlim;
64 register unsigned long int i;
65 register const WCHAR *s;
66 register WCHAR c;
67 const WCHAR *save, *end;
68 int overflow;
70 if (base < 0 || base == 1 || base > 36) return 0;
72 save = s = nptr;
74 /* Skip white space. */
75 while (isspaceW (*s))
76 ++s;
77 if (!*s) goto noconv;
79 /* Check for a sign. */
80 negative = 0;
81 if (*s == '-')
83 negative = 1;
84 ++s;
86 else if (*s == '+')
87 ++s;
89 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
90 if (*s == '0')
92 if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
94 s += 2;
95 base = 16;
97 else if (base == 0)
98 base = 8;
100 else if (base == 0)
101 base = 10;
103 /* Save the pointer so we can check later if anything happened. */
104 save = s;
105 end = NULL;
107 cutoff = ULONG_MAX / (unsigned long int) base;
108 cutlim = ULONG_MAX % (unsigned long int) base;
110 overflow = 0;
111 i = 0;
112 c = *s;
113 for (;c != '\0'; c = *++s)
115 if (s == end)
116 break;
117 if (c >= '0' && c <= '9')
118 c -= '0';
119 else if (isalphaW (c))
120 c = toupperW (c) - 'A' + 10;
121 else
122 break;
123 if ((int) c >= base)
124 break;
125 /* Check for overflow. */
126 if (i > cutoff || (i == cutoff && c > cutlim))
127 overflow = 1;
128 else
130 i *= (unsigned long int) base;
131 i += c;
135 /* Check if anything actually happened. */
136 if (s == save)
137 goto noconv;
139 /* Store in ENDPTR the address of one character
140 past the last character we converted. */
141 if (endptr != NULL)
142 *endptr = (WCHAR *)s;
144 /* Check for a value that is within the range of
145 `unsigned LONG int', but outside the range of `LONG int'. */
146 if (overflow == 0
147 && i > (negative
148 ? -((unsigned long int) (LONG_MIN + 1)) + 1
149 : (unsigned long int) LONG_MAX))
150 overflow = 1;
152 if (overflow)
154 return negative ? LONG_MIN : LONG_MAX;
157 /* Return the result of the appropriate sign. */
158 return negative ? -i : i;
160 noconv:
161 /* We must handle a special case here: the base is 0 or 16 and the
162 first two characters are '0' and 'x', but the rest are no
163 hexadecimal digits. This is no error case. We return 0 and
164 ENDPTR points to the `x`. */
165 if (endptr != NULL)
167 if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
168 && save[-2] == '0')
169 *endptr = (WCHAR *)&save[-1];
170 else
171 /* There was no number to convert. */
172 *endptr = (WCHAR *)nptr;
175 return 0L;
179 unsigned long int strtoulW( const WCHAR *nptr, WCHAR **endptr, int base )
181 int negative;
182 register unsigned long int cutoff;
183 register unsigned int cutlim;
184 register unsigned long int i;
185 register const WCHAR *s;
186 register WCHAR c;
187 const WCHAR *save, *end;
188 int overflow;
190 if (base < 0 || base == 1 || base > 36) return 0;
192 save = s = nptr;
194 /* Skip white space. */
195 while (isspaceW (*s))
196 ++s;
197 if (!*s) goto noconv;
199 /* Check for a sign. */
200 negative = 0;
201 if (*s == '-')
203 negative = 1;
204 ++s;
206 else if (*s == '+')
207 ++s;
209 /* Recognize number prefix and if BASE is zero, figure it out ourselves. */
210 if (*s == '0')
212 if ((base == 0 || base == 16) && toupperW(s[1]) == 'X')
214 s += 2;
215 base = 16;
217 else if (base == 0)
218 base = 8;
220 else if (base == 0)
221 base = 10;
223 /* Save the pointer so we can check later if anything happened. */
224 save = s;
225 end = NULL;
227 cutoff = ULONG_MAX / (unsigned long int) base;
228 cutlim = ULONG_MAX % (unsigned long int) base;
230 overflow = 0;
231 i = 0;
232 c = *s;
233 for (;c != '\0'; c = *++s)
235 if (s == end)
236 break;
237 if (c >= '0' && c <= '9')
238 c -= '0';
239 else if (isalphaW (c))
240 c = toupperW (c) - 'A' + 10;
241 else
242 break;
243 if ((int) c >= base)
244 break;
245 /* Check for overflow. */
246 if (i > cutoff || (i == cutoff && c > cutlim))
247 overflow = 1;
248 else
250 i *= (unsigned long int) base;
251 i += c;
255 /* Check if anything actually happened. */
256 if (s == save)
257 goto noconv;
259 /* Store in ENDPTR the address of one character
260 past the last character we converted. */
261 if (endptr != NULL)
262 *endptr = (WCHAR *)s;
264 if (overflow)
266 return ULONG_MAX;
269 /* Return the result of the appropriate sign. */
270 return negative ? -i : i;
272 noconv:
273 /* We must handle a special case here: the base is 0 or 16 and the
274 first two characters are '0' and 'x', but the rest are no
275 hexadecimal digits. This is no error case. We return 0 and
276 ENDPTR points to the `x`. */
277 if (endptr != NULL)
279 if (save - nptr >= 2 && toupperW (save[-1]) == 'X'
280 && save[-2] == '0')
281 *endptr = (WCHAR *)&save[-1];
282 else
283 /* There was no number to convert. */
284 *endptr = (WCHAR *)nptr;
287 return 0L;