예제 #1
0
파일: patterns.c 프로젝트: Frankie-666/lwan
static int
str_find_aux(struct match_state *ms, const char *pattern, const char *string,
    struct str_find *sm, size_t nsm, off_t init)
{
	size_t		 ls = strlen(string);
	size_t		 lp = strlen(pattern);
	const char	*s = string;
	const char	*p = pattern;
	const char	*s1, *s2;
	int		 anchor, i;

	if (init < 0)
		init = 0;
	else if (init > (off_t)ls)
		return match_error(ms, "starting after string's end");
	s1 = s + init;

	if (nospecials(p, lp)) {
		/* do a plain search */
		s2 = lmemfind(s1, ls - (size_t)init, p, lp);
		if (s2 == NULL)
			return (0);

		i = 0;
		sm[i].sm_so = 0;
		sm[i].sm_eo = (off_t)ls;
		if (nsm > 1) {
			i++;
			sm[i].sm_so = s2 - s;
			sm[i].sm_eo = (off_t)((s2 - s) + (off_t)lp);
		}
		return (i + 1);
	}

	anchor = (*p == '^');
	if (anchor) {
		p++;
		lp--;	/* skip anchor character */
	}
	ms->maxcaptures = (int)((nsm > MAXCAPTURES ? MAXCAPTURES : nsm) - 1);
	ms->matchdepth = MAXCCALLS;
	ms->repetitioncounter = MAXREPETITION;
	ms->src_init = s;
	ms->src_end = s + ls;
	ms->p_end = p + lp;
	do {
		const char *res;
		ms->level = 0;
		if ((res = match(ms, s1, p)) != NULL) {
			sm->sm_so = 0;
			sm->sm_eo = (off_t)ls;
			return push_captures(ms, s1, res, sm + 1, nsm - 1) + 1;

		} else if (ms->error != NULL) {
			return 0;
		}
	} while (s1++ < ms->src_end && !anchor);

