Download
(21Kb)
Download
Updated: 01-11-07 05:26 AM
Pictures
File Info
Updated:01-11-07 05:26 AM
Created:unknown
Downloads:2,890
Favorites:2
MD5:

Toolbox 1.0

Version: r24648
by: Kodewulf [More]

Toolbox is an Ace2 library that addon authors can include in there addons to provide some utility functions.

Dependency: Ace2

Current functions:
* Trim -- Trims the whitespace from the beginning and end of a string.
* LTrim -- Trims the whitespace from the beginning of a string.
* RTrim -- Trims the whitespace from the end of a string.
* Split -- Takes a string, ie. "Item 1:Item 2:Item 3:Item 4" and creates a LUA table from it. The delimiter can be any regex pattern. Will also optionally trim the whitespace from each entry.
* Reverse -- Reverses a given string, ie. "teststring" will return "gnirtstset"
* Commify -- Adds commas to numbers, ie. 10000 will return "10,000"
* Wordify -- Returns words for a number, ie 1000 will return "one thousand"
* Clone -- Creates a copy (clone) of a table
* Unit related functions -- Returns Realm, Race, Faction, Class, Name, Level of player

Installation: Just unzip into %WOWFolder%\Interface\Addons or embed the file into your addon.

Wiki: http://www.wowace.com/wiki/Toolbox-1.0

More libraries can be found at http://www.wowace.com

Changelog
Toolbox-1.0-r24648
* Updated TOC
-- Changed interface version to 20003 (2.0.3)
* Removed Debugging functions
-- Causes conflict with AceDebug

Toolbox-1.0-r22633
* Updated Clone function
-- Should be able to copy tables with tables as keys, please test and report
* Added some Unit related functions
-- Race, Faction, Server, Realm, Class, Name, Level, etc.
* Added some basic debug functions
-- CustomDebug
-- Debug

Toolbox-1.0-r19331
* Removed 1.12 compatibility code

Toolbox-1.0-r17737
! TBC compatible
-- This version should now work in both 1.12 and TBC
-- Can someone please test it on PTR
* Updated Reverse function to be TBC compatible
* Updated TOC to make Toolbox-1.0 work in TBC

Toolbox-1.0-r17619
+ Added Clone function
-- Creates a shallow copy (clone) of a table

Toolbox-1.0-r16120
* Renamed function: NumberToWords
-- NumberToWords is now called Wordify

Toolbox-1.0-r15803
* Update TOC
-- Added some Ace2 tokens
+ New function: NumberToWords
-- Returns words for a number
-- NumberToWords(1000) returns "one thousand" as string
+ Added localization
-- AceLocale-2.2 is now a requirement.

Toolbox-1.0-r15663
+ Added LUA 5.1 string.gmatch check, courtesy Tekkub
* Update function: Commify
-- Fixed problem with decimals

Toolbox-1.0-r15616
* Update function: Commify
-- Changed some sanity checks around (thx phyber)

Toolbox-1.0-r15615
+ New function: Commify
-- Adds commas to numbers
-- Commify(10000) returns "10,000" as string
+ New function: Reverse
-- Reverse a string
-- Reverse("teststring") will return "gnirtstset"

Post A Reply Comment Options
Unread 02-12-07, 05:13 AM  
Rycochet
A Kobold Labourer
AddOn Author - Click to view AddOns

Forum posts: 0
File comments: 1
Uploads: 1
New function maybe?

Just a quick function that I feel is missing (especially coming from a php background) -

Code:
-- Split a string based on pattern into a return
-- optionally trim each element
function Toolbox:Explode(str, pat, trim)
 	return unpack(self:Split(str, pat or ",", trim))
end
Hope it makes it into the next release, and more ppl come up with ideas to add ;-)

And another one -
Code:
-- Returns the correct colors for the specified quality
-- gets them from bliz and caches the result
function Toolbox:RGB(q)
	if not self.color then
		self.color = {};
		for i=0, 6 do
			local r, g, b, hx = GetItemQualityColor(q)
			self.color[i] = {}
			self.color[i]['r'] = r
			self.color[i]['g'] = g
			self.color[i]['b'] = b
			self.color[i]['hx'] = hx
		end
	end
	if q<0 or q>6 then q = 1 end
	return self.color[q]['r'], self.color[q]['g'], self.color[q]['b'], self.color[q]['hx']
end
Robin
Last edited by Rycochet : 02-12-07 at 05:36 AM.
Report comment to moderator  
Reply With Quote
Unread 11-24-06, 06:00 AM  
disht
A Kobold Labourer
AddOn Author - Click to view AddOns

Forum posts: 0
File comments: 3
Uploads: 1
I think I am probably not making myself clear :-)

So let's try another approach: What do you think is the difference between calling:

Toolbox:Clone({ 0, 1, { 0, } }, true)

and

Toolbox:Clone({ 0, 1, { 0, } }, false)

And a second question: Will the result of:

Toolbox:Clone({ [{ 0, 1 }] = 1 }, true)

