Lines Matching refs:ms

236 static const char *match (MatchState *ms, const char *s, const char *p);
249 static int check_capture (MatchState *ms, int l) { in check_capture() argument
251 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) in check_capture()
252 return luaL_error(ms->L, "invalid capture index %%%d", l + 1); in check_capture()
257 static int capture_to_close (MatchState *ms) { in capture_to_close() argument
258 int level = ms->level; in capture_to_close()
260 if (ms->capture[level].len == CAP_UNFINISHED) return level; in capture_to_close()
261 return luaL_error(ms->L, "invalid pattern capture"); in capture_to_close()
265 static const char *classend (MatchState *ms, const char *p) { in classend() argument
268 if (p == ms->p_end) in classend()
269 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); in classend()
275 if (p == ms->p_end) in classend()
276 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); in classend()
277 if (*(p++) == L_ESC && p < ms->p_end) in classend()
339 static int singlematch (MatchState *ms, const char *s, const char *p, in singlematch() argument
341 if (s >= ms->src_end) in singlematch()
355 static const char *matchbalance (MatchState *ms, const char *s, in matchbalance() argument
357 if (p >= ms->p_end - 1) in matchbalance()
358 luaL_error(ms->L, "malformed pattern " in matchbalance()
365 while (++s < ms->src_end) { in matchbalance()
376 static const char *max_expand (MatchState *ms, const char *s, in max_expand() argument
379 while (singlematch(ms, s + i, p, ep)) in max_expand()
383 const char *res = match(ms, (s+i), ep+1); in max_expand()
391 static const char *min_expand (MatchState *ms, const char *s, in min_expand() argument
394 const char *res = match(ms, s, ep+1); in min_expand()
397 else if (singlematch(ms, s, p, ep)) in min_expand()
404 static const char *start_capture (MatchState *ms, const char *s, in start_capture() argument
407 int level = ms->level; in start_capture()
408 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); in start_capture()
409 ms->capture[level].init = s; in start_capture()
410 ms->capture[level].len = what; in start_capture()
411 ms->level = level+1; in start_capture()
412 if ((res=match(ms, s, p)) == NULL) /* match failed? */ in start_capture()
413 ms->level--; /* undo capture */ in start_capture()
418 static const char *end_capture (MatchState *ms, const char *s, in end_capture() argument
420 int l = capture_to_close(ms); in end_capture()
422 ms->capture[l].len = s - ms->capture[l].init; /* close capture */ in end_capture()
423 if ((res = match(ms, s, p)) == NULL) /* match failed? */ in end_capture()
424 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ in end_capture()
429 static const char *match_capture (MatchState *ms, const char *s, int l) { in match_capture() argument
431 l = check_capture(ms, l); in match_capture()
432 len = ms->capture[l].len; in match_capture()
433 if ((size_t)(ms->src_end-s) >= len && in match_capture()
434 memcmp(ms->capture[l].init, s, len) == 0) in match_capture()
440 static const char *match (MatchState *ms, const char *s, const char *p) { in match() argument
441 if (ms->matchdepth-- == 0) in match()
442 luaL_error(ms->L, "pattern too complex"); in match()
444 if (p != ms->p_end) { /* end of pattern? */ in match()
448 s = start_capture(ms, s, p + 2, CAP_POSITION); in match()
450 s = start_capture(ms, s, p + 1, CAP_UNFINISHED); in match()
454 s = end_capture(ms, s, p + 1); in match()
458 if ((p + 1) != ms->p_end) /* is the `$' the last char in pattern? */ in match()
460 s = (s == ms->src_end) ? s : NULL; /* check end of string */ in match()
466 s = matchbalance(ms, s, p + 2); in match()
476 luaL_error(ms->L, "missing " LUA_QL("[") " after " in match()
478 ep = classend(ms, p); /* points to what is next */ in match()
479 previous = (s == ms->src_init) ? '\0' : *(s - 1); in match()
490 s = match_capture(ms, s, uchar(*(p + 1))); in match()
501 const char *ep = classend(ms, p); /* points to optional suffix */ in match()
503 if (!singlematch(ms, s, p, ep)) { in match()
514 if ((res = match(ms, s + 1, ep + 1)) != NULL) in match()
525 s = max_expand(ms, s, p, ep); in match()
528 s = min_expand(ms, s, p, ep); in match()
538 ms->matchdepth++; in match()
566 static void push_onecapture (MatchState *ms, int i, const char *s, in push_onecapture() argument
568 if (i >= ms->level) { in push_onecapture()
570 lua_pushlstring(ms->L, s, e - s); /* add whole match */ in push_onecapture()
572 luaL_error(ms->L, "invalid capture index"); in push_onecapture()
575 ptrdiff_t l = ms->capture[i].len; in push_onecapture()
576 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); in push_onecapture()
578 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); in push_onecapture()
580 lua_pushlstring(ms->L, ms->capture[i].init, l); in push_onecapture()
585 static int push_captures (MatchState *ms, const char *s, const char *e) { in push_captures() argument
587 int nlevels = (ms->level == 0 && s) ? 1 : ms->level; in push_captures()
588 luaL_checkstack(ms->L, nlevels, "too many captures"); in push_captures()
590 push_onecapture(ms, i, s, e); in push_captures()
628 MatchState ms; in str_find_aux() local
634 ms.L = L; in str_find_aux()
635 ms.matchdepth = MAXCCALLS; in str_find_aux()
636 ms.src_init = s; in str_find_aux()
637 ms.src_end = s + ls; in str_find_aux()
638 ms.p_end = p + lp; in str_find_aux()
641 ms.level = 0; in str_find_aux()
642 lua_assert(ms.matchdepth == MAXCCALLS); in str_find_aux()
643 if ((res=match(&ms, s1, p)) != NULL) { in str_find_aux()
647 return push_captures(&ms, NULL, 0) + 2; in str_find_aux()
650 return push_captures(&ms, s1, res); in str_find_aux()
652 } while (s1++ < ms.src_end && !anchor); in str_find_aux()
670 MatchState ms; in gmatch_aux() local
675 ms.L = L; in gmatch_aux()
676 ms.matchdepth = MAXCCALLS; in gmatch_aux()
677 ms.src_init = s; in gmatch_aux()
678 ms.src_end = s+ls; in gmatch_aux()
679 ms.p_end = p + lp; in gmatch_aux()
681 src <= ms.src_end; in gmatch_aux()
684 ms.level = 0; in gmatch_aux()
685 lua_assert(ms.matchdepth == MAXCCALLS); in gmatch_aux()
686 if ((e = match(&ms, src, p)) != NULL) { in gmatch_aux()
691 return push_captures(&ms, src, e); in gmatch_aux()
708 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, in add_s() argument
711 const char *news = lua_tolstring(ms->L, 3, &l); in add_s()
719 luaL_error(ms->L, "invalid use of " LUA_QL("%c") in add_s()
726 push_onecapture(ms, news[i] - '1', s, e); in add_s()
734 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, in add_value() argument
736 lua_State *L = ms->L; in add_value()
741 n = push_captures(ms, s, e); in add_value()
746 push_onecapture(ms, 0, s, e); in add_value()
751 add_s(ms, b, s, e); in add_value()
773 MatchState ms; in str_gsub() local
782 ms.L = L; in str_gsub()
783 ms.matchdepth = MAXCCALLS; in str_gsub()
784 ms.src_init = src; in str_gsub()
785 ms.src_end = src+srcl; in str_gsub()
786 ms.p_end = p + lp; in str_gsub()
789 ms.level = 0; in str_gsub()
790 lua_assert(ms.matchdepth == MAXCCALLS); in str_gsub()
791 e = match(&ms, src, p); in str_gsub()
794 add_value(&ms, &b, src, e, tr); in str_gsub()
798 else if (src < ms.src_end) in str_gsub()
803 luaL_addlstring(&b, src, ms.src_end-src); in str_gsub()