Lines Matching refs:ms
224 static const char *match (MatchState *ms, const char *s, const char *p);
237 static int check_capture (MatchState *ms, int l) { in check_capture() argument
239 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) in check_capture()
240 return luaL_error(ms->L, "invalid capture index %%%d", l + 1); in check_capture()
245 static int capture_to_close (MatchState *ms) { in capture_to_close() argument
246 int level = ms->level; in capture_to_close()
248 if (ms->capture[level].len == CAP_UNFINISHED) return level; in capture_to_close()
249 return luaL_error(ms->L, "invalid pattern capture"); in capture_to_close()
253 static const char *classend (MatchState *ms, const char *p) { in classend() argument
256 if (p == ms->p_end) in classend()
257 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); in classend()
263 if (p == ms->p_end) in classend()
264 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); in classend()
265 if (*(p++) == L_ESC && p < ms->p_end) in classend()
320 static int singlematch (MatchState *ms, const char *s, const char *p, in singlematch() argument
322 if (s >= ms->src_end) in singlematch()
336 static const char *matchbalance (MatchState *ms, const char *s, in matchbalance() argument
338 if (p >= ms->p_end - 1) in matchbalance()
339 luaL_error(ms->L, "malformed pattern " in matchbalance()
346 while (++s < ms->src_end) { in matchbalance()
357 static const char *max_expand (MatchState *ms, const char *s, in max_expand() argument
360 while (singlematch(ms, s + i, p, ep)) in max_expand()
364 const char *res = match(ms, (s+i), ep+1); in max_expand()
372 static const char *min_expand (MatchState *ms, const char *s, in min_expand() argument
375 const char *res = match(ms, s, ep+1); in min_expand()
378 else if (singlematch(ms, s, p, ep)) in min_expand()
385 static const char *start_capture (MatchState *ms, const char *s, in start_capture() argument
388 int level = ms->level; in start_capture()
389 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); in start_capture()
390 ms->capture[level].init = s; in start_capture()
391 ms->capture[level].len = what; in start_capture()
392 ms->level = level+1; in start_capture()
393 if ((res=match(ms, s, p)) == NULL) /* match failed? */ in start_capture()
394 ms->level--; /* undo capture */ in start_capture()
399 static const char *end_capture (MatchState *ms, const char *s, in end_capture() argument
401 int l = capture_to_close(ms); in end_capture()
403 ms->capture[l].len = s - ms->capture[l].init; /* close capture */ in end_capture()
404 if ((res = match(ms, s, p)) == NULL) /* match failed? */ in end_capture()
405 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ in end_capture()
410 static const char *match_capture (MatchState *ms, const char *s, int l) { in match_capture() argument
412 l = check_capture(ms, l); in match_capture()
413 len = ms->capture[l].len; in match_capture()
414 if ((size_t)(ms->src_end-s) >= len && in match_capture()
415 memcmp(ms->capture[l].init, s, len) == 0) in match_capture()
421 static const char *match (MatchState *ms, const char *s, const char *p) { in match() argument
422 if (ms->matchdepth-- == 0) in match()
423 luaL_error(ms->L, "pattern too complex"); in match()
425 if (p != ms->p_end) { /* end of pattern? */ in match()
429 s = start_capture(ms, s, p + 2, CAP_POSITION); in match()
431 s = start_capture(ms, s, p + 1, CAP_UNFINISHED); in match()
435 s = end_capture(ms, s, p + 1); in match()
439 if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */ in match()
441 s = (s == ms->src_end) ? s : NULL; /* check end of string */ in match()
447 s = matchbalance(ms, s, p + 2); in match()
457 luaL_error(ms->L, "missing " LUA_QL("[") " after " in match()
459 ep = classend(ms, p); /* points to what is next */ in match()
460 previous = (s == ms->src_init) ? '\0' : *(s - 1); in match()
471 s = match_capture(ms, s, uchar(*(p + 1))); in match()
482 const char *ep = classend(ms, p); /* points to optional suffix */ in match()
484 if (!singlematch(ms, s, p, ep)) { in match()
495 if ((res = match(ms, s + 1, ep + 1)) != NULL) in match()
506 s = max_expand(ms, s, p, ep); in match()
509 s = min_expand(ms, s, p, ep); in match()
519 ms->matchdepth++; in match()
547 static void push_onecapture (MatchState *ms, int i, const char *s, in push_onecapture() argument
549 if (i >= ms->level) { in push_onecapture()
551 lua_pushlstring(ms->L, s, e - s); /* add whole match */ in push_onecapture()
553 luaL_error(ms->L, "invalid capture index"); in push_onecapture()
556 ptrdiff_t l = ms->capture[i].len; in push_onecapture()
557 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); in push_onecapture()
559 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); in push_onecapture()
561 lua_pushlstring(ms->L, ms->capture[i].init, l); in push_onecapture()
566 static int push_captures (MatchState *ms, const char *s, const char *e) { in push_captures() argument
568 int nlevels = (ms->level == 0 && s) ? 1 : ms->level; in push_captures()
569 luaL_checkstack(ms->L, nlevels, "too many captures"); in push_captures()
571 push_onecapture(ms, i, s, e); in push_captures()
609 MatchState ms; in str_find_aux() local
615 ms.L = L; in str_find_aux()
616 ms.matchdepth = MAXCCALLS; in str_find_aux()
617 ms.src_init = s; in str_find_aux()
618 ms.src_end = s + ls; in str_find_aux()
619 ms.p_end = p + lp; in str_find_aux()
622 ms.level = 0; in str_find_aux()
623 lua_assert(ms.matchdepth == MAXCCALLS); in str_find_aux()
624 if ((res=match(&ms, s1, p)) != NULL) { in str_find_aux()
628 return push_captures(&ms, NULL, 0) + 2; in str_find_aux()
631 return push_captures(&ms, s1, res); in str_find_aux()
633 } while (s1++ < ms.src_end && !anchor); in str_find_aux()
651 MatchState ms; in gmatch_aux() local
656 ms.L = L; in gmatch_aux()
657 ms.matchdepth = MAXCCALLS; in gmatch_aux()
658 ms.src_init = s; in gmatch_aux()
659 ms.src_end = s+ls; in gmatch_aux()
660 ms.p_end = p + lp; in gmatch_aux()
662 src <= ms.src_end; in gmatch_aux()
665 ms.level = 0; in gmatch_aux()
666 lua_assert(ms.matchdepth == MAXCCALLS); in gmatch_aux()
667 if ((e = match(&ms, src, p)) != NULL) { in gmatch_aux()
672 return push_captures(&ms, src, e); in gmatch_aux()
689 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, in add_s() argument
692 const char *news = lua_tolstring(ms->L, 3, &l); in add_s()
700 luaL_error(ms->L, "invalid use of " LUA_QL("%c") in add_s()
707 push_onecapture(ms, news[i] - '1', s, e); in add_s()
715 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, in add_value() argument
717 lua_State *L = ms->L; in add_value()
722 n = push_captures(ms, s, e); in add_value()
727 push_onecapture(ms, 0, s, e); in add_value()
732 add_s(ms, b, s, e); in add_value()
754 MatchState ms; in str_gsub() local
763 ms.L = L; in str_gsub()
764 ms.matchdepth = MAXCCALLS; in str_gsub()
765 ms.src_init = src; in str_gsub()
766 ms.src_end = src+srcl; in str_gsub()
767 ms.p_end = p + lp; in str_gsub()
770 ms.level = 0; in str_gsub()
771 lua_assert(ms.matchdepth == MAXCCALLS); in str_gsub()
772 e = match(&ms, src, p); in str_gsub()
775 add_value(&ms, &b, src, e, tr); in str_gsub()
779 else if (src < ms.src_end) in str_gsub()
784 luaL_addlstring(&b, src, ms.src_end-src); in str_gsub()