be a clone of the table passed?
Report comment to moderator  
Reply With Quote
Unread 11-22-06, 04:22 PM  
Kodewulf
Premium Member
 
Kodewulf's Avatar
Premium Member
AddOn Author - Click to view AddOns

Forum posts: 114
File comments: 104
Uploads: 18
Re: Re: Re: Clone function is buggy

Originally posted by disht
I understand, but the code you wrote doesn't create shallow copies. It always creates deep copies independently of whether you pass true or false to the Clone function. I changed the code in my last post to make two functions: one for deep copy and one for shallow one.

As for metatables and recursive datastructures, just leave it until you really need that functionality. Until then just comment that Clone doesn't handle them.

I have some code that checks for Containment and Equality of tables if you are interested in adding it.
In any other language, yes I would agree with you. In LUA I see a deep copy as not only traversing the object and copying the results, but actually creating a exact copy of the object including metatables and such. A bit confusing this. Maybe I should add a function called copy that just uses a normal "in pairs(table)", while leaving the clone function to actually recreate an exact replica of the original (metatables included). Your opinion on this?

Damn... I knew this was gonna come back and bite me...

If you don't mind sharing your code, I'd appreciate it.

Thank you for the feedback. With a bit of luck we can get this squared away soon.
__________________
I will never forget. I will never regret. I will live my life.
Report comment to moderator  
Reply With Quote
Unread 11-22-06, 09:17 AM  
disht
A Kobold Labourer
AddOn Author - Click to view AddOns

Forum posts: 0
File comments: 3
Uploads: 1
Re: Re: Clone function is buggy

Originally posted by Kodewulf
Thanks for the feedback.

I just needed something to create shallow copies of tables, that's why I stuck Clone in the Toolbox... It still needs a LOT of work. I still need to figure out how to copy metatables and how to handle recursive data structures like the one in your example. If you you wish to contribute to the code, please feel free. Any suggestions for further functionality would also be appreciated.
I understand, but the code you wrote doesn't create shallow copies. It always creates deep copies independently of whether you pass true or false to the Clone function. I changed the code in my last post to make two functions: one for deep copy and one for shallow one.

As for metatables and recursive datastructures, just leave it until you really need that functionality. Until then just comment that Clone doesn't handle them.

I have some code that checks for Containment and Equality of tables if you are interested in adding it.
Last edited by disht : 11-22-06 at 09:59 AM.
Report comment to moderator  
Reply With Quote
Unread 11-22-06, 12:17 AM  
Kodewulf
Premium Member
 
Kodewulf's Avatar
Premium Member
AddOn Author - Click to view AddOns

Forum posts: 114
File comments: 104
Uploads: 18
Re: Clone function is buggy

Originally posted by disht
deep or not deep does exactly the same thing. The function should look like this instead:

Code:
function Toolbox:Clone(source, deep)
    if (not source) then return end
    if (type(source) ~= "table") then return source end
    
    if (not deep) then deep = false end
    if (type(deep) ~= "boolean") then deep = false end

    local target = {}

    for k,v in pairs(source) do
        if (type(v) == "table" and deep) then
            target[k] = self:Clone(v, deep)
        else
            target[k] = v
        end
    end

    return target
end
You also might want to mention in the comments that this will not work for recursive datastrures. This for example won't work:

Code:
local t = { }
t[1] = t
local s = Toolbox:Clone(t, true)
Thanks for the feedback.

I just needed something to create shallow copies of tables, that's why I stuck Clone in the Toolbox... It still needs a LOT of work. I still need to figure out how to copy metatables and how to handle recursive data structures like the one in your example. If you you wish to contribute to the code, please feel free. Any suggestions for further functionality would also be appreciated.
__________________
I will never forget. I will never regret. I will live my life.
Report comment to moderator  
Reply With Quote
Unread 11-21-06, 09:23 PM  
disht
A Kobold Labourer
AddOn Author - Click to view AddOns

Forum posts: 0
File comments: 3
Uploads: 1
Clone function is buggy

deep or not deep does exactly the same thing. Better make this into two functions as follows:

Code:
function Toolbox:DeepClone(source)
    if (type(source) ~= "table") then return source end
    
    local target = {}

    for k,v in pairs(source) do
        local k2 = self:DeepClone(k, deep)
        local v2 = self:DeepClone(v, deep)
        target[k2] = v2
    end

    return target
end
and

Code:
function Toolbox:ShallowClone(source)
    if (type(source) ~= "table") then return source end
    
    local target = {}

    for k,v in pairs(source) do
        local k2 = k
        local v2 = v
        target[k2] = v2
    end

    return target
end

You also might want to mention in the comments that this will not work for recursive datastrures. This for example won't work:

Code:
local t = { }
t[1] = t
local s = Toolbox:Clone(t, true)
Last edited by disht : 11-22-06 at 09:58 AM.
Report comment to moderator  
Reply With Quote
Post A Reply



Category Jump: