]> git.lizzy.rs Git - minetest.git/commitdiff
Error when string.split is given empty separator (#13132)
authorJude Melton-Houghton <jwmhjwmh@gmail.com>
Tue, 10 Jan 2023 14:25:48 +0000 (09:25 -0500)
committerGitHub <noreply@github.com>
Tue, 10 Jan 2023 14:25:48 +0000 (09:25 -0500)
builtin/common/misc_helpers.lua
builtin/common/tests/misc_helpers_spec.lua
doc/lua_api.txt

index 1a2b9500ae556831d97308667b4d09399af81520..90ac2ae4ef1d6c93e9aabea48e9a59deb4775130 100644 (file)
@@ -170,6 +170,9 @@ end
 --------------------------------------------------------------------------------
 function string.split(str, delim, include_empty, max_splits, sep_is_pattern)
        delim = delim or ","
+       if delim == "" then
+               error("string.split separator is empty", 2)
+       end
        max_splits = max_splits or -2
        local items = {}
        local pos, len = 1, #str
index ff0f0298ab51f9436f8b60eaefc77519aabb70a1..73a66737fde61feb8e4d241bc9706e5f3e0933dd 100644 (file)
@@ -38,6 +38,12 @@ describe("string", function()
                        assert.same({ "one", "two" }, string.split("one,two", ",", false, -1, true))
                        assert.same({ "one", "two", "three" }, string.split("one2two3three", "%d", false, -1, true))
                end)
+
+               it("rejects empty separator", function()
+                       assert.has.errors(function()
+                               string.split("", "")
+                       end)
+               end)
        end)
 end)
 
index fb6bc9df8e54dda759a875f1eaaeb57e07f2fc1f..de0517d42d6a75b41875d8b08d8dbe0d58ddb6fd 100644 (file)
@@ -3623,7 +3623,7 @@ Helper functions
 * `math.round(x)`: Returns `x` rounded to the nearest integer.
     * At a multiple of 0.5, rounds away from zero.
 * `string.split(str, separator, include_empty, max_splits, sep_is_pattern)`
-    * `separator`: string, default: `","`
+    * `separator`: string, cannot be empty, default: `","`
     * `include_empty`: boolean, default: `false`
     * `max_splits`: number, if it's negative, splits aren't limited,
       default: `-1`