Reactos
1/***
2*wcsncoll.c - Collate wide-character locale strings
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* Compare two wchar_t strings using the locale LC_COLLATE information.
8* Compares at most n characters of two strings.
9*
10*******************************************************************************/
11#include <corecrt_internal.h>
12#include <ctype.h>
13#include <locale.h>
14#include <string.h>
15
16/***
17*int _wcsncoll() - Collate wide-character locale strings
18*
19*Purpose:
20* Compare two wchar_t strings using the locale LC_COLLATE information
21* Compares at most n characters of two strings.
22* In the C locale, _wcsncmp() is used to make the comparison.
23*
24*Entry:
25* const wchar_t *s1 = pointer to the first string
26* const wchar_t *s2 = pointer to the second string
27* size_t count - maximum number of characters to compare
28*
29*Exit:
30* -1 = first string less than second string
31* 0 = strings are equal
32* 1 = first string greater than second string
33* Returns _NLSCMPERROR is something went wrong
34* This range of return values may differ from other *cmp/*coll functions.
35*
36*Exceptions:
37* Input parameters are validated. Refer to the validation section of the function.
38*
39*******************************************************************************/
40
41extern "C" int __cdecl _wcsncoll_l (
42 const wchar_t *_string1,
43 const wchar_t *_string2,
44 size_t count,
45 _locale_t plocinfo
46 )
47{
48 int ret;
49
50 if (!count)
51 {
52 return 0;
53 }
54
55 _LocaleUpdate _loc_update(plocinfo);
56
57 /* validation section */
58 _VALIDATE_RETURN(_string1 != nullptr, EINVAL, _NLSCMPERROR);
59 _VALIDATE_RETURN(_string2 != nullptr, EINVAL, _NLSCMPERROR);
60 _VALIDATE_RETURN(count <= INT_MAX, EINVAL, _NLSCMPERROR);
61
62 if ( _loc_update.GetLocaleT()->locinfo->locale_name[LC_COLLATE] == nullptr )
63 {
64 return wcsncmp(_string1, _string2, count);
65 }
66
67 if ( 0 == (ret = __acrt_CompareStringW(
68 _loc_update.GetLocaleT()->locinfo->locale_name[LC_COLLATE],
69 SORT_STRINGSORT,
70 _string1,
71 (int)count,
72 _string2,
73 (int)count)) )
74 {
75 errno = EINVAL;
76 return _NLSCMPERROR;
77 }
78
79 return (ret - 2);
80
81}
82
83extern "C" int __cdecl _wcsncoll (
84 const wchar_t *_string1,
85 const wchar_t *_string2,
86 size_t count
87 )
88{
89 if (!__acrt_locale_changed())
90 {
91
92 /* validation section */
93 _VALIDATE_RETURN(_string1 != nullptr, EINVAL, _NLSCMPERROR);
94 _VALIDATE_RETURN(_string2 != nullptr, EINVAL, _NLSCMPERROR);
95 _VALIDATE_RETURN(count <= INT_MAX, EINVAL, _NLSCMPERROR);
96 return wcsncmp(_string1, _string2, count);
97 }
98 else
99 {
100 return _wcsncoll_l(_string1, _string2, count, nullptr);
101 }
102
103}
104