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., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301, USA
27 WINE_DEFAULT_DEBUG_CHANNEL(commctrl
);
29 /*************************************************************************
30 * COMCTL32_ChrCmpHelperA
32 * Internal helper for ChrCmpA/COMCTL32_ChrCmpIA.
35 * Both this function and its Unicode counterpart are very inefficient. To
36 * fix this, CompareString must be completely implemented and optimised
37 * first. Then the core character test can be taken out of that function and
38 * placed here, so that it need never be called at all. Until then, do not
39 * attempt to optimise this code unless you are willing to test that it
40 * still performs correctly.
42 static BOOL
COMCTL32_ChrCmpHelperA(WORD ch1
, WORD ch2
, DWORD dwFlags
)
44 char str1
[3], str2
[3];
46 str1
[0] = LOBYTE(ch1
);
47 if (IsDBCSLeadByte(str1
[0]))
49 str1
[1] = HIBYTE(ch1
);
55 str2
[0] = LOBYTE(ch2
);
56 if (IsDBCSLeadByte(str2
[0]))
58 str2
[1] = HIBYTE(ch2
);
64 return CompareStringA(GetThreadLocale(), dwFlags
, str1
, -1, str2
, -1) - CSTR_EQUAL
;
67 /*************************************************************************
68 * COMCTL32_ChrCmpA (internal)
70 * Internal helper function.
72 static BOOL
COMCTL32_ChrCmpA(WORD ch1
, WORD ch2
)
74 return COMCTL32_ChrCmpHelperA(ch1
, ch2
, 0);
77 /*************************************************************************
78 * COMCTL32_ChrCmpIA (internal)
80 * Compare two characters, ignoring case.
83 * ch1 [I] First character to compare
84 * ch2 [I] Second character to compare
87 * FALSE, if the characters are equal.
90 static BOOL
COMCTL32_ChrCmpIA(WORD ch1
, WORD ch2
)
92 TRACE("(%d,%d)\n", ch1
, ch2
);
94 return COMCTL32_ChrCmpHelperA(ch1
, ch2
, NORM_IGNORECASE
);
97 /*************************************************************************
100 * Internal helper function.
102 static inline BOOL
COMCTL32_ChrCmpIW(WCHAR ch1
, WCHAR ch2
)
104 return CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, &ch1
, 1, &ch2
, 1) - CSTR_EQUAL
;
107 /**************************************************************************
108 * Str_GetPtrA [COMCTL32.233]
110 * Copies a string into a destination buffer.
113 * lpSrc [I] Source string
114 * lpDest [O] Destination buffer
115 * nMaxLen [I] Size of buffer in characters
118 * The number of characters copied.
120 INT WINAPI
Str_GetPtrA (LPCSTR lpSrc
, LPSTR lpDest
, INT nMaxLen
)
124 TRACE("(%p %p %d)\n", lpSrc
, lpDest
, nMaxLen
);
126 if ((!lpDest
|| nMaxLen
== 0) && lpSrc
)
127 return (strlen(lpSrc
) + 1);
137 len
= strlen(lpSrc
) + 1;
141 RtlMoveMemory (lpDest
, lpSrc
, len
- 1);
142 lpDest
[len
- 1] = '\0';
147 /**************************************************************************
148 * Str_SetPtrA [COMCTL32.234]
150 * Makes a copy of a string, allocating memory if necessary.
153 * lppDest [O] Pointer to destination string
154 * lpSrc [I] Source string
161 * Set lpSrc to NULL to free the memory allocated by a previous call
164 BOOL WINAPI
Str_SetPtrA (LPSTR
*lppDest
, LPCSTR lpSrc
)
166 TRACE("(%p %p)\n", lppDest
, lpSrc
);
169 LPSTR ptr
= ReAlloc (*lppDest
, strlen (lpSrc
) + 1);
183 /**************************************************************************
184 * Str_GetPtrW [COMCTL32.235]
188 INT WINAPI
Str_GetPtrW (LPCWSTR lpSrc
, LPWSTR lpDest
, INT nMaxLen
)
192 TRACE("(%p %p %d)\n", lpSrc
, lpDest
, nMaxLen
);
194 if (!lpDest
&& lpSrc
)
195 return strlenW (lpSrc
);
205 len
= strlenW (lpSrc
);
209 RtlMoveMemory (lpDest
, lpSrc
, len
*sizeof(WCHAR
));
215 /**************************************************************************
216 * Str_SetPtrW [COMCTL32.236]
220 BOOL WINAPI
Str_SetPtrW (LPWSTR
*lppDest
, LPCWSTR lpSrc
)
222 TRACE("(%p %s)\n", lppDest
, debugstr_w(lpSrc
));
225 INT len
= strlenW (lpSrc
) + 1;
226 LPWSTR ptr
= ReAlloc (*lppDest
, len
* sizeof(WCHAR
));
229 strcpyW (ptr
, lpSrc
);
240 /**************************************************************************
241 * StrChrA [COMCTL32.350]
243 * Find a given character in a string.
246 * lpszStr [I] String to search in.
247 * ch [I] Character to search for.
250 * Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
252 * Failure: NULL, if any arguments are invalid.
254 LPSTR WINAPI
StrChrA(LPCSTR lpszStr
, WORD ch
)
256 TRACE("(%s,%i)\n", debugstr_a(lpszStr
), ch
);
262 if (!COMCTL32_ChrCmpA(*lpszStr
, ch
))
263 return (LPSTR
)lpszStr
;
264 lpszStr
= CharNextA(lpszStr
);
270 /**************************************************************************
271 * StrCmpNIA [COMCTL32.353]
273 * Compare two strings, up to a maximum length, ignoring case.
276 * lpszStr [I] First string to compare
277 * lpszComp [I] Second string to compare
278 * iLen [I] Maximum number of chars to compare.
281 * An integer less than, equal to or greater than 0, indicating that
282 * lpszStr is less than, the same, or greater than lpszComp.
284 INT WINAPI
StrCmpNIA(LPCSTR lpszStr
, LPCSTR lpszComp
, INT iLen
)
286 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
287 return CompareStringA(GetThreadLocale(), NORM_IGNORECASE
, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
290 /*************************************************************************
291 * StrCmpNIW [COMCTL32.361]
295 INT WINAPI
StrCmpNIW(LPCWSTR lpszStr
, LPCWSTR lpszComp
, INT iLen
)
297 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
), iLen
);
298 return CompareStringW(GetThreadLocale(), NORM_IGNORECASE
, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
301 /*************************************************************************
302 * COMCTL32_StrStrHelperA
304 * Internal implementation of StrStrA/StrStrIA
306 static LPSTR
COMCTL32_StrStrHelperA(LPCSTR lpszStr
, LPCSTR lpszSearch
,
307 INT (WINAPI
*pStrCmpFn
)(LPCSTR
,LPCSTR
,INT
))
311 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
314 iLen
= strlen(lpszSearch
);
318 if (!pStrCmpFn(lpszStr
, lpszSearch
, iLen
))
319 return (LPSTR
)lpszStr
;
320 lpszStr
= CharNextA(lpszStr
);
325 /**************************************************************************
326 * StrStrIA [COMCTL32.355]
328 * Find a substring within a string, ignoring case.
331 * lpszStr [I] String to search in
332 * lpszSearch [I] String to look for
335 * The start of lpszSearch within lpszStr, or NULL if not found.
337 LPSTR WINAPI
StrStrIA(LPCSTR lpszStr
, LPCSTR lpszSearch
)
339 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
341 return COMCTL32_StrStrHelperA(lpszStr
, lpszSearch
, StrCmpNIA
);
344 /**************************************************************************
345 * StrToIntA [COMCTL32.357]
347 * Read a signed integer from a string.
350 * lpszStr [I] String to read integer from
353 * The signed integer value represented by the string, or 0 if no integer is
356 INT WINAPI
StrToIntA (LPCSTR lpszStr
)
358 return atoi(lpszStr
);
361 /**************************************************************************
362 * StrStrIW [COMCTL32.363]
366 LPWSTR WINAPI
StrStrIW(LPCWSTR lpszStr
, LPCWSTR lpszSearch
)
370 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSearch
));
372 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
375 iLen
= strlenW(lpszSearch
);
379 if (!StrCmpNIW(lpszStr
, lpszSearch
, iLen
))
380 return (LPWSTR
)lpszStr
;
386 /**************************************************************************
387 * StrToIntW [COMCTL32.365]
391 INT WINAPI
StrToIntW (LPCWSTR lpString
)
393 return atoiW(lpString
);
396 /*************************************************************************
397 * COMCTL32_StrSpnHelperA (internal)
399 * Internal implementation of StrSpnA/StrCSpnA/StrCSpnIA
401 static int COMCTL32_StrSpnHelperA(LPCSTR lpszStr
, LPCSTR lpszMatch
,
402 LPSTR (WINAPI
*pStrChrFn
)(LPCSTR
,WORD
),
405 LPCSTR lpszRead
= lpszStr
;
406 if (lpszStr
&& *lpszStr
&& lpszMatch
)
410 LPCSTR lpszTest
= pStrChrFn(lpszMatch
, *lpszRead
);
412 if (!bInvert
&& !lpszTest
)
414 if (bInvert
&& lpszTest
)
416 lpszRead
= CharNextA(lpszRead
);
419 return lpszRead
- lpszStr
;
422 /**************************************************************************
423 * StrCSpnA [COMCTL32.356]
425 * Find the length of the start of a string that does not contain certain
429 * lpszStr [I] String to search
430 * lpszMatch [I] Characters that cannot be in the substring
433 * The length of the part of lpszStr containing only chars not in lpszMatch,
434 * or 0 if any parameter is invalid.
436 int WINAPI
StrCSpnA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
438 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
440 return COMCTL32_StrSpnHelperA(lpszStr
, lpszMatch
, StrChrA
, TRUE
);
443 /**************************************************************************
444 * StrChrW [COMCTL32.358]
448 LPWSTR WINAPI
StrChrW(LPCWSTR lpszStr
, WCHAR ch
)
450 LPWSTR lpszRet
= NULL
;
452 TRACE("(%s,%i)\n", debugstr_w(lpszStr
), ch
);
455 lpszRet
= strchrW(lpszStr
, ch
);
459 /**************************************************************************
460 * StrCmpNA [COMCTL32.352]
462 * Compare two strings, up to a maximum length.
465 * lpszStr [I] First string to compare
466 * lpszComp [I] Second string to compare
467 * iLen [I] Maximum number of chars to compare.
470 * An integer less than, equal to or greater than 0, indicating that
471 * lpszStr is less than, the same, or greater than lpszComp.
473 INT WINAPI
StrCmpNA(LPCSTR lpszStr
, LPCSTR lpszComp
, INT iLen
)
475 TRACE("(%s,%s,%i)\n", debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
476 return CompareStringA(GetThreadLocale(), 0, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
479 /**************************************************************************
480 * StrCmpNW [COMCTL32.360]
484 INT WINAPI
StrCmpNW(LPCWSTR lpszStr
, LPCWSTR lpszComp
, INT iLen
)
486 TRACE("(%s,%s,%i)\n", debugstr_w(lpszStr
), debugstr_w(lpszComp
), iLen
);
487 return CompareStringW(GetThreadLocale(), 0, lpszStr
, iLen
, lpszComp
, iLen
) - CSTR_EQUAL
;
490 /**************************************************************************
491 * StrRChrA [COMCTL32.351]
493 * Find the last occurrence of a character in string.
496 * lpszStr [I] String to search in
497 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
498 * ch [I] Character to search for.
501 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
502 * or NULL if not found.
503 * Failure: NULL, if any arguments are invalid.
505 LPSTR WINAPI
StrRChrA(LPCSTR lpszStr
, LPCSTR lpszEnd
, WORD ch
)
507 LPCSTR lpszRet
= NULL
;
509 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr
), debugstr_a(lpszEnd
), ch
);
516 lpszEnd
= lpszStr
+ lstrlenA(lpszStr
);
518 while (*lpszStr
&& lpszStr
<= lpszEnd
)
520 ch2
= IsDBCSLeadByte(*lpszStr
)? *lpszStr
<< 8 | lpszStr
[1] : *lpszStr
;
522 if (!COMCTL32_ChrCmpA(ch
, ch2
))
524 lpszStr
= CharNextA(lpszStr
);
527 return (LPSTR
)lpszRet
;
531 /**************************************************************************
532 * StrRChrW [COMCTL32.359]
536 LPWSTR WINAPI
StrRChrW(LPCWSTR str
, LPCWSTR end
, WORD ch
)
540 if (!str
) return NULL
;
541 if (!end
) end
= str
+ strlenW(str
);
544 if (*str
== ch
) ret
= (WCHAR
*)str
;
550 /**************************************************************************
551 * StrStrA [COMCTL32.354]
553 * Find a substring within a string.
556 * lpszStr [I] String to search in
557 * lpszSearch [I] String to look for
560 * The start of lpszSearch within lpszStr, or NULL if not found.
562 LPSTR WINAPI
StrStrA(LPCSTR lpszStr
, LPCSTR lpszSearch
)
564 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
566 return COMCTL32_StrStrHelperA(lpszStr
, lpszSearch
, StrCmpNA
);
569 /**************************************************************************
570 * StrStrW [COMCTL32.362]
574 LPWSTR WINAPI
StrStrW(LPCWSTR lpszStr
, LPCWSTR lpszSearch
)
576 if (!lpszStr
|| !lpszSearch
) return NULL
;
577 return strstrW( lpszStr
, lpszSearch
);
580 /*************************************************************************
581 * StrChrIA [COMCTL32.366]
583 * Find a given character in a string, ignoring case.
586 * lpszStr [I] String to search in.
587 * ch [I] Character to search for.
590 * Success: A pointer to the first occurrence of ch in lpszStr, or NULL if
592 * Failure: NULL, if any arguments are invalid.
594 LPSTR WINAPI
StrChrIA(LPCSTR lpszStr
, WORD ch
)
596 TRACE("(%s,%i)\n", debugstr_a(lpszStr
), ch
);
602 if (!COMCTL32_ChrCmpIA(*lpszStr
, ch
))
603 return (LPSTR
)lpszStr
;
604 lpszStr
= CharNextA(lpszStr
);
610 /*************************************************************************
611 * StrChrIW [COMCTL32.367]
615 LPWSTR WINAPI
StrChrIW(LPCWSTR lpszStr
, WCHAR ch
)
617 TRACE("(%s,%i)\n", debugstr_w(lpszStr
), ch
);
624 if (toupperW(*lpszStr
) == ch
)
625 return (LPWSTR
)lpszStr
;
630 return (LPWSTR
)lpszStr
;
633 /*************************************************************************
634 * StrRStrIA [COMCTL32.372]
636 * Find the last occurrence of a substring within a string.
639 * lpszStr [I] String to search in
640 * lpszEnd [I] End of lpszStr
641 * lpszSearch [I] String to look for
644 * The last occurrence lpszSearch within lpszStr, or NULL if not found.
646 LPSTR WINAPI
StrRStrIA(LPCSTR lpszStr
, LPCSTR lpszEnd
, LPCSTR lpszSearch
)
648 LPSTR lpszRet
= NULL
;
652 TRACE("(%s,%s)\n", debugstr_a(lpszStr
), debugstr_a(lpszSearch
));
654 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
658 lpszEnd
= lpszStr
+ lstrlenA(lpszStr
);
660 if (IsDBCSLeadByte(*lpszSearch
))
661 ch1
= *lpszSearch
<< 8 | lpszSearch
[1];
664 iLen
= lstrlenA(lpszSearch
);
666 while (lpszStr
<= lpszEnd
&& *lpszStr
)
668 ch2
= IsDBCSLeadByte(*lpszStr
)? *lpszStr
<< 8 | lpszStr
[1] : *lpszStr
;
669 if (!COMCTL32_ChrCmpIA(ch1
, ch2
))
671 if (!StrCmpNIA(lpszStr
, lpszSearch
, iLen
))
672 lpszRet
= (LPSTR
)lpszStr
;
674 lpszStr
= CharNextA(lpszStr
);
679 /*************************************************************************
680 * StrRStrIW [COMCTL32.373]
684 LPWSTR WINAPI
StrRStrIW(LPCWSTR lpszStr
, LPCWSTR lpszEnd
, LPCWSTR lpszSearch
)
686 LPWSTR lpszRet
= NULL
;
689 TRACE("(%s,%s)\n", debugstr_w(lpszStr
), debugstr_w(lpszSearch
));
691 if (!lpszStr
|| !lpszSearch
|| !*lpszSearch
)
695 lpszEnd
= lpszStr
+ strlenW(lpszStr
);
697 iLen
= strlenW(lpszSearch
);
699 while (lpszStr
<= lpszEnd
&& *lpszStr
)
701 if (!COMCTL32_ChrCmpIW(*lpszSearch
, *lpszStr
))
703 if (!StrCmpNIW(lpszStr
, lpszSearch
, iLen
))
704 lpszRet
= (LPWSTR
)lpszStr
;
711 /*************************************************************************
712 * StrCSpnIA [COMCTL32.374]
714 * Find the length of the start of a string that does not contain certain
715 * characters, ignoring case.
718 * lpszStr [I] String to search
719 * lpszMatch [I] Characters that cannot be in the substring
722 * The length of the part of lpszStr containing only chars not in lpszMatch,
723 * or 0 if any parameter is invalid.
725 int WINAPI
StrCSpnIA(LPCSTR lpszStr
, LPCSTR lpszMatch
)
727 TRACE("(%s,%s)\n",debugstr_a(lpszStr
), debugstr_a(lpszMatch
));
729 return COMCTL32_StrSpnHelperA(lpszStr
, lpszMatch
, StrChrIA
, TRUE
);
732 /*************************************************************************
733 * StrCSpnIW [COMCTL32.375]
737 int WINAPI
StrCSpnIW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
739 LPCWSTR lpszRead
= lpszStr
;
741 TRACE("(%s,%s)\n",debugstr_w(lpszStr
), debugstr_w(lpszMatch
));
743 if (lpszStr
&& *lpszStr
&& lpszMatch
)
747 if (StrChrIW(lpszMatch
, *lpszRead
)) break;
751 return lpszRead
- lpszStr
;
754 /**************************************************************************
755 * StrRChrIA [COMCTL32.368]
757 * Find the last occurrence of a character in string, ignoring case.
760 * lpszStr [I] String to search in
761 * lpszEnd [I] Place to end search, or NULL to search until the end of lpszStr
762 * ch [I] Character to search for.
765 * Success: A pointer to the last occurrence of ch in lpszStr before lpszEnd,
766 * or NULL if not found.
767 * Failure: NULL, if any arguments are invalid.
769 LPSTR WINAPI
StrRChrIA(LPCSTR lpszStr
, LPCSTR lpszEnd
, WORD ch
)
771 LPCSTR lpszRet
= NULL
;
773 TRACE("(%s,%s,%x)\n", debugstr_a(lpszStr
), debugstr_a(lpszEnd
), ch
);
780 lpszEnd
= lpszStr
+ lstrlenA(lpszStr
);
782 while (*lpszStr
&& lpszStr
<= lpszEnd
)
784 ch2
= IsDBCSLeadByte(*lpszStr
)? *lpszStr
<< 8 | lpszStr
[1] : *lpszStr
;
788 lpszStr
= CharNextA(lpszStr
);
791 return (LPSTR
)lpszRet
;
794 /**************************************************************************
795 * StrRChrIW [COMCTL32.369]
799 LPWSTR WINAPI
StrRChrIW(LPCWSTR str
, LPCWSTR end
, WORD ch
)
803 if (!str
) return NULL
;
804 if (!end
) end
= str
+ strlenW(str
);
807 if (!COMCTL32_ChrCmpIW(*str
, ch
)) ret
= (WCHAR
*)str
;
813 /*************************************************************************
814 * StrCSpnW [COMCTL32.364]
818 int WINAPI
StrCSpnW(LPCWSTR lpszStr
, LPCWSTR lpszMatch
)
820 if (!lpszStr
|| !lpszMatch
) return 0;
821 return strcspnW( lpszStr
, lpszMatch
);
824 /*************************************************************************
825 * IntlStrEqWorkerA [COMCTL32.376]
827 * Compare two strings.
830 * bCase [I] Whether to compare case sensitively
831 * lpszStr [I] First string to compare
832 * lpszComp [I] Second string to compare
833 * iLen [I] Length to compare
836 * TRUE If the strings are equal.
839 BOOL WINAPI
IntlStrEqWorkerA(BOOL bCase
, LPCSTR lpszStr
, LPCSTR lpszComp
,
842 DWORD dwFlags
= LOCALE_USE_CP_ACP
;
845 TRACE("(%d,%s,%s,%d)\n", bCase
,
846 debugstr_a(lpszStr
), debugstr_a(lpszComp
), iLen
);
848 /* FIXME: This flag is undocumented and unknown by our CompareString.
849 * We need a define for it.
851 dwFlags
= 0x10000000;
852 if (!bCase
) dwFlags
|= NORM_IGNORECASE
;
854 iRet
= CompareStringA(GetThreadLocale(),
855 dwFlags
, lpszStr
, iLen
, lpszComp
, iLen
);
858 iRet
= CompareStringA(2048, dwFlags
, lpszStr
, iLen
, lpszComp
, iLen
);
860 return iRet
== CSTR_EQUAL
;
863 /*************************************************************************
864 * IntlStrEqWorkerW [COMCTL32.377]
866 * See IntlStrEqWorkerA.
868 BOOL WINAPI
IntlStrEqWorkerW(BOOL bCase
, LPCWSTR lpszStr
, LPCWSTR lpszComp
,
874 TRACE("(%d,%s,%s,%d)\n", bCase
,
875 debugstr_w(lpszStr
),debugstr_w(lpszComp
), iLen
);
877 /* FIXME: This flag is undocumented and unknown by our CompareString.
878 * We need a define for it.
880 dwFlags
= 0x10000000;
881 if (!bCase
) dwFlags
|= NORM_IGNORECASE
;
883 iRet
= CompareStringW(GetThreadLocale(),
884 dwFlags
, lpszStr
, iLen
, lpszComp
, iLen
);
887 iRet
= CompareStringW(2048, dwFlags
, lpszStr
, iLen
, lpszComp
, iLen
);
889 return iRet
== CSTR_EQUAL
;