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.

134 lines
4.0 KiB

  1. /***
  2. *memicmp.c - compare memory, ignore case
  3. *
  4. * Copyright (c) 1988-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _memicmp() - compare two blocks of memory for lexical
  8. * order. Case is ignored in the comparison.
  9. *
  10. *Revision History:
  11. * 05-31-89 JCR C version created.
  12. * 02-27-90 GJF Fixed calling type, #include <cruntime.h>, fixed
  13. * copyright. Also, fixed compiler warnings.
  14. * 10-01-90 GJF New-style function declarator. Also, rewrote expr. to
  15. * avoid using cast as an lvalue.
  16. * 01-17-91 GJF ANSI naming.
  17. * 10-11-91 GJF Bug fix! Comparison of final bytes must use unsigned
  18. * chars.
  19. * 09-01-93 GJF Replaced _CALLTYPE1 with __cdecl.
  20. * 10-18-94 GJF Sped up, especially for C locale. Also, made multi-
  21. * thread safe.
  22. * 12-29-94 CFW Merge non-Win32.
  23. * 09-26-95 GJF New locking macro, and scheme, for functions which
  24. * reference the locale.
  25. * 11-15-95 BWT Fix _NTSUBSET_
  26. * 08-27-98 GJF Revised multithread support based on threadlocinfo
  27. * struct.
  28. * 09-08-98 GJF Split out ASCII-only version.
  29. * 05-17-99 PML Remove all Macintosh support.
  30. * 10-27-99 PML Win64 fix: unsigned int -> size_t
  31. * 26-01-00 GB Modified memicmp for performance.
  32. * 09-03-00 GB Moved the performance code to toupper and tolower.
  33. * restored the original file.
  34. *
  35. *******************************************************************************/
  36. #include <cruntime.h>
  37. #include <string.h>
  38. #include <mtdll.h>
  39. #include <ctype.h>
  40. #include <setlocal.h>
  41. #include <locale.h>
  42. /***
  43. *int _memicmp(first, last, count) - compare two blocks of memory, ignore case
  44. *
  45. *Purpose:
  46. * Compares count bytes of the two blocks of memory stored at first
  47. * and last. The characters are converted to lowercase before
  48. * comparing (not permanently), so case is ignored in the search.
  49. *
  50. *Entry:
  51. * char *first, *last - memory buffers to compare
  52. * size_t count - maximum length to compare
  53. *
  54. *Exit:
  55. * returns < 0 if first < last
  56. * returns 0 if first == last
  57. * returns > 0 if first > last
  58. *
  59. *Uses:
  60. *
  61. *Exceptions:
  62. *
  63. *******************************************************************************/
  64. int __cdecl _memicmp (
  65. const void * first,
  66. const void * last,
  67. size_t count
  68. )
  69. {
  70. int f = 0, l = 0;
  71. const char *dst = first, *src = last;
  72. #if !defined(_NTSUBSET_)
  73. #ifdef _MT
  74. pthreadlocinfo ptloci = _getptd()->ptlocinfo;
  75. if ( ptloci != __ptlocinfo )
  76. ptloci = __updatetlocinfo();
  77. if ( ptloci->lc_handle[LC_CTYPE] == _CLOCALEHANDLE )
  78. #else
  79. if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE )
  80. #endif
  81. {
  82. #endif /* !_NTSUBSET_ */
  83. return __ascii_memicmp(first, last, count);
  84. #if !defined(_NTSUBSET_)
  85. }
  86. else {
  87. while (count-- && f==l)
  88. {
  89. #ifdef _MT
  90. f = __tolower_mt( ptloci, (unsigned char)(*(dst++)) );
  91. l = __tolower_mt( ptloci, (unsigned char)(*(src++)) );
  92. #else
  93. f = tolower( (unsigned char)(*(dst++)) );
  94. l = tolower( (unsigned char)(*(src++)) );
  95. #endif
  96. }
  97. }
  98. #endif /* !_NTSUBSET_ */
  99. return ( f - l );
  100. }
  101. #ifndef _M_IX86
  102. int __cdecl __ascii_memicmp (
  103. const void * first,
  104. const void * last,
  105. size_t count
  106. )
  107. {
  108. int f = 0;
  109. int l = 0;
  110. while ( count-- )
  111. {
  112. if ( (*(unsigned char *)first == *(unsigned char *)last) ||
  113. ((f = __ascii_tolower( *(unsigned char *)first )) ==
  114. (l = __ascii_tolower( *(unsigned char *)last ))) )
  115. {
  116. first = (char *)first + 1;
  117. last = (char *)last + 1;
  118. }
  119. else
  120. break;
  121. }
  122. return ( f - l );
  123. }
  124. #endif