Lines Matching +full:1 +full:ms
48 luaL_checklstring(L, 1, &l); in str_len()
50 return 1; in str_len()
58 else return len - ((size_t)-pos) + 1; in posrelat()
64 const char *s = luaL_checklstring(L, 1, &l); in str_sub()
66 size_t end = posrelat(luaL_optinteger(L, 3, -1), l); in str_sub()
67 if (start < 1) start = 1; in str_sub()
70 lua_pushlstring(L, s + start - 1, end - start + 1); in str_sub()
72 return 1; in str_sub()
79 const char *s = luaL_checklstring(L, 1, &l); in str_reverse()
82 p[i] = s[l - i - 1]; in str_reverse()
84 return 1; in str_reverse()
92 const char *s = luaL_checklstring(L, 1, &l); in str_lower()
97 return 1; in str_lower()
105 const char *s = luaL_checklstring(L, 1, &l); in str_upper()
110 return 1; in str_upper()
115 #define MAXSIZE ((~(size_t)0) >> 1)
119 const char *s = luaL_checklstring(L, 1, &l); in str_rep()
126 size_t totallen = n * l + (n - 1) * lsep; in str_rep()
129 while (n-- > 1) { /* first n-1 copies (followed by separator) */ in str_rep()
138 return 1; in str_rep()
144 const char *s = luaL_checklstring(L, 1, &l); in str_byte()
145 size_t posi = posrelat(luaL_optinteger(L, 2, 1), l); in str_byte()
148 if (posi < 1) posi = 1; in str_byte()
151 n = (int)(pose - posi + 1); in str_byte()
156 lua_pushinteger(L, uchar(s[posi+i-1])); in str_byte()
166 for (i=1; i<=n; i++) { in str_char()
169 p[i - 1] = uchar(c); in str_char()
172 return 1; in str_char()
186 luaL_checktype(L, 1, LUA_TFUNCTION); in str_dump()
187 lua_settop(L, 1); in str_dump()
192 return 1; in str_dump()
204 #define CAP_UNFINISHED (-1)
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
237 l -= '1'; in check_capture()
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()
257 return p+1; 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()
267 return p+1; in classend()
297 int sig = 1; in matchbracketclass()
298 if (*(p+1) == '^') { in matchbracketclass()
308 else if ((*(p+1) == '-') && (p+2 < ec)) { in matchbracketclass()
319 static int singlematch (MatchState *ms, const char *s, const char *p, in singlematch() argument
321 if (s >= ms->src_end) in singlematch()
326 case '.': return 1; /* matches any char */ in singlematch()
327 case L_ESC: return match_class(c, uchar(*(p+1))); in singlematch()
328 case '[': return matchbracketclass(c, p, ep-1); 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()
343 int e = *(p+1); in matchbalance()
344 int cont = 1; in matchbalance()
345 while (++s < ms->src_end) { in matchbalance()
347 if (--cont == 0) return s+1; 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()
365 i--; /* else didn't match; reduce 1 repetition to try again */ 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()
427 if (*(p + 1) == ')') /* position capture? */ 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()
444 switch (*(p + 1)) { 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()
460 if (!matchbracketclass(uchar(previous), p, ep - 1) && in match()
461 matchbracketclass(uchar(*s), p, ep - 1)) { in match()
462 p = ep; goto init; /* return match(ms, s, ep); */ in match()
467 case '0': case '1': case '2': case '3': 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()
501 case '+': /* 1 or more repetitions */ in match()
502 s++; /* 1 match already done */ 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()
530 l2--; /* 1st char will be checked by `memchr' */ in lmemfind()
533 init++; /* 1st char is already checked */ in lmemfind()
534 if (memcmp(init, s2+1, l2) == 0) in lmemfind()
535 return init-1; in lmemfind()
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()
581 upto += strlen(p + upto) + 1; /* may have more after \0 */ in nospecials()
583 return 1; /* no special chars found */ in nospecials()
589 const char *s = luaL_checklstring(L, 1, &ls); in str_find_aux()
591 size_t init = posrelat(luaL_optinteger(L, 3, 1), ls); in str_find_aux()
592 if (init < 1) init = 1; in str_find_aux()
593 else if (init > ls + 1) { /* start after string's end? */ in str_find_aux()
595 return 1; in str_find_aux()
600 const char *s2 = lmemfind(s + init - 1, ls - init + 1, p, lp); in str_find_aux()
602 lua_pushinteger(L, s2 - s + 1); in str_find_aux()
608 MatchState ms; in str_find_aux() local
609 const char *s1 = s + init - 1; in str_find_aux()
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()
625 lua_pushinteger(L, s1 - s + 1); /* start */ 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()
635 return 1; in str_find_aux()
640 return str_find_aux(L, 1); in str_find()
650 MatchState ms; in gmatch_aux() local
652 const char *s = lua_tolstring(L, lua_upvalueindex(1), &ls); in gmatch_aux()
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()
679 luaL_checkstring(L, 1); in str_gmatch()
684 return 1; in str_gmatch()
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()
722 lua_call(L, n, 1); in add_value()
726 push_onecapture(ms, 0, s, e); in add_value()
731 add_s(ms, b, s, e); in add_value()
735 if (!lua_toboolean(L, -1)) { /* nil or false? */ in add_value()
736 lua_pop(L, 1); in add_value()
739 else if (!lua_isstring(L, -1)) in add_value()
740 luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); in add_value()
747 const char *src = luaL_checklstring(L, 1, &srcl); in str_gsub()
750 size_t max_s = luaL_optinteger(L, 4, srcl+1); in str_gsub()
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()
832 /* maximum size of each formatted item (> len(format('%99.99f', -1e308))) */
854 if (!isdigit(uchar(*(s+1)))) in addquoted()
882 memcpy(form, strfrmt, (p - strfrmt + 1) * sizeof(char)); in scanformat()
883 form += p - strfrmt + 1; in scanformat()
895 char spec = form[l - 1]; in addlenmod()
896 strlcpy(form + l - 1, lenmod, size - (l - 1)); in addlenmod()
897 form[l + lm - 1] = spec; in addlenmod()
904 int arg = 1; in str_format()
931 luaL_argcheck(L, -1 < diff && diff < 1, arg, in str_format()
941 luaL_argcheck(L, -1 < diff && diff < 1, arg, in str_format()
973 lua_pop(L, 1); /* remove result from 'luaL_tolstring' */ in str_format()
979 LUA_QL("format"), *(strfrmt - 1)); in str_format()
986 return 1; in str_format()
1014 lua_createtable(L, 0, 1); /* table to be metatable for strings */ in createmetatable()
1018 lua_pop(L, 1); /* pop dummy string */ in createmetatable()
1021 lua_pop(L, 1); /* pop metatable */ in createmetatable()
1031 return 1; in luaopen_string()