Leaked source code of windows server 2003
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

160 lines
4.8 KiB

  1. /***
  2. *mbsnicmp.c - Compare n characters of strings, ignoring case (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Compare n characters of strings, ignoring case (MBCS)
  8. *
  9. *Revision History:
  10. * 11-19-92 KRS Ported from 16-bit sources.
  11. * 09-29-93 CFW Merge _KANJI and _MBCS_OS
  12. * 10-05-93 GJF Replaced _CRTAPI1 with __cdecl.
  13. * 10-12-93 CFW Compare lower case, not upper.
  14. * 04-12-94 CFW Make function generic.
  15. * 04-15-93 CFW Add _MB_CP_LOCK.
  16. * 04-21-93 CFW Update pointer.
  17. * 05-05-94 CFW Work around NT/Chico bug: CompareString ignores
  18. * control characters.
  19. * 05-09-94 CFW Optimize for SBCS, no re-scan if CompareString fixed.
  20. * 05-12-94 CFW Back to hard-coded, CompareString sort is backwards.
  21. * 05-16-94 CFW Use _mbbtolower/upper.
  22. * 05-19-94 CFW Enable non-Win32.
  23. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  24. * 04-17-98 GJF Revised multithread support based on threadmbcinfo
  25. * structs
  26. *
  27. *******************************************************************************/
  28. #ifdef _MBCS
  29. #include <mtdll.h>
  30. #include <cruntime.h>
  31. #include <mbdata.h>
  32. #include <mbctype.h>
  33. #include <string.h>
  34. #include <mbstring.h>
  35. /***
  36. * _mbsnicmp - Compare n characters of strings, ignoring case (MBCS)
  37. *
  38. *Purpose:
  39. * Compares up to n charcters of two strings for lexical order.
  40. * Strings are compared on a character basis, not a byte basis.
  41. * Case of characters is not considered.
  42. *
  43. *Entry:
  44. * unsigned char *s1, *s2 = strings to compare
  45. * size_t n = maximum number of characters to compare
  46. *
  47. *Exit:
  48. * returns <0 if s1 < s2
  49. * returns 0 if s1 == s2
  50. * returns >0 if s1 > s2
  51. *
  52. *Exceptions:
  53. *
  54. *******************************************************************************/
  55. int __cdecl _mbsnicmp(
  56. const unsigned char *s1,
  57. const unsigned char *s2,
  58. size_t n
  59. )
  60. {
  61. unsigned short c1, c2;
  62. #ifdef _MT
  63. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  64. if ( ptmbci != __ptmbcinfo )
  65. ptmbci = __updatetmbcinfo();
  66. #endif
  67. if (n==0)
  68. return(0);
  69. #ifdef _MT
  70. if ( _ISNOTMBCP_MT(ptmbci) )
  71. #else
  72. if ( _ISNOTMBCP )
  73. #endif
  74. return _strnicmp(s1, s2, n);
  75. while (n--) {
  76. c1 = *s1++;
  77. #ifdef _MT
  78. if ( __ismbblead_mt(ptmbci, c1) ) {
  79. #else
  80. if ( _ismbblead(c1) ) {
  81. #endif
  82. if (*s1 == '\0')
  83. c1 = 0;
  84. else {
  85. c1 = ((c1<<8) | *s1++);
  86. #ifdef _MT
  87. if ( ((c1 >= _MBUPPERLOW1_MT(ptmbci)) &&
  88. (c1 <= _MBUPPERHIGH1_MT(ptmbci))) )
  89. c1 += _MBCASEDIFF1_MT(ptmbci);
  90. else if ( ((c1 >= _MBUPPERLOW2_MT(ptmbci)) &&
  91. (c1 <= _MBUPPERHIGH2_MT(ptmbci))) )
  92. c1 += _MBCASEDIFF2_MT(ptmbci);
  93. #else
  94. if ( ((c1 >= _MBUPPERLOW1) && (c1 <= _MBUPPERHIGH1)) )
  95. c1 += _MBCASEDIFF1;
  96. else if ( ((c1 >= _MBUPPERLOW2) && (c1 <= _MBUPPERHIGH2)) )
  97. c1 += _MBCASEDIFF2;
  98. #endif
  99. }
  100. }
  101. else
  102. #ifdef _MT
  103. c1 = __mbbtolower_mt(ptmbci, c1);
  104. #else
  105. c1 = _mbbtolower(c1);
  106. #endif
  107. c2 = *s2++;
  108. #ifdef _MT
  109. if ( __ismbblead_mt(ptmbci, c2) ) {
  110. #else
  111. if ( _ismbblead(c2) ) {
  112. #endif
  113. if (*s2 == '\0')
  114. c2 = 0;
  115. else {
  116. c2 = ((c2<<8) | *s2++);
  117. #ifdef _MT
  118. if ( ((c2 >= _MBUPPERLOW1_MT(ptmbci)) &&
  119. (c2 <= _MBUPPERHIGH1_MT(ptmbci))) )
  120. c2 += _MBCASEDIFF1_MT(ptmbci);
  121. else if ( ((c2 >= _MBUPPERLOW2_MT(ptmbci)) &&
  122. (c2 <= _MBUPPERHIGH2_MT(ptmbci))) )
  123. c2 += _MBCASEDIFF2_MT(ptmbci);
  124. #else
  125. if ( ((c2 >= _MBUPPERLOW1) && (c2 <= _MBUPPERHIGH1)) )
  126. c2 += _MBCASEDIFF1;
  127. else if ( ((c2 >= _MBUPPERLOW2) && (c2 <= _MBUPPERHIGH2)) )
  128. c2 += _MBCASEDIFF2;
  129. #endif
  130. }
  131. }
  132. else
  133. #ifdef _MT
  134. c2 = __mbbtolower_mt(ptmbci, c2);
  135. #else
  136. c2 = _mbbtolower(c2);
  137. #endif
  138. if (c1 != c2)
  139. return( (c1 > c2) ? 1 : -1 );
  140. if (c1 == 0)
  141. return(0);
  142. }
  143. return(0);
  144. }
  145. #endif /* _MBCS */