home *** CD-ROM | disk | FTP | other *** search
/ Liren Large Software Subsidy 7 / 07.iso / c / c065 / 2.ddi / CLIB2.ZIP / STRICMP.CAS < prev    next >
Encoding:
Text File  |  1990-06-07  |  4.0 KB  |  112 lines

  1. /*-----------------------------------------------------------------------*
  2.  * filename - stricmp.cas
  3.  *
  4.  * function(s)
  5.  *        stricmp - compare  one  string  to  another  without case
  6.  *                  sensitivity
  7.  *-----------------------------------------------------------------------*/
  8.  
  9. /*[]------------------------------------------------------------[]*/
  10. /*|                                                              |*/
  11. /*|     Turbo C Run Time Library - Version 3.0                   |*/
  12. /*|                                                              |*/
  13. /*|                                                              |*/
  14. /*|     Copyright (c) 1987,1988,1990 by Borland International    |*/
  15. /*|     All Rights Reserved.                                     |*/
  16. /*|                                                              |*/
  17. /*[]------------------------------------------------------------[]*/
  18.  
  19.  
  20. #pragma inline
  21. #include <asmrules.h>
  22. #include <string.h>
  23.  
  24. /*-----------------------------------------------------------------------*
  25.  
  26. Name            stricmp - compare  one  string  to  another  without case
  27.                           sensitivity
  28.  
  29. Usage           int stricmp(const char *str1, const char *str2);
  30.  
  31. Prototype in    string.h
  32.  
  33. Description     Case-independent  comparison of  *str1 with  *str2. Compare
  34.                 the strings, but act as  if upper and lower case characters
  35.                 were  always  upper-case,  returning  a  negative, zero, or
  36.                 positive integer  according to whether *str1  is less than,
  37.                 equal, or greater than *str2, respectively.
  38.  
  39.                 The strings *str1 and *str2 are not changed.
  40.  
  41.                 When  comparing to  punctuation characters  alphabetics are
  42.                 always treated as upper-case.
  43.  
  44. Return value    strcmp return an integer value such as:
  45.                         < 0     if str1 is less than str2
  46.                         = 0     if str1 is the same as str2
  47.                         > 0     if str2 is greater than str2
  48.  
  49. *------------------------------------------------------------------------*/
  50. int _CType stricmp(const char *str1, const char *str2)
  51. {
  52. #if  defined(__LARGE__) || defined(__COMPACT__)
  53. asm     mov     dx, ds
  54. #endif
  55. #if !(LDATA)
  56. asm     mov     ax, ds
  57. asm     mov     es, ax
  58. #endif
  59. asm     cld
  60.  
  61. asm     LDS_    si, str1
  62. asm     LES_    di, str2
  63.  
  64. /*
  65.   We setup some constants in registers because there's a slight payoff
  66.   when the strings get longer than 3-4 characters (which should be most
  67.   of the time in a typical program).
  68. */
  69. asm     xor     ax, ax  /* AH and BH stay zero until the end            */
  70. asm     mov     bx, ax  /* when the final sub AX, BX is done            */
  71. asm     mov     cx, 617aH       /* CH = 'a',  CL = 'z'                  */
  72.  
  73. cmi_nextCh:
  74. asm     lodsb                   /* AL <- str1[i]        */
  75. asm     mov     bl, ES_ [di]    /* BL <- str2[i]        */
  76. asm     or      al, al          /* null terminator?     */
  77. asm     jz      cmi_end
  78. asm     scasb                   /* test & advance DI    */
  79. asm     je      cmi_nextCh
  80.  
  81. cmi_alUpper:
  82. asm     cmp     al, ch          /* str1[i] < 'a' */
  83. asm     jb      cmi_blUpper
  84. asm     cmp     al, cl          /* str1[i] > 'z' */
  85. asm     ja      cmi_blUpper
  86. asm     sub     al, 'a'-'A'     /* upper case str1[i] */
  87.  
  88. cmi_blUpper:
  89. asm     cmp     bl, ch          /* str2[i] < 'a' */
  90. asm     jb      cmi_compareAgain
  91. asm     cmp     bl, cl          /* str2[i] > 'z' */
  92. asm     ja      cmi_compareAgain
  93. asm     sub     bl, 'a'-'A'     /* upper case str2[i] */
  94.  
  95. cmi_compareAgain:
  96. asm     cmp     al, bl          /* str1[i] == str2[i] */
  97. asm     je      cmi_nextCh
  98.  
  99. cmi_end:
  100. /*
  101.   We need to do the full word subtract here because ANSI requires unsigned
  102.   comparisons. A simple byte subtract with a CBW would produce the wrong
  103.   result in some cases. Remember that AH, BH are still zero.
  104. */
  105. asm     sub     ax, bx
  106.  
  107. #if  defined(__LARGE__) || defined(__COMPACT__)
  108. asm     mov     ds, dx
  109. #endif
  110.         return _AX;
  111. }
  112.