Reactos
at master 107 lines 2.9 kB view raw
1/*** 2*mbsnbcmp.c - Compare n bytes of two MBCS strings 3* 4* Copyright (c) Microsoft Corporation. All rights reserved. 5* 6*Purpose: 7* Compare 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 <locale.h> 16 17#pragma warning(disable:__WARNING_POTENTIAL_BUFFER_OVERFLOW_NULLTERMINATED) // 26018 18 19/*** 20*int mbsnbcmp(s1, s2, n) - Compare n bytes of two MBCS strings 21* 22*Purpose: 23* Compares up to n bytes of two strings for ordinal order. 24* 25* UTF-8 and SBCS are merely compared in byte order. 26* DBCS are compared by codepoint to ensure double byte chars sort last 27* 28*Entry: 29* unsigned char *s1, *s2 = strings to compare 30* size_t n = maximum number of bytes to compare 31* 32*Exit: 33* Returns <0 if s1 < s2 34* Returns 0 if s1 == s2 35* Returns >0 if s1 > s2 36* Returns _NLSCMPERROR is something went wrong 37* 38*Exceptions: 39* Input parameters are validated. Refer to the validation section of the function. 40* 41*******************************************************************************/ 42 43extern "C" int __cdecl _mbsnbcmp_l( 44 const unsigned char *s1, 45 const unsigned char *s2, 46 size_t n, 47 _locale_t plocinfo 48 ) 49{ 50 unsigned short c1, c2; 51 52 if (n==0) 53 return(0); 54 55 _LocaleUpdate _loc_update(plocinfo); 56 57 if (_loc_update.GetLocaleT()->mbcinfo->ismbcodepage == 0) 58 return strncmp((const char *)s1, (const char *)s2, n); 59 60 /* validation section */ 61 _VALIDATE_RETURN(s1 != nullptr, EINVAL, _NLSCMPERROR); 62 _VALIDATE_RETURN(s2 != nullptr, EINVAL, _NLSCMPERROR); 63 64 while (n--) { 65 66 c1 = *s1++; 67 if ( _ismbblead_l(c1, _loc_update.GetLocaleT()) ) 68 { 69 if (n==0) 70 { 71 c1 = 0; /* 'naked' lead - end of string */ 72 c2 = _ismbblead_l(*s2, _loc_update.GetLocaleT()) ? 0 : *s2; 73 goto test; 74 } 75 c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) ); 76 } 77 78 c2 = *s2++; 79 if ( _ismbblead_l(c2, _loc_update.GetLocaleT()) ) 80 { 81 if (n==0) 82 { 83 c2 = 0; /* 'naked' lead - end of string */ 84 goto test; 85 } 86 --n; 87 c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) ); 88 } 89test: 90 if (c1 != c2) 91 return( (c1 > c2) ? 1 : -1); 92 93 if (c1 == 0) 94 return(0); 95 } 96 97 return(0); 98} 99 100extern "C" int (__cdecl _mbsnbcmp)( 101 const unsigned char *s1, 102 const unsigned char *s2, 103 size_t n 104 ) 105{ 106 return _mbsnbcmp_l(s1, s2, n, nullptr); 107}