Annotation of 43BSD/contrib/icon/functions/match.c, revision 1.1.1.1

1.1       root        1: #include "../h/rt.h"
                      2: 
                      3: /*
                      4:  * match(s1,s2,i,j) - test if s1 is prefix of s2[i:j].
                      5:  */
                      6: Xmatch(nargs, arg4, arg3, arg2, arg1, arg0)
                      7: int nargs;
                      8: struct descrip arg4, arg3, arg2, arg1, arg0;
                      9:    {
                     10:    register int i;
                     11:    register char *s1, *s2;
                     12:    int j, t;
                     13:    long l1, l2;
                     14:    char sbuf1[MAXSTRING], sbuf2[MAXSTRING];
                     15: 
                     16:    /*
                     17:     * s1 must be a string.  s2 defaults to &subject;  i defaults to &pos
                     18:     *  if s defaulted, 1 otherwise; j defaults to 0.
                     19:     */
                     20:    if (cvstr(&arg1, sbuf1) == NULL)
                     21:       runerr(103, &arg1);
                     22:    if (defstr(&arg2, sbuf2, &k_subject))
                     23:       defint(&arg3, &l1, k_pos);
                     24:    else
                     25:       defint(&arg3, &l1, 1);
                     26:    defint(&arg4, &l2, 0);
                     27: 
                     28:    /*
                     29:     * Convert i and j to absolute positions and then make i the smaller
                     30:     *  of the two positions and make j the length of the substring.
                     31:     */
                     32:    i = cvpos(l1, STRLEN(arg2));
                     33:    j = cvpos(l2, STRLEN(arg2));
                     34:    if (i > j) {
                     35:       t = i;
                     36:       i = j;
                     37:       j = t - j;
                     38:       }
                     39:    else
                     40:       j = j - i;
                     41: 
                     42:    /*
                     43:     * Can't match unless s1 is as long as s2[i:j].
                     44:     */
                     45:    if (j < STRLEN(arg1))
                     46:       fail();
                     47: 
                     48:    /*
                     49:     * Compare s1 with s2[i:j] for *s1 characters; fail if an inequality
                     50:     *  if found.
                     51:     */
                     52:    s1 = STRLOC(arg1);
                     53:    s2 = STRLOC(arg2) + i - 1;
                     54:    for (j = STRLEN(arg1); j > 0; j--)
                     55:       if (*s1++ != *s2++)
                     56:          fail();
                     57: 
                     58:    /*
                     59:     * Return position of end of matched string in s2.
                     60:     */
                     61:    arg0.type = D_INTEGER;
                     62:    INTVAL(arg0) = i + STRLEN(arg1);
                     63:    }
                     64: 
                     65: Procblock(match,4)

unix.superglobalmegacorp.com

This archive runs on limited infrastructure. Preserving old code on modern bandwidth. Automated agents are requested to crawl responsibly.