| 872 | |
| 873 | |
| 874 | static void add_s (MatchState *ms, luaL_Buffer *b, const char *s, |
| 875 | const char *e) { |
| 876 | size_t l; |
| 877 | lua_State *L = ms->L; |
| 878 | const char *news = lua_tolstring(L, 3, &l); |
| 879 | const char *p; |
| 880 | while ((p = (char *)memchr(news, L_ESC, l)) != NULL) { |
| 881 | luaL_addlstring(b, news, ct_diff2sz(p - news)); |
| 882 | p++; /* skip ESC */ |
| 883 | if (*p == L_ESC) /* '%%' */ |
| 884 | luaL_addchar(b, *p); |
| 885 | else if (*p == '0') /* '%0' */ |
| 886 | luaL_addlstring(b, s, ct_diff2sz(e - s)); |
| 887 | else if (isdigit(cast_uchar(*p))) { /* '%n' */ |
| 888 | const char *cap; |
| 889 | ptrdiff_t resl = get_onecapture(ms, *p - '1', s, e, &cap); |
| 890 | if (resl == CAP_POSITION) |
| 891 | luaL_addvalue(b); /* add position to accumulated result */ |
| 892 | else |
| 893 | luaL_addlstring(b, cap, cast_sizet(resl)); |
| 894 | } |
| 895 | else |
| 896 | luaL_error(L, "invalid use of '%c' in replacement string", L_ESC); |
| 897 | l -= ct_diff2sz(p + 1 - news); |
| 898 | news = p + 1; |
| 899 | } |
| 900 | luaL_addlstring(b, news, l); |
| 901 | } |
| 902 | |
| 903 | |
| 904 | /* |
no test coverage detected