xref: /reactos/sdk/lib/ucrt/mbstring/mbsnbico.cpp (revision b09b5584)
1 /***
2 *mbsnbico.c - Collate n bytes of strings, ignoring case (MBCS)
3 *
4 *       Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 *       Collate n bytes of strings, ignoring case (MBCS)
8 *
9 *******************************************************************************/
10 #ifndef _MBCS
11     #error This file should only be compiled with _MBCS defined
12 #endif
13 
14 #include <corecrt_internal_mbstring.h>
15 #include <locale.h>
16 
17 
18 /***
19 * _mbsnbicoll - Collate n bytes of strings, ignoring case (MBCS)
20 *
21 *Purpose:
22 *       Collates up to n bytes of two strings for lexical order.
23 *       Strings are collated on a character basis, not a byte basis.
24 *       Case of characters is not considered.
25 *
26 *Entry:
27 *       unsigned char *s1, *s2 = strings to collate
28 *       size_t n = maximum number of bytes to collate
29 *
30 *Exit:
31 *       Returns <0 if s1 < s2
32 *       Returns  0 if s1 == s2
33 *       Returns >0 if s1 > s2
34 *       Returns _NLSCMPERROR is something went wrong
35 *
36 *Exceptions:
37 *       Input parameters are validated. Refer to the validation section of the function.
38 *
39 *******************************************************************************/
40 
41 extern "C" int __cdecl _mbsnbicoll_l(
42         const unsigned char *s1,
43         const unsigned char *s2,
44         size_t n,
45         _locale_t plocinfo
46         )
47 {
48         int ret;
49         _LocaleUpdate _loc_update(plocinfo);
50 
51         if (n == 0)
52             return 0;
53 
54         /* validation section */
55         _VALIDATE_RETURN(s1 != nullptr, EINVAL, _NLSCMPERROR);
56         _VALIDATE_RETURN(s2 != nullptr, EINVAL, _NLSCMPERROR);
57         _VALIDATE_RETURN(n <= INT_MAX, EINVAL, _NLSCMPERROR);
58 
59         if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
60             return _strnicoll_l((const char *)s1, (const char *)s2, n, plocinfo);
61 
62         if ( 0 == (ret = __acrt_CompareStringA(_loc_update.GetLocaleT(),
63                                              _loc_update.GetLocaleT()->mbcinfo->mblocalename,
64                                               SORT_STRINGSORT | NORM_IGNORECASE,
65                                               (const char *)s1,
66                                               (int)n,
67                                               (char *)s2,
68                                               (int)n,
69                                               _loc_update.GetLocaleT()->mbcinfo->mbcodepage )) )
70             return _NLSCMPERROR;
71 
72         return ret - 2;
73 
74 }
75 extern "C" int __cdecl _mbsnbicoll(
76         const unsigned char *s1,
77         const unsigned char *s2,
78         size_t n
79         )
80 {
81     return _mbsnbicoll_l(s1, s2, n, nullptr);
82 }
83