Back to Home

ESO Lua File v101041

libraries/zo_stringsearch/zo_stringsearch.lua

[◄ back to folders ]
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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
---------------
--String Search
---------------
ZO_StringSearch = ZO_Object:Subclass()
function ZO_StringSearch:New(doCaching)
    local search = ZO_Object.New(self)
    search.data = {}
    search.processors = {}
    search.cache = doCaching or false
    return search
end
function ZO_StringSearch:AddProcessor(typeId, processingFunction)
    self.processors[typeId] = processingFunction
end
function ZO_StringSearch:Insert(data)
    if(data) then
        table.insert(self.data, data)
    end
end
function ZO_StringSearch:Remove(data)
    if(data) then
        local numData = #self.data
        for i = 1, numData do
            if(self.data[i] == data) then
                self.data[i] = self.data[numData]
                table.remove(self.data, numData)
                return
            end
        end
    end
end
function ZO_StringSearch:RemoveAll()
    self.data = {}
end
function ZO_StringSearch:ClearCache()
    local data = self.data
    local max = #data
    for i = 1, max do
        data[i].cache = nil
        data[i].cached = false
    end
end
function ZO_StringSearch:Process(data, searchTerms)
    local numTerms = #searchTerms
    for i = 1, numTerms do
        local processFunc = self.processors[data.type]
        if(not processFunc(self, data, searchTerms[i], self.cache)) then
            return false
        end
    end
    return true
end
function ZO_StringSearch:GetSearchTerms(str)
    local strLen = #str
    local lowerStr = str:lower()
    local searchTerms = {}
    for term in lowerStr:gmatch("%S+") do
        table.insert(searchTerms, term:lower())
    end
    return searchTerms
end
function ZO_StringSearch:IsMatch(str, data)
    if(not str or str == "") then return true end
    local searchTerms = self:GetSearchTerms(str)
    return self:Process(data, searchTerms)
end
function ZO_StringSearch:GetFromCache(data, cache, dataFunction, ...)
    if(cache) then
        if(not data.cached) then
            --store the result list as a table
            data.cache = { dataFunction(...) }
            data.cached = true
        end
        --return the list of results
        return unpack(data.cache)
    else
        --if we're not using caching, just call the function
        return dataFunction(...)
    end
end