There is a much faster way to parse long than Long.parseLong. If you want to see an example of a method that is not optimized, you should look at parseLong :)
Do you really need to consider โnumbersโ that are not ASCII?
You really need to make several calling methods that go around the radix, even hard, you probably parse base 10?
:)
Using a regular expression is not the way: itโs harder to determine if you are too long for a long time: how do you use a regular expression to determine what 9223372036854775807 can take longer, but 9223372036854775907 cannot?
However, the answer to a very fast parsing method is a finite state machine and that no matter whether you want to check whether it is being processed or parsed. Itโs just not a general finite state machine that takes a complex regular expression, but a hard-coded one.
I can write you a method that analyzes a long one and another that determines if a long analysis can be completely superior to Long.parseLong ().
Now what do you need? State testing method? In this case, the state testing method may be undesirable if you want to avoid computing twice as long.
Just wrap your call in try / catch.
And if you really want something faster than the default value of Long.parseLong, write what corresponds to your problem: base 10 if you are base 10 without checking numbers outside of ASCII (because you are probably not interested in Japanese itchi- ni-yon-go, etc.).
SyntaxT3rr0r Apr 01 '10 at 10:36
source share