xref: /reactos/sdk/lib/ucrt/mbstring/mbscoll.cpp (revision 04e0dc4a)
1 /***
2 *mbscoll.c - Collate MBCS strings
3 *
4 *       Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 *       Collate MBCS strings
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 #include <string.h>
17 
18 
19 /***
20 * _mbscoll - Collate MBCS strings
21 *
22 *Purpose:
23 *       Collates two strings for lexical order.   Strings
24 *       are collated on a character basis, not a byte basis.
25 *
26 *Entry:
27 *       char *s1, *s2 = strings to collate
28 *
29 *Exit:
30 *       Returns <0 if s1 < s2
31 *       Returns  0 if s1 == s2
32 *       Returns >0 if s1 > s2
33 *       Returns _NLSCMPERROR is something went wrong
34 *
35 *Exceptions:
36 *       Input parameters are validated. Refer to the validation section of the function.
37 *
38 *******************************************************************************/
39 
_mbscoll_l(const unsigned char * s1,const unsigned char * s2,_locale_t plocinfo)40 extern "C" int __cdecl _mbscoll_l(
41         const unsigned char *s1,
42         const unsigned char *s2,
43         _locale_t plocinfo
44         )
45 {
46         int ret;
47         _LocaleUpdate _loc_update(plocinfo);
48 
49         /* validation section */
50         _VALIDATE_RETURN(s1 != nullptr, EINVAL, _NLSCMPERROR);
51         _VALIDATE_RETURN(s2 != nullptr, EINVAL, _NLSCMPERROR);
52 
53         if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
54             return _strcoll_l((const char *)s1, (const char *)s2, plocinfo);
55 
56         if (0 == (ret = __acrt_CompareStringA(
57                         _loc_update.GetLocaleT(),
58                         _loc_update.GetLocaleT()->mbcinfo->mblocalename,
59                         SORT_STRINGSORT,
60                         (LPCSTR)s1,
61                         -1,
62                         (LPSTR)s2,
63                         -1,
64                         _loc_update.GetLocaleT()->mbcinfo->mbcodepage )))
65         {
66             errno = EINVAL;
67 
68             return _NLSCMPERROR;
69         }
70 
71         return ret - 2;
72 
73 }
74 
75 extern "C" int (__cdecl _mbscoll)(
76         const unsigned char *s1,
77         const unsigned char *s2
78         )
79 {
80     return _mbscoll_l(s1, s2, nullptr);
81 }
82