| 610 | |
| 611 | |
| 612 | static void add_value (MatchState *ms, luaL_Buffer *b, const char *s, |
| 613 | const char *e) { |
| 614 | lua_State *L = ms->L; |
| 615 | switch (lua_type(L, 3)) { |
| 616 | case LUA_TNUMBER: |
| 617 | case LUA_TSTRING: { |
| 618 | add_s(ms, b, s, e); |
| 619 | return; |
| 620 | } |
| 621 | case LUA_TFUNCTION: { |
| 622 | int n; |
| 623 | lua_pushvalue(L, 3); |
| 624 | n = push_captures(ms, s, e); |
| 625 | lua_call(L, n, 1); |
| 626 | break; |
| 627 | } |
| 628 | case LUA_TTABLE: { |
| 629 | push_onecapture(ms, 0, s, e); |
| 630 | lua_gettable(L, 3); |
| 631 | break; |
| 632 | } |
| 633 | } |
| 634 | if (!lua_toboolean(L, -1)) { /* nil or false? */ |
| 635 | lua_pop(L, 1); |
| 636 | lua_pushlstring(L, s, e - s); /* keep original text */ |
| 637 | } |
| 638 | else if (!lua_isstring(L, -1)) |
| 639 | luaL_error(L, "invalid replacement value (a %s)", luaL_typename(L, -1)); |
| 640 | luaL_addvalue(b); /* add result to accumulator */ |
| 641 | } |
| 642 | |
| 643 | |
| 644 | static int str_gsub (lua_State *L) { |
no test coverage detected