home *** CD-ROM | disk | FTP | other *** search
/ Usenet 1994 October / usenetsourcesnewsgroupsinfomagicoctober1994disk2.iso / misc / volume6 / copt / abbrev.c next >
Encoding:
C/C++ Source or Header  |  1989-03-07  |  2.3 KB  |  82 lines

  1. static char rcsid[] = "$Header";
  2. /* $Log:    abbrev.c,v $
  3.  * Revision 1.1  89/03/03  12:53:21  np
  4.  * Initial revision
  5.  * 
  6.  */
  7.  
  8. /*
  9.  * Copyright (C) 1985-1989 Nigel Perry
  10.  *
  11.  * This program is distributed without any warranty.
  12.  * The author and and distributors accept no resposibility
  13.  * to anyone for the consequence of using this program or
  14.  * whether it serves any particular purpose or works at all,
  15.  * unless they say so in writing.
  16.  *
  17.  * Everyone is granted permission to copy, modify and
  18.  * redistribute this program, but only provided that:
  19.  *
  20.  * 1) They do so without financial or material gain.
  21.  *
  22.  * 2) The copyright notice and this notice are preserved on
  23.  *    all copies.
  24.  *
  25.  * 3) The original source is preserved in any redistribution.
  26.  *
  27.  * I hope you find this program useful!
  28.  *
  29.  */
  30.  
  31. #define repeat while(1)  /* try to make C look high level! */
  32.  
  33. #define REQUIRED 32
  34. #define OPTIONAL 1
  35.  
  36. /* check if string is a valid abbreviation of pattern
  37.  *
  38.  * In a pattern:
  39.  *   a thru z and _ are optional
  40.  *   other characters must be present
  41.  *
  42.  * Return:
  43.  *  -1 : no match
  44.  *  n = REQUIRED * (number of required matches) + OPTIONAL * (number of optional characters matched)
  45.  *
  46.  * e.g
  47.  *    Pattern        String        Return
  48.  *    LineFeed    lf        2 * REQUIRED
  49.  *    Longform    lf        1 * REQUIRED + 1 * OPTIONAL
  50.  * => lf is a better match for LineFeed
  51.  *
  52.  * The higher the result the better the match.
  53.  *
  54.  */
  55.  
  56. int abbrev(pat, str) register char *pat, *str;
  57. {  register char c, pc;
  58.    register int score, sc;
  59.  
  60.    score = 0;
  61.    c = *str++;
  62.    if(c >= 'a' && c <= 'z') c += 'A' - 'a';
  63.    repeat
  64.       if( (pc = *pat++) == '\0' )        /* this SHOULD be a case... */
  65.      return( c == '\0' ? score : -1 );
  66.       else if(pc == '_') /* optional */
  67.       {  if(c != '_') continue;            /* try next pattern character */
  68.      if( (sc = abbrev(pat, str)) >= 0 ) return(score + OPTIONAL + sc);
  69.      /* rest of match failed, try next pattern char */
  70.       }
  71.       else if(pc >= 'a' && pc <= 'z')        /* = case 'a' :: 'z', B rules! */
  72.       {  if( (c - 'A' + 'a') != pc ) continue;    /* no match, try next pat */
  73.      if( (sc = abbrev(pat, str)) >= 0 ) return(score + OPTIONAL + sc);
  74.       }
  75.       else /* must match */
  76.       {  if(c != pc) return(-1);
  77.      score += REQUIRED;
  78.      c = *str++;                /* get next str char */
  79.      if(c >= 'a' && c <= 'z') c += 'A' - 'a';
  80.       }
  81. }
  82.