| 642 | |
| 643 | |
| 644 | static int str_gsub (lua_State *L) { |
| 645 | size_t srcl; |
| 646 | const char *src = luaL_checklstring(L, 1, &srcl); |
| 647 | const char *p = luaL_checkstring(L, 2); |
| 648 | int tr = lua_type(L, 3); |
| 649 | int max_s = luaL_optint(L, 4, srcl+1); |
| 650 | int anchor = (*p == '^') ? (p++, 1) : 0; |
| 651 | int n = 0; |
| 652 | MatchState ms; |
| 653 | luaL_Buffer b; |
| 654 | luaL_argcheck(L, tr == LUA_TNUMBER || tr == LUA_TSTRING || |
| 655 | tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3, |
| 656 | "string/function/table expected"); |
| 657 | luaL_buffinit(L, &b); |
| 658 | ms.L = L; |
| 659 | ms.src_init = src; |
| 660 | ms.src_end = src+srcl; |
| 661 | while (n < max_s) { |
| 662 | const char *e; |
| 663 | ms.level = 0; |
| 664 | e = match(&ms, src, p); |
| 665 | if (e) { |
| 666 | n++; |
| 667 | add_value(&ms, &b, src, e); |
| 668 | } |
| 669 | if (e && e>src) /* non empty match? */ |
| 670 | src = e; /* skip it */ |
| 671 | else if (src < ms.src_end) |
| 672 | luaL_addchar(&b, *src++); |
| 673 | else break; |
| 674 | if (anchor) break; |
| 675 | } |
| 676 | luaL_addlstring(&b, src, ms.src_end-src); |
| 677 | luaL_pushresult(&b); |
| 678 | lua_pushinteger(L, n); /* number of substitutions */ |
| 679 | return 2; |
| 680 | } |
| 681 | |
| 682 | /* }====================================================== */ |
| 683 |
nothing calls this directly
no test coverage detected