ReactOS 0.4.16-dev-822-gbcedb53
mbsncmp.cpp
Go to the documentation of this file.
1/***
2*mbsncmp.c - Compare n characters of two MBCS strings
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* Compare n characters of two MBCS strings
8*
9*******************************************************************************/
10#ifndef _MBCS
11 #error This file should only be compiled with _MBCS defined
12#endif
13
15#include <locale.h>
16#include <string.h>
17
18#pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018
19
20/***
21*int mbsncmp(s1, s2, n) - Compare n characters of two MBCS strings
22*
23*Purpose:
24* Compares up to n characters of two strings for ordinal order.
25* Strings are compared on a character basis, not a byte basis.
26*
27* UTF-8 and SBCS are merely compared in byte order.
28* DBCS are compared by codepoint to ensure double byte chars sort last
29*
30*Entry:
31* unsigned char *s1, *s2 = strings to compare
32* size_t n = maximum number of characters to compare
33*
34*Exit:
35* Returns <0 if s1 < s2
36* Returns 0 if s1 == s2
37* Returns >0 if s1 > s2
38* Returns _NLSCMPERROR is something went wrong
39*
40*Exceptions:
41* Input parameters are validated. Refer to the validation section of the function.
42*
43*******************************************************************************/
44
45extern "C" int __cdecl _mbsncmp_l(
46 const unsigned char *s1,
47 const unsigned char *s2,
48 size_t n,
50 )
51{
52 unsigned short c1, c2;
53 _LocaleUpdate _loc_update(plocinfo);
54
55 if (n==0)
56 return(0);
57
58 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
59 return strncmp((const char *)s1, (const char *)s2, n);
60
61 /* validation section */
64
65 while (n--) {
66
67 c1 = *s1++;
68 if ( _ismbblead_l(c1, _loc_update.GetLocaleT()) )
69 c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) );
70
71 c2 = *s2++;
72 if ( _ismbblead_l(c2, _loc_update.GetLocaleT()) )
73 c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) );
74
75 if (c1 != c2)
76 return( (c1 > c2) ? 1 : -1);
77
78 if (c1 == 0)
79 return(0);
80 }
81
82 return(0);
83}
84
85extern "C" int (__cdecl _mbsncmp)(
86 const unsigned char *s1,
87 const unsigned char *s2,
88 size_t n
89 )
90{
91 return _mbsncmp_l(s1, s2, n, nullptr);
92}
#define EINVAL
Definition: acclib.h:90
int strncmp(const char *String1, const char *String2, ACPI_SIZE Count)
Definition: utclib.c:534
#define __cdecl
Definition: accygwin.h:79
#define _ismbblead_l(_c, p)
#define _VALIDATE_RETURN(expr, errorcode, retexpr)
unsigned int(__cdecl typeof(jpeg_read_scanlines))(struct jpeg_decompress_struct *
Definition: typeof.h:31
GLdouble n
Definition: glext.h:7729
_Check_return_ _CRTIMP int __cdecl _mbsncmp(_In_z_ const unsigned char *_Str1, _In_z_ const unsigned char *_Str2, _In_ size_t _MaxCount)
_locale_t plocinfo
Definition: ismbbyte.cpp:75
int __cdecl _mbsncmp_l(const unsigned char *s1, const unsigned char *s2, size_t n, _locale_t plocinfo)
Definition: mbsncmp.cpp:45
const unsigned char * s2
Definition: mbsncmp.cpp:87
const unsigned char size_t n
Definition: mbsncmp.cpp:90
struct S1 s1
#define _NLSCMPERROR
Definition: string.h:19