| 733 | |
| 734 | |
| 735 | static int str_gsub (lua_State *L) { |
| 736 | size_t srcl, lp; |
| 737 | const char *src = luaL_checklstring(L, 1, &srcl); |
| 738 | const char *p = luaL_checklstring(L, 2, &lp); |
| 739 | int tr = lua_type(L, 3); |
| 740 | size_t max_s = luaL_optinteger(L, 4, srcl+1); |
| 741 | int anchor = (*p == '^'); |
| 742 | size_t n = 0; |
| 743 | MatchState ms; |
| 744 | luaL_Buffer b; |
| 745 | luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || |
| 746 | tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, |
| 747 | "string/function/table expected"); |
| 748 | luaL_buffinit(L, &b); |
| 749 | if (anchor) { |
| 750 | p++; lp--; /* skip anchor character */ |
| 751 | } |
| 752 | ms.L = L; |
| 753 | ms.matchdepth = MAXCCALLS; |
| 754 | ms.src_init = src; |
| 755 | ms.src_end = src+srcl; |
| 756 | ms.p_end = p + lp; |
| 757 | while (n < max_s) { |
| 758 | const char *e; |
| 759 | ms.level = 0; |
| 760 | lua_assert(ms.matchdepth == MAXCCALLS); |
| 761 | e = match(&ms, src, p); |
| 762 | if (e) { |
| 763 | n++; |
| 764 | add_value(&ms, &b, src, e, tr); |
| 765 | } |
| 766 | if (e && e>src) /* non empty match? */ |
| 767 | src = e; /* skip it */ |
| 768 | else if (src < ms.src_end) |
| 769 | luaL_addchar(&b, *src++); |
| 770 | else break; |
| 771 | if (anchor) break; |
| 772 | } |
| 773 | luaL_addlstring(&b, src, ms.src_end-src); |
| 774 | luaL_pushresult(&b); |
| 775 | lua_pushinteger(L, n); /* number of substitutions */ |
| 776 | return 2; |
| 777 | } |
| 778 | |
| 779 | /* }====================================================== */ |
| 780 |
nothing calls this directly
no test coverage detected