| 843 | |
| 844 | |
| 845 | static int str_gsub(lua_State *L) { |
| 846 | size_t srcl, lp; |
| 847 | const char *src = luaL_checklstring(L, 1, &srcl); |
| 848 | const char *p = luaL_checklstring(L, 2, &lp); |
| 849 | int tr = lua_type(L, 3); |
| 850 | lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); |
| 851 | int anchor = (*p == '^'); |
| 852 | lua_Integer n = 0; |
| 853 | MatchState ms; |
| 854 | luaL_Buffer b; |
| 855 | luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || |
| 856 | tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, |
| 857 | "string/function/table expected"); |
| 858 | luaL_buffinit(L, &b); |
| 859 | if (anchor) { |
| 860 | p++; lp--; /* skip anchor character */ |
| 861 | } |
| 862 | prepstate(&ms, L, src, srcl, p, lp); |
| 863 | while (n < max_s) { |
| 864 | const char *e; |
| 865 | reprepstate(&ms); |
| 866 | if ((e = match(&ms, src, p)) != nullptr) { |
| 867 | n++; |
| 868 | add_value(&ms, &b, src, e, tr); |
| 869 | } |
| 870 | if (e && e > src) /* non empty match? */ |
| 871 | src = e; /* skip it */ |
| 872 | else if (src < ms.src_end) |
| 873 | luaL_addchar(&b, *src++); |
| 874 | else break; |
| 875 | if (anchor) break; |
| 876 | } |
| 877 | luaL_addlstring(&b, src, ms.src_end - src); |
| 878 | luaL_pushresult(&b); |
| 879 | lua_pushinteger(L, n); /* number of substitutions */ |
| 880 | return 2; |
| 881 | } |
| 882 | |
| 883 | /* }====================================================== */ |
| 884 |
nothing calls this directly
no test coverage detected