Модуль:Loc: различия между версиями

Нет описания правки
Нет описания правки
Строка 2: Строка 2:


local p = {}
local p = {}
-- индекс для регистронезависимого поиска
local lowerIndex = nil


local function tolower(s)
local function tolower(s)
Строка 10: Строка 7:
end
end


local function buildLowerIndex()
local complexLowerIndex = nil
     if lowerIndex then return end
 
     lowerIndex = {}
local function buildComplexIndex()
     if complexLowerIndex then return end
     complexLowerIndex = {}
     for k, v in pairs(data) do
     for k, v in pairs(data) do
         if type(k) == "string" then
         if type(v) == "table" and type(k) == "string" then
             local lk = tolower(k)
             local lk = tolower(k)
             if not lowerIndex[lk] then
             if not complexLowerIndex[lk] then
                 lowerIndex[lk] = v
                 complexLowerIndex[lk] = v
             end
             end
         end
         end
Строка 23: Строка 22:
end
end


-- Возвращает значение по ключу
local function getValueFromEntry(entry, subkey)
local function getValueByKeyRaw(value, subkey)
     if entry == nil then return nil end
     if value == nil then return nil end
     local t = type(entry)
     local t = type(value)
     if t == "string" then
     if t == "string" then
         return value
         return entry
     elseif t == "table" then
     elseif t == "table" then
         if subkey and value[subkey] then
         if subkey then
             return value[subkey]
            return entry[subkey]
        else
             return entry["_value"]
         end
         end
        return nil
     end
     end
     return nil
     return nil
end
end


-- Основной поиск
local function findTextByKeyOptimized(key, subkey)
local function findTextByKey(key, subkey)
     if not key then return nil end
     if not key then return nil end


     local direct = data[key]
     local direct = data[key]
     local result = getValueByKeyRaw(direct, subkey)
     local r = getValueFromEntry(direct, subkey)
     if result then return result end
     if r then return r end


     buildLowerIndex()
     buildComplexIndex()
     local lk = tolower(key)
     local lk = tolower(key)
     local v = lowerIndex[lk]
     local complexEntry = complexLowerIndex[lk]
     result = getValueByKeyRaw(v, subkey)
     if complexEntry then
    if result then return result end
        local rr = getValueFromEntry(complexEntry, subkey)
        if rr then return rr end
    end


     return nil
     return nil
Строка 63: Строка 63:
     end
     end


     local result = findTextByKey(key, subkey)
     local result = findTextByKeyOptimized(key, subkey)


     if not result then
     if not result then
Строка 80: Строка 80:
     end
     end


     local result = findTextByKey(key, subkey)
     local result = findTextByKeyOptimized(key, subkey)
 
     if not result then
     if not result then
         return key
         return key