Пример #1
0
static int wstr_gsub (lua_State *L) {
    size_t srcl;
    const lua_WChar *src = luaL_checklwstring(L, 1, &srcl);
    const lua_WChar *p = luaL_checkwstring(L, 2);
    int max_s = luaL_optint(L, 4, srcl+1);
    int anchor = (*p == '^') ? (p++, 1) : 0;
    int n = 0;
    WMatchState ms;
    luaL_Buffer b;
    luaL_wbuffinit(L, &b);
    ms.L = L;
    ms.src_init = src;
    ms.src_end = src+srcl;
    while (n < max_s) {
        const lua_WChar *e;
        ms.level = 0;
        e = wmatch(&ms, src, p);
        if (e) {
            n++;
            wadd_value(&ms, &b, src, e);
        }
        if (e && e>src) /* non empty wmatch? */
            src = e;  /* skip it */
        else if (src < ms.src_end)
            luaL_addwchar(&b, *src++);
        else break;
        if (anchor) break;
    }
    luaL_addlwstring(&b, src, ms.src_end-src);
    luaL_pushresult(&b);
    lua_pushinteger(L, n);  /* number of substitutions */
    return 2;
}
Пример #2
0
int
main() {
  int i,j,u,v,setnum,sum;

  setnum=0;
  while( scanf("%d %d", &u, &v) == 2 ) {
    for( i = 0; i < u; i++ ) {
      for( j = 0; j < v; j++ ) {
	scanf("%d",&wbipgraph[i][j]);
      }
    }

    wmatch(u,v);

/*      printf("Problem #%d:\n", ++setnum); */
/*      for( i = 0; i < u; i ++ ) { */
/*        if( matching[i] != -1 )  */
/*  	printf("match %d to %d\n", i, matching[i]); */
/*      } */
    sum=0;
    for(i=0; i<u; i++) {
      sum += coverU[i];
    }
    for(j=0; j<v; j++) {
      sum += coverV[j];
    }

    printf("%d\n",sum);
  }
  return 0;
}
Пример #3
0
static const lua_WChar *wend_capture (WMatchState *ms, const lua_WChar *s,
                                      const lua_WChar *p) {
    int l = wcapture_to_close(ms);
    const lua_WChar *res;
    ms->capture[l].len = s - ms->capture[l].init;  /* close capture */
    if ((res = wmatch(ms, s, p)) == NULL)  /* wmatch failed? */
        ms->capture[l].len = CAP_UNFINISHED;  /* undo capture */
    return res;
}
Пример #4
0
static const lua_WChar *wmin_expand (WMatchState *ms, const lua_WChar *s,
                                     const lua_WChar *p, const lua_WChar *ep) {
    for (;;) {
        const lua_WChar *res = wmatch(ms, s, ep+1);
        if (res != NULL)
            return res;
        else if (s<ms->src_end && wsinglematch(*s, p, ep))
            s++;  /* try with one more repetition */
        else return NULL;
    }
}
Пример #5
0
static const lua_WChar *wstart_capture (WMatchState *ms, const lua_WChar *s,
                                        const lua_WChar *p, int what) {
    const lua_WChar *res;
    int level = ms->level;
    if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures");
    ms->capture[level].init = s;
    ms->capture[level].len = what;
    ms->level = level+1;
    if ((res=wmatch(ms, s, p)) == NULL)  /* wmatch failed? */
        ms->level--;  /* undo capture */
    return res;
}
Пример #6
0
static const lua_WChar *wmax_expand (WMatchState *ms, const lua_WChar *s,
                                     const lua_WChar *p, const lua_WChar *ep) {
    ptrdiff_t i = 0;  /* counts maximum expand for item */
    while ((s+i)<ms->src_end && wsinglematch(*(s+i), p, ep))
        i++;
    /* keeps trying to wmatch with the maximum repetitions */
    while (i>=0) {
        const lua_WChar *res = wmatch(ms, (s+i), ep+1);
        if (res) return res;
        i--;  /* else didn't wmatch; reduce 1 repetition to try again */
    }
    return NULL;
}
Пример #7
0
static int wstr_find_aux (lua_State *L, int find) {
    size_t l1, l2;
    const lua_WChar *s = luaL_checklwstring(L, 1, &l1);
    const lua_WChar *p = luaL_checklwstring(L, 2, &l2);
    ptrdiff_t init = wstr_posrelat(luaL_optinteger(L, 3, 1), l1) - 1;
    if (init < 0) init = 0;
    else if ((size_t)(init) > l1) init = (ptrdiff_t)l1;
    if (find && (lua_toboolean(L, 4) ||  /* explicit request? */
                 lua_WChar_pbrk(p, WSPECIALS) == NULL)) {  /* or no special characters? */
        /* do a plain search */
        const lua_WChar *s2 = wlmemfind(s+init, l1-init, p, l2);
        if (s2) {
            lua_pushinteger(L, s2-s+1);
            lua_pushinteger(L, s2-s+l2);
            return 2;
        }
    }
    else {
        WMatchState ms;
        int anchor = (*p == '^') ? (p++, 1) : 0;
        const lua_WChar *s1=s+init;
        ms.L = L;
        ms.src_init = s;
        ms.src_end = s+l1;
        do {
            const lua_WChar *res;
            ms.level = 0;
            if ((res=wmatch(&ms, s1, p)) != NULL) {
                if (find) {
                    lua_pushinteger(L, s1-s+1);  /* start */
                    lua_pushinteger(L, res-s);   /* end */
                    return wpush_captures(&ms, NULL, 0) + 2;
                }
                else
                    return wpush_captures(&ms, s1, res);
            }
        } while (s1++ < ms.src_end && !anchor);
    }
    lua_pushnil(L);  /* not found */
    return 1;
}
Пример #8
0
static int wgmatch_aux (lua_State *L) {
    WMatchState ms;
    size_t ls;
    const lua_WChar *s = lua_tolwstring(L, lua_upvalueindex(1), &ls);
    const lua_WChar *p = lua_towstring(L, lua_upvalueindex(2));
    const lua_WChar *src;
    ms.L = L;
    ms.src_init = s;
    ms.src_end = s+ls;
    for (src = s + (size_t)lua_tointeger(L, lua_upvalueindex(3));
            src <= ms.src_end;
            src++) {
        const lua_WChar *e;
        ms.level = 0;
        if ((e = wmatch(&ms, src, p)) != NULL) {
            lua_Integer newstart = e-s;
            if (e == src) newstart++;  /* empty wmatch? go at least one position */
            lua_pushinteger(L, newstart);
            lua_replace(L, lua_upvalueindex(3));
            return wpush_captures(&ms, src, e);
        }
    }
    return 0;  /* not found */
}
Пример #9
0
static const lua_WChar *wmatch (WMatchState *ms, const lua_WChar *s, const lua_WChar *p) {
init: /* using goto's to optimize tail recursion */
    switch (*p) {
    case '(': {  /* start capture */
        if (*(p+1) == ')')  /* position capture? */
            return wstart_capture(ms, s, p+2, CAP_POSITION);
        else
            return wstart_capture(ms, s, p+1, CAP_UNFINISHED);
    }
    case ')': {  /* end capture */
        return wend_capture(ms, s, p+1);
    }
    case WL_ESC: {
        switch (*(p+1)) {
        case 'b': {  /* balanced string? */
            s = wmatchbalance(ms, s, p+2);
            if (s == NULL) return NULL;
            p+=4;
            goto init;  /* else return wmatch(ms, s, p+4); */
        }
        case 'f': {  /* frontier? */
            const lua_WChar *ep;
            lua_WChar previous;
            p += 2;
            if (*p != '[')
                luaL_error(ms->L, "missing " LUA_QL("[") " after "
                           LUA_QL("%%f") " in pattern");
            ep = wclassend(ms, p);  /* points to what is next */
            previous = (s == ms->src_init) ? '\0' : *(s-1);
            if (wmatchbracketclass(previous, p, ep-1) ||
                    !wmatchbracketclass(*s, p, ep-1)) return NULL;
            p=ep;
            goto init;  /* else return wmatch(ms, s, ep); */
        }
        default: {
            if (iswdigit(*(p+1))) {  /* capture results (%0-%9)? */
                s = wmatch_capture(ms, s, *(p+1));
                if (s == NULL) return NULL;
                p+=2;
                goto init;  /* else return wmatch(ms, s, p+2) */
            }
            goto dflt;  /* case default */
        }
        }
    }
    case '\0': {  /* end of pattern */
        return s;  /* wmatch succeeded */
    }
    case '$': {
        if (*(p+1) == '\0')  /* is the `$' the last char in pattern? */
            return (s == ms->src_end) ? s : NULL;  /* check end of string */
        else goto dflt;
    }
    default:
dflt: {  /* it is a pattern item */
            const lua_WChar *ep = wclassend(ms, p);  /* points to what is next */
            int m = s<ms->src_end && wsinglematch(*s, p, ep);
            switch (*ep) {
            case '?': {  /* optional */
                const lua_WChar *res;
                if (m && ((res=wmatch(ms, s+1, ep+1)) != NULL))
                    return res;
                p=ep+1;
                goto init;  /* else return wmatch(ms, s, ep+1); */
            }
            case '*': {  /* 0 or more repetitions */
                return wmax_expand(ms, s, p, ep);
            }
            case '+': {  /* 1 or more repetitions */
                return (m ? wmax_expand(ms, s+1, p, ep) : NULL);
            }
            case '-': {  /* 0 or more repetitions (minimum) */
                return wmin_expand(ms, s, p, ep);
            }
            default: {
                if (!m) return NULL;
                s++;
                p=ep;
                goto init;  /* else return wmatch(ms, s+1, ep); */
            }
            }
        }
    }
}
Пример #10
0
int 
smatch(char *s1, char *s2)
{
    char    ch, *start = s2;

    while (*s1) {
	switch (*s1) {
	    case '\\':
		if (!*(s1+1)) {
		    return 1;
		} else {
		    s1++;
		    if (DOWNCASE(*s1++) != DOWNCASE(*s2++))
			return 1;
		}
		break;
	    case '?':
		if (!*s2++)
		    return 1;
		s1++;
		break;
	    case '*':
		while (*s1 == '*' || (*s1 == '?' && *s2++))
		    s1++;
		if (*s1 == '?')
		    return 1;
		if (*s1 == '{') {
		    if (s2 == start)
			if (!smatch(s1, s2))
			    return 0;
		    while ((s2 = strchr(s2, ' ')) != NULL)
			if (!smatch(s1, ++s2))
			    return 0;
		    return 1;
		} else if (*s1 == '[') {
		    while (*s2)
			if (!smatch(s1, s2++))
			    return 0;
		    return 1;
		}
		if (*s1 == '\\' && *(s1+1))
		    ch = *(s1 + 1);
		else
		    ch = *s1;
		while ((s2 = cstrchr(s2, ch)) != NULL) {
		    if (!smatch(s1, s2))
			return 0;
		    s2++;
		}
		return 1;
	    case '[':
		{
		    char   *end;
		    int     tmpflg;

		    if (!(end = estrchr(s1, ']', '\\'))) {
			return 1;
		    }
		    *end = '\0';
		    tmpflg = cmatch(&s1[1], *s2++);
		    *end = ']';

		    if (tmpflg) {
			return 1;
		    }
		    s1 = end + 1;
		}
		break;
	    case '{':
		if (s2 != start && *(s2 - 1) != ' ')
		    return 1;
		{
		    char   *end;
		    int     tmpflg = 0;

		    if (s1[1] == '^')
			tmpflg = 1;

		    if (!(end = estrchr(s1, '}', '\\'))) {
			return 1;
		    }
		    *end = '\0';
		    tmpflg -= (wmatch(&s1[tmpflg + 1], &s2)) ? 1 : 0;
		    *end = '}';

		    if (tmpflg) {
			return 1;
		    }
		    s1 = end + 1;
		}
		break;
	    default:
		if (DOWNCASE(*s1++) != DOWNCASE(*s2++))
		    return 1;
		break;
	}
    }
    return DOWNCASE(*s1) - DOWNCASE(*s2);
}
Пример #11
0
static int
grep_search(fastgrep_t *fg, char *data, size_t dataLen, regmatch_t *pmatch)
{
#ifdef SMALL
	return 0;
#else
	regoff_t j;
	int rtrnVal = REG_NOMATCH;

	pmatch->rm_so = -1;
	pmatch->rm_eo = -1;

	/* No point in going farther if we do not have enough data. */
	if (dataLen < fg->patternLen)
		return (rtrnVal);

	/* Only try once at the beginning or ending of the line. */
	if (fg->bol || fg->eol) {
		/* Simple text comparison. */
		/* Verify data is >= pattern length before searching on it. */
		if (dataLen >= fg->patternLen) {
			/* Determine where in data to start search at. */
			if (fg->eol)
				j = dataLen - fg->patternLen;
			else
				j = 0;
			if (!((fg->bol && fg->eol) && (dataLen != fg->patternLen)))
				if (grep_cmp(fg->pattern, data + j,
				    fg->patternLen) == -1) {
					pmatch->rm_so = j;
					pmatch->rm_eo = j + fg->patternLen;
					if (!fg->wmatch || wmatch(data, dataLen,
					    pmatch->rm_so, pmatch->rm_eo))
						rtrnVal = 0;
				}
		}
	} else if (fg->reversedSearch) {
		/* Quick Search algorithm. */
		j = dataLen;
		do {
			if (grep_cmp(fg->pattern, data + j - fg->patternLen,
			    fg->patternLen) == -1) {
				pmatch->rm_so = j - fg->patternLen;
				pmatch->rm_eo = j;
				if (!fg->wmatch || wmatch(data, dataLen,
				    pmatch->rm_so, pmatch->rm_eo)) {
					rtrnVal = 0;
					break;
				}
			}
			/* Shift if within bounds, otherwise, we are done. */
			if (j == fg->patternLen)
				break;
			j -= fg->qsBc[(unsigned char)data[j - fg->patternLen - 1]];
		} while (j >= fg->patternLen);
	} else {
		/* Quick Search algorithm. */
		j = 0;
		do {
			if (grep_cmp(fg->pattern, data + j, fg->patternLen) == -1) {
				pmatch->rm_so = j;
				pmatch->rm_eo = j + fg->patternLen;
				if (fg->patternLen == 0 || !fg->wmatch ||
				    wmatch(data, dataLen, pmatch->rm_so,
				    pmatch->rm_eo)) {
					rtrnVal = 0;
					break;
				}
			}

			/* Shift if within bounds, otherwise, we are done. */
			if (j + fg->patternLen == dataLen)
				break;
			else
				j += fg->qsBc[(unsigned char)data[j + fg->patternLen]];
		} while (j <= (dataLen - fg->patternLen));
	}

	return (rtrnVal);
#endif
}