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.

136 lines
3.9 KiB

  1. /***
  2. *strnicmp.c - compare n chars of strings, ignoring case
  3. *
  4. * Copyright (c) 1985-2001, Microsoft Corporation. All rights reserved.
  5. *
  6. *Purpose:
  7. * defines _strnicmp() - Compares at most n characters of two strings,
  8. * without regard to case.
  9. *
  10. *Revision History:
  11. * 02-27-90 GJF Fixed calling type, #include <cruntime.h>, fixed
  12. * copyright.
  13. * 10-02-90 GJF New-style function declarator.
  14. * 01-18-91 GJF ANSI naming.
  15. * 10-11-91 GJF Bug fix! Comparison of final bytes must use unsigned
  16. * chars.
  17. * 09-03-93 GJF Replaced _CALLTYPE1 with __cdecl.
  18. * 09-21-93 CFW Avoid cast bug.
  19. * 01-13-94 CFW Fix Comments.
  20. * 10-19-94 GJF Sped up C locale. Also, made multi-thread safe.
  21. * 12-29-94 CFW Merge non-Win32.
  22. * 09-26-95 GJF New locking macro, and scheme, for functions which
  23. * reference the locale.
  24. * 11-15-95 BWT Fix _NTSUBSET_
  25. * 08-11-98 GJF Revised multithread support based on threadlocinfo
  26. * struct.
  27. * 09-08-98 GJF Split out ASCII-only version.
  28. * 05-17-99 PML Remove all Macintosh support.
  29. * 26-01-00 GB Modified strnicmp for performance.
  30. * 09-03-00 GB Moved the performance code to toupper and tolower.
  31. * restored the original file.
  32. *
  33. *******************************************************************************/
  34. #include <cruntime.h>
  35. #include <string.h>
  36. #include <mtdll.h>
  37. #include <ctype.h>
  38. #include <setlocal.h>
  39. #include <locale.h>
  40. /***
  41. *int _strnicmp(first, last, count) - compares count char of strings, ignore case
  42. *
  43. *Purpose:
  44. * Compare the two strings for lexical order. Stops the comparison
  45. * when the following occurs: (1) strings differ, (2) the end of the
  46. * strings is reached, or (3) count characters have been compared.
  47. * For the purposes of the comparison, upper case characters are
  48. * converted to lower case.
  49. *
  50. *Entry:
  51. * char *first, *last - strings to compare
  52. * size_t count - maximum number of characters 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. *Exceptions:
  60. *
  61. *******************************************************************************/
  62. int __cdecl _strnicmp (
  63. const char * dst,
  64. const char * src,
  65. size_t count
  66. )
  67. {
  68. int f,l;
  69. #ifdef _MT
  70. pthreadlocinfo ptloci = _getptd()->ptlocinfo;
  71. if ( ptloci != __ptlocinfo )
  72. ptloci = __updatetlocinfo();
  73. #endif
  74. if ( count )
  75. {
  76. #if !defined(_NTSUBSET_)
  77. #ifdef _MT
  78. if ( ptloci->lc_handle[LC_CTYPE] == _CLOCALEHANDLE ) {
  79. #else
  80. if ( __lc_handle[LC_CTYPE] == _CLOCALEHANDLE ) {
  81. #endif
  82. #endif /* !_NTSUBSET_ */
  83. return __ascii_strnicmp(dst, src, count);
  84. #if !defined(_NTSUBSET_)
  85. }
  86. else {
  87. do {
  88. #ifdef _MT
  89. f = __tolower_mt( ptloci, (unsigned char)(*(dst++)) );
  90. l = __tolower_mt( ptloci, (unsigned char)(*(src++)) );
  91. #else
  92. f = tolower( (unsigned char)(*(dst++)) );
  93. l = tolower( (unsigned char)(*(src++)) );
  94. #endif
  95. } while (--count && f && (f == l) );
  96. }
  97. #endif /* !_NTSUBSET_ */
  98. return( f - l );
  99. }
  100. return( 0 );
  101. }
  102. #ifndef _M_IX86
  103. int __cdecl __ascii_strnicmp (
  104. const char * first,
  105. const char * last,
  106. size_t count
  107. )
  108. {
  109. int f, l;
  110. do {
  111. if ( ((f = (unsigned char)(*(first++))) >= 'A') &&
  112. (f <= 'Z') )
  113. f -= 'A' - 'a';
  114. if ( ((l = (unsigned char)(*(last++))) >= 'A') &&
  115. (l <= 'Z') )
  116. l -= 'A' - 'a';
  117. } while ( --count && f && (f == l) );
  118. return ( f - l );
  119. }
  120. #endif