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

  1. /*-----------------------------------------------------------------------*
  2.  * filename - strnicmp.cas
  3.  *
  4.  * function(s)
  5.  *        strnicmp  -  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            strnicmp  -  compare  one  string  to  another  without case
  27.                              sensitivity
  28.  
  29. Usage           int strnicmp(const char *str1, const char *str2, size_t maxlen);
  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.                 At most "maxlen" bytes will  be compared before stopping. A
  40.                 zero "maxlen"  results in an  equal strnicmp, i.e.  returns
  41.                 zero.
  42.  
  43.                 The strings *str1 and *str2 are not changed.
  44.  
  45.                 When  comparing to  punctuation characters  alphabetics are
  46.                 always treated as upper-case.
  47.  
  48. Return value    strcmp returns an integer value such as:
  49.                         < 0     if str1 is less than str2
  50.                         = 0     if str1 is the same as str2
  51.                         > 0     if str2 is greater than str2
  52.  
  53. *------------------------------------------------------------------------*/
  54. int  strnicmp(const char *str1, const char *str2, size_t maxlen)
  55. {
  56.         pushDS_
  57. #if !(LDATA)
  58.         _ES = _DS;
  59. #endif
  60. asm     cld
  61.  
  62. asm     LDS_    si, str1
  63. asm     LES_    di, str2
  64. asm     mov     cx, maxlen
  65.  
  66. /*
  67.   AH and BH will stay zero during this process. This'll be handy when its
  68.   time for a final subtract later.  Setting up the 'az' constant in DX can
  69.   have a slight payoff for strings greater than 3-4 chars in length(which
  70.   should be the usual case).
  71. */
  72. asm     xor     ax, ax        /* AX <- 0        */
  73. asm    mov    bx, ax        /* BX <- 0        */
  74. asm    mov    dx, 617aH    /* DH <- 'a', DL <- 'z'    */
  75.  
  76. nci_nextCh:
  77. asm     jcxz    nci_end        /* length limited?    */
  78. asm     lodsb            /* AL <- str1[i]    */
  79. asm     mov     bl, ES_ [di]    /* BL <- str2[i]    */
  80. asm     or      al, al        /* end of string?    */
  81. asm     jz      nci_end
  82. asm     scasb            /* str1[i] == str2[i]?, advance DI    */
  83. asm     loope   nci_nextCh
  84.  
  85. nci_alUpper:
  86. asm     cmp     al, dh        /* str1[i] < 'a'    */
  87. asm     jb      nci_blUpper
  88. asm     cmp     al, dl        /* str1[i] > 'z'    */
  89. asm     ja      nci_blUpper
  90. asm     sub     al, 'a' - 'A'    /* upper case str1[i]    */
  91.  
  92. nci_blUpper:
  93. asm     cmp     bl, dh        /* str2[i] < 'a'    */
  94. asm     jb      nci_finalDif
  95. asm     cmp     bl, dl        /* str2[i] > 'z'    */
  96. asm     ja      nci_finalDif
  97. asm     sub     bl, 'a' - 'A'    /* upper case str2[i]    */
  98.  
  99. nci_finalDif:
  100. asm     cmp     al, bl        /* compare after conversion */
  101. asm     je      nci_nextCh
  102.  
  103. nci_end:
  104. /*
  105.   We need to do the full word subtract here because ANSI requires unsigned
  106.   comparisons. A simple byte subtract with a CBW would produce the wrong
  107.   result in some cases. Remember that AH, BH are still zero.
  108. */
  109. asm     sub     ax, bx    /* Get the result. Note: AH & BH are still zero    */
  110.  
  111.         popDS_
  112.     return      _AX;
  113. }
  114.