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.

107 lines
2.8 KiB

  1. /***
  2. *mbsncmp.c - Compare n characters of two MBCS strings
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * Compare n characters of two MBCS strings
  8. *
  9. *Revision History:
  10. * 11-19-92 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-15-93 CFW Add _MB_CP_LOCK.
  14. * 04-21-93 CFW Update pointer.
  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-19-94 CFW Enable non-Win32.
  20. * 09-11-97 GJF Replaced __mbcodepage == 0 with _ISNOTMBCP.
  21. * 04-15-98 GJF Revised multithread support based on threadmbcinfo
  22. * structs
  23. *
  24. *******************************************************************************/
  25. #ifdef _MBCS
  26. #include <mtdll.h>
  27. #include <cruntime.h>
  28. #include <mbdata.h>
  29. #include <mbctype.h>
  30. #include <string.h>
  31. #include <mbstring.h>
  32. /***
  33. *int mbsncmp(s1, s2, n) - Compare n characters of two MBCS strings
  34. *
  35. *Purpose:
  36. * Compares up to n charcters of two strings for lexical order.
  37. * Strings are compared on a character basis, not a byte basis.
  38. *
  39. *Entry:
  40. * unsigned char *s1, *s2 = strings to compare
  41. * size_t n = maximum number of characters to compare
  42. *
  43. *Exit:
  44. * returns <0 if s1 < s2
  45. * returns 0 if s1 == s2
  46. * returns >0 if s1 > s2
  47. *
  48. *Exceptions:
  49. *
  50. *******************************************************************************/
  51. int __cdecl _mbsncmp(
  52. const unsigned char *s1,
  53. const unsigned char *s2,
  54. size_t n
  55. )
  56. {
  57. unsigned short c1, c2;
  58. #ifdef _MT
  59. pthreadmbcinfo ptmbci = _getptd()->ptmbcinfo;
  60. if ( ptmbci != __ptmbcinfo )
  61. ptmbci = __updatetmbcinfo();
  62. #endif
  63. if (n==0)
  64. return(0);
  65. #ifdef _MT
  66. if ( _ISNOTMBCP_MT(ptmbci) )
  67. #else
  68. if ( _ISNOTMBCP )
  69. #endif
  70. return strncmp(s1, s2, n);
  71. while (n--) {
  72. c1 = *s1++;
  73. #ifdef _MT
  74. if ( __ismbblead_mt(ptmbci, c1) )
  75. #else
  76. if ( _ismbblead(c1) )
  77. #endif
  78. c1 = ( (*s1 == '\0') ? 0 : ((c1<<8) | *s1++) );
  79. c2 = *s2++;
  80. #ifdef _MT
  81. if ( __ismbblead_mt(ptmbci, c2) )
  82. #else
  83. if ( _ismbblead(c2) )
  84. #endif
  85. c2 = ( (*s2 == '\0') ? 0 : ((c2<<8) | *s2++) );
  86. if (c1 != c2)
  87. return( (c1 > c2) ? 1 : -1);
  88. if (c1 == 0)
  89. return(0);
  90. }
  91. return(0);
  92. }
  93. #endif /* _MBCS */