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