Preprocess field allowed and aliases values to lowercase keys for faster value checks

This commit is contained in:
Jason Perkins 2014-03-17 15:46:35 -04:00
parent b05df0faa1
commit 708ec0e48e

View File

@ -100,6 +100,22 @@
field.paths = premake.field.property(field, "paths") field.paths = premake.field.property(field, "paths")
-- Add preprocessed, lowercase keys to the allowed and aliased value
-- lists to speed up value checking later on.
if type(field.allowed) == "table" then
for i, item in ipairs(field.allowed) do
field.allowed[item:lower()] = item
end
end
if type(field.aliases) == "table" then
local keys = table.keys(field.aliases)
for i, key in ipairs(keys) do
field.aliases[key:lower()] = field.aliases[key]
end
end
-- create a setter function for it -- create a setter function for it
_G[name] = function(value) _G[name] = function(value)
return api.callback(field, value) return api.callback(field, value)
@ -147,20 +163,19 @@
error("No such field: " .. fieldName, 2) error("No such field: " .. fieldName, 2)
end end
if not field.allowed then
field.allowed = {}
end
if type(value) == "table" then if type(value) == "table" then
field.allowed = table.join(field.allowed, value) for i, item in ipairs(value) do
api.addAllowed(fieldName, item)
end
else else
field.allowed = field.allowed or {}
table.insert(field.allowed, value) table.insert(field.allowed, value)
field.allowed[value:lower()] = value
end end
table.sort(field.allowed)
end end
-- --
-- Mark an API field as deprecated. -- Mark an API field as deprecated.
-- --
@ -367,30 +382,14 @@
local lowerValue = value:lower() local lowerValue = value:lower()
if field.aliases then if field.aliases then
for k,v in pairs(field.aliases) do canonical = field.aliases[lowerValue]
-- if I find a matching alias, assume that it has already
-- been set to the right canonical value; no further
-- checking against allowed values is needed
if lowerValue == k:lower() then
canonical = k
result = v
break
end
end
end end
if not canonical then if not canonical then
if type(field.allowed) == "function" then if type(field.allowed) == "function" then
canonical = field.allowed(value) canonical = field.allowed(value)
else else
local n = #field.allowed canonical = field.allowed[lowerValue]
for i = 1, n do
local v = field.allowed[i]
if lowerValue == v:lower() then
canonical = v
break
end
end
end end
end end
@ -410,7 +409,7 @@
end end
end end
return result or canonical return canonical
end end