Lines Matching full:ms
223 static const char *match (MatchState *ms, const char *s, const char *p);
236 static int check_capture (MatchState *ms, int l) { in check_capture() argument
238 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) in check_capture()
239 return luaL_error(ms->L, "invalid capture index %%%d", l + 1); in check_capture()
244 static int capture_to_close (MatchState *ms) { in capture_to_close() argument
245 int level = ms->level; in capture_to_close()
247 if (ms->capture[level].len == CAP_UNFINISHED) return level; in capture_to_close()
248 return luaL_error(ms->L, "invalid pattern capture"); in capture_to_close()
252 static const char *classend (MatchState *ms, const char *p) { in classend() argument
255 if (p == ms->p_end) in classend()
256 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); in classend()
262 if (p == ms->p_end) in classend()
263 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); in classend()
264 if (*(p++) == L_ESC && p < ms->p_end) in classend()
319 static int singlematch (MatchState *ms, const char *s, const char *p, in singlematch() argument
321 if (s >= ms->src_end) in singlematch()
335 static const char *matchbalance (MatchState *ms, const char *s, in matchbalance() argument
337 if (p >= ms->p_end - 1) in matchbalance()
338 luaL_error(ms->L, "malformed pattern " in matchbalance()
345 while (++s < ms->src_end) { in matchbalance()
356 static const char *max_expand (MatchState *ms, const char *s, in max_expand() argument
359 while (singlematch(ms, s + i, p, ep)) in max_expand()
363 const char *res = match(ms, (s+i), ep+1); in max_expand()
371 static const char *min_expand (MatchState *ms, const char *s, in min_expand() argument
374 const char *res = match(ms, s, ep+1); in min_expand()
377 else if (singlematch(ms, s, p, ep)) in min_expand()
384 static const char *start_capture (MatchState *ms, const char *s, in start_capture() argument
387 int level = ms->level; in start_capture()
388 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); in start_capture()
389 ms->capture[level].init = s; in start_capture()
390 ms->capture[level].len = what; in start_capture()
391 ms->level = level+1; in start_capture()
392 if ((res=match(ms, s, p)) == NULL) /* match failed? */ in start_capture()
393 ms->level--; /* undo capture */ in start_capture()
398 static const char *end_capture (MatchState *ms, const char *s, in end_capture() argument
400 int l = capture_to_close(ms); in end_capture()
402 ms->capture[l].len = s - ms->capture[l].init; /* close capture */ in end_capture()
403 if ((res = match(ms, s, p)) == NULL) /* match failed? */ in end_capture()
404 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ in end_capture()
409 static const char *match_capture (MatchState *ms, const char *s, int l) { in match_capture() argument
411 l = check_capture(ms, l); in match_capture()
412 len = ms->capture[l].len; in match_capture()
413 if ((size_t)(ms->src_end-s) >= len && in match_capture()
414 memcmp(ms->capture[l].init, s, len) == 0) in match_capture()
420 static const char *match (MatchState *ms, const char *s, const char *p) { in match() argument
421 if (ms->matchdepth-- == 0) in match()
422 luaL_error(ms->L, "pattern too complex"); in match()
424 if (p != ms->p_end) { /* end of pattern? */ in match()
428 s = start_capture(ms, s, p + 2, CAP_POSITION); in match()
430 s = start_capture(ms, s, p + 1, CAP_UNFINISHED); in match()
434 s = end_capture(ms, s, p + 1); in match()
438 if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */ in match()
440 s = (s == ms->src_end) ? s : NULL; /* check end of string */ in match()
446 s = matchbalance(ms, s, p + 2); in match()
448 p += 4; goto init; /* return match(ms, s, p + 4); */ in match()
456 luaL_error(ms->L, "missing " LUA_QL("[") " after " in match()
458 ep = classend(ms, p); /* points to what is next */ in match()
459 previous = (s == ms->src_init) ? '\0' : *(s - 1); in match()
462 p = ep; goto init; /* return match(ms, s, ep); */ in match()
470 s = match_capture(ms, s, uchar(*(p + 1))); in match()
472 p += 2; goto init; /* return match(ms, s, p + 2) */ in match()
481 const char *ep = classend(ms, p); /* points to optional suffix */ in match()
483 if (!singlematch(ms, s, p, ep)) { in match()
485 p = ep + 1; goto init; /* return match(ms, s, ep + 1); */ in match()
494 if ((res = match(ms, s + 1, ep + 1)) != NULL) in match()
497 p = ep + 1; goto init; /* else return match(ms, s, ep + 1); */ in match()
505 s = max_expand(ms, s, p, ep); in match()
508 s = min_expand(ms, s, p, ep); in match()
511 s++; p = ep; goto init; /* return match(ms, s + 1, ep); */ in match()
518 ms->matchdepth++; in match()
546 static void push_onecapture (MatchState *ms, int i, const char *s, in push_onecapture() argument
548 if (i >= ms->level) { in push_onecapture()
549 if (i == 0) /* ms->level == 0, too */ in push_onecapture()
550 lua_pushlstring(ms->L, s, e - s); /* add whole match */ in push_onecapture()
552 luaL_error(ms->L, "invalid capture index"); in push_onecapture()
555 ptrdiff_t l = ms->capture[i].len; in push_onecapture()
556 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); in push_onecapture()
558 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); in push_onecapture()
560 lua_pushlstring(ms->L, ms->capture[i].init, l); in push_onecapture()
565 static int push_captures (MatchState *ms, const char *s, const char *e) { in push_captures() argument
567 int nlevels = (ms->level == 0 && s) ? 1 : ms->level; in push_captures()
568 luaL_checkstack(ms->L, nlevels, "too many captures"); in push_captures()
570 push_onecapture(ms, i, s, e); in push_captures()
608 MatchState ms; in str_find_aux() local
614 ms.L = L; in str_find_aux()
615 ms.matchdepth = MAXCCALLS; in str_find_aux()
616 ms.src_init = s; in str_find_aux()
617 ms.src_end = s + ls; in str_find_aux()
618 ms.p_end = p + lp; in str_find_aux()
621 ms.level = 0; in str_find_aux()
622 lua_assert(ms.matchdepth == MAXCCALLS); in str_find_aux()
623 if ((res=match(&ms, s1, p)) != NULL) { in str_find_aux()
627 return push_captures(&ms, NULL, 0) + 2; in str_find_aux()
630 return push_captures(&ms, s1, res); in str_find_aux()
632 } while (s1++ < ms.src_end && !anchor); in str_find_aux()
650 MatchState ms; in gmatch_aux() local
655 ms.L = L; in gmatch_aux()
656 ms.matchdepth = MAXCCALLS; in gmatch_aux()
657 ms.src_init = s; in gmatch_aux()
658 ms.src_end = s+ls; in gmatch_aux()
659 ms.p_end = p + lp; in gmatch_aux()
661 src <= ms.src_end; in gmatch_aux()
664 ms.level = 0; in gmatch_aux()
665 lua_assert(ms.matchdepth == MAXCCALLS); in gmatch_aux()
666 if ((e = match(&ms, src, p)) != NULL) { in gmatch_aux()
671 return push_captures(&ms, src, e); in gmatch_aux()
688 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, in add_s() argument
691 const char *news = lua_tolstring(ms->L, 3, &l); in add_s()
699 luaL_error(ms->L, "invalid use of " LUA_QL("%c") in add_s()
706 push_onecapture(ms, news[i] - '1', s, e); in add_s()
714 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, in add_value() argument
716 lua_State *L = ms->L; in add_value()
721 n = push_captures(ms, s, e); in add_value()
726 push_onecapture(ms, 0, s, e); in add_value()
731 add_s(ms, b, s, e); in add_value()
753 MatchState ms; in str_gsub() local
762 ms.L = L; in str_gsub()
763 ms.matchdepth = MAXCCALLS; in str_gsub()
764 ms.src_init = src; in str_gsub()
765 ms.src_end = src+srcl; in str_gsub()
766 ms.p_end = p + lp; in str_gsub()
769 ms.level = 0; in str_gsub()
770 lua_assert(ms.matchdepth == MAXCCALLS); in str_gsub()
771 e = match(&ms, src, p); in str_gsub()
774 add_value(&ms, &b, src, e, tr); in str_gsub()
778 else if (src < ms.src_end) in str_gsub()
783 luaL_addlstring(&b, src, ms.src_end-src); in str_gsub()