-
Notifications
You must be signed in to change notification settings - Fork 0
/
Utility.lua
83 lines (73 loc) · 1.66 KB
/
Utility.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
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
local GottaGoFast = LibStub("AceAddon-3.0"):GetAddon("GottaGoFast")
local Utility = {};
function Utility.ExplodeStr(div,str)
--[[
if (div=='') then return {} end
local pos,arr = 0,{}
local count = 0;
for st,sp in function() return string.find(str,div,pos,true) end do
table.insert(arr,string.sub(str,pos,st-1))
pos = sp + 1
count = count + 1;
end
table.insert(arr,string.sub(str,pos))
return arr, count + 1;
]]--
return strsplit(div, str);
end
function Utility.TrimStr(str)
if (str == nil) then
return "";
end
return str:match( "^%s*(.-)%s*$" );
end
function Utility.DebugPrint(str)
if (GottaGoFast.db.profile.DebugMode) then
GottaGoFast:Print(str);
end
end
function Utility.ShortenStr(str, by)
if (str and by) then
return string.sub(str, 1, string.len(str) - by);
else
return str
end
end
function Utility.count_all(f)
local seen = {}
local count_table
count_table = function(t)
if seen[t] then return end
f(t)
seen[t] = true
for k,v in pairs(t) do
if type(v) == "table" then
count_table(v)
elseif type(v) == "userdata" then
f(v)
end
end
end
count_table(_G)
end
local global_type_table = nil
function Utility.type_name(o)
if global_type_table == nil then
global_type_table = {}
for k,v in pairs(_G) do
global_type_table[v] = k
end
global_type_table[0] = "table"
end
return global_type_table[getmetatable(o) or 0] or "Unknown"
end
function Utility.type_count()
local counts = {}
local enumerate = function (o)
local t = Utility.type_name(o)
counts[t] = (counts[t] or 0) + 1
end
Utility.count_all(enumerate)
return counts
end
GottaGoFast.Utility = Utility;