1: #include "../h/rt.h"
   2: 
   3: /*
   4:  * find(s1,s2,i,j) - find string s1 in s2[i:j] and return position in
   5:  *  s2 of beginning of s1.
   6:  * Generates successive positions.
   7:  */
   8: 
   9: Xfind(nargs, arg4, arg3, arg2, arg1, arg0)
  10: int nargs;
  11: struct descrip arg4, arg3, arg2, arg1, arg0;
  12:    {
  13:    register int l;
  14:    register char *s1, *s2;
  15:    int i, j, t;
  16:    long l1, l2;
  17:    char sbuf1[MAXSTRING], sbuf2[MAXSTRING];
  18: 
  19:    /*
  20:     * s1 must be a string.  s2 defaults to &subject; i defaults to &pos
  21:     *  if s defaulted, 1 otherwise;  j defaults to 0.
  22:     */
  23:    if (cvstr(&arg1, sbuf1) == NULL)
  24:       runerr(103, &arg1);
  25:    if (defstr(&arg2, sbuf2, &k_subject))
  26:       defint(&arg3, &l1, k_pos);
  27:    else
  28:       defint(&arg3, &l1, 1);
  29:    defint(&arg4, &l2, 0);
  30: 
  31:    /*
  32:     * Convert i and j to absolute positions in s2 and order them so i <= j.
  33:     */
  34:    i = cvpos(l1, STRLEN(arg2));
  35:    j = cvpos(l2, STRLEN(arg2));
  36:    if (i > j) {
  37:       t = i;
  38:       i = j;
  39:       j = t;
  40:       }
  41: 
  42:    /*
  43:     * Loop through s2[i:j] trying to find s1 at each point, stopping
  44:     *  when the remaining portion s2[i:j] is too short to contain s1.
  45:     */
  46:    while (i <= j - STRLEN(arg1)) {
  47:       s1 = STRLOC(arg1);
  48:       s2 = STRLOC(arg2) + i - 1;
  49:       l = STRLEN(arg1);
  50:       /*
  51:        * Compare strings on bytewise basis; if end is reached before
  52:        *  inequality is found, suspend the position of the string.
  53:        */
  54:       do {
  55:          if (l-- <= 0) {
  56:             arg0.type = D_INTEGER;
  57:             INTVAL(arg0) = i;
  58:             suspend();
  59:             break;
  60:             }
  61:          } while (*s1++ == *s2++);
  62:       i++;
  63:       }
  64: 
  65:    fail();
  66:    }
  67: 
  68: Procblock(find,4)

Defined functions

Xfind defined in line 9; never used
Last modified: 1984-11-18
Generated: 2016-12-26
Generated by src2html V0.67
page hit count: 791
Valid CSS Valid XHTML 1.0 Strict