	return 0;
}
예제 #2
0
static int str_find_aux(LuaMatchState *ms, int find, const char *s, size_t ls,const char *p, size_t lp, size_t init, int raw_find)
{
    int result = 0; // not found
    ms->error = NULL;
    init = posrelat(init, ls);
    if ((int)init < 0) init = 0;
    else if (init > ls + 1) {  // start after string's end?
        return 0; // cannot find anything
    }
    // explicit request or no special characters?
    if (find && (raw_find || nospecials(p, lp)))
    {
        // do a plain search
        const char *s2 = lmemfind(s + init, ls - init + 1, p, lp);
        if (s2) {
            long start_pos = ((long)(s2 - s));
            ms->level = 1;
            ms->capture[0].len = CAP_POSITION;
            ms->capture[0].init = (const char *)start_pos;
            result = (int)(start_pos + lp);
        }
    }
    else
    {
        const char *s1 = s + init;
        int anchor = (*p == '^');
        if (anchor)
            p++, lp--;  // skip anchor character
        ms->src_init = s;
        ms->src_end = s + ls;
        ms->p_end = p + lp;
        do {
            const char *res;
            ms->level = 0;
            if ((res=match(ms, s1, p)) != NULL)
            {
                result = (int)(res - s);
                goto eofunc;
            }
        } while (s1++ < ms->src_end && !anchor);
    }
    
eofunc:
    
    if(result)
    {
        int i;
        for(i=0; i<ms->level; ++i)
        {
            if(ms->capture[i].len == CAP_UNFINISHED)
            {
                ms->error = "unfinished capture";
                return 0;
            }
        }
    }
    return result;
}
예제 #3
0
static int str_find_aux (lua_State *L, int find) {
  size_t ls, lp;
  const char *s = luaL_checklstring(L, 1, &ls);
  const char *p = luaL_checklstring(L, 2, &lp);
  size_t init = posrelat(luaL_optinteger(L, 3, 1), ls);
  if (init < 1) init = 1;
  else if (init > ls + 1) {  /* start after string's end? */
    lua_pushnil(L);  /* cannot find anything */
    return 1;
  }
  /* explicit request or no special characters? */
  if (find && (lua_toboolean(L, 4) || nospecials(p, lp))) {
    /* do a plain search */
    const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp);
    if (s2) {
      lua_pushinteger(L, s2 - s + 1);
      lua_pushinteger(L, s2 - s + lp);
      return 2;
    }
  }
  else {
    MatchState ms;
    const char *s1 = s + init - 1;
    int anchor = (*p == '^');
    if (anchor) {
      p++; lp--;  /* skip anchor character */
    }
    ms.L = L;
    ms.matchdepth = MAXCCALLS;
    ms.src_init = s;
    ms.src_end = s + ls;
    ms.p_end = p + lp;
    do {
      const char *res;
      ms.level = 0;
      lua_assert(ms.matchdepth == MAXCCALLS);
      if ((res=match(&ms, s1, p)) != NULL) {
        if (find) {
          lua_pushinteger(L, s1 - s + 1);  /* start */
          lua_pushinteger(L, res - s);   /* end */
          return push_captures(&ms, NULL, 0) + 2;
        }
        else
          return push_captures(&ms, s1, res);
      }
    } while (s1++ < ms.src_end && !anchor);
  }
  lua_pushnil(L);  /* not found */
  return 1;
}
예제 #4
0
//mod by nirenr
static int gfind_aux (lua_State *L) {
  size_t ls, lp;
  const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls);
  const char *p = lua_tolstring(L, lua_upvalueindex(2), &lp);
  lua_Integer init = posrelat(luaL_optinteger(L, lua_upvalueindex(3), 1), ls);
  if (init < 1) init = 1;
  else if (init > (lua_Integer)ls + 1) {  /* start after string's end? */
    return 0;  /* cannot find anything */
  }
  /* explicit request or no special characters? */
  if (lua_toboolean(L, lua_upvalueindex(4)) || nospecials(p, lp)) {
    /* do a plain search */
    const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
    if (s2) {
      lua_pushinteger(L, (s2 - s) + 1);
      lua_pushinteger(L, (s2 - s) + lp);
      lua_pushinteger(L,(s2 - s) + lp + 1);
      lua_replace(L, lua_upvalueindex(3));
      return 2;
    }
  }
  else {
    MatchState ms;
    const char *s1 = s + init - 1;
    int anchor = (*p == '^');
    if (anchor) {
      p++; lp--;  /* skip anchor character */
    }
    ms.L = L;
    ms.matchdepth = MAXCCALLS;
    ms.src_init = s;
    ms.src_end = s + ls;
    ms.p_end = p + lp;
    do {
      const char *res;
      ms.level = 0;
      lua_assert(ms.matchdepth == MAXCCALLS);
      if ((res=match(&ms, s1, p)) != NULL) {
        lua_pushinteger(L, (s1 - s) + 1);  /* start */
        lua_pushinteger(L, res - s);   /* end */
        lua_pushinteger(L, res - s + 1);
        lua_replace(L, lua_upvalueindex(3));
        return push_captures(&ms, NULL, 0) + 2;
      }
    } while (s1++ < ms.src_end && !anchor);
  }
  return 0;  /* not found */
}
예제 #5
0
static int str_find_aux (lua_State *L, int mode) {  /* EXT */
  size_t ls, lp;
  const char *s = luaL_checklstring(L, 1, &ls);
  const char *p = luaL_checklstring(L, 2, &lp);
  lua_Integer init = posrelat(luaL_optinteger(L, 3, 1), ls);
  if (init < 1) init = 1;
  else if (init > (lua_Integer)ls + 1) {  /* start after string's end? */
    lua_pushnil(L);  /* cannot find anything */
    return 1;
  }
  /* explicit request or no special characters? */  /* EXT */
  if (mode == MODE_FIND && (lua_toboolean(L, 4) || nospecials(p, lp))) {
    /* do a plain search */
    const char *s2 = lmemfind(s + init - 1, ls - (size_t)init + 1, p, lp);
    if (s2) {
      lua_pushinteger(L, (s2 - s) + 1);
      lua_pushinteger(L, (s2 - s) + lp);
      return 2;
    }
  }
  else {
    MatchState ms;
    const char *s1 = s + init - 1;
    int anchor = (*p == '^');
    prepstate(&ms, L, s, ls, p, lp);  /* EXT (moved before anchor check) */
    if (anchor)
      p++;  /* skip anchor character */  /* EXT */
    do {
      const char *res;
      reprepstate(&ms);
      if ((res=match(&ms, s1, p)) != NULL) {
        if (mode == MODE_FIND) {  /* EXT */
          lua_pushinteger(L, (s1 - s) + 1);  /* start */
          lua_pushinteger(L, res - s);   /* end */
          return push_captures(&ms, NULL, 0) + 2;
        }
        else if (mode == MODE_MATCH)  /* EXT */
          return push_captures(&ms, s1, res);
        else {  /* EXT */
          return build_result_table(&ms, s1, res);
        }
      }
    } while (s1++ < ms.src_end && !anchor);
  }
  lua_pushnil(L);  /* not found */
  return 1;
}