public static function getColorFromString(color:String):int
{
return parseInt(new RegExp(/[0-9a-fA-F]+/).exec(color),16);
}
Refactorings
No refactoring yet !
Juha Hollanti
July 13, 2008, July 13, 2008 12:08, permalink
I can't picture getting around this faster without using RegExes.
However i might have a few options to speed things up a bit. I remember reading that RegEx literals would be faster than creating an alltogether new RegEx object. Atleast when you're using it only once like in this case. You could make the RegEx object a class variable?
My version below with the slightly altered RegEx scored an average of 57ms when put through a loop of 10 000. Your code got an average of 116ms in the same test.
public static function getColorFromString(color:String):int
{
// Convert '#' to '0x'. Everything starting with '0x' is interpreted by AS automatically as a hex number.
return parseInt( color.replace(/^#/, "0x") );
}
shaman4d.blogspot.com
July 13, 2008, July 13, 2008 19:21, permalink
Thanks, but wgat we have for exmple for string "reyyt#ff0000" ? My code return correct value.
Juha Hollanti
July 13, 2008, July 13, 2008 21:04, permalink
Ahh, ok. I misunderstood the goal a bit. Maybe we can get through by building upon your earlier code and replacing the '+' quantifier with '{6}' quantifier. Is this more on the way that you meant it?
public static function getColorFromString(color:String):int
{
return parseInt(new RegExp(/[0-9a-fA-F]{6}/).exec(color),16);
}
Nosredna
August 4, 2008, August 04, 2008 22:47, permalink
A lot depends on what you know about the string coming in. If you know the string only has one # in it, then maybe you can do something like this, which is probably fast. (Sorry, I tested in JavaScript in Firebug rather than AS, but this should work as well in AS).
It's really important to know what the possible inputs are. That'll guide your solution.
public static function getColorFromString(color:String):int
return parseInt(color.split("#")[1],16);
}
shaman4d.blogspot.com
August 4, 2008, August 04, 2008 23:42, permalink
Yeah - its a much better than RegExp - I like strings operation %)... and this work well!
Nosredna
August 4, 2008, August 04, 2008 23:52, permalink
If anyone wants to know how it works, the split() method breaks a string into an array of strings. The parameter given to split tells where to break the string. If there's only one #, the stuff to the left of the # will go into [0] and the stuff to the right will go into [1]. Note that we never even bother to name the array. It's sort of an anonymous array.
I use this to break up csv and tsv formatted spreadsheet files. Load the whole file in as a string, then break on the CRLF, then for each line break on tab or comma.
Jay
December 31, 2009, December 31, 2009 14:50, permalink
You should move the regular expression instantiation outside the function body so it runs only once.
Script was written on AS3. Translated parametr like "#ff6766". Need to get 0xff6766. I think RegExp slow for this purposes.