summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorruki <[email protected]>2019-07-13 21:51:57 +0800
committerGitHub <[email protected]>2019-07-13 21:51:57 +0800
commit757cbe8b28ddc852fdbac81f57e18c078d470181 (patch)
tree08858cca8ee06ac7c08b5984bca1e901af985742
parent2a77f8a757ead54b5103e77b793cef7d9c0e2351 (diff)
parent7cff50433386d8c7793884cddd1eb6e1c3c2681e (diff)
Merge pull request #484 from OpportunityLiu/dev
Improve string.convert, dump & file.lines
-rw-r--r--core/src/xmake/string/convert.c10
-rw-r--r--xmake/core/base/dump.lua143
-rw-r--r--xmake/core/base/hashset.lua26
-rw-r--r--xmake/core/base/io.lua20
-rw-r--r--xmake/rules/cuda/gencodes/xmake.lua15
5 files changed, 141 insertions, 73 deletions
diff --git a/core/src/xmake/string/convert.c b/core/src/xmake/string/convert.c
index a6edd5692..49ad3ca7a 100644
--- a/core/src/xmake/string/convert.c
+++ b/core/src/xmake/string/convert.c
@@ -53,12 +53,12 @@ static xm_charset_entry_t g_charsets[] =
, {TB_CHARSET_TYPE_GB2312, "gb2312" }
, {TB_CHARSET_TYPE_GBK, "gbk" }
, {TB_CHARSET_TYPE_ISO8859, "iso8859" }
-, {TB_CHARSET_TYPE_UCS2, "ucs2" }
-, {TB_CHARSET_TYPE_UCS4, "ucs4" }
-, {TB_CHARSET_TYPE_UTF16, "utf16" }
+, {TB_CHARSET_TYPE_UCS2 | TB_CHARSET_TYPE_NE, "ucs2" }
+, {TB_CHARSET_TYPE_UCS4 | TB_CHARSET_TYPE_NE, "ucs4" }
+, {TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_NE, "utf16" }
, {TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_BE, "utf16be" }
, {TB_CHARSET_TYPE_UTF16 | TB_CHARSET_TYPE_LE, "utf16le" }
-, {TB_CHARSET_TYPE_UTF32, "utf32" }
+, {TB_CHARSET_TYPE_UTF32 | TB_CHARSET_TYPE_NE, "utf32" }
, {TB_CHARSET_TYPE_UTF32 | TB_CHARSET_TYPE_BE, "utf32be" }
, {TB_CHARSET_TYPE_UTF32 | TB_CHARSET_TYPE_LE, "utf32le" }
, {TB_CHARSET_TYPE_UTF8, "utf8" }
@@ -115,6 +115,8 @@ tb_int_t xm_string_convert(lua_State* lua)
// find charsets
xm_charset_entry_ref_t fcharset = xm_string_charset_find_by_name(ftype_cstr);
xm_charset_entry_ref_t tcharset = xm_string_charset_find_by_name(ttype_cstr);
+ luaL_argcheck(lua, fcharset, 2, "charset not found");
+ luaL_argcheck(lua, tcharset, 3, "charset not found");
tb_check_return_val(fcharset && tcharset, 0);
// empty string?
diff --git a/xmake/core/base/dump.lua b/xmake/core/base/dump.lua
index fc67749ad..cae90e9a7 100644
--- a/xmake/core/base/dump.lua
+++ b/xmake/core/base/dump.lua
@@ -111,10 +111,99 @@ function dump._print_table_anchor(value, printed_set)
printed_set.len = printed_set.len + 1
io.write(" ")
dump._print_anchor(printed_set.len)
- io.write("\n")
printed_set[value] = printed_set.len
end
+function dump._print_metatable(value, metatable, inner_indent, printed_set, print_archor)
+ if not metatable then
+ return false
+ end
+
+ local has_record = false
+ local has_index_table = false
+ -- print metamethods
+ for k, v in pairs(metatable) do
+ if k == "__index" and type(v) == "table" then
+ has_index_table = true
+ elseif k:startswith("__") then
+ if not has_record then
+ has_record = true
+ if print_archor then
+ dump._print_table_anchor(value, printed_set)
+ end
+ end
+ io.write("\n", inner_indent)
+ local funcname = k:sub(3)
+ dump._print_keyword(funcname)
+ io.write(colors.translate("${dim} = "))
+ if funcname == "tostring" or funcname == "len" then
+ local ok, result = pcall(v, value, value)
+ if ok then
+ dump._print_scalar(result)
+ io.write(" (evaluated)")
+ else
+ dump._print_scalar(v)
+ end
+ elseif v and printed_set[v] then
+ dump._print_reference(printed_set[v])
+ else
+ dump._print_scalar(v)
+ end
+ io.write(",")
+ end
+ end
+
+ if not has_index_table then
+ return has_record
+ end
+
+ local index_table = metatable and rawget(metatable, "__index")
+ -- print index methods
+ for k, v in pairs(index_table) do
+ -- hide private interfaces
+ if type(k) ~= "string" or not k:startswith("_") then
+ if not has_record then
+ has_record = true
+ if print_archor then
+ dump._print_table_anchor(value, printed_set)
+ end
+ end
+ io.write("\n", inner_indent)
+ dump._print_keyword("(")
+ dump._print_scalar(k, true)
+ dump._print_keyword(")")
+ io.write(colors.translate("${dim} = "))
+ if v and printed_set[v] then
+ dump._print_reference(printed_set[v])
+ else
+ dump._print_scalar(v)
+ end
+ io.write(",")
+ end
+ end
+
+ return has_record
+end
+
+-- print udata
+function dump._print_udata(value, first_indent, remain_indent)
+
+ io.write(first_indent)
+ local metatable = getmetatable(value)
+ local inner_indent = remain_indent .. " "
+ -- print open brackets
+ io.write(colors.translate("${dim}["))
+
+ local no_value = not dump._print_metatable(value, metatable, inner_indent, { len = 0 }, false)
+
+ -- print close brackets
+ if no_value then
+ io.write(colors.translate(" ${dim}]"))
+ else
+ io.write("\b \n" .. remain_indent .. colors.translate("${dim}]"))
+ end
+end
+
-- print table
function dump._print_table(value, first_indent, remain_indent, printed_set)
@@ -130,8 +219,6 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
end
printed_set = printed_set or { len = 0 }
local inner_indent = remain_indent .. " "
- local index_table = metatable and rawget(metatable, "__index")
- if type(index_table) ~="table" then index_table = nil end
local first_value = true
-- print open brackets
io.write(colors.translate("${dim}{"))
@@ -140,52 +227,12 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
if first_value then
dump._print_table_anchor(value, printed_set)
first_value = false
- else
- io.write(",\n")
end
- io.write(inner_indent)
+ io.write("\n", inner_indent)
end
if first_level then
- -- print metamethods
- for k, v in pairs(metatable or {}) do
- if k:startswith("__") and not (k == "__index" and type(v) == "table") then
- print_newline()
- local funcname = k:sub(3)
- dump._print_keyword(funcname)
- io.write(colors.translate("${dim} = "))
- if funcname == "tostring" or funcname == "len" then
- local ok, result = pcall(v, value, value)
- if ok then
- dump._print_scalar(result)
- io.write(" (evaluated)")
- else
- dump._print_scalar(v)
- end
- elseif v and printed_set[v] then
- dump._print_reference(printed_set[v])
- else
- dump._print_scalar(v)
- end
- end
- end
-
- -- print index methods
- for k, v in pairs(index_table or {}) do
- -- hide private interfaces
- if type(k) ~= "string" or not k:startswith("_") then
- print_newline()
- dump._print_keyword("(")
- dump._print_scalar(k, true)
- dump._print_keyword(")")
- io.write(colors.translate("${dim} = "))
- if v and printed_set[v] then
- dump._print_reference(printed_set[v])
- else
- dump._print_scalar(v)
- end
- end
- end
+ first_value = not dump._print_metatable(value, metatable, inner_indent, printed_set, true)
end
-- print array items
@@ -203,6 +250,7 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
else
dump._print_scalar(v)
end
+ io.write(",")
end
end
@@ -221,6 +269,7 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
else
dump._print_scalar(v)
end
+ io.write(",")
end
end
@@ -228,7 +277,7 @@ function dump._print_table(value, first_indent, remain_indent, printed_set)
if first_value then
io.write(colors.translate(" ${dim}}"))
else
- io.write("\n" .. remain_indent .. colors.translate("${dim}}"))
+ io.write("\b \n" .. remain_indent .. colors.translate("${dim}}"))
end
end
@@ -237,6 +286,8 @@ function dump._print(value, indent)
indent = tostring(indent or "")
if type(value) == "table" then
dump._print_table(value, indent, indent:gsub(".", " "), nil)
+ elseif type(value) == "userdata" then
+ dump._print_udata(value, indent, indent:gsub(".", " "))
else
io.write(indent)
dump._print_scalar(value)
diff --git a/xmake/core/base/hashset.lua b/xmake/core/base/hashset.lua
index 7ec5bd4a0..4e3b1975e 100644
--- a/xmake/core/base/hashset.lua
+++ b/xmake/core/base/hashset.lua
@@ -37,7 +37,7 @@ end
-- make a new hashset
function hashset.new()
- return setmetatable({ _DATA = {} }, hashset)
+ return setmetatable({ _DATA = {}, _SIZE = 0 }, hashset)
end
-- construct from list of items
@@ -45,7 +45,7 @@ function hashset.of(...)
local result = hashset.new()
local data = table.pack(...)
for i = 1, data.n do
- result._DATA[hashset._to_key(data[i])] = true
+ result:insert(data[i])
end
return result
end
@@ -65,9 +65,10 @@ end
-- insert value to hashset, returns false if value has already in the hashset
function hashset_impl:insert(value)
value = hashset._to_key(value)
- local result = self._DATA[value] or false
- if not result then
+ local result = not (self._DATA[value] or false)
+ if result then
self._DATA[value] = true
+ self._SIZE = self._SIZE + 1
end
return result
end
@@ -78,6 +79,7 @@ function hashset_impl:remove(value)
local result = self._DATA[value] or false
if result then
self._DATA[value] = nil
+ self._SIZE = self._SIZE - 1
end
return result
end
@@ -93,6 +95,22 @@ function hashset_impl:to_array()
return result
end
+-- get size of hashset
+function hashset_impl:size()
+ return self._SIZE
+end
+
+-- get data of hashset
+function hashset_impl:data()
+ return self._DATA
+end
+
+-- clear hashset
+function hashset_impl:clear()
+ self._DATA = {}
+ self._SIZE = 0
+end
+
-- return module
hashset.__index = hashset_impl
return hashset
diff --git a/xmake/core/base/io.lua b/xmake/core/base/io.lua
index 114a7a00c..e14cd32e9 100644
--- a/xmake/core/base/io.lua
+++ b/xmake/core/base/io.lua
@@ -37,16 +37,18 @@ function _file:read(fmt, opt)
return self:_read(fmt, opt.continuation)
end
+-- iterator of lines
+function _file._lines_iter(data)
+ local l = data.file:read("l", data.opt)
+ if not l and data.opt.close_on_finished then
+ data.file:close()
+ end
+ return l
+end
+
-- read all lines from a file
function _file:lines(opt)
- opt = opt or {}
- return function()
- local l = self:read("l", opt)
- if not l and opt.close_on_finished then
- self:close()
- end
- return l
- end
+ return _file._lines_iter, { file = assert(self), opt = opt or {} }
end
-- print file
@@ -223,7 +225,7 @@ function io.save(filepath, object, opt)
-- ok
return true
end
-
+
-- load object from the given file
function io.load(filepath, opt)
diff --git a/xmake/rules/cuda/gencodes/xmake.lua b/xmake/rules/cuda/gencodes/xmake.lua
index cecf83694..785b60d30 100644
--- a/xmake/rules/cuda/gencodes/xmake.lua
+++ b/xmake/rules/cuda/gencodes/xmake.lua
@@ -38,16 +38,11 @@ rule("cuda.gencodes")
import("core.platform.platform")
import("lib.detect.find_cudadevices")
-
- local function set (list)
- local result = {}
- for _, l in ipairs(list) do result[l] = true end
- return result
- end
+ import("core.base.hashset")
-- sm_20 and compute_20 is supported until CUDA 8
- local known_v_archs = set { 20, 30, 32, 35, 37, 50, 52, 53, 60, 61, 62, 70, 72, 75, }
- local known_r_archs = set { 20, 30, 32, 35, 37, 50, 52, 53, 60, 61, 62, 70, 72, 75, }
+ local known_v_archs = hashset.of(20, 30, 32, 35, 37, 50, 52, 53, 60, 61, 62, 70, 72, 75)
+ local known_r_archs = hashset.of(20, 30, 32, 35, 37, 50, 52, 53, 60, 61, 62, 70, 72, 75)
local function nf_cugencode(archs)
if type(archs) ~= 'string' then
@@ -73,8 +68,8 @@ rule("cuda.gencodes")
if arch == nil then
raise("Unknown architecture: " .. value)
end
- if not know_list[arch] then
- if arch <= table.maxn(know_list) then
+ if not know_list:has(arch) then
+ if arch <= table.maxn(know_list:data()) then
raise("Unknown architecture: " .. prefix .. "_" .. arch)
else
utils.warning("Unknown architecture: " .. prefix .. "_" .. arch)