diff options
author | Diego Martinez <kaeza@users.sf.net> | 2014-12-18 23:50:31 -0300 |
---|---|---|
committer | kwolekr <kwolekr@minetest.net> | 2014-12-28 23:27:07 -0500 |
commit | ab55da589c27e65e54956b32a1550e21169447ae (patch) | |
tree | e157ce96c036e618154996f90bb7b5be8a762585 | |
parent | 86cfbc21da10e9984a1b9f8f2f5bf259fba5efb9 (diff) | |
download | minetest-ab55da589c27e65e54956b32a1550e21169447ae.tar.gz minetest-ab55da589c27e65e54956b32a1550e21169447ae.tar.bz2 minetest-ab55da589c27e65e54956b32a1550e21169447ae.zip |
Faster string.split implementation.
-rw-r--r-- | builtin/common/misc_helpers.lua | 44 |
1 files changed, 22 insertions, 22 deletions
diff --git a/builtin/common/misc_helpers.lua b/builtin/common/misc_helpers.lua index 713555109..2ee990e5c 100644 --- a/builtin/common/misc_helpers.lua +++ b/builtin/common/misc_helpers.lua @@ -155,33 +155,33 @@ function dump(o, indent, nested, level) end -------------------------------------------------------------------------------- -function string.split(str, delim, include_empty, max_splits) +-- Localize functions to avoid table lookups (better performance). +local table_insert = table.insert +local str_sub, str_find = string.sub, string.find +function string.split(str, delim, include_empty, max_splits, sep_is_pattern) delim = delim or "," - max_splits = max_splits or 0 - local fields = {} - local num_splits = 0 - local last_pos = 0 - for part, pos in str:gmatch("(.-)[" .. delim .. "]()") do - last_pos = pos - if include_empty or part ~= "" then - num_splits = num_splits + 1 - fields[num_splits] = part - if max_splits > 0 and num_splits + 1 >= max_splits then - break - end + max_splits = max_splits or -1 + local items = {} + local pos, len, seplen = 1, #str, #delim + local plain = not sep_is_pattern + max_splits = max_splits + 1 + repeat + local np, npe = str_find(str, delim, pos, plain) + np, npe = (np or (len+1)), (npe or (len+1)) + if (not np) or (max_splits == 1) then + np = len + 1 + npe = np end - end - -- Handle the last field - if max_splits <= 0 or num_splits <= max_splits then - local last_part = str:sub(last_pos) - if include_empty or last_part ~= "" then - fields[num_splits + 1] = last_part + local s = str_sub(str, pos, np - 1) + if include_empty or (s ~= "") then + max_splits = max_splits - 1 + table_insert(items, s) end - end - return fields + pos = npe + 1 + until (max_splits == 0) or (pos > len) + return items end - -------------------------------------------------------------------------------- function file_exists(filename) local f = io.open(filename, "r") |