changeset 122:6724bc8eface

Reduced usage of global functions by defining them locally.
author Zerotorescue
date Sat, 15 Jan 2011 18:52:01 +0100
parents ca6280dc2f5b
children ee4672f21586
files Core.lua Modules/Mover.lua Modules/Scanner.lua Modules/Summary.lua
diffstat 4 files changed, 69 insertions(+), 60 deletions(-) [+]
line wrap: on
line diff
--- a/Core.lua	Sat Jan 15 17:09:13 2011 +0100
+++ b/Core.lua	Sat Jan 15 18:52:01 2011 +0100
@@ -2,6 +2,10 @@
 local addon = select(2, ...);
 addon = LibStub("AceAddon-3.0"):NewAddon(addon, "Inventorium", "AceEvent-3.0");
 
+local _G = _G;
+local sformat, ssplit, slower, strim, smatch = _G.string.format, _G.string.split, _G.string.lower, _G.string.trim, _G.string.match;
+local mfloor, print, pairs, tonumber = _G.math.floor, _G.print, _G.pairs, _G.tonumber;
+
 --@debug@
 local addonRevision = 1;
 --@end-debug@
@@ -9,9 +13,6 @@
 local addonRevision = @project-revision@;
 --@end-non-debug@]===]
 
-local _G = _G;
-local sformat, print, pairs, tonumber = _G.string.format, _G.print, _G.pairs, _G.tonumber;
-
 --  All modules must be able to retrieve our supported addons database, thus keep it a part of the addon object rather than local
 addon.supportedAddons = {};
 addon.supportedAddons.auctionPricing = {};
@@ -309,8 +310,8 @@
 local slashError = "Wrong argument, the following arguments are available:";
 
 function addon:CommandHandler(message)
-	local cmd, arg = string.split(" ", (message or ""), 2);
-	cmd = string.lower(cmd);
+	local cmd, arg = ssplit(" ", (message or ""), 2);
+	cmd = slower(cmd);
 	
 	if slashArgs[cmd] then
 		-- Pass a reference to the addon (to be used as "self") and the provided arg
@@ -369,19 +370,19 @@
 function addon:ReadableMoney(copper, clean)
 	local text = "";
 	
-	local gold = floor( copper / COPPER_PER_GOLD );
+	local gold = mfloor( copper / COPPER_PER_GOLD );
 	if gold > 0 then
 		text = sformat(goldText, text, gold);
 	end
 	
 	if not clean or (not gold or gold < 10) then
-		local silver = floor( ( copper % COPPER_PER_GOLD ) / COPPER_PER_SILVER );
+		local silver = mfloor( ( copper % COPPER_PER_GOLD ) / COPPER_PER_SILVER );
 		if silver > 0 then
 			text = sformat(silverText, text, silver);
 		end
 		
 		if not clean or (not gold or gold < 1) then
-			local copper = floor( copper % COPPER_PER_SILVER );
+			local copper = mfloor( copper % COPPER_PER_SILVER );
 			if copper > 0 or text == "" then
 				text = sformat(copperText, text, copper);
 			end
@@ -389,16 +390,16 @@
 	end
 	
 	
-	return string.trim(text);
+	return strim(text);
 end
 
 function addon:ReadableMoneyToCopper(value)
 	-- If a player enters a value it will be filled without color codes
 	-- If it is retrieved from the database, it will be colored coded
 	-- Thus we look for both
-	local gold = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+g|r") or string.match(value, "(%d+)g"));
-	local silver = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+s|r") or string.match(value, "(%d+)s"));
-	local copper = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+c|r") or string.match(value, "(%d+)c"));
+	local gold = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+g|r") or smatch(value, "(%d+)g"));
+	local silver = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+s|r") or smatch(value, "(%d+)s"));
+	local copper = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+c|r") or smatch(value, "(%d+)c"));
 		
 	return ( (gold or 0) * COPPER_PER_GOLD ) + ( (silver or 0) * COPPER_PER_SILVER ) + (copper or 0);
 end
@@ -407,9 +408,9 @@
 	-- If a player enters a value it will be filled without color codes
 	-- If it is retrieved from the database, it will be colored coded
 	-- Thus we look for both
