home *** CD-ROM | disk | FTP | other *** search
/ The C Users' Group Library 1994 August / wc-cdrom-cusersgrouplibrary-1994-08.iso / listings / v_10_11 / 1011020a < prev    next >
Text File  |  1992-09-03  |  1KB  |  56 lines

  1. #include "xstrxfrm.h"
  2.  
  3.         /* type definitions */
  4. typedef struct {
  5.     char buf[32];
  6.     const unsigned char *s1, *s2, *sout;
  7.     _Cosave state;
  8.     } Sctl;
  9.  
  10. static size_t getxfrm(Sctl *p)
  11.     {    /* get transformed chars */
  12.     size_t i;
  13.  
  14.     do    {    /* loop until chars delivered */
  15.         p->sout = (const unsigned char *)p->buf;
  16.         i = _Strxfrm(p->buf, &p->s1, sizeof (p->buf), &p->state);
  17.         if (0 < i && p->buf[i - 1] == '\0')
  18.             return (i - 1);
  19.         else if (*p->s1 == '\0')
  20.             p->s1 = p->s2;    /* rescan */
  21.         } while (i == 0);
  22.     return (i);
  23.     }
  24.  
  25. int (strcoll)(const char *s1, const char *s2)
  26.     {    /* compare s1[], s2[] using locale-dependent rule */
  27.     size_t n1, n2;
  28.     Sctl st1, st2;
  29.     static const _Cosave initial = {0};
  30.  
  31.     st1.s1 = (const unsigned char *)s1;
  32.     st1.s2 = (const unsigned char *)s1;
  33.     st1.state = initial;
  34.     st2.s1 = (const unsigned char *)s2;
  35.     st2.s2 = (const unsigned char *)s2;
  36.     st2.state = initial;
  37.     for (n1 = n2 = 0; ; )
  38.         {    /* compare transformed chars */
  39.         int ans;
  40.         size_t n;
  41.  
  42.         if (n1 == 0)
  43.             n1 = getxfrm(&st1);
  44.         if (n2 == 0)
  45.             n2 = getxfrm(&st2);
  46.         n = n1 < n2 ? n1 : n2;
  47.         if (n == 0)
  48.             return (n1 == n2 ? 0 : 0 < n2 ? -1 : +1);
  49.         else if ((ans = memcmp(st1.sout, st2.sout, n)) != 0)
  50.             return (ans);
  51.         st1.sout += n, n1 -= n;
  52.         st2.sout += n, n2 -= n;
  53.         }
  54.     }
  55.  
  56.