home *** CD-ROM | disk | FTP | other *** search
/ Tricks of the Windows Gam…ming Gurus (2nd Edition) / Disc2.iso / vc98 / crt / src / mbsnbicm.c < prev    next >
C/C++ Source or Header  |  1998-06-17  |  3KB  |  124 lines

  1. /***
  2. *mbsnbicmp.c - Compare n bytes of strings, ignoring case (MBCS)
  3. *
  4. *       Copyright (c) 1985-1997, Microsoft Corporation.  All rights reserved.
  5. *
  6. *Purpose:
  7. *       Compare n bytes of strings, ignoring case (MBCS)
  8. *
  9. *******************************************************************************/
  10.  
  11. #ifdef _MBCS
  12.  
  13. #include <mtdll.h>
  14. #include <cruntime.h>
  15. #include <mbdata.h>
  16. #include <mbctype.h>
  17. #include <string.h>
  18. #include <mbstring.h>
  19.  
  20. /***
  21. * _mbsnbicmp - Compare n bytes of strings, ignoring case (MBCS)
  22. *
  23. *Purpose:
  24. *       Compares up to n bytes of two strings for lexical order.
  25. *       Strings are compared on a character basis, not a byte basis.
  26. *       Case of characters is not considered.
  27. *
  28. *Entry:
  29. *       unsigned char *s1, *s2 = strings to compare
  30. *       size_t n = maximum number of bytes to compare
  31. *
  32. *Exit:
  33. *       returns <0 if s1 < s2
  34. *       returns  0 if s1 == s2
  35. *       returns >0 if s1 > s2
  36. *
  37. *Exceptions:
  38. *
  39. *******************************************************************************/
  40.  
  41. int __cdecl _mbsnbicmp(
  42.         const unsigned char *s1,
  43.         const unsigned char *s2,
  44.         size_t n
  45.         )
  46. {
  47.         unsigned short c1, c2;
  48.  
  49.         if (n==0)
  50.                 return(0);
  51.  
  52.         if ( _ISNOTMBCP )
  53.             return _strnicmp(s1, s2, n);
  54.  
  55.         _mlock(_MB_CP_LOCK);
  56.  
  57.         while (n--) {
  58.  
  59.                 c1 = *s1++;
  60.                 if (_ISLEADBYTE(c1)) {
  61.                         if (n==0)
  62.                 {
  63.                     c1 = 0; /* 'naked' lead - end of string */
  64.                     c2 = _ISLEADBYTE(*s2) ? 0 : *s2;
  65.                     goto test;
  66.                 }
  67.                         if (*s1 == '\0')
  68.                                 c1 = 0;
  69.                         else {
  70.                                 c1 = ((c1<<8) | *s1++);
  71.  
  72.                     if ( ((c1 >= _MBUPPERLOW1) && (c1 <= _MBUPPERHIGH1)) )
  73.                         c1 += _MBCASEDIFF1;
  74.  
  75.                     else if ( ((c1 >= _MBUPPERLOW2) && (c1 <= _MBUPPERHIGH2)) )
  76.                         c1 += _MBCASEDIFF2;
  77.                         }
  78.                 }
  79.                 else
  80.                 c1 = _mbbtolower(c1);
  81.  
  82.                 c2 = *s2++;
  83.                 if (_ISLEADBYTE(c2)) {
  84.                         if (n==0)
  85.                 {
  86.                     c2 = 0; /* 'naked' lead - end of string */
  87.                     goto test;
  88.                 }
  89.                 n--;
  90.                         if (*s2 == '\0')
  91.                                 c2 = 0;
  92.                         else {
  93.                                 c2 = ((c2<<8) | *s2++);
  94.  
  95.                     if ( ((c2 >= _MBUPPERLOW1) && (c2 <= _MBUPPERHIGH1)) )
  96.                         c2 += _MBCASEDIFF1;
  97.  
  98.                     else if ( ((c2 >= _MBUPPERLOW2) && (c2 <= _MBUPPERHIGH2)) )
  99.                         c2 += _MBCASEDIFF2;
  100.                         }
  101.                 }
  102.                 else
  103.                 c2 = _mbbtolower(c2);
  104.  
  105. test:
  106.             if (c1 != c2)
  107.             {
  108.                 _munlock(_MB_CP_LOCK);
  109.                         return( (c1 > c2) ? 1 : -1);
  110.             }
  111.  
  112.             if (c1 == 0)
  113.             {
  114.                 _munlock(_MB_CP_LOCK);
  115.                         return(0);
  116.             }
  117.         }
  118.  
  119.         _munlock(_MB_CP_LOCK);
  120.         return(0);
  121. }
  122.  
  123. #endif  /* _MBCS */
  124.