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.

129 lines
3.3 KiB

  1. /***
  2. *mbsnbcmp.c - Compare n bytes of two MBCS strings
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Compare n bytes of two MBCS strings
  8. *
  9. *Revision History:
  10. * 08-03-93 KRS Ported from 16-bit sources.
  11. * 10-05-93 GJF Replaced _CRTAPI1 with __cdecl.
  12. * 04-12-94 CFW Make function generic.
  13. * 04-18-94 CFW Don't ignore case.
  14. * 05-05-94 CFW Work around NT/Chico bug: CompareString ignores
  15. * control characters.
  16. * 05-09-94 CFW Return EQUAL when loop exits.
  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-19-94 CFW Enable non-Win32.
  20. * 05-20-94 CFW Bug fix: if last char is LB, must still test values.
  21. * 05-27-94 CFW Last char LB should end string.
  22. * 06-02-94 CFW Fix comments.
  23. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  24. * 04-13-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. *int mbsnbcmp(s1, s2, n) - Compare n bytes of two MBCS strings
  37. *
  38. *Purpose:
  39. * Compares up to n bytes of two strings for lexical order.
  40. *
  41. *Entry:
  42. * unsigned char *s1, *s2 = strings to compare
  43. * size_t n = maximum number of bytes to compare
  44. *
  45. *Exit:
  46. * returns <0 if s1 < s2
  47. * returns 0 if s1 == s2
  48. * returns >0 if s1 > s2
  49. *
  50. *Exceptions:
  51. *
  52. *******************************************************************************/
  53. int __cdecl _mbsnbcmp(
  54. const unsigned char *s1,
  55. const unsigned char *s2,
  56. size_t n
  57. )
  58. {
  59. unsigned short c1, c2;
  60. #ifdef _MT
  61. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  62. #endif
  63. if (n==0)
  64. return(0);
  65. #ifdef _MT
  66. if ( ptmbci != __ptmbcinfo )
  67. ptmbci = __updatetmbcinfo();
  68. if ( _ISNOTMBCP_MT(ptmbci) )
  69. #else
  70. if ( _ISNOTMBCP )
  71. #endif
  72. return strncmp(s1, s2, n);
  73. while (n--) {
  74. c1 = *s1++;
  75. #ifdef _MT
  76. if ( __ismbblead_mt(ptmbci, c1) )
  77. #else
  78. if ( _ismbblead(c1) )
  79. #endif
  80. {
  81. if (n==0)
  82. {
  83. c1 = 0; /* 'naked' lead - end of string */
  84. #ifdef _MT
  85. c2 = __ismbblead_mt(ptmbci, *s2) ? 0 : *s2;
  86. #else
  87. c2 = _ismbblead(*s2) ? 0 : *s2;
  88. #endif
  89. goto test;
  90. }
  91. c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) );
  92. }
  93. c2 = *s2++;
  94. #ifdef _MT
  95. if ( __ismbblead_mt(ptmbci, c2) )
  96. #else
  97. if ( _ismbblead(c2) )
  98. #endif
  99. {
  100. if (n==0)
  101. {
  102. c2 = 0; /* 'naked' lead - end of string */
  103. goto test;
  104. }
  105. --n;
  106. c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) );
  107. }
  108. test:
  109. if (c1 != c2)
  110. return( (c1 > c2) ? 1 : -1);
  111. if (c1 == 0)
  112. return(0);
  113. }
  114. return(0);
  115. }
  116. #endif /* _MBCS */