ReactOS 0.4.16-dev-814-g656a5dc
mbscmp.cpp
Go to the documentation of this file.
1/***
2*mbscmp.c - Compare MBCS strings
3*
4* Copyright (c) Microsoft Corporation. All rights reserved.
5*
6*Purpose:
7* Compare 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* _mbscmp - Compare MBCS strings
22*
23*Purpose:
24* Compares two strings in ordinal order.
25* Single byte strings are compared byte-by-byte
26* Double byte strings are compared by character
27* (basically ensuring that double byte sequences are higher than single byte)
28* UTF-8 is compared byte-by-byte, which work since its the same as character order.
29*
30*Entry:
31* char *s1, *s2 = strings to compare
32*
33*WARNING:
34* No validation for improper UTF-8 strings, mixed up lead/trail byte orders are not defined.
35*
36*Exit:
37* Returns <0 if s1 < s2
38* Returns 0 if s1 == s2
39* Returns >0 if s1 > s2
40* Returns _NLSCMPERROR is something went wrong
41*
42*Exceptions:
43* Input parameters are validated. Refer to the validation section of the function.
44*
45*******************************************************************************/
46
47extern "C" int __cdecl _mbscmp_l(
48 const unsigned char *s1,
49 const unsigned char *s2,
51 )
52{
53 unsigned short c1, c2;
54 _LocaleUpdate _loc_update(plocinfo);
55
56 /* validation section */
59 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0)
60 return strcmp((const char *)s1, (const char *)s2);
61
62 for (;;) {
63
64 c1 = *s1++;
65 if ( _ismbblead_l(c1, _loc_update.GetLocaleT()) )
66 c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) );
67
68 c2 = *s2++;
69 if ( _ismbblead_l(c2, _loc_update.GetLocaleT()) )
70 c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) );
71
72 if (c1 != c2)
73 return (c1 > c2) ? 1 : -1;
74
75 if (c1 == 0)
76 return 0;
77
78 }
79}
80
81extern "C" int (__cdecl _mbscmp)(
82 const unsigned char *s1,
83 const unsigned char *s2
84 )
85{
86 return _mbscmp_l(s1, s2, nullptr);
87}
#define EINVAL
Definition: acclib.h:90
int strcmp(const char *String1, const char *String2)
Definition: utclib.c:469
#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
_Check_return_ _CRTIMP int __cdecl _mbscmp(_In_z_ const unsigned char *_Str1, _In_z_ const unsigned char *_Str2)
_locale_t plocinfo
Definition: ismbbyte.cpp:75
const unsigned char * s2
Definition: mbscmp.cpp:85
int __cdecl _mbscmp_l(const unsigned char *s1, const unsigned char *s2, _locale_t plocinfo)
Definition: mbscmp.cpp:47
struct S1 s1
#define _NLSCMPERROR
Definition: string.h:19