Lines Matching refs:ms

186 static int check_capture (MatchState *ms, int l) {  in check_capture()  argument
188 if (l < 0 || l >= ms->level || ms->capture[l].len == CAP_UNFINISHED) in check_capture()
189 return luaL_error(ms->L, "invalid capture index"); in check_capture()
194 static int capture_to_close (MatchState *ms) { in capture_to_close() argument
195 int level = ms->level; in capture_to_close()
197 if (ms->capture[level].len == CAP_UNFINISHED) return level; in capture_to_close()
198 return luaL_error(ms->L, "invalid pattern capture"); in capture_to_close()
202 static const char *classend (MatchState *ms, const char *p) { in classend() argument
206 luaL_error(ms->L, "malformed pattern (ends with " LUA_QL("%%") ")"); in classend()
213 luaL_error(ms->L, "malformed pattern (missing " LUA_QL("]") ")"); in classend()
278 static const char *match (MatchState *ms, const char *s, const char *p);
281 static const char *matchbalance (MatchState *ms, const char *s, in matchbalance() argument
284 luaL_error(ms->L, "unbalanced pattern"); in matchbalance()
290 while (++s < ms->src_end) { in matchbalance()
301 static const char *max_expand (MatchState *ms, const char *s, in max_expand() argument
304 while ((s+i)<ms->src_end && singlematch(uchar(*(s+i)), p, ep)) in max_expand()
308 const char *res = match(ms, (s+i), ep+1); in max_expand()
316 static const char *min_expand (MatchState *ms, const char *s, in min_expand() argument
319 const char *res = match(ms, s, ep+1); in min_expand()
322 else if (s<ms->src_end && singlematch(uchar(*s), p, ep)) in min_expand()
329 static const char *start_capture (MatchState *ms, const char *s, in start_capture() argument
332 int level = ms->level; in start_capture()
333 if (level >= LUA_MAXCAPTURES) luaL_error(ms->L, "too many captures"); in start_capture()
334 ms->capture[level].init = s; in start_capture()
335 ms->capture[level].len = what; in start_capture()
336 ms->level = level+1; in start_capture()
337 if ((res=match(ms, s, p)) == NULL) /* match failed? */ in start_capture()
338 ms->level--; /* undo capture */ in start_capture()
343 static const char *end_capture (MatchState *ms, const char *s, in end_capture() argument
345 int l = capture_to_close(ms); in end_capture()
347 ms->capture[l].len = s - ms->capture[l].init; /* close capture */ in end_capture()
348 if ((res = match(ms, s, p)) == NULL) /* match failed? */ in end_capture()
349 ms->capture[l].len = CAP_UNFINISHED; /* undo capture */ in end_capture()
354 static const char *match_capture (MatchState *ms, const char *s, int l) { in match_capture() argument
356 l = check_capture(ms, l); in match_capture()
357 len = ms->capture[l].len; in match_capture()
358 if ((size_t)(ms->src_end-s) >= len && in match_capture()
359 memcmp(ms->capture[l].init, s, len) == 0) in match_capture()
365 static const char *match (MatchState *ms, const char *s, const char *p) { in match() argument
370 return start_capture(ms, s, p+2, CAP_POSITION); in match()
372 return start_capture(ms, s, p+1, CAP_UNFINISHED); in match()
375 return end_capture(ms, s, p+1); in match()
380 s = matchbalance(ms, s, p+2); in match()
388 luaL_error(ms->L, "missing " LUA_QL("[") " after " in match()
390 ep = classend(ms, p); /* points to what is next */ in match()
391 previous = (s == ms->src_init) ? '\0' : *(s-1); in match()
398 s = match_capture(ms, s, uchar(*(p+1))); in match()
411 return (s == ms->src_end) ? s : NULL; /* check end of string */ in match()
415 const char *ep = classend(ms, p); /* points to what is next */ in match()
416 int m = s<ms->src_end && singlematch(uchar(*s), p, ep); in match()
420 if (m && ((res=match(ms, s+1, ep+1)) != NULL)) in match()
425 return max_expand(ms, s, p, ep); in match()
428 return (m ? max_expand(ms, s+1, p, ep) : NULL); in match()
431 return min_expand(ms, s, p, ep); in match()
466 static void push_onecapture (MatchState *ms, int i, const char *s, in push_onecapture() argument
468 if (i >= ms->level) { in push_onecapture()
470 lua_pushlstring(ms->L, s, e - s); /* add whole match */ in push_onecapture()
472 luaL_error(ms->L, "invalid capture index"); in push_onecapture()
475 ptrdiff_t l = ms->capture[i].len; in push_onecapture()
476 if (l == CAP_UNFINISHED) luaL_error(ms->L, "unfinished capture"); in push_onecapture()
478 lua_pushinteger(ms->L, ms->capture[i].init - ms->src_init + 1); in push_onecapture()
480 lua_pushlstring(ms->L, ms->capture[i].init, l); in push_onecapture()
485 static int push_captures (MatchState *ms, const char *s, const char *e) { in push_captures() argument
487 int nlevels = (ms->level == 0 && s) ? 1 : ms->level; in push_captures()
488 luaL_checkstack(ms->L, nlevels, "too many captures"); in push_captures()
490 push_onecapture(ms, i, s, e); in push_captures()
513 MatchState ms; in str_find_aux() local
516 ms.L = L; in str_find_aux()
517 ms.src_init = s; in str_find_aux()
518 ms.src_end = s+l1; in str_find_aux()
521 ms.level = 0; in str_find_aux()
522 if ((res=match(&ms, s1, p)) != NULL) { in str_find_aux()
526 return push_captures(&ms, NULL, 0) + 2; in str_find_aux()
529 return push_captures(&ms, s1, res); in str_find_aux()
531 } while (s1++ < ms.src_end && !anchor); in str_find_aux()
549 MatchState ms; in gmatch_aux() local
554 ms.L = L; in gmatch_aux()
555 ms.src_init = s; in gmatch_aux()
556 ms.src_end = s+ls; in gmatch_aux()
558 src <= ms.src_end; in gmatch_aux()
561 ms.level = 0; in gmatch_aux()
562 if ((e = match(&ms, src, p)) != NULL) { in gmatch_aux()
567 return push_captures(&ms, src, e); in gmatch_aux()
590 static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, in add_s() argument
593 const char *news = lua_tolstring(ms->L, 3, &l); in add_s()
604 push_onecapture(ms, news[i] - '1', s, e); in add_s()
612 static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, in add_value() argument
614 lua_State *L = ms->L; in add_value()
618 add_s(ms, b, s, e); in add_value()
624 n = push_captures(ms, s, e); in add_value()
629 push_onecapture(ms, 0, s, e); in add_value()
652 MatchState ms; in str_gsub() local
658 ms.L = L; in str_gsub()
659 ms.src_init = src; in str_gsub()
660 ms.src_end = src+srcl; in str_gsub()
663 ms.level = 0; in str_gsub()
664 e = match(&ms, src, p); in str_gsub()
667 add_value(&ms, &b, src, e); in str_gsub()
671 else if (src < ms.src_end) in str_gsub()
676 luaL_addlstring(&b, src, ms.src_end-src); in str_gsub()