-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathutil.lua
66 lines (57 loc) · 2.02 KB
/
util.lua
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
--[[ Copyright (C) 2015 Roberto Azevedo
This file is part of ncls3d.
ncls3d is free software: you can redistribute it and/or modify
it under the terms of the GNU General Public License as published by
the Free Software Foundation, either version 3 of the License, or
(at your option) any later version.
ncls3d is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License for more details.
You should have received a copy of the GNU General Public License
along with ncls3d. If not, see <http://www.gnu.org/licenses/>.
--]]
-- This file contains some utility functions for ncls3d.
-- Copy a table (not recursive version)
-- @param orig the table to be copied.
-- @return a copy of orig table
function table.shallowcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in pairs(orig) do
copy[orig_key] = orig_value
end
else -- number, string, boolean, etc
copy = orig
end
return copy
end
-- Copy a table (recursive version)
-- @param orig the table to be copied.
-- @return a copy of orig table
function table.deepcopy(orig)
local orig_type = type(orig)
local copy
if orig_type == 'table' then
copy = {}
for orig_key, orig_value in next, orig, nil do
copy[table.deepcopy(orig_key)] = table.deepcopy(orig_value)
end
setmetatable(copy, table.deepcopy(getmetatable(orig)))
else -- number, string, boolean, etc
copy = orig
end
return copy
end
-- Transform a percent value to the equivalent double
-- (e.g. return 0.1 for "100%")
-- @param percent
function double_from_percent (percent)
assert (type (percent) == "string", percent .. "is not a string!")
assert (percent:sub(#percent, #percent) == "%", "Error getting percent " ..
percent)
local v = percent:sub(1, #percent - 1)
return v / 100.0;
end