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