home *** CD-ROM | disk | FTP | other *** search
/ Unix System Administration Handbook 1997 October / usah_oct97.iso / news / nn.tar / nn-6.5.1 / match.c < prev    next >
C/C++ Source or Header  |  1995-04-29  |  3KB  |  151 lines

  1. /*
  2.  *    (c) Copyright 1990, Kim Fabricius Storm.  All rights reserved.
  3.  *
  4.  *    String/Subject matching routines.
  5.  */
  6.  
  7. #include "config.h"
  8. #include "regexp.h"
  9.  
  10. export int case_fold_search = 1;
  11.  
  12. #define MAXFOLD    256    /* max length of any string */
  13.  
  14. /*
  15.  *    Systems which have a tolower(c) function which is defined for
  16.  *    all characters, but no _tolower(c) macro which works for
  17.  *    isupper(c) only, may define HAVE_GENERIC_TOLOWER -- it
  18.  *    may give a slight speed-up, but is not mandatory.
  19.  */
  20.  
  21. #ifndef HAVE_GENERIC_TOLOWER
  22. #ifndef _tolower
  23. #define _tolower(c) tolower(c)
  24. #endif
  25. #endif
  26.  
  27. void
  28. fold_string(mask)    /* convert mask to lower-case */
  29. register char *mask;
  30. {
  31.     register char c;
  32.  
  33.     for ( ; (c = *mask); mask++) {
  34. #ifdef _tolower
  35.     if (!isascii(c) || !isupper(c)) continue;
  36.     *mask = _tolower(c);
  37. #else
  38.     *mask = tolower(c);
  39. #endif
  40.     }
  41. }
  42.  
  43. int
  44. streq_fold(mask, str)        /* mask is prefix of str - FOLD */
  45. register char *mask, *str;
  46. {
  47.     register char c = 0, d;
  48.  
  49.     while ((d = *mask++)) {
  50.     if ((c = *str++) == NUL) return 0;
  51.     if (c == d) continue;
  52. #ifdef _tolower
  53.     if (!isascii(c) || !isupper(c) || _tolower(c) != (unsigned)d) return 0;
  54. #else
  55.     if (tolower(c) != d) return 0;
  56. #endif
  57.     }
  58.     return c == NUL ? 1 : 2;
  59. }
  60.  
  61. int
  62. strmatch_fold(mask, str)    /* mask occurs anywhere in str - FOLD */
  63. char *mask;
  64. register char *str;
  65. {
  66.     register char c, m1 = *mask++;
  67.  
  68.     for (;;) {
  69.     while ((c = *str++)) {    /* find first occ. of mask[0] in str. */
  70.         if (c == m1) break;
  71. #ifdef _tolower
  72.         if (!isascii(c) || !isupper(c)) continue;
  73.         if (_tolower(c) == (unsigned)m1) break;
  74. #else
  75.         if (tolower(c) == m1) break;
  76. #endif
  77.     }
  78.     if (c == NUL) return 0;
  79.     if (streq_fold(mask, str)) return 1;
  80.     }
  81. }
  82.  
  83. int
  84. strmatch(mask, str)        /* mask occurs anywhere in str - CASE */
  85. char *mask;
  86. register char *str;
  87. {
  88.     register char *q, *m;
  89.     register char m1 = *mask;
  90.  
  91.     for (; *str; str++) {
  92.     if (*str != m1) continue;
  93.  
  94.     q = str; m = mask;
  95.     do
  96.         if (*++m == NUL) return 1;
  97.     while (*++q == *m);
  98.     }
  99.     return 0;
  100. }
  101.  
  102. int
  103. strmatch_cf(mask, str)        /* fold if case_fold_search is set */
  104. char *mask;
  105. char *str;
  106. {
  107.     if (case_fold_search)
  108.     return strmatch_fold(mask, str);
  109.  
  110.     return strmatch(mask, str);
  111. }
  112.  
  113. /*
  114.  *    case insensitive regexp matching
  115.  */
  116.  
  117. int regexec_fold(prog, string)
  118. register regexp *prog;
  119. char  *string;
  120. {
  121.     char buf[256];
  122.     register char c, *bp, *str, *maxb;
  123.  
  124.     bp = buf, maxb = &buf[255];
  125.     str = string;
  126.     while (bp < maxb && (c = *str++) != NUL)
  127. #ifdef _tolower
  128.     *bp++ = (!isascii(c) || !isupper(c)) ? c : _tolower(c);
  129. #else
  130.     *bp++ = tolower(c);
  131. #endif
  132.     *bp = NUL;
  133.  
  134.     if (!regexec(prog, buf)) return 0;
  135.  
  136.     prog->startp[0] = string + (prog->startp[0] - buf);
  137.     prog->endp[0] = string + (prog->endp[0] - buf);
  138.     return 1;
  139. }
  140.  
  141. int regexec_cf(prog, string)
  142. register regexp *prog;
  143. char  *string;
  144. {
  145.     if (case_fold_search)
  146.     return regexec_fold(prog, string);
  147.  
  148.     return regexec(prog, string);
  149. }
  150.  
  151.