diff --git a/data/lua/core.lua b/data/lua/core.lua index d4746e15bf8..d4431b7d0d6 100644 --- a/data/lua/core.lua +++ b/data/lua/core.lua @@ -36,6 +36,27 @@ function stringx.anim_split(str, sep) return stringx.split(str, sep, {expand_anim = true, strip_spaces = true, remove_empty = true}); end +function stringx.iter_range(str) + return coroutine.wrap(function() + local lo, hi = stringx.parse_range(str) + for i = lo, hi do + coroutine.yield(i) + end + end) +end + +function stringx.iter_ranges(str) + return coroutine.wrap(function() + local split = str:split() + for _,s in ipairs(split) do + local lo, hi = s:parse_range() + for i = lo, hi do + coroutine.yield(i) + end + end + end) +end + --[========[Config Manipulation Functions]========] local function ensure_config(cfg) diff --git a/src/scripting/lua_kernel_base.cpp b/src/scripting/lua_kernel_base.cpp index 3b95dcb7f43..425e6de3104 100644 --- a/src/scripting/lua_kernel_base.cpp +++ b/src/scripting/lua_kernel_base.cpp @@ -720,6 +720,19 @@ static int intf_str_trim(lua_State* L) return 1; } +/** + * Parses a range string of the form a-b into an interval pair + * Accepts the string "infinity" as representing a Very Large Number + */ +static int intf_parse_range(lua_State* L) +{ + const std::string str = luaL_checkstring(L, 1); + auto interval = utils::parse_range(str); + lua_pushnumber(L, interval.first); + lua_pushnumber(L, interval.second); + return 2; +} + static int intf_get_language(lua_State* L) { lua_push(L, get_language().localename); @@ -968,6 +981,7 @@ lua_kernel_base::lua_kernel_base() { "join", &intf_str_join }, { "join_map", &intf_str_join_map }, { "trim", &intf_str_trim }, + { "parse_range", &intf_parse_range }, { "vformat", &intf_format }, { "format_conjunct_list", &intf_format_list }, { "format_disjunct_list", &intf_format_list },