From df27e347f0eab0264239b86e0a71e6a35191798b Mon Sep 17 00:00:00 2001 From: Ray Greenwell Date: Fri, 25 May 2007 21:19:31 +0000 Subject: [PATCH] Created parseInteger() as a replacement for the built-in parseInt(), which parses a String like " 7pigs" as the value 7. git-svn-id: svn+ssh://src.earth.threerings.net/narya/trunk@4723 542714f4-19e9-0310-aa3c-eee0fc999fb1 --- src/as/com/threerings/util/StringUtil.as | 54 ++++++++++++++++++++++++ 1 file changed, 54 insertions(+) diff --git a/src/as/com/threerings/util/StringUtil.as b/src/as/com/threerings/util/StringUtil.as index 96a313612..792160246 100644 --- a/src/as/com/threerings/util/StringUtil.as +++ b/src/as/com/threerings/util/StringUtil.as @@ -86,6 +86,60 @@ public class StringUtil (firstChar.toLowerCase() != firstChar); } + /** + * Parse an integer more anally than the built-in parseInt() function, + * throwing an ArgumentError if there are any invalid characters. + * + * The built-in parseInt() will ignore trailing non-integer characters. + * + * @param str The string to parse. + * @param radix The radix to use, from 2 to 16. If not specified the radix will be 10, + * unless the String begins with "0x" in which case it will be 16, + * or the String begins with "0" in which case it will be 8. + */ + public static function parseInteger (str :String, radix :uint = 0) :int + { + if (str == null) { + throw new ArgumentError("Cannot parseInt(null)"); + } + + if (radix == 0) { + if (startsWith(str, "0x")) { + str = str.substring(2); + radix = 16; + + } else if (startsWith(str, "0")) { + str = str.substring(1); + radix = 8; + + } else { + radix = 10; + } + + } else if (radix == 16 && startsWith(str, "0x")) { + str = str.substring(2); + + } else if (radix < 2 || radix > 16) { + throw new ArgumentError("Radix out of range: " + radix); + } + + // now verify that str only contains valid chars for the radix + for (var ii :int = 0; ii < str.length; ii++) { + var dex :int = HEX.indexOf(str.charAt(ii).toLowerCase()); + if (dex == -1 || dex >= radix) { + throw new ArgumentError("Invalid characters in String parseInt='" + arguments[0] + + "', radix=" + radix); + } + } + + var result :Number = parseInt(str, radix); + if (isNaN(result)) { + // this shouldn't happen.. + throw new ArgumentError("Could not parseInt=" + arguments[0]); + } + return int(result); + } + /** * Append 0 or more copies of the padChar String to the input String * until it is at least the specified length.