2 * String manipulation functions
4 * Copyright 1998 Eric Kohl
5 * 1998 Juergen Schmied <j.schmied@metronet.de>
6 * 2000 Eric Kohl for CodeWeavers
7 * Copyright 2002 Jon Griffiths
9 * This library is free software; you can redistribute it and/or
10 * modify it under the terms of the GNU Lesser General Public
11 * License as published by the Free Software Foundation; either
12 * version 2.1 of the License, or (at your option) any later version.
14 * This library is distributed in the hope that it will be useful,
15 * but WITHOUT ANY WARRANTY; without even the implied warranty of
16 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
17 * Lesser General Public License for more details.
19 * You should have received a copy of the GNU Lesser General Public
20 * License along with this library; if not, write to the Free Software
21 * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA 02111-1307 USA
27 #include <stdlib.h> /* atoi */
34 #include "wine/unicode.h"
36 #include "wine/debug.h"
38 WINE_DEFAULT_DEBUG_CHANNEL(commctrl);
40 /*************************************************************************
41 * COMCTL32_ChrCmpHelperA
43 * Internal helper for ChrCmpA/COMCTL32_ChrCmpIA.
46 * Both this function and its Unicode counterpart are very inneficient. To
47 * fix this, CompareString must be completely implemented and optimised
48 * first. Then the core character test can be taken out of that function and
49 * placed here, so that it need never be called at all. Until then, do not
50 * attempt to optimise this code unless you are willing to test that it
51 * still performs correctly.
53 static BOOL COMCTL32_ChrCmpHelperA(WORD ch1, WORD ch2, DWORD dwFlags)
55 char str1[3], str2[3];
57 str1[0] = LOBYTE(ch1);
58 if (IsDBCSLeadByte(str1[0]))
60 str1[1] = HIBYTE(ch1);
66 str2[0] = LOBYTE(ch2);
67 if (IsDBCSLeadByte(str2[0]))
69 str2[1] = HIBYTE(ch2);
75 return CompareStringA(GetThreadLocale(), dwFlags, str1, -1, str2, -1) - 2;
78 /*************************************************************************
79 * COMCTL32_ChrCmpHelperW
81 * Internal helper for COMCTL32_ChrCmpW/ChrCmpIW.
83 static BOOL COMCTL32_ChrCmpHelperW(WCHAR ch1, WCHAR ch2, DWORD dwFlags)
85 WCHAR str1[2], str2[2];
91 return CompareStringW(GetThreadLocale(), dwFlags, str1, 2, str2, 2) - 2;
94 /*************************************************************************
95 * COMCTL32_ChrCmpA (internal)
97 * Internal helper function.
99 static BOOL COMCTL32_ChrCmpA(WORD ch1, WORD ch2)
101 return COMCTL32_ChrCmpHelperA(ch1, ch2, 0);
104 /*************************************************************************
105 * COMCTL32_ChrCmpIA (internal)
107 * Compare two characters, ignoring case.
110 * ch1 [I] First character to compare
111 * ch2 [I] Second character to compare
114 * FALSE, if the characters are equal.
115 * Non-zero otherwise.
117 static BOOL COMCTL32_ChrCmpIA(WORD ch1, WORD ch2)
119 TRACE("(%d,%d)\n", ch1, ch2);
121 return COMCTL32_ChrCmpHelperA(ch1, ch2, NORM_IGNORECASE);
124 /*************************************************************************
127 * Internal helper function.
129 static BOOL COMCTL32_ChrCmpW(WCHAR ch1, WCHAR ch2)
131 return COMCTL32_ChrCmpHelperW(ch1, ch2, 0);
134 /**************************************************************************
135 * StrChrA [COMCTL32.350]
137 * Find a given character in a string.
140 * lpszStr [I] String to search in.
141 * ch [I] Character to search for.
144 * Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
146 * Failure: NULL, if any arguments are invalid.
148 LPSTR WINAPI StrChrA(LPCSTR lpszStr, WORD ch)
150 TRACE("(%s,%i)\n", debugstr_a(lpszStr), ch);
156 if (!COMCTL32_ChrCmpA(*lpszStr, ch))
157 return (LPSTR)lpszStr;
158 lpszStr = CharNextA(lpszStr);
164 /*************************************************************************
165 * COMCTL32_StrStrHelperA
167 * Internal implementation of StrStrA/StrStrIA
169 static LPSTR COMCTL32_StrStrHelperA(LPCSTR lpszStr, LPCSTR lpszSearch,
170 int (*pStrCmpFn)(LPCSTR,LPCSTR,size_t))
174 if (!lpszStr || !lpszSearch || !*lpszSearch)
177 iLen = strlen(lpszSearch);
181 if (!pStrCmpFn(lpszStr, lpszSearch, iLen))
182 return (LPSTR)lpszStr;
183 lpszStr = CharNextA(lpszStr);
188 /*************************************************************************
189 * COMCTL32_StrStrHelperW
191 * Internal implementation of StrStrW/StrStrIW
193 static LPWSTR COMCTL32_StrStrHelperW(LPCWSTR lpszStr, LPCWSTR lpszSearch,
194 int (*pStrCmpFn)(LPCWSTR,LPCWSTR,int))
198 if (!lpszStr || !lpszSearch || !*lpszSearch)
201 iLen = strlenW(lpszSearch);
205 if (!pStrCmpFn(lpszStr, lpszSearch, iLen))
206 return (LPWSTR)lpszStr;
207 lpszStr = CharNextW(lpszStr);
212 /**************************************************************************
213 * StrStrIA [COMCTL32.355]
215 * Find a substring within a string, ignoring case.
218 * lpszStr [I] String to search in
219 * lpszSearch [I] String to look for
222 * The start of lpszSearch within lpszStr, or NULL if not found.
224 LPSTR WINAPI StrStrIA(LPCSTR lpszStr, LPCSTR lpszSearch)
226 TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));
228 return COMCTL32_StrStrHelperA(lpszStr, lpszSearch, strncasecmp);
231 /**************************************************************************
232 * StrToIntA [COMCTL32.357]
234 * Read a signed integer from a string.
237 * lpszStr [I] String to read integer from
240 * The signed integer value represented by the string, or 0 if no integer is
243 INT WINAPI StrToIntA (LPSTR lpszStr)
245 return atoi(lpszStr);
248 /**************************************************************************
249 * StrStrIW [COMCTL32.363]
253 LPWSTR WINAPI StrStrIW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
255 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSearch));
257 return COMCTL32_StrStrHelperW(lpszStr, lpszSearch, strncmpiW);
260 /**************************************************************************
261 * StrToIntW [COMCTL32.365]
265 INT WINAPI StrToIntW (LPWSTR lpString)
267 return atoiW(lpString);
270 /*************************************************************************
271 * COMCTL32_StrSpnHelperA (internal)
273 * Internal implementation of StrSpnA/StrCSpnA/StrCSpnIA
275 static int COMCTL32_StrSpnHelperA(LPCSTR lpszStr, LPCSTR lpszMatch,
276 LPSTR (WINAPI *pStrChrFn)(LPCSTR,WORD),
279 LPCSTR lpszRead = lpszStr;
280 if (lpszStr && *lpszStr && lpszMatch)
284 LPCSTR lpszTest = pStrChrFn(lpszMatch, *lpszRead);
286 if (!bInvert && !lpszTest)
288 if (bInvert && lpszTest)
290 lpszRead = CharNextA(lpszRead);
293 return lpszRead - lpszStr;
296 /**************************************************************************
297 * StrCSpnA [COMCTL32.356]
299 * Find the length of the start of a string that does not contain certain
303 * lpszStr [I] String to search
304 * lpszMatch [I] Characters that cannot be in the substring
307 * The length of the part of lpszStr containing only chars not in lpszMatch,
308 * or 0 if any parameter is invalid.
310 int WINAPI StrCSpnA(LPCSTR lpszStr, LPCSTR lpszMatch)
312 TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
314 return COMCTL32_StrSpnHelperA(lpszStr, lpszMatch, StrChrA, TRUE);
317 /**************************************************************************
318 * StrChrW [COMCTL32.358]
322 LPWSTR WINAPI StrChrW(LPCWSTR lpszStr, WCHAR ch)
324 LPWSTR lpszRet = NULL;
326 TRACE("(%s,%i)\n", debugstr_w(lpszStr), ch);
329 lpszRet = strchrW(lpszStr, ch);
333 /**************************************************************************
334 * StrCmpNA [COMCTL32.352]
336 * Compare two strings, up to a maximum length.
339 * lpszStr [I] First string to compare
340 * lpszComp [I] Second string to compare
341 * iLen [I] Maximum number of chars to compare.
344 * An integer less than, equal to or greater than 0, indicating that
345 * lpszStr is less than, the same, or greater than lpszComp.
347 INT WINAPI StrCmpNA(LPCSTR lpszStr, LPCSTR lpszComp, INT iLen)
351 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);
353 iRet = CompareStringA(GetThreadLocale(), 0, lpszStr, iLen, lpszComp, iLen);
354 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
357 /**************************************************************************
358 * StrCmpNIA [COMCTL32.353]
360 * Compare two strings, up to a maximum length, ignoring case.
363 * lpszStr [I] First string to compare
364 * lpszComp [I] Second string to compare
365 * iLen [I] Maximum number of chars to compare.
368 * An integer less than, equal to or greater than 0, indicating that
369 * lpszStr is less than, the same, or greater than lpszComp.
371 int WINAPI StrCmpNIA(LPCSTR lpszStr, LPCSTR lpszComp, int iLen)
375 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);
377 iRet = CompareStringA(GetThreadLocale(), NORM_IGNORECASE, lpszStr, iLen, lpszComp, iLen);
378 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
381 /*************************************************************************
382 * StrCmpNIW [COMCTL32.361]
386 INT WINAPI StrCmpNIW(LPCWSTR lpszStr, LPCWSTR lpszComp, int iLen)
390 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr), debugstr_w(lpszComp), iLen);
392 iRet = CompareStringW(GetThreadLocale(), NORM_IGNORECASE, lpszStr, iLen, lpszComp, iLen);
393 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
396 /**************************************************************************
397 * StrCmpNW [COMCTL32.360]
401 INT WINAPI StrCmpNW(LPCWSTR lpszStr, LPCWSTR lpszComp, INT iLen)
405 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr), debugstr_w(lpszComp), iLen);
407 iRet = CompareStringW(GetThreadLocale(), 0, lpszStr, iLen, lpszComp, iLen);
408 return iRet == CSTR_LESS_THAN ? -1 : iRet == CSTR_GREATER_THAN ? 1 : 0;
411 /**************************************************************************
412 * StrRChrA [COMCTL32.351]
414 * Find the last occurence of a character in string.
417 * lpszStr [I] String to search in
418 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
419 * ch [I] Character to search for.
422 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
423 * or NULL if not found.
424 * Failure: NULL, if any arguments are invalid.
426 LPSTR WINAPI StrRChrA(LPCSTR lpszStr, LPCSTR lpszEnd, WORD ch)
428 LPCSTR lpszRet = NULL;
430 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr), debugstr_a(lpszEnd), ch);
437 lpszEnd = lpszStr + lstrlenA(lpszStr);
439 while (*lpszStr && lpszStr <= lpszEnd)
441 ch2 = IsDBCSLeadByte(*lpszStr)? *lpszStr << 8 | lpszStr[1] : *lpszStr;
443 if (!COMCTL32_ChrCmpA(ch, ch2))
445 lpszStr = CharNextA(lpszStr);
448 return (LPSTR)lpszRet;
452 /**************************************************************************
453 * StrRChrW [COMCTL32.359]
457 LPWSTR WINAPI StrRChrW(LPCWSTR lpszStr, LPCWSTR lpszEnd, WORD ch)
459 LPCWSTR lpszRet = NULL;
461 TRACE("(%s,%s,%x)\n", debugstr_w(lpszStr), debugstr_w(lpszEnd), ch);
466 lpszEnd = lpszStr + strlenW(lpszStr);
468 while (*lpszStr && lpszStr <= lpszEnd)
470 if (!COMCTL32_ChrCmpW(ch, *lpszStr))
472 lpszStr = CharNextW(lpszStr);
475 return (LPWSTR)lpszRet;
478 /**************************************************************************
479 * StrStrA [COMCTL32.354]
481 * Find a substring within a string.
484 * lpszStr [I] String to search in
485 * lpszSearch [I] String to look for
488 * The start of lpszSearch within lpszStr, or NULL if not found.
490 LPSTR WINAPI StrStrA(LPCSTR lpszStr, LPCSTR lpszSearch)
492 TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));
494 return COMCTL32_StrStrHelperA(lpszStr, lpszSearch, strncmp);
497 /**************************************************************************
498 * StrStrW [COMCTL32.362]
502 LPWSTR WINAPI StrStrW(LPCWSTR lpszStr, LPCWSTR lpszSearch)
504 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSearch));
506 return COMCTL32_StrStrHelperW(lpszStr, lpszSearch, strncmpW);
509 /*************************************************************************
510 * StrChrIA [COMCTL32.366]
512 * Find a given character in a string, ignoring case.
515 * lpszStr [I] String to search in.
516 * ch [I] Character to search for.
519 * Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
521 * Failure: NULL, if any arguments are invalid.
523 LPSTR WINAPI StrChrIA(LPCSTR lpszStr, WORD ch)
525 TRACE("(%s,%i)\n", debugstr_a(lpszStr), ch);
531 if (!COMCTL32_ChrCmpIA(*lpszStr, ch))
532 return (LPSTR)lpszStr;
533 lpszStr = CharNextA(lpszStr);
539 /*************************************************************************
540 * StrChrIW [COMCTL32.367]
544 LPWSTR WINAPI StrChrIW(LPCWSTR lpszStr, WCHAR ch)
546 TRACE("(%s,%i)\n", debugstr_w(lpszStr), ch);
553 if (toupperW(*lpszStr) == ch)
554 return (LPWSTR)lpszStr;
555 lpszStr = CharNextW(lpszStr);
559 return (LPWSTR)lpszStr;
562 /*************************************************************************
563 * StrRStrIA [COMCTL32.372]
565 * Find the last occurence of a substring within a string.
568 * lpszStr [I] String to search in
569 * lpszEnd [I] End of lpszStr
570 * lpszSearch [I] String to look for
573 * The last occurence lpszSearch within lpszStr, or NULL if not found.
575 LPSTR WINAPI StrRStrIA(LPCSTR lpszStr, LPCSTR lpszEnd, LPCSTR lpszSearch)
577 LPSTR lpszRet = NULL;
581 TRACE("(%s,%s)\n", debugstr_a(lpszStr), debugstr_a(lpszSearch));
583 if (!lpszStr || !lpszSearch || !*lpszSearch)
587 lpszEnd = lpszStr + lstrlenA(lpszStr);
589 if (IsDBCSLeadByte(*lpszSearch))
590 ch1 = *lpszSearch << 8 | lpszSearch[1];
593 iLen = lstrlenA(lpszSearch);
595 while (lpszStr <= lpszEnd && *lpszStr)
597 ch2 = IsDBCSLeadByte(*lpszStr)? *lpszStr << 8 | lpszStr[1] : *lpszStr;
598 if (!COMCTL32_ChrCmpIA(ch1, ch2))
600 if (!StrCmpNIA(lpszStr, lpszSearch, iLen))
601 lpszRet = (LPSTR)lpszStr;
603 lpszStr = CharNextA(lpszStr);
608 /*************************************************************************
609 * StrRStrIW [COMCTL32.373]
613 LPWSTR WINAPI StrRStrIW(LPCWSTR lpszStr, LPCWSTR lpszEnd, LPCWSTR lpszSearch)
615 LPWSTR lpszRet = NULL;
618 TRACE("(%s,%s)\n", debugstr_w(lpszStr), debugstr_w(lpszSearch));
620 if (!lpszStr || !lpszSearch || !*lpszSearch)
624 lpszEnd = lpszStr + strlenW(lpszStr);
626 iLen = strlenW(lpszSearch);
628 while (lpszStr <= lpszEnd && *lpszStr)
630 if (!COMCTL32_ChrCmpIA(*lpszSearch, *lpszStr))
632 if (!StrCmpNIW(lpszStr, lpszSearch, iLen))
633 lpszRet = (LPWSTR)lpszStr;
635 lpszStr = CharNextW(lpszStr);
640 /*************************************************************************
641 * COMCTL32_StrSpnHelperW
643 * Internal implementation of StrSpnW/StrCSpnW/StrCSpnIW
645 static int COMCTL32_StrSpnHelperW(LPCWSTR lpszStr, LPCWSTR lpszMatch,
646 LPWSTR (WINAPI *pStrChrFn)(LPCWSTR,WCHAR),
649 LPCWSTR lpszRead = lpszStr;
650 if (lpszStr && *lpszStr && lpszMatch)
654 LPCWSTR lpszTest = pStrChrFn(lpszMatch, *lpszRead);
656 if (!bInvert && !lpszTest)
658 if (bInvert && lpszTest)
660 lpszRead = CharNextW(lpszRead);
663 return lpszRead - lpszStr;
666 /*************************************************************************
667 * StrCSpnIA [COMCTL32.374]
669 * Find the length of the start of a string that does not contain certain
670 * characters, ignoring case.
673 * lpszStr [I] String to search
674 * lpszMatch [I] Characters that cannot be in the substring
677 * The length of the part of lpszStr containing only chars not in lpszMatch,
678 * or 0 if any parameter is invalid.
680 int WINAPI StrCSpnIA(LPCSTR lpszStr, LPCSTR lpszMatch)
682 TRACE("(%s,%s)\n",debugstr_a(lpszStr), debugstr_a(lpszMatch));
684 return COMCTL32_StrSpnHelperA(lpszStr, lpszMatch, StrChrIA, TRUE);
687 /*************************************************************************
688 * StrCSpnIW [COMCTL32.375]
692 int WINAPI StrCSpnIW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
694 TRACE("(%s,%s)\n",debugstr_w(lpszStr), debugstr_w(lpszMatch));
696 return COMCTL32_StrSpnHelperW(lpszStr, lpszMatch, StrChrIW, TRUE);
699 /**************************************************************************
700 * StrRChrIA [COMCTL32.368]
702 * Find the last occurence of a character in string, ignoring case.
705 * lpszStr [I] String to search in
706 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
707 * ch [I] Character to search for.
710 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
711 * or NULL if not found.
712 * Failure: NULL, if any arguments are invalid.
714 LPSTR WINAPI StrRChrIA(LPCSTR lpszStr, LPCSTR lpszEnd, WORD ch)
716 LPCSTR lpszRet = NULL;
718 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr), debugstr_a(lpszEnd), ch);
725 lpszEnd = lpszStr + lstrlenA(lpszStr);
727 while (*lpszStr && lpszStr <= lpszEnd)
729 ch2 = IsDBCSLeadByte(*lpszStr)? *lpszStr << 8 | lpszStr[1] : *lpszStr;
733 lpszStr = CharNextA(lpszStr);
736 return (LPSTR)lpszRet;
739 /**************************************************************************
740 * StrRChrIW [COMCTL32.369]
744 LPWSTR WINAPI StrRChrIW(LPCWSTR lpszStr, LPCWSTR lpszEnd, WORD ch)
746 LPCWSTR lpszRet = NULL;
748 TRACE("(%s,%s,%x)\n", debugstr_w(lpszStr), debugstr_w(lpszEnd), ch);
753 lpszEnd = lpszStr + strlenW(lpszStr);
755 while (*lpszStr && lpszStr <= lpszEnd)
759 lpszStr = CharNextW(lpszStr);
762 return (LPWSTR)lpszRet;
765 /*************************************************************************
766 * StrCSpnW [COMCTL32.364]
770 int WINAPI StrCSpnW(LPCWSTR lpszStr, LPCWSTR lpszMatch)
772 TRACE("(%s,%s)\n",debugstr_w(lpszStr), debugstr_w(lpszMatch));
774 return COMCTL32_StrSpnHelperW(lpszStr, lpszMatch, StrChrW, TRUE);
777 /*************************************************************************
778 * IntlStrEqWorkerA [COMCTL32.376]
780 * Compare two strings.
783 * bCase [I] Whether to compare case sensitively
784 * lpszStr [I] First string to compare
785 * lpszComp [I] Second string to compare
786 * iLen [I] Length to compare
789 * TRUE If the strings are equal.
792 BOOL WINAPI IntlStrEqWorkerA(BOOL bCase, LPCSTR lpszStr, LPCSTR lpszComp,
795 DWORD dwFlags = LOCALE_USE_CP_ACP;
798 TRACE("(%d,%s,%s,%d)\n", bCase,
799 debugstr_a(lpszStr), debugstr_a(lpszComp), iLen);
801 /* FIXME: These flags are undocumented and unknown by our CompareString.
802 * We need defines for them.
804 dwFlags |= bCase ? 0x10000000 : 0x10000001;
806 iRet = CompareStringA(GetThreadLocale(),
807 dwFlags, lpszStr, iLen, lpszComp, iLen);
810 iRet = CompareStringA(2048, dwFlags, lpszStr, iLen, lpszComp, iLen);
812 return iRet == 2 ? TRUE : FALSE;
815 /*************************************************************************
816 * IntlStrEqWorkerW [COMCTL32.377]
818 * See IntlStrEqWorkerA.
820 BOOL WINAPI IntlStrEqWorkerW(BOOL bCase, LPCWSTR lpszStr, LPCWSTR lpszComp,
826 TRACE("(%d,%s,%s,%d)\n", bCase,
827 debugstr_w(lpszStr),debugstr_w(lpszComp), iLen);
829 /* FIXME: These flags are undocumented and unknown by our CompareString.
830 * We need defines for them.
832 dwFlags = bCase ? 0x10000000 : 0x10000001;
834 iRet = CompareStringW(GetThreadLocale(),
835 dwFlags, lpszStr, iLen, lpszComp, iLen);
838 iRet = CompareStringW(2048, dwFlags, lpszStr, iLen, lpszComp, iLen);
840 return iRet == 2 ? TRUE : FALSE;