home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 January / usenetsourcesnewsgroupsinfomagicjanuary1994.iso / sources / unix / volume22 / nn6.4 / part20 / match.c < prev    next >
Encoding:
C/C++ Source or Header  |  1990-06-07  |  2.8 KB  |  146 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. fold_string(mask)    /* convert mask to lower-case */
  28. register char *mask;
  29. {
  30.     register char c;
  31.  
  32.     for ( ; c = *mask; mask++) {
  33. #ifdef _tolower
  34.     if (!isascii(c) || !isupper(c)) continue;
  35.     *mask = _tolower(c);
  36. #else
  37.     *mask = tolower(c);
  38. #endif
  39.     }
  40. }
  41.  
  42. streq_fold(mask, str)        /* mask is prefix of str - FOLD */
  43. register char *mask, *str;
  44. {
  45.     register char c, d;
  46.  
  47.     while (d = *mask++) {
  48.     if ((c = *str++) == NUL) return 0;
  49.     if (c == d) continue;
  50. #ifdef _tolower
  51.     if (!isascii(c) || !isupper(c) || _tolower(c) != d) return 0;
  52. #else
  53.     if (tolower(c) != d) return 0;
  54. #endif
  55.     }
  56.     return c == NUL ? 1 : 2;
  57. }
  58.  
  59. strmatch_fold(mask, str)    /* mask occurs anywhere in str - FOLD */
  60. char *mask;
  61. register char *str;
  62. {
  63.     register char c, m1 = *mask++;
  64.  
  65.     for (;;) {
  66.     while (c = *str++) {    /* find first occ. of mask[0] in str. */
  67.         if (c == m1) break;
  68. #ifdef _tolower
  69.         if (!isascii(c) || !isupper(c)) continue;
  70.         if (_tolower(c) == m1) break;
  71. #else
  72.         if (tolower(c) == m1) break;
  73. #endif
  74.     }
  75.     if (c == NUL) return 0;
  76.     if (streq_fold(mask, str)) return 1;
  77.     }
  78. }
  79.  
  80. strmatch(mask, str)        /* mask occurs anywhere in str - CASE */
  81. char *mask;
  82. register char *str;
  83. {
  84.     register char *q, *m;
  85.     register char m1 = *mask;
  86.  
  87.     for (; *str; str++) {
  88.     if (*str != m1) continue;
  89.  
  90.     q = str; m = mask;
  91.     do
  92.         if (*++m == NUL) return 1;
  93.     while (*++q == *m);
  94.     }
  95.     return 0;
  96. }
  97.  
  98. strmatch_cf(mask, str)        /* fold if case_fold_search is set */
  99. char *mask;
  100. char *str;
  101. {
  102.     if (case_fold_search)
  103.     return strmatch_fold(mask, str);
  104.  
  105.     return strmatch(mask, str);
  106. }
  107.  
  108. /*
  109.  *    case insensitive regexp matching
  110.  */
  111.  
  112. int regexec_fold(prog, string)
  113. register regexp *prog;
  114. char  *string;
  115. {
  116.     char buf[256];
  117.     register char c, *bp, *str, *maxb;
  118.  
  119.     bp = buf, maxb = &buf[255];
  120.     str = string;
  121.     while (bp < maxb && (c = *str++) != NUL)
  122. #ifdef _tolower
  123.     *bp++ = (!isascii(c) || !isupper(c)) ? c : _tolower(c);
  124. #else
  125.     *bp++ = tolower(c);
  126. #endif
  127.     *bp = NUL;
  128.  
  129.     if (!regexec(prog, buf)) return 0;
  130.  
  131.     prog->startp[0] = string + (prog->startp[0] - buf);
  132.     prog->endp[0] = string + (prog->endp[0] - buf);
  133.     return 1;
  134. }
  135.  
  136. int regexec_cf(prog, string)
  137. register regexp *prog;
  138. char  *string;
  139. {
  140.     if (case_fold_search)
  141.     return regexec_fold(prog, string);
  142.  
  143.     return regexec(prog, string);
  144. }
  145.  
  146.