-	local gold = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+g|r") or string.match(value, "(%d+)g"));
-	local silver = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+s|r") or string.match(value, "(%d+)s"));
-	local copper = tonumber(string.match(value, "(%d+)|c[a-fA-F0-9]+c|r") or string.match(value, "(%d+)c"));
+	local gold = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+g|r") or smatch(value, "(%d+)g"));
+	local silver = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+s|r") or smatch(value, "(%d+)s"));
+	local copper = tonumber(smatch(value, "(%d+)|c[a-fA-F0-9]+c|r") or smatch(value, "(%d+)c"));
 	
 	if not gold and not silver and not copper then
 		return "The provided amount of money is invalid. Please provide the amount of money as #g#s#c, e.g. 591617g24s43c.";
--- a/Modules/Mover.lua	Sat Jan 15 17:09:13 2011 +0100
+++ b/Modules/Mover.lua	Sat Jan 15 18:52:01 2011 +0100
@@ -1,6 +1,10 @@
 local addon = select(2, ...);
 local mod = addon:NewModule("Mover", "AceEvent-3.0", "AceTimer-3.0");
 
+local _G = _G;
+local select, pairs = _G.select, _G.pairs;
+local tinsert, twipe, treverse, tsort, tremove = _G.table.insert, _G.table.wipe, _G.table.reverse, _G.table.sort, _G.table.remove;
+
 local Scanner;
 local queuedMoves = {}; -- table storing all queued moves before BeginMove is called
 local combinedMoves = {}; -- table storing all combined moves (with source and target) that is to be processed by the actual mover in the order of the index (1 to #)
@@ -58,7 +62,7 @@
 };
 
 function mod:AddMove(itemId, amount, numMissing, numAvailable, cost)
