Lines Matching refs:ms
229 static const char *match (MatchState *ms, const char *s, const char *p);
242 static int check_capture (MatchState *ms, int l) { in check_capture() argument
244 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) in check_capture()
245 return luaL_error(ms->L, "invalid capture index %%%d", l + 1); in check_capture()
250 static int capture_to_close (MatchState *ms) { in capture_to_close() argument
251 int level = ms->level; in capture_to_close()
253 if (ms->capture[level].len == CAP_UNFINISHED) return level; in capture_to_close()
254 return luaL_error(ms->L, "invalid pattern capture"); in capture_to_close()
258 static const char *classend (MatchState *ms, const char *p) { in classend() argument
261 if (p == ms->p_end) in classend()
262 luaL_error(ms->L, "malformed pattern (ends with '%%')"); in classend()
268 if (p == ms->p_end) in classend()
269 luaL_error(ms->L, "malformed pattern (missing ']')"); in classend()
270 if (*(p++) == L_ESC && p < ms->p_end) in classend()
325 static int singlematch (MatchState *ms, const char *s, const char *p, in singlematch() argument
327 if (s >= ms->src_end) in singlematch()
341 static const char *matchbalance (MatchState *ms, const char *s, in matchbalance() argument
343 if (p >= ms->p_end - 1) in matchbalance()
344 luaL_error(ms->L, "malformed pattern (missing arguments to '%%b')"); in matchbalance()
350 while (++s < ms->src_end) { in matchbalance()
361 static const char *max_expand (MatchState *ms, const char *s, in max_expand() argument
364 while (singlematch(ms, s + i, p, ep)) in max_expand()
368 const char *res = match(ms, (s+i), ep+1); in max_expand()
376 static const char *min_expand (MatchState *ms, const char *s, in min_expand() argument
379 const char *res = match(ms, s, ep+1); in min_expand()
382 else if (singlematch(ms, s, p, ep)) in min_expand()
389 static const char *start_capture (MatchState *ms, const char *s, in start_capture() argument
392 int level = ms->level; in start_capture()
393 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); in start_capture()
394 ms->capture[level].init = s; in start_capture()
395 ms->capture[level].len = what; in start_capture()
396 ms->level = level+1; in start_capture()
397 if ((res=match(ms, s, p)) == NULL) /* match failed? */ in start_capture()
398 ms->level--; /* undo capture */ in start_capture()
403 static const char *end_capture (MatchState *ms, const char *s, in end_capture() argument
405 int l = capture_to_close(ms); in end_capture()
407 ms->capture[l].len = s - ms->capture[l].init; /* close capture */ in end_capture()
408 if ((res = match(ms, s, p)) == NULL) /* match failed? */ in end_capture()
409 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ in end_capture()
414 static const char *match_capture (MatchState *ms, const char *s, int l) { in match_capture() argument
416 l = check_capture(ms, l); in match_capture()
417 len = ms->capture[l].len; in match_capture()
418 if ((size_t)(ms->src_end-s) >= len && in match_capture()
419 memcmp(ms->capture[l].init, s, len) == 0) in match_capture()
425 static const char *match (MatchState *ms, const char *s, const char *p) { in match() argument
426 if (ms->matchdepth-- == 0) in match()
427 luaL_error(ms->L, "pattern too complex"); in match()
429 if (p != ms->p_end) { /* end of pattern? */ in match()
433 s = start_capture(ms, s, p + 2, CAP_POSITION); in match()
435 s = start_capture(ms, s, p + 1, CAP_UNFINISHED); in match()
439 s = end_capture(ms, s, p + 1); in match()
443 if ((p + 1) != ms->p_end) /* is the '$' the last char in pattern? */ in match()
445 s = (s == ms->src_end) ? s : NULL; /* check end of string */ in match()
451 s = matchbalance(ms, s, p + 2); in match()
461 luaL_error(ms->L, "missing '[' after '%%f' in pattern"); in match()
462 ep = classend(ms, p); /* points to what is next */ in match()
463 previous = (s == ms->src_init) ? '\0' : *(s - 1); in match()
474 s = match_capture(ms, s, uchar(*(p + 1))); in match()
485 const char *ep = classend(ms, p); /* points to optional suffix */ in match()
487 if (!singlematch(ms, s, p, ep)) { in match()
498 if ((res = match(ms, s + 1, ep + 1)) != NULL) in match()
509 s = max_expand(ms, s, p, ep); in match()
512 s = min_expand(ms, s, p, ep); in match()
522 ms->matchdepth++; in match()
550 static void push_onecapture (MatchState *ms, int i, const char *s, in push_onecapture() argument
552 if (i >= ms->level) { in push_onecapture()
554 lua_pushlstring(ms->L, s, e - s); /* add whole match */ in push_onecapture()
556 luaL_error(ms->L, "invalid capture index %%%d", i + 1); in push_onecapture()
559 ptrdiff_t l = ms->capture[i].len; in push_onecapture()
560 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); in push_onecapture()
562 lua_pushinteger(ms->L, (ms->capture[i].init - ms->src_init) + 1); in push_onecapture()
564 lua_pushlstring(ms->L, ms->capture[i].init, l); in push_onecapture()
569 static int push_captures (MatchState *ms, const char *s, const char *e) { in push_captures() argument
571 int nlevels = (ms->level == 0 && s) ? 1 : ms->level; in push_captures()
572 luaL_checkstack(ms->L, nlevels, "too many captures"); in push_captures()
574 push_onecapture(ms, i, s, e); in push_captures()
591 static void prepstate (MatchState *ms, lua_State *L, in prepstate() argument
593 ms->L = L; in prepstate()
594 ms->matchdepth = MAXCCALLS; in prepstate()
595 ms->src_init = s; in prepstate()
596 ms->src_end = s + ls; in prepstate()
597 ms->p_end = p + lp; in prepstate()
601 static void reprepstate (MatchState *ms) { in reprepstate() argument
602 ms->level = 0; in reprepstate()
603 lua_assert(ms->matchdepth == MAXCCALLS); in reprepstate()
628 MatchState ms; in str_find_aux() local
634 prepstate(&ms, L, s, ls, p, lp); in str_find_aux()
637 reprepstate(&ms); in str_find_aux()
638 if ((res=match(&ms, s1, p)) != NULL) { in str_find_aux()
642 return push_captures(&ms, NULL, 0) + 2; in str_find_aux()
645 return push_captures(&ms, s1, res); in str_find_aux()
647 } while (s1++ < ms.src_end && !anchor); in str_find_aux()
669 MatchState ms; /* match state */ member
676 gm->ms.L = L; in gmatch_aux()
677 for (src = gm->src; src <= gm->ms.src_end; src++) { in gmatch_aux()
679 reprepstate(&gm->ms); in gmatch_aux()
680 if ((e = match(&gm->ms, src, gm->p)) != NULL && e != gm->lastmatch) { in gmatch_aux()
682 return push_captures(&gm->ms, src, e); in gmatch_aux()
696 prepstate(&gm->ms, L, s, ls, p, lp); in gmatch()
703 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, in add_s() argument
706 lua_State *L = ms->L; in add_s()
721 push_onecapture(ms, news[i] - '1', s, e); in add_s()
731 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, in add_value() argument
733 lua_State *L = ms->L; in add_value()
738 n = push_captures(ms, s, e); in add_value()
743 push_onecapture(ms, 0, s, e); in add_value()
748 add_s(ms, b, s, e); in add_value()
771 MatchState ms; in str_gsub() local
780 prepstate(&ms, L, src, srcl, p, lp); in str_gsub()
783 reprepstate(&ms); /* (re)prepare state for new match */ in str_gsub()
784 if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */ in str_gsub()
786 add_value(&ms, &b, src, e, tr); /* add replacement to buffer */ in str_gsub()
789 else if (src < ms.src_end) /* otherwise, skip one character */ in str_gsub()
794 luaL_addlstring(&b, src, ms.src_end-src); in str_gsub()