Source code of Windows XP (NT5)
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.

181 lines
5.6 KiB

  1. /***
  2. *mbsicmp.c - Case-insensitive string comparision routine (MBCS)
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Case-insensitive string comparision routine (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. * 05-05-94 CFW Work around NT/Chico bug: CompareString ignores
  16. * control characters.
  17. * 05-09-94 CFW Optimize for SBCS, no re-scan if CompareString fixed.
  18. * 05-12-94 CFW Back to hard-coded, CompareString sort is backwards.
  19. * 05-16-94 CFW Use _mbbtolower/upper.
  20. * 05-19-94 CFW Enable non-Win32.
  21. * 08-15-96 RDK For Win32, use NLS call to make uppercase.
  22. * 03-17-97 RDK Added error flag to __crtLCMapStringA.
  23. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  24. * 09-26-97 BWT Fix POSIX
  25. * 04-07-98 GJF Revised multithread support based on threadmbcinfo
  26. * structs
  27. * 05-17-99 PML Remove all Macintosh support.
  28. *
  29. *******************************************************************************/
  30. #ifdef _MBCS
  31. #include <awint.h>
  32. #include <mtdll.h>
  33. #include <cruntime.h>
  34. #include <mbdata.h>
  35. #include <mbctype.h>
  36. #include <string.h>
  37. #include <mbstring.h>
  38. /***
  39. * _mbsicmp - Case-insensitive string comparision routine (MBCS)
  40. *
  41. *Purpose:
  42. * Compares two strings for lexical order without regard to case.
  43. * Strings are compared on a character basis, not a byte basis.
  44. *
  45. *Entry:
  46. * char *s1, *s2 = strings to compare
  47. *
  48. *Exit:
  49. * returns <0 if s1 < s2
  50. * returns 0 if s1 == s2
  51. * returns >0 if s1 > s2
  52. * returns _NLSCMPERROR if NLS error
  53. *
  54. *Exceptions:
  55. *
  56. *******************************************************************************/
  57. int __cdecl _mbsicmp(
  58. const unsigned char *s1,
  59. const unsigned char *s2
  60. )
  61. {
  62. unsigned short c1, c2;
  63. #if !defined(_POSIX_)
  64. int retval;
  65. unsigned char szResult[4];
  66. #endif /* !_POSIX_ */
  67. #ifdef _MT
  68. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  69. if ( ptmbci != __ptmbcinfo )
  70. ptmbci = __updatetmbcinfo();
  71. if ( _ISNOTMBCP_MT(ptmbci) )
  72. #else
  73. if ( _ISNOTMBCP )
  74. #endif
  75. return _stricmp(s1, s2);
  76. for (;;)
  77. {
  78. c1 = *s1++;
  79. #ifdef _MT
  80. if ( __ismbblead_mt(ptmbci, c1) )
  81. #else
  82. if ( _ismbblead(c1) )
  83. #endif
  84. {
  85. if (*s1 == '\0')
  86. c1 = 0;
  87. else
  88. {
  89. #if !defined(_POSIX_)
  90. #ifdef _MT
  91. retval = __crtLCMapStringA( ptmbci->mblcid, LCMAP_UPPERCASE,
  92. s1 - 1, 2, szResult, 2,
  93. ptmbci->mbcodepage, TRUE );
  94. #else
  95. retval = __crtLCMapStringA( __mblcid, LCMAP_UPPERCASE,
  96. s1 - 1, 2, szResult, 2,
  97. __mbcodepage, TRUE );
  98. #endif
  99. if (retval == 1)
  100. c1 = szResult[0];
  101. else if (retval == 2)
  102. c1 = (szResult[0] << 8) + szResult[1];
  103. else
  104. return _NLSCMPERROR;
  105. s1++;
  106. #else /* !_POSIX_ */
  107. c1 = ((c1 << 8) | *s1++);
  108. if (c1 >= _MBUPPERLOW1 && c1 <= _MBUPPERHIGH1)
  109. c1 += _MBCASEDIFF1;
  110. else if (c1 >= _MBUPPERLOW2 && c1 <= _MBUPPERHIGH2)
  111. c1 += _MBCASEDIFF2;
  112. #endif /* !_POSIX_ */
  113. }
  114. }
  115. else
  116. #ifdef _MT
  117. c1 = __mbbtolower_mt(ptmbci, c1);
  118. #else
  119. c1 = _mbbtolower(c1);
  120. #endif
  121. c2 = *s2++;
  122. #ifdef _MT
  123. if ( __ismbblead_mt(ptmbci, c2) )
  124. #else
  125. if ( _ismbblead(c2) )
  126. #endif
  127. {
  128. if (*s2 == '\0')
  129. c2 = 0;
  130. else
  131. {
  132. #if !defined(_POSIX_)
  133. #ifdef _MT
  134. retval = __crtLCMapStringA( ptmbci->mblcid, LCMAP_UPPERCASE,
  135. s2 - 1, 2, szResult, 2,
  136. ptmbci->mbcodepage, TRUE );
  137. #else
  138. retval = __crtLCMapStringA( __mblcid, LCMAP_UPPERCASE,
  139. s2 - 1, 2, szResult, 2,
  140. __mbcodepage, TRUE );
  141. #endif
  142. if (retval == 1)
  143. c2 = szResult[0];
  144. else if (retval == 2)
  145. c2 = (szResult[0] << 8) + szResult[1];
  146. else
  147. return _NLSCMPERROR;
  148. s2++;
  149. #else /* !_POSIX_ */
  150. c2 = ((c2 << 8) | *s2++);
  151. if (c2 >= _MBUPPERLOW1 && c2 <= _MBUPPERHIGH1)
  152. c2 += _MBCASEDIFF1;
  153. else if (c2 >= _MBUPPERLOW2 && c2 <= _MBUPPERHIGH2)
  154. c2 += _MBCASEDIFF2;
  155. #endif /* !_POSIX_ */
  156. }
  157. }
  158. else
  159. #ifdef _MT
  160. c2 = __mbbtolower_mt(ptmbci, c2);
  161. #else
  162. c2 = _mbbtolower(c2);
  163. #endif
  164. if (c1 != c2)
  165. return( (c1 > c2) ? 1 : -1 );
  166. if (c1 == 0)
  167. return(0);
  168. }
  169. }
  170. #endif /* _MBCS */