-	table.insert(queuedMoves, {
+	tinsert(queuedMoves, {
 		["itemId"] = itemId,
 		["num"] = amount, -- can not be unlimited
 		["missing"] = numMissing,
@@ -76,11 +80,11 @@
 end
 
 function mod:ResetQueue()
-	table.wipe(queuedMoves);
+	twipe(queuedMoves);
 end
 
-if not table.reverse then
-	table.reverse = function(orig)
+if not treverse then
+	treverse = function(orig)
 		local temp = {};
 		local origLength = #orig;
 		for i = 1, origLength do
@@ -106,7 +110,7 @@
 			local bagFamily = select(2, GetContainerNumFreeSlots(bagId));
 			
 			if not itemId then
-				table.insert(emptySlots, {
+				tinsert(emptySlots, {
 					["container"] = bagId,
 					["slot"] = slotId,
 					["family"] = bagFamily,
@@ -154,7 +158,7 @@
 			addon:Print(("Can't move %s, this doesn't exist in the source."):format(IdToItemLink(singleMove.itemId)), addon.Colors.Red);
 		else
 			-- We want to move the smallest stacks first to keep stuff pretty (and minimize space usage, splitting a stack takes 2 slots, moving something only 1)
-			table.sort(sourceItem.locations, function(a, b)
+			tsort(sourceItem.locations, function(a, b)
 				-- -1 indicates unlimited, this is always more than an actual amount
 				if a.count == -1 then
 					return false;
@@ -179,7 +183,7 @@
 						while movingNum > stackSize do
 							-- Move a single stack size while the amount remaining to be moved is above the stack size num
 							
-							table.insert(outgoingMoves, {
+							tinsert(outgoingMoves, {
 								["itemId"] = singleMove.itemId,
 								["num"] = stackSize,
 								["container"] = itemLocation.container,
@@ -192,7 +196,7 @@
 					end
 				end
 				
-				table.insert(outgoingMoves, {
+				tinsert(outgoingMoves, {
 					["itemId"] = singleMove.itemId,
 					["num"] = movingNum,
 					["container"] = itemLocation.container,
@@ -212,7 +216,7 @@
 	addon:Debug("%d outgoing moves are possible.", #outgoingMoves);
 	
 	-- No longer needed
-	table.wipe(queuedMoves);
+	twipe(queuedMoves);
 	
 	
 	
@@ -264,7 +268,7 @@
 					else
 						-- Consume empty slot
 						
-						table.insert(combinedMoves, {
+						tinsert(combinedMoves, {
 							["itemId"] = outgoingMove.itemId,
 							["num"] = outgoingMove.num,
 							["sourceContainer"] = outgoingMove.container,
@@ -279,7 +283,7 @@
 						itemMove:AddLocation(firstAvailableSlot.container, firstAvailableSlot.slot, outgoingMove.num);
 						targetContents[outgoingMove.itemId] = itemMove;
 						
-						table.remove(emptySlots, 1); -- no longer empty
+						tremove(emptySlots, 1); -- no longer empty
 						
 						outgoingMove.num = 0; -- nothing remaining - sanity check
 						outgoingMove.itemId = nil; -- remove this record from the outgoingMoves-table
@@ -289,7 +293,7 @@
 					local itemStackCount = select(8, GetItemInfo(outgoingMove.itemId));
 					
 					-- We want to move to the largest stacks first to keep stuff pretty
-					table.sort(targetItem.locations, function(a, b)
+					tsort(targetItem.locations, function(a, b)
 						return a.count > b.count;
 					end);
 					
@@ -302,7 +306,7 @@
 								-- Enough room to move this entire stack
 								-- Deposit this item and then forget this outgoing move as everything in it was processed
 								
-								table.insert(combinedMoves, {
+								tinsert(combinedMoves, {
 									["itemId"] = outgoingMove.itemId,
 									["num"] = outgoingMove.num,
 									["sourceContainer"] = outgoingMove.container,
@@ -318,7 +322,7 @@
 							else
 								-- Deposit this item but don't remove the outgoing move as there are some items left to move
 								
-								table.insert(combinedMoves, {
+								tinsert(combinedMoves, {
 									["itemId"] = outgoingMove.itemId,
 									["num"] = outgoingMove.num,
 									["sourceContainer"] = outgoingMove.container,
@@ -351,7 +355,7 @@
 			-- Check if the item id is nil, this is set to nil when this outgoing move has been processed
 			if not outgoingMoves[numOutgoingMoves].itemId or outgoingMoves[numOutgoingMoves].num == 0 then
 				-- Remove this element from the array
-				table.remove(outgoingMoves, numOutgoingMoves);
+				tremove(outgoingMoves, numOutgoingMoves);
 			end
 			
 			-- Proceed with the next element (or previous considering we're going from last to first)
@@ -362,7 +366,7 @@
 		
 		backup = (backup + 1);
 		if backup > 1000 then
-			table.wipe(outgoingMoves);
+			twipe(outgoingMoves);
 			self:Abort("mover crashed", "Error preparing moves, hit an endless loop");
 			onFinish();
 			return;
@@ -370,12 +374,12 @@
 	end
 	
 	-- Reverse table, we need to go through it from last to first because we'll be removing elements, but we don't want the actions to be executed in a different order
-	combinedMoves = table.reverse(combinedMoves);
+	combinedMoves = treverse(combinedMoves);
 
 	addon:Debug("%d moves should be possible.", #combinedMoves);
 	
 	-- No longer needed
-	table.wipe(emptySlots);
+	twipe(emptySlots);
 	
 	self:ProcessMove();
 	
@@ -409,7 +413,7 @@
 		MailAddonBusy = addon:GetName();
 		
 		-- Since mailbox indexes change as mail is emptied (emptied mail is automatically deleted, thus number 50 would become 49 when 1 disappears), we must start with the last mail first
-		table.sort(combinedMoves, function(a, b)
+		tsort(combinedMoves, function(a, b)
 			return a.sourceContainer < b.sourceContainer;
 		end);
 	end
@@ -482,7 +486,7 @@
 					targetLocationsLocked[move.targetContainer][move.targetSlot] = true;
 					
 					-- This move was processed
-					table.remove(combinedMoves, numCurrentMove);
+					tremove(combinedMoves, numCurrentMove);
 				else
 					self:Abort("item disappeared from mouse", "Couldn't move " .. IdToItemLink(move.itemId) .. ", CursorHasItem() is false");
 					return;
@@ -491,7 +495,7 @@
 				-- When items are deposit automatically we still need to remember when a move has been processed
 				
 				-- This move was processed
-				table.remove(combinedMoves, numCurrentMove);
+				tremove(combinedMoves, numCurrentMove);
 			end
 		end
 		
@@ -549,7 +553,7 @@
 	self:UnregisterEvent("UI_ERROR_MESSAGE");
 	
 	-- Reset vars
-	table.wipe(combinedMoves);
+	twipe(combinedMoves);
 	movesSource = nil;
 	if MailAddonBusy == addon:GetName() then
 		MailAddonBusy = nil;
--- a/Modules/Scanner.lua	Sat Jan 15 17:09:13 2011 +0100
+++ b/Modules/Scanner.lua	Sat Jan 15 18:52:01 2011 +0100
@@ -1,6 +1,10 @@
 local addon = select(2, ...);
 local mod = addon:NewModule("Scanner", "AceEvent-3.0", "AceTimer-3.0");
 
+local _G = _G;
+local select, pairs = _G.select, _G.pairs;
+local twipe, tinsert, mceil = _G.table.wipe, _G.table.insert, _G.math.ceil;
+
 local Mover, paused, currentLocation;
 local itemCache = {};
 
@@ -21,10 +25,10 @@
 local function GetSmallCoinTextureString(coins)
 	if coins >= 10000000 then
 		-- When we have1000g, hide silver and copper
-		coins = (math.ceil(coins / 10000) * 10000);
+		coins = (mceil(coins / 10000) * 10000);
 	elseif coins >= 10000 then
 		-- When we have 1g, hide copper
-		coins = (math.ceil(coins / 100) * 100);
+		coins = (mceil(coins / 100) * 100);
 	end
 	
 	return GetCoinTextureString(coins);
@@ -169,7 +173,7 @@
 end
 
 function mod:ClearCache()
-	table.wipe(itemCache);
+	twipe(itemCache);
 end
 
 function mod:CacheLocation(location, remember)
@@ -442,7 +446,7 @@
 			local data = {};
 			
 			for i, move in pairs(moves) do
-				table.insert(data, {
+				tinsert(data, {
 					["rowData"] = move, -- this is not a key usually found in a row item and ignored by the library
 					["cols"] = columns,
 				});
--- a/Modules/Summary.lua	Sat Jan 15 17:09:13 2011 +0100
+++ b/Modules/Summary.lua	Sat Jan 15 18:52:01 2011 +0100
@@ -1,14 +1,15 @@
 local addon = select(2, ...); -- Get a reference to the main addon object
 local mod = addon:NewModule("Summary", "AceEvent-3.0", "AceTimer-3.0"); -- register a new module, Summary: resposible for building the summary window
 
+local _G = _G; -- prevent looking up of the global table
+local sformat, sgsub, supper, mceil, mfloor, tinsert, twipe, tsort = _G.string.format, _G.string.gsub, _G.string.upper, _G.math.ceil, _G.math.floor, _G.table.insert, _G.table.wipe, _G.table.sort;
+local pairs, type, select = _G.pairs, _G.type, _G.select;
+
 local unknownItemName = "Unknown (#%d)";
 
 local CACHE_ITEMS_TOTAL, CACHE_ITEMS_CURRENT, itemsCache = 0, 0, {};
 local AceGUI, cacheStart;
 
-local _G = _G; -- prevent looking up of the global table
-local sformat, sgsub, supper, tinsert, pairs, ceil, GetItemInfo = _G.string.format, _G.string.gsub, _G.string.upper, _G.table.insert, _G.pairs, _G.ceil, _G.GetItemInfo; -- prevent looking up of the most used globals all the time
-
 function mod:OnEnable()
 	-- Register the summary specific widget
 	addon:GetModule("Widgets"):InlineGroupWithButton();
@@ -85,7 +86,7 @@
 	mod.frame:AddChild(mod.scrollFrame);
 	
 	-- Reset items cache
-	table.wipe(itemsCache);
+	twipe(itemsCache);
 end
 
 function mod:CloseFrame()
@@ -121,7 +122,7 @@
 local function pairsByKeys (t, f)
 	local a = {}
 	for n in pairs(t) do tinsert(a, n) end
-		table.sort(a, f)
+		tsort(a, f)
 	local i = 0      -- iterator variable
 	local iter = function ()   -- iterator function
 		i = i + 1
@@ -147,7 +148,7 @@
 	btnRefresh:SetRelativeWidth(.2);
 	btnRefresh:SetCallback("OnClick", function()
 		-- Reset items cache
-		table.wipe(itemsCache);
+		twipe(itemsCache);
 		
 		-- Rebuild itemlist and start caching
 		mod:Build();
@@ -171,9 +172,7 @@
 	sdrSpeed:SetIsPercent(true);
 	sdrSpeed:SetRelativeWidth(.3);
 	sdrSpeed:SetCallback("OnMouseUp", function(self, event, value)
-		addon.db.profile.defaults.summary.speed = ceil( ( ( value * 100 ) / 5) - .5 );
-		
-		CACHE_ITEMS_PER_UPDATE = addon.db.profile.defaults.summary.speed; -- max = 20, min = 1
+		addon.db.profile.defaults.summary.speed = mceil( ( ( value * 100 ) / 5) - .5 );
 	end);
 	sdrSpeed:SetValue( addon.db.profile.defaults.summary.speed * 5 / 100 );
 	sdrSpeed:SetCallback("OnEnter", ShowTooltip);
@@ -222,7 +221,7 @@
 	
 	mod.scrollFrame:AddChild(btnQueueAll);
 	
-	times.init = ceil( ( GetTime() - buildStartTime ) * 1000 );
+	times.init = mceil( ( GetTime() - buildStartTime ) * 1000 );
 	addon:Debug("Time spent legend: (init / sorting / preparing / building / all).");
 	
 	local playerName = UnitName("player");
@@ -339,12 +338,12 @@
 				end
 			end
 			
-			groupTimes.init = ceil( ( GetTime() - groupStartTime ) * 1000 );
+			groupTimes.init = mceil( ( GetTime() - groupStartTime ) * 1000 );
 			
 			
 			
 			-- Sort items
-			table.sort(itemsCache[groupName], function(a, b)
+			tsort(itemsCache[groupName], function(a, b)
 				local aRarity = a.rarity or 1;
 				local bRarity = b.rarity or 1;
 				
@@ -387,7 +386,7 @@
 				end
 			end);
 			
-			groupTimes.sorting = ceil( ( GetTime() - groupStartTime ) * 1000 );
+			groupTimes.sorting = mceil( ( GetTime() - groupStartTime ) * 1000 );
 			
 			
 			
@@ -448,23 +447,23 @@
 				end
 			end
 			
-			groupTimes.preparing = ceil( ( GetTime() - groupStartTime ) * 1000 );
+			groupTimes.preparing = mceil( ( GetTime() - groupStartTime ) * 1000 );
 			
 			iGroup:ResumeLayout();
 			mod.scrollFrame:AddChild(iGroup); -- this can take up to .5 seconds, might need to look into again at a later time
 			
-			groupTimes.building = ceil( ( GetTime() - groupStartTime ) * 1000 );
+			groupTimes.building = mceil( ( GetTime() - groupStartTime ) * 1000 );
 		end
 		
 		if groupStartTime and groupTimes then
-			addon:Debug("Building of %s took %d ms (%d / %d / %d / %d / %d).", groupName, ceil( ( GetTime() - groupStartTime ) * 1000 ), groupTimes.init or 0, groupTimes.sorting or 0, groupTimes.preparing or 0, groupTimes.building or 0, ceil( ( GetTime() - buildStartTime ) * 1000 ));
+			addon:Debug("Building of %s took %d ms (%d / %d / %d / %d / %d).", groupName, mceil( ( GetTime() - groupStartTime ) * 1000 ), groupTimes.init or 0, groupTimes.sorting or 0, groupTimes.preparing or 0, groupTimes.building or 0, mceil( ( GetTime() - buildStartTime ) * 1000 ));
 		end
 	end
 	
 	mod.scrollFrame:ResumeLayout();
 	mod.scrollFrame:DoLayout();
 	
-	addon:Debug("Done building summary after %d ms.", ceil( ( GetTime() - buildStartTime ) * 1000 ));
+	addon:Debug("Done building summary after %d ms.", mceil( ( GetTime() - buildStartTime ) * 1000 ));
 	
 	if CACHE_ITEMS_TOTAL > 0 then
 		cacheStart = GetTime();
@@ -517,7 +516,7 @@
 				CACHE_ITEMS_CURRENT = CACHE_ITEMS_CURRENT + 1;
 				
 				if mod.frame then
-					mod.frame:SetStatusText(sformat("Caching auction values and item-counts... %d%% has already been processed.", floor(CACHE_ITEMS_CURRENT / CACHE_ITEMS_TOTAL * 100)));
+					mod.frame:SetStatusText(sformat("Caching auction values and item-counts... %d%% has already been processed.", mfloor(CACHE_ITEMS_CURRENT / CACHE_ITEMS_TOTAL * 100)));
 				end
 				
 				if i >= addon.db.profile.defaults.summary.speed then
@@ -538,7 +537,7 @@
 	self:Build();
 	
 	-- Announce
-	mod.frame:SetStatusText("All required prices and itemcounts have been cached. This process took " .. ceil(GetTime() - cacheStart) .. " seconds.");
+	mod.frame:SetStatusText("All required prices and itemcounts have been cached. This process took " .. mceil(GetTime() - cacheStart) .. " seconds.");
 	
 	-- Forget time
 	cacheStart = nil;
@@ -564,6 +563,7 @@
 
 function mod:NumberFormat(num)
 	local formatted = sgsub(num, "(%d)(%d%d%d)$", "%1,%2", 1);
+	local matches;
 	
 	while true do
 		formatted, matches = sgsub(formatted, "(%d)(%d%d%d),", "%1,%2,", 1);