Partially repair hotspot handling.
[wine/testsucceed.git] / memory / string.c
blobb2ac9e15773301118a5f4300095595b6df8153ed
1 /*
2 * String functions
4 * Copyright 1993 Yngvi Sigurjonsson
5 * Copyright 1996 Alexandre Julliard
6 */
8 #include <ctype.h>
9 #include <string.h>
11 #include "windef.h"
12 #include "winbase.h"
13 #include "wine/winbase16.h"
14 #include "wine/exception.h"
15 #include "wine/unicode.h"
16 #include "winerror.h"
17 #include "winnls.h"
18 #include "msvcrt/excpt.h"
19 #include "debugtools.h"
21 DEFAULT_DEBUG_CHANNEL(string);
23 /* filter for page-fault exceptions */
24 static WINE_EXCEPTION_FILTER(page_fault)
26 if (GetExceptionCode() == EXCEPTION_ACCESS_VIOLATION)
27 return EXCEPTION_EXECUTE_HANDLER;
28 return EXCEPTION_CONTINUE_SEARCH;
32 /***********************************************************************
33 * hmemcpy (KERNEL.348)
35 void WINAPI hmemcpy16( LPVOID dst, LPCVOID src, LONG count )
37 memcpy( dst, src, count );
41 /***********************************************************************
42 * lstrcat (KERNEL.89)
44 SEGPTR WINAPI lstrcat16( SEGPTR dst, LPCSTR src )
46 /* Windows does not check for NULL pointers here, so we don't either */
47 strcat( MapSL(dst), src );
48 return dst;
52 /***********************************************************************
53 * lstrcat (KERNEL32.@)
54 * lstrcatA (KERNEL32.@)
56 LPSTR WINAPI lstrcatA( LPSTR dst, LPCSTR src )
58 __TRY
60 strcat( dst, src );
62 __EXCEPT(page_fault)
64 SetLastError( ERROR_INVALID_PARAMETER );
65 return NULL;
67 __ENDTRY
68 return dst;
72 /***********************************************************************
73 * lstrcatW (KERNEL32.@)
75 LPWSTR WINAPI lstrcatW( LPWSTR dst, LPCWSTR src )
77 __TRY
79 strcatW( dst, src );
81 __EXCEPT(page_fault)
83 SetLastError( ERROR_INVALID_PARAMETER );
84 return NULL;
86 __ENDTRY
87 return dst;
91 /***********************************************************************
92 * lstrcatn (KERNEL.352)
94 SEGPTR WINAPI lstrcatn16( SEGPTR dst, LPCSTR src, INT16 n )
96 LPSTR p = MapSL(dst);
97 LPSTR start = p;
99 while (*p) p++;
100 if ((n -= (p - start)) <= 0) return dst;
101 lstrcpynA( p, src, n );
102 return dst;
106 /***********************************************************************
107 * lstrcmp (KERNEL32.@)
108 * lstrcmpA (KERNEL32.@)
110 INT WINAPI lstrcmpA( LPCSTR str1, LPCSTR str2 )
112 return CompareStringA(LOCALE_SYSTEM_DEFAULT,0,str1,-1,str2,-1) - 2 ;
116 /***********************************************************************
117 * lstrcmpW (KERNEL32.@)
118 * FIXME : should call CompareStringW, when it is implemented.
119 * This implementation is not "word sort", as it should.
121 INT WINAPI lstrcmpW( LPCWSTR str1, LPCWSTR str2 )
123 TRACE("%s and %s\n",
124 debugstr_w (str1), debugstr_w (str2));
125 if (!str1 || !str2) {
126 SetLastError(ERROR_INVALID_PARAMETER);
127 return 0;
129 while (*str1 && (*str1 == *str2)) { str1++; str2++; }
130 return (INT)(*str1 - *str2);
134 /***********************************************************************
135 * lstrcmpi (KERNEL32.@)
136 * lstrcmpiA (KERNEL32.@)
138 INT WINAPI lstrcmpiA( LPCSTR str1, LPCSTR str2 )
139 { TRACE("strcmpi %s and %s\n",
140 debugstr_a (str1), debugstr_a (str2));
141 return CompareStringA(LOCALE_SYSTEM_DEFAULT,NORM_IGNORECASE,str1,-1,str2,-1)-2;
145 /***********************************************************************
146 * lstrcmpiW (KERNEL32.@)
148 INT WINAPI lstrcmpiW( LPCWSTR str1, LPCWSTR str2 )
150 if (!str1 || !str2) {
151 SetLastError(ERROR_INVALID_PARAMETER);
152 return 0;
154 return strcmpiW( str1, str2 );
158 /***********************************************************************
159 * lstrcpy (KERNEL.88)
161 SEGPTR WINAPI lstrcpy16( SEGPTR dst, LPCSTR src )
163 if (!lstrcpyA( MapSL(dst), src )) dst = 0;
164 return dst;
168 /***********************************************************************
169 * lstrcpy (KERNEL32.@)
170 * lstrcpyA (KERNEL32.@)
172 LPSTR WINAPI lstrcpyA( LPSTR dst, LPCSTR src )
174 __TRY
176 /* this is how Windows does it */
177 memmove( dst, src, strlen(src)+1 );
179 __EXCEPT(page_fault)
181 ERR("(%p, %p): page fault occurred ! Caused by bug ?\n", dst, src);
182 SetLastError( ERROR_INVALID_PARAMETER );
183 return NULL;
185 __ENDTRY
186 return dst;
190 /***********************************************************************
191 * lstrcpyW (KERNEL32.@)
193 LPWSTR WINAPI lstrcpyW( LPWSTR dst, LPCWSTR src )
195 __TRY
197 strcpyW( dst, src );
199 __EXCEPT(page_fault)
201 SetLastError( ERROR_INVALID_PARAMETER );
202 return NULL;
204 __ENDTRY
205 return dst;
209 /***********************************************************************
210 * lstrcpyn (KERNEL.353)
212 SEGPTR WINAPI lstrcpyn16( SEGPTR dst, LPCSTR src, INT16 n )
214 lstrcpynA( MapSL(dst), src, n );
215 return dst;
219 /***********************************************************************
220 * lstrcpyn (KERNEL32.@)
221 * lstrcpynA (KERNEL32.@)
223 * Note: this function differs from the UNIX strncpy, it _always_ writes
224 * a terminating \0
226 LPSTR WINAPI lstrcpynA( LPSTR dst, LPCSTR src, INT n )
228 LPSTR p = dst;
229 TRACE("(%p, %s, %i)\n", dst, debugstr_an(src,n), n);
230 /* In real windows the whole function is protected by an exception handler
231 * that returns ERROR_INVALID_PARAMETER on faulty parameters
232 * We currently just check for NULL.
234 if (!dst || !src) {
235 SetLastError(ERROR_INVALID_PARAMETER);
236 return 0;
238 while ((n-- > 1) && *src) *p++ = *src++;
239 if (n >= 0) *p = 0;
240 return dst;
244 /***********************************************************************
245 * lstrcpynW (KERNEL32.@)
246 * Note: this function differs from the UNIX strncpy, it _always_ writes
247 * a terminating \0
249 LPWSTR WINAPI lstrcpynW( LPWSTR dst, LPCWSTR src, INT n )
251 LPWSTR p = dst;
252 TRACE("(%p, %s, %i)\n", dst, debugstr_wn(src,n), n);
253 /* In real windows the whole function is protected by an exception handler
254 * that returns ERROR_INVALID_PARAMETER on faulty parameters
255 * We currently just check for NULL.
257 if (!dst || !src) {
258 SetLastError(ERROR_INVALID_PARAMETER);
259 return 0;
261 while ((n-- > 1) && *src) *p++ = *src++;
262 if (n >= 0) *p = 0;
263 return dst;
267 /***********************************************************************
268 * lstrlen (KERNEL.90)
270 INT16 WINAPI lstrlen16( LPCSTR str )
272 return (INT16)lstrlenA( str );
276 /***********************************************************************
277 * lstrlen (KERNEL32.@)
278 * lstrlenA (KERNEL32.@)
280 INT WINAPI lstrlenA( LPCSTR str )
282 INT ret;
283 __TRY
285 ret = strlen(str);
287 __EXCEPT(page_fault)
289 SetLastError( ERROR_INVALID_PARAMETER );
290 return 0;
292 __ENDTRY
293 return ret;
297 /***********************************************************************
298 * lstrlenW (KERNEL32.@)
300 INT WINAPI lstrlenW( LPCWSTR str )
302 INT ret;
303 __TRY
305 ret = strlenW(str);
307 __EXCEPT(page_fault)
309 SetLastError( ERROR_INVALID_PARAMETER );
310 return 0;
312 __ENDTRY
313 return ret;
317 /***********************************************************************
318 * UnicodeToAnsi (KERNEL.434)
320 INT16 WINAPI UnicodeToAnsi16( LPCWSTR src, LPSTR dst, INT16 codepage )
322 if ( codepage == -1 )
323 codepage = CP_ACP;
325 return WideCharToMultiByte( codepage, 0, src, -1, dst, 0x7fffffff, NULL, NULL );