Added a function to combine similar strings
Kevin Lyles [02-27-11 - 02:33]
Added a function to combine similar strings
Took the oportunity to move ww_deepTableCopy to the new utils.lua as well
diff --git a/Upgrade.lua b/Upgrade.lua
index cbf3529..3c9868d 100644
--- a/Upgrade.lua
+++ b/Upgrade.lua
@@ -1,24 +1,5 @@
local L = ww_localization
--- TODO: move this to a util(s).lua?
-function ww_deepTableCopy(object)
- local lookup_table = {}
- local function _copy(object)
- if type(object) ~= "table" then
- return object
- elseif lookup_table[object] then
- return lookup_table[object]
- end
- local new_table = {}
- lookup_table[object] = new_table
- for index, value in pairs(object) do
- new_table[_copy(index)] = _copy(value)
- end
- return setmetatable(new_table, _copy(getmetatable(object)))
- end
- return _copy(object)
-end
-
local function stringsToFuncs(strTable)
local funcTable = {}
diff --git a/WeightsWatcher.toc b/WeightsWatcher.toc
index 124599e..30183a0 100644
--- a/WeightsWatcher.toc
+++ b/WeightsWatcher.toc
@@ -10,6 +10,7 @@
## X-Compatible-With: 40000
## X-License: GPLv2
+utils.lua
WeightsWatcher.xml
Regexps.lua
diff --git a/utils.lua b/utils.lua
new file mode 100644
index 0000000..37d3746
--- /dev/null
+++ b/utils.lua
@@ -0,0 +1,72 @@
+function ww_deepTableCopy(object)
+ local lookup_table = {}
+ local function _copy(object)
+ if type(object) ~= "table" then
+ return object
+ elseif lookup_table[object] then
+ return lookup_table[object]
+ end
+ local new_table = {}
+ lookup_table[object] = new_table
+ for index, value in pairs(object) do
+ new_table[_copy(index)] = _copy(value)
+ end
+ return setmetatable(new_table, _copy(getmetatable(object)))
+ end
+ return _copy(object)
+end
+
+function ww_combineStrings(strings)
+ local startSame, endSame = "", ""
+ local stringPieces = {}
+ for i, string in ipairs(strings) do
+ local pieces = { strsplit(" ", string) }
+ for j, piece in ipairs(pieces) do
+ if not stringPieces[j] then
+ stringPieces[j] = {}
+ end
+ stringPieces[j][piece] = (stringPieces[j][piece] or 0) + 1
+ end
+ end
+ local done = false
+ for i = 1, #(stringPieces) do
+ for piece, count in pairs(stringPieces[i]) do
+ if count == #(strings) then
+ startSame = (startSame == "" and "" or startSame .. " ") .. piece
+ else
+ done = true
+ break
+ end
+ end
+ if done then
+ break
+ end
+ end
+ done = false
+ for i = #(stringPieces), 1, -1 do
+ for piece, count in pairs(stringPieces[i]) do
+ if count == #(strings) then
+ endSame = piece .. (endSame == "" and "" or " " .. endSame)
+ else
+ done = true
+ break
+ end
+ end
+ if done then
+ break
+ end
+ end
+
+ if startSame ~= "" then
+ startSame = startSame .. " "
+ end
+
+ if endSame ~= "" then
+ endSame = " " .. endSame
+ end
+
+ for i = 1, #(strings) do
+ strings[i] = strings[i]:sub(startSame:len() + 1, -endSame:len() - 1)
+ end
+ return startSame .. table.concat(strings, "/") .. endSame
+end