** Add the replacement value to the string buffer 'b'. ** Return true if the original string was changed. (Function calls and ** table indexing resulting in nil or false do not change the subject.) */
| 904 | ** table indexing resulting in nil or false do not change the subject.) |
| 905 | */ |
| 906 | static int add_value (MatchState *ms, luaL_Buffer *b, const char *s, |
| 907 | const char *e, int tr) { |
| 908 | lua_State *L = ms->L; |
| 909 | switch (tr) { |
| 910 | case LUA_TFUNCTION: { /* call the function */ |
| 911 | int n; |
| 912 | lua_pushvalue(L, 3); /* push the function */ |
| 913 | n = push_captures(ms, s, e); /* all captures as arguments */ |
| 914 | lua_call(L, n, 1); /* call it */ |
| 915 | break; |
| 916 | } |
| 917 | case LUA_TTABLE: { /* index the table */ |
| 918 | push_onecapture(ms, 0, s, e); /* first capture is the index */ |
| 919 | lua_gettable(L, 3); |
| 920 | break; |
| 921 | } |
| 922 | default: { /* LUA_TNUMBER or LUA_TSTRING */ |
| 923 | add_s(ms, b, s, e); /* add value to the buffer */ |
| 924 | return 1; /* something changed */ |
| 925 | } |
| 926 | } |
| 927 | if (!lua_toboolean(L, -1)) { /* nil or false? */ |
| 928 | lua_pop(L, 1); /* remove value */ |
| 929 | luaL_addlstring(b, s, e - s); /* keep original text */ |
| 930 | return 0; /* no changes */ |
| 931 | } |
| 932 | else if (l_unlikely(!lua_isstring(L, -1))) |
| 933 | return luaL_error(L, "invalid replacement value (a %s)", |
| 934 | luaL_typename(L, -1)); |
| 935 | else { |
| 936 | luaL_addvalue(b); /* add result to accumulator */ |
| 937 | return 1; /* something changed */ |
| 938 | } |
| 939 | } |
| 940 | |
| 941 | |
| 942 | static int str_gsub (lua_State *L) { |
no test coverage detected