xref: /reactos/sdk/lib/ucrt/mbstring/mbsnbcol.cpp (revision fe11f7a2)
1 /***
2 *mbsnbcol.c - Collate n bytes of two MBCS strings
3 *
4 *       Copyright (c) Microsoft Corporation. All rights reserved.
5 *
6 *Purpose:
7 *       Collate n bytes of two 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 <limits.h>
16 #include <locale.h>
17 #include <string.h>
18 
19 /***
20 * _mbsnbcoll(s1, s2, n) - Collate n bytes of two MBCS strings
21 *
22 *Purpose:
23 *       Collates up to n bytes of two strings for lexical order.
24 *
25 *Entry:
26 *       unsigned char *s1, *s2 = strings to collate
27 *       size_t n = maximum number of bytes 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 
40 extern "C" int __cdecl _mbsnbcoll_l(
41         const unsigned char *s1,
42         const unsigned char *s2,
43         size_t n,
44         _locale_t plocinfo
45         )
46 {
47         int ret;
48 
49         if (n == 0)
50             return 0;
51 
52         /* validation section */
53         _VALIDATE_RETURN(s1 != nullptr, EINVAL, _NLSCMPERROR);
54         _VALIDATE_RETURN(s2 != nullptr, EINVAL, _NLSCMPERROR);
55         _VALIDATE_RETURN(n <= INT_MAX, EINVAL, _NLSCMPERROR);
56 
57         _LocaleUpdate _loc_update(plocinfo);
58 
59         if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
60             return _strncoll_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,
65                                               (const char *)s1,
66                                               (int)n,
67                                               (char *)s2,
68                                               (int)n,
69                                               _loc_update.GetLocaleT()->mbcinfo->mbcodepage )) )
70         {
71             errno = EINVAL;
72             return _NLSCMPERROR;
73         }
74 
75         return ret - 2;
76 
77 }
78 
79 extern "C" int (__cdecl _mbsnbcoll)(
80         const unsigned char *s1,
81         const unsigned char *s2,
82         size_t n
83         )
84 {
85     return _mbsnbcoll_l(s1, s2, n, nullptr);
86 }
87