MCPcopy Create free account
hub / github.com/ObEngine/ObEngine / str_gsub

Function str_gsub

extlibs/lua/src/lstrlib.c:939–980  ·  view source on GitHub ↗

Source from the content-addressed store, hash-verified

937
938
939static int str_gsub (lua_State *L) {
940 size_t srcl, lp;
941 const char *src = luaL_checklstring(L, 1, &srcl); /* subject */
942 const char *p = luaL_checklstring(L, 2, &lp); /* pattern */
943 const char *lastmatch = NULL; /* end of last match */
944 int tr = lua_type(L, 3); /* replacement type */
945 lua_Integer max_s = luaL_optinteger(L, 4, srcl + 1); /* max replacements */
946 int anchor = (*p == '^');
947 lua_Integer n = 0; /* replacement count */
948 int changed = 0; /* change flag */
949 MatchState ms;
950 luaL_Buffer b;
951 luaL_argexpected(L, tr == LUA_TNUMBER || tr == LUA_TSTRING ||
952 tr == LUA_TFUNCTION || tr == LUA_TTABLE, 3,
953 "string/function/table");
954 luaL_buffinit(L, &b);
955 if (anchor) {
956 p++; lp--; /* skip anchor character */
957 }
958 prepstate(&ms, L, src, srcl, p, lp);
959 while (n < max_s) {
960 const char *e;
961 reprepstate(&ms); /* (re)prepare state for new match */
962 if ((e = match(&ms, src, p)) != NULL && e != lastmatch) { /* match? */
963 n++;
964 changed = add_value(&ms, &b, src, e, tr) | changed;
965 src = lastmatch = e;
966 }
967 else if (src < ms.src_end) /* otherwise, skip one character */
968 luaL_addchar(&b, *src++);
969 else break; /* end of subject */
970 if (anchor) break;
971 }
972 if (!changed) /* no changes? */
973 lua_pushvalue(L, 1); /* return original string */
974 else { /* something changed */
975 luaL_addlstring(&b, src, ms.src_end-src);
976 luaL_pushresult(&b); /* create and return new string */
977 }
978 lua_pushinteger(L, n); /* number of substitutions */
979 return 2;
980}
981
982/* }====================================================== */
983

Callers

nothing calls this directly

Calls 12

luaL_checklstringFunction · 0.85
lua_typeFunction · 0.85
luaL_optintegerFunction · 0.85
prepstateFunction · 0.85
reprepstateFunction · 0.85
add_valueFunction · 0.85
lua_pushvalueFunction · 0.85
lua_pushintegerFunction · 0.85
luaL_buffinitFunction · 0.70
matchFunction · 0.70
luaL_addlstringFunction · 0.70
luaL_pushresultFunction · 0.70

Tested by

no test coverage detected