summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-06-04 22:41:25 +0800
committerruki <[email protected]>2019-06-04 10:08:31 +0800
commit18663d52e5ff484eea423f88cbf52cc10c5721f3 (patch)
tree3e1e147713b4ca50276b676b9b346f52f893708b
parent255d51be7e7ed10a13a4cd55b5aae55079ab4bb9 (diff)
improve string.split
-rw-r--r--xmake/actions/config/configfiles.lua4
-rw-r--r--xmake/core/base/colors.lua4
-rw-r--r--xmake/core/base/interpreter.lua8
-rw-r--r--xmake/core/base/string.lua39
-rw-r--r--xmake/core/language/language.lua2
-rw-r--r--xmake/core/project/project.lua2
-rw-r--r--xmake/core/sandbox/modules/import/core/package/repository.lua2
-rw-r--r--xmake/core/sandbox/modules/import/lib/detect/find_program.lua2
-rw-r--r--xmake/core/ui/label.lua4
-rw-r--r--xmake/modules/core/tools/cl.lua6
-rw-r--r--xmake/modules/core/tools/gcc.lua4
-rw-r--r--xmake/modules/core/tools/nvcc.lua4
-rw-r--r--xmake/modules/devel/git/ls_remote.lua2
-rw-r--r--xmake/modules/lib/detect/pkg_config.lua2
-rw-r--r--xmake/modules/package/manager/find_package.lua4
-rw-r--r--xmake/modules/package/manager/install_package.lua4
-rw-r--r--xmake/modules/private/tools/gcc/parse_deps.lua2
17 files changed, 59 insertions, 36 deletions
diff --git a/xmake/actions/config/configfiles.lua b/xmake/actions/config/configfiles.lua
index 90ed578d5..08ef5401e 100644
--- a/xmake/actions/config/configfiles.lua
+++ b/xmake/actions/config/configfiles.lua
@@ -181,7 +181,7 @@ function _generate_configfile(srcfile, dstfile, fileinfo, targets)
-- is ${define variable}?
local isdefine = false
if variable:startswith("define ") then
- variable = variable:split("%s+")[2]
+ variable = variable:split("%s")[2]
isdefine = true
end
@@ -189,7 +189,7 @@ function _generate_configfile(srcfile, dstfile, fileinfo, targets)
local default = nil
local isdefault = false
if variable:startswith("default ") then
- local varinfo = variable:split("%s+")
+ local varinfo = variable:split("%s")
variable = varinfo[2]
default = varinfo[3]
isdefault = true
diff --git a/xmake/core/base/colors.lua b/xmake/core/base/colors.lua
index 8c5b2c287..2223c3f85 100644
--- a/xmake/core/base/colors.lua
+++ b/xmake/core/base/colors.lua
@@ -324,7 +324,7 @@ function colors.translate(str)
end
-- split words
- local blocks_raw = word:split("%s+")
+ local blocks_raw = word:split("%s")
-- translate theme color first, e.g ${color.error}
local blocks = {}
@@ -332,7 +332,7 @@ function colors.translate(str)
if theme then
local theme_block = theme:get(block)
if theme_block then
- for _, theme_block_sub in ipairs(theme_block:split("%s+")) do
+ for _, theme_block_sub in ipairs(theme_block:split("%s")) do
table.insert(blocks, theme_block_sub)
end
else
diff --git a/xmake/core/base/interpreter.lua b/xmake/core/base/interpreter.lua
index 029fe3e60..01d76973e 100644
--- a/xmake/core/base/interpreter.lua
+++ b/xmake/core/base/interpreter.lua
@@ -119,7 +119,7 @@ function interpreter._fetch_root_scope(root)
for scope_kind_and_name, _ in pairs(root or {}) do
-- is scope_kind@@scope_name?
- scope_kind_and_name = scope_kind_and_name:split("@@")
+ scope_kind_and_name = scope_kind_and_name:split("@@", {plain = true})
if #scope_kind_and_name == 2 then
local scope_kind = scope_kind_and_name[1]
local scope_name = scope_kind_and_name[2]
@@ -1413,7 +1413,7 @@ function interpreter:api_define(apis)
-- get api function
local apiscope = nil
local funcname = nil
- apifunc = apifunc:split('.')
+ apifunc = apifunc:split('.', {plain = true})
assert(apifunc)
if #apifunc == 2 then
apiscope = apifunc[1]
@@ -1470,7 +1470,7 @@ function interpreter:api_builtin_set_xmakever(minver)
end
-- parse minimum version
- local minvers = minver:split('.')
+ local minvers = minver:split('.', {plain = true})
if not minvers or #minvers ~= 3 then
os.raise("[nobacktrace]: set_xmakever(\"%s\"): invalid version format!", minver)
end
@@ -1479,7 +1479,7 @@ function interpreter:api_builtin_set_xmakever(minver)
local minvers_num = minvers[1] * 100 + minvers[2] * 10 + minvers[3]
-- parse current version
- local curvers = xmake._VERSION_SHORT:split('.')
+ local curvers = xmake._VERSION_SHORT:split('.', {plain = true})
-- make current numerical version
local curvers_num = curvers[1] * 100 + curvers[2] * 10 + curvers[3]
diff --git a/xmake/core/base/string.lua b/xmake/core/base/string.lua
index b27d076d5..878a2f683 100644
--- a/xmake/core/base/string.lua
+++ b/xmake/core/base/string.lua
@@ -143,19 +143,42 @@ function string:find_last(pattern, plain)
end
end
--- split string with the given characters
+-- split string with the given substring/characters
--
+-- pattern match and ignore empty string
-- ("1\n\n2\n3"):split('\n') => 1, 2, 3
--- ("1\n\n2\n3"):split('\n', true) => 1, , 2, 3
+-- ("abc123123xyz123abc"):split('123') => abc, xyz, abc
+-- ("abc123123xyz123abc"):split('[123]+') => abc, xyz, abc
--
-function string:split(delimiter, strict)
+-- plain match and ignore empty string
+-- ("1\n\n2\n3"):split('\n', {plain = true}) => 1, 2, 3
+-- ("abc123123xyz123abc"):split('123', {plain = true}) => abc, xyz, abc
+--
+-- pattern match and contains empty string
+-- ("1\n\n2\n3"):split('\n', {strict = true}) => 1, , 2, 3
+-- ("abc123123xyz123abc"):split('123', {strict = true}) => abc, , xyz, abc
+-- ("abc123123xyz123abc"):split('[123]+', {strict = true}) => abc, xyz, abc
+--
+-- plain match and contains empty string
+-- ("1\n\n2\n3"):split('\n', {plain = true, strict = true}) => 1, , 2, 3
+-- ("abc123123xyz123abc"):split('123', {plain = true, strict = true}) => abc, , xyz, abc
+--
+function string:split(delimiter, opt)
local result = {}
- if strict then
- for match in (self .. delimiter):gmatch("(.-)" .. delimiter) do
- table.insert(result, match)
+ local start = 1
+ local pos, epos = self:find(delimiter, start, opt and opt.plain)
+ while pos do
+ local substr = self:sub(start, pos - 1)
+ if #substr > 0 then
+ table.insert(result, substr)
+ elseif opt and opt.strict then
+ table.insert(result, substr)
end
- else
- self:gsub("[^" .. delimiter .."]+", function(v) table.insert(result, v) end)
+ start = epos + 1
+ pos, epos = self:find(delimiter, start, opt and opt.plain)
+ end
+ if start <= #self then
+ table.insert(result, self:sub(start))
end
return result
end
diff --git a/xmake/core/language/language.lua b/xmake/core/language/language.lua
index 682532dcf..a7fdddcc0 100644
--- a/xmake/core/language/language.lua
+++ b/xmake/core/language/language.lua
@@ -162,7 +162,7 @@ function _instance:nameflags()
for _, namedflag in ipairs(nameflags) do
-- split it by '.'
- local splitinfo = namedflag:split('.')
+ local splitinfo = namedflag:split('.', {plain = true})
assert(#splitinfo == 2)
-- get flag scope
diff --git a/xmake/core/project/project.lua b/xmake/core/project/project.lua
index 03909c93f..07fb6792f 100644
--- a/xmake/core/project/project.lua
+++ b/xmake/core/project/project.lua
@@ -660,7 +660,7 @@ function project._load_requires()
for _, requirestr in ipairs(table.wrap(requires_str)) do
-- get the package name
- local packagename = requirestr:split('%s+')[1]
+ local packagename = requirestr:split('%s')[1]
-- get alias
local alias = nil
diff --git a/xmake/core/sandbox/modules/import/core/package/repository.lua b/xmake/core/sandbox/modules/import/core/package/repository.lua
index f6080d965..80f266d02 100644
--- a/xmake/core/sandbox/modules/import/core/package/repository.lua
+++ b/xmake/core/sandbox/modules/import/core/package/repository.lua
@@ -114,7 +114,7 @@ function sandbox_core_package_repository.repositories(is_global)
--
if not is_global then
for _, repo in ipairs(table.wrap(project.get("repositories"))) do
- local repoinfo = repo:split(' ')
+ local repoinfo = repo:split('%s')
if #repoinfo <= 3 then
local repo = repository.load(repoinfo[1], repoinfo[2], repoinfo[3], is_global)
if repo then
diff --git a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
index fda3e03df..d9cfbf484 100644
--- a/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
+++ b/xmake/core/sandbox/modules/import/lib/detect/find_program.lua
@@ -103,7 +103,7 @@ function sandbox_lib_detect_find_program._find_from_pathes(name, pathes, opt)
end
-- the program path
- if program_path and (os.isexec(program_path) or os.isexec(program_path:split("%s+")[1])) then
+ if program_path and (os.isexec(program_path) or os.isexec(program_path:split("%s")[1])) then
-- check it
if sandbox_lib_detect_find_program._check(program_path, opt) then
return program_path
diff --git a/xmake/core/ui/label.lua b/xmake/core/ui/label.lua
index 894cae478..e8fc0bfc6 100644
--- a/xmake/core/ui/label.lua
+++ b/xmake/core/ui/label.lua
@@ -110,7 +110,7 @@ function label:textattr_val()
end
-- update the cache
- value = curses.calc_attr(textattr:split("%s+"))
+ value = curses.calc_attr(textattr:split("%s"))
self._TEXTATTR[textattr] = value
return value
end
@@ -123,7 +123,7 @@ function label:splitext(text, width)
-- split text first
local result = {}
- local lines = text:split('\n', true)
+ local lines = text:split('\n', {strict = true})
for idx = 1, #lines do
local line = lines[idx]
while #line > width do
diff --git a/xmake/modules/core/tools/cl.lua b/xmake/modules/core/tools/cl.lua
index 99c9b982f..3859358d3 100644
--- a/xmake/modules/core/tools/cl.lua
+++ b/xmake/modules/core/tools/cl.lua
@@ -313,7 +313,7 @@ function _include_deps(self, outdata)
-- translate it
local results = {}
local uniques = {}
- for _, line in ipairs(outdata:split("\r\n")) do
+ for _, line in ipairs(outdata:split("\r\n", {plain = true})) do
-- get includefile
local includefile = _include_note(self, line)
@@ -410,7 +410,7 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags)
-- filter includes notes: "Note: including file: xxx.h", @note maybe not english language
local results = ""
- for _, line in ipairs(tostring(errors):split("\r\n")) do
+ for _, line in ipairs(tostring(errors):split("\r\n", {plain = true})) do
if not _include_note(self, line) then
results = results .. line .. "\r\n"
end
@@ -430,7 +430,7 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags)
end
if #output:trim() > 0 then
local lines = {}
- for _, line in ipairs(output:split("\r\n")) do
+ for _, line in ipairs(output:split("\r\n", {plain = true})) do
if line:match("warning %a+[0-9]+%s*:") then
table.insert(lines, line)
end
diff --git a/xmake/modules/core/tools/gcc.lua b/xmake/modules/core/tools/gcc.lua
index 8396c1746..ad4104cd9 100644
--- a/xmake/modules/core/tools/gcc.lua
+++ b/xmake/modules/core/tools/gcc.lua
@@ -425,7 +425,7 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags)
os.tryrm(objectfile)
-- parse and strip errors
- local lines = errors and tostring(errors):split('\n') or {}
+ local lines = errors and tostring(errors):split('\n', {plain = true}) or {}
if not option.get("verbose") then
-- find the start line of error
@@ -452,7 +452,7 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags)
function (ok, outdata, errdata)
-- show warnings?
if ok and errdata and #errdata > 0 and (option.get("diagnosis") or option.get("warning")) then
- local lines = errdata:split('\n')
+ local lines = errdata:split('\n', {plain = true})
if #lines > 0 then
local warnings = table.concat(table.slice(lines, 1, ifelse(#lines > 8, 8, #lines)), "\n")
cprint("${color.warning}%s", warnings)
diff --git a/xmake/modules/core/tools/nvcc.lua b/xmake/modules/core/tools/nvcc.lua
index 066e86cc9..568c6aef9 100644
--- a/xmake/modules/core/tools/nvcc.lua
+++ b/xmake/modules/core/tools/nvcc.lua
@@ -309,7 +309,7 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags)
os.tryrm(objectfile)
-- find the start line of error
- local lines = tostring(errors):split("\n")
+ local lines = tostring(errors):split("\n", {plain = true})
local start = 0
for index, line in ipairs(lines) do
if line:find("error:", 1, true) or line:find("错误:", 1, true) then
@@ -334,7 +334,7 @@ function _compile1(self, sourcefile, objectfile, dependinfo, flags)
-- print some warnings
if warnings and #warnings > 0 and (option.get("verbose") or option.get("warning")) then
- cprint("${color.warning}%s", table.concat(table.slice(warnings:split('\n'), 1, 8), '\n'))
+ cprint("${color.warning}%s", table.concat(table.slice(warnings:split('\n', {plain = true}), 1, 8), '\n'))
end
-- generate the dependent includes
diff --git a/xmake/modules/devel/git/ls_remote.lua b/xmake/modules/devel/git/ls_remote.lua
index e7e37b8ac..391b85caf 100644
--- a/xmake/modules/devel/git/ls_remote.lua
+++ b/xmake/modules/devel/git/ls_remote.lua
@@ -58,7 +58,7 @@ function main(reftype, url)
for _, line in ipairs(data:split('\n')) do
-- parse commit and ref
- local refinfo = line:split('%s+')
+ local refinfo = line:split('%s')
-- get commit
local commit = refinfo[1]
diff --git a/xmake/modules/lib/detect/pkg_config.lua b/xmake/modules/lib/detect/pkg_config.lua
index 174dd7bd2..2e645379f 100644
--- a/xmake/modules/lib/detect/pkg_config.lua
+++ b/xmake/modules/lib/detect/pkg_config.lua
@@ -65,7 +65,7 @@ function info(name, opt)
-- init result
result = {}
- for _, flag in ipairs(flags:split('%s*')) do
+ for _, flag in ipairs(flags:split('%s')) do
-- get links
local link = flag:match("%-l(.*)")
diff --git a/xmake/modules/package/manager/find_package.lua b/xmake/modules/package/manager/find_package.lua
index d9391d1a5..287747d17 100644
--- a/xmake/modules/package/manager/find_package.lua
+++ b/xmake/modules/package/manager/find_package.lua
@@ -159,7 +159,7 @@ function main(name, opt)
opt.mode = opt.mode or config.mode() or "release"
-- get package manager name
- local manager_name, package_name = unpack(name:split("::", true))
+ local manager_name, package_name = unpack(name:split("::", {plain = true, strict = true}))
if package_name == nil then
package_name = manager_name
manager_name = nil
@@ -169,7 +169,7 @@ function main(name, opt)
-- get package name and require version
local require_version = nil
- package_name, require_version = unpack(package_name:trim():split("%s+"))
+ package_name, require_version = unpack(package_name:trim():split("%s"))
opt.version = require_version or opt.version
-- find package
diff --git a/xmake/modules/package/manager/install_package.lua b/xmake/modules/package/manager/install_package.lua
index b46062c42..1a5451758 100644
--- a/xmake/modules/package/manager/install_package.lua
+++ b/xmake/modules/package/manager/install_package.lua
@@ -92,7 +92,7 @@ function main(name, opt)
opt.mode = opt.mode or config.mode() or "release"
-- get package manager name
- local manager_name, package_name = unpack(name:split("::", true))
+ local manager_name, package_name = unpack(name:split("::", {plain = true, strict = true}))
if package_name == nil then
package_name = manager_name
manager_name = nil
@@ -102,7 +102,7 @@ function main(name, opt)
-- get package name and require version
local require_version = nil
- package_name, require_version = unpack(package_name:trim():split("%s+"))
+ package_name, require_version = unpack(package_name:trim():split("%s"))
opt.version = require_version or opt.version
-- do install package
diff --git a/xmake/modules/private/tools/gcc/parse_deps.lua b/xmake/modules/private/tools/gcc/parse_deps.lua
index 382a3dcdf..4d38aede9 100644
--- a/xmake/modules/private/tools/gcc/parse_deps.lua
+++ b/xmake/modules/private/tools/gcc/parse_deps.lua
@@ -64,7 +64,7 @@ function main(depsdata)
-- parse results
local results = {}
local data = depsdata:gsub("\\\n", "")
- for _, line in ipairs(data:split("\n")) do
+ for _, line in ipairs(data:split("\n", {plain = true})) do
local p = line:find(':', 1, true)
if p then
line = line:sub(p + 1)