2 * NTDLL string functions
4 * Copyright 2000 Alexandre Julliard
5 * Copyright 2000 Jon Griffiths
6 * Copyright 2003 Thomas Mertes
8 * This library is free software; you can redistribute it and/or
9 * modify it under the terms of the GNU Lesser General Public
10 * License as published by the Free Software Foundation; either
11 * version 2.1 of the License, or (at your option) any later version.
13 * This library is distributed in the hope that it will be useful,
14 * but WITHOUT ANY WARRANTY; without even the implied warranty of
15 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
16 * Lesser General Public License for more details.
18 * You should have received a copy of the GNU Lesser General Public
19 * License along with this library; if not, write to the Free Software
20 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
36 /*********************************************************************
39 * Compare two blocks of memory as strings, ignoring case.
42 * s1 [I] First string to compare to s2
43 * s2 [I] Second string to compare to s1
44 * len [I] Number of bytes to compare
47 * An integer less than, equal to, or greater than zero indicating that
48 * s1 is less than, equal to or greater than s2 respectively.
51 * Any Nul characters in s1 or s2 are ignored. This function always
52 * compares up to len bytes or the first place where s1 and s2 differ.
54 INT __cdecl
NTDLL__memicmp( LPCSTR s1
, LPCSTR s2
, DWORD len
)
59 if ((ret
= tolower(*s1
) - tolower(*s2
))) break;
67 /*********************************************************************
70 * Convert a string to upper case.
73 * str [I/O] String to convert
76 * str. There is no error return, if str is NULL or invalid, this
77 * function will crash.
79 LPSTR __cdecl
_strupr( LPSTR str
)
82 for ( ; *str
; str
++) *str
= toupper(*str
);
87 /*********************************************************************
90 * Convert a string to lowercase
93 * str [I/O] String to convert
96 * str. There is no error return, if str is NULL or invalid, this
97 * function will crash.
99 LPSTR __cdecl
_strlwr( LPSTR str
)
102 for ( ; *str
; str
++) *str
= tolower(*str
);
107 /*********************************************************************
110 * Convert an unsigned long integer to a string.
116 * - Converts value to a Nul terminated string which is copied to str.
117 * - The maximum length of the copied str is 33 bytes.
118 * - Does not check if radix is in the range of 2 to 36.
119 * - If str is NULL it crashes, as the native function does.
121 char * __cdecl
_ultoa(
122 unsigned long value
, /* [I] Value to be converted */
123 char *str
, /* [O] Destination for the converted value */
124 int radix
) /* [I] Number base for conversion */
134 digit
= value
% radix
;
135 value
= value
/ radix
;
137 *--pos
= '0' + digit
;
139 *--pos
= 'a' + digit
- 10;
141 } while (value
!= 0L);
143 memcpy(str
, pos
, &buffer
[32] - pos
+ 1);
148 /*********************************************************************
151 * Convert a long integer to a string.
157 * - Converts value to a Nul terminated string which is copied to str.
158 * - The maximum length of the copied str is 33 bytes. If radix
159 * is 10 and value is negative, the value is converted with sign.
160 * - Does not check if radix is in the range of 2 to 36.
161 * - If str is NULL it crashes, as the native function does.
163 char * __cdecl
_ltoa(
164 long value
, /* [I] Value to be converted */
165 char *str
, /* [O] Destination for the converted value */
166 int radix
) /* [I] Number base for conversion */
174 if (value
< 0 && radix
== 10) {
189 *--pos
= '0' + digit
;
191 *--pos
= 'a' + digit
- 10;
199 memcpy(str
, pos
, &buffer
[32] - pos
+ 1);
204 /*********************************************************************
207 * Converts an integer to a string.
213 * - Converts value to a '\0' terminated string which is copied to str.
214 * - The maximum length of the copied str is 33 bytes. If radix
215 * is 10 and value is negative, the value is converted with sign.
216 * - Does not check if radix is in the range of 2 to 36.
217 * - If str is NULL it crashes, as the native function does.
219 char * __cdecl
_itoa(
220 int value
, /* [I] Value to be converted */
221 char *str
, /* [O] Destination for the converted value */
222 int radix
) /* [I] Number base for conversion */
224 return _ltoa(value
, str
, radix
);
228 /*********************************************************************
231 * Converts a large unsigned integer to a string.
237 * - Converts value to a '\0' terminated string which is copied to str.
238 * - The maximum length of the copied str is 65 bytes.
239 * - Does not check if radix is in the range of 2 to 36.
240 * - If str is NULL it crashes, as the native function does.
242 char * __cdecl
_ui64toa(
243 ULONGLONG value
, /* [I] Value to be converted */
244 char *str
, /* [O] Destination for the converted value */
245 int radix
) /* [I] Number base for conversion */
255 digit
= value
% radix
;
256 value
= value
/ radix
;
258 *--pos
= '0' + digit
;
260 *--pos
= 'a' + digit
- 10;
262 } while (value
!= 0L);
264 memcpy(str
, pos
, &buffer
[64] - pos
+ 1);
269 /*********************************************************************
272 * Converts a large integer to a string.
278 * - Converts value to a Nul terminated string which is copied to str.
279 * - The maximum length of the copied str is 65 bytes. If radix
280 * is 10 and value is negative, the value is converted with sign.
281 * - Does not check if radix is in the range of 2 to 36.
282 * - If str is NULL it crashes, as the native function does.
285 * - The native DLL converts negative values (for base 10) wrong:
286 *| -1 is converted to -18446744073709551615
287 *| -2 is converted to -18446744073709551614
288 *| -9223372036854775807 is converted to -9223372036854775809
289 *| -9223372036854775808 is converted to -9223372036854775808
290 * The native msvcrt _i64toa function and our ntdll _i64toa function
291 * do not have this bug.
293 char * __cdecl
_i64toa(
294 LONGLONG value
, /* [I] Value to be converted */
295 char *str
, /* [O] Destination for the converted value */
296 int radix
) /* [I] Number base for conversion */
304 if (value
< 0 && radix
== 10) {
319 *--pos
= '0' + digit
;
321 *--pos
= 'a' + digit
- 10;
329 memcpy(str
, pos
, &buffer
[64] - pos
+ 1);
334 /*********************************************************************
337 * Convert a string to a large integer.
340 * str [I] String to be converted
343 * Success: The integer value represented by str.
344 * Failure: 0. Note that this cannot be distinguished from a successful
345 * return, if the string contains "0".
348 * - Accepts: {whitespace} [+|-] {digits}
349 * - No check is made for value overflow, only the lower 64 bits are assigned.
350 * - If str is NULL it crashes, as the native function does.
352 LONGLONG __cdecl
_atoi64( char *str
)
354 ULONGLONG RunningTotal
= 0;
357 while (*str
== ' ' || (*str
>= '\011' && *str
<= '\015')) {
363 } else if (*str
== '-') {
368 while (*str
>= '0' && *str
<= '9') {
369 RunningTotal
= RunningTotal
* 10 + *str
- '0';
373 return bMinus
? -RunningTotal
: RunningTotal
;
377 /*********************************************************************
378 * _splitpath (NTDLL.@)
380 * Split a path into its component pieces.
383 * inpath [I] Path to split
384 * drv [O] Destination for drive component (e.g. "A:"). Must be at least 3 characters.
385 * dir [O] Destination for directory component. Should be at least MAX_PATH characters.
386 * fname [O] Destination for File name component. Should be at least MAX_PATH characters.
387 * ext [O] Destination for file extension component. Should be at least MAX_PATH characters.
389 void __cdecl
_splitpath(const char* inpath
, char * drv
, char * dir
,
390 char* fname
, char * ext
)
394 if (inpath
[0] && inpath
[1] == ':')
404 else if (drv
) drv
[0] = 0;
406 /* look for end of directory part */
408 for (p
= inpath
; *p
; p
++) if (*p
== '/' || *p
== '\\') end
= p
+ 1;
410 if (end
) /* got a directory */
414 memcpy( dir
, inpath
, end
- inpath
);
415 dir
[end
- inpath
] = 0;
419 else if (dir
) dir
[0] = 0;
421 /* look for extension: what's after the last dot */
423 for (p
= inpath
; *p
; p
++) if (*p
== '.') end
= p
;
425 if (!end
) end
= p
; /* there's no extension */
429 memcpy( fname
, inpath
, end
- inpath
);
430 fname
[end
- inpath
] = 0;
432 if (ext
) strcpy( ext
, end
);