Chromium Code Reviews| Index: sdk/lib/core/num.dart |
| diff --git a/sdk/lib/core/num.dart b/sdk/lib/core/num.dart |
| index 674ab2a469f22d96f265a20fdece0016e3bc52a7..d4eaf6116fa2a4bf68438c628061b95422a0582e 100644 |
| --- a/sdk/lib/core/num.dart |
| +++ b/sdk/lib/core/num.dart |
| @@ -359,4 +359,33 @@ abstract class num implements Comparable<num> { |
| * |
| */ |
| String toString(); |
| + |
| + /** |
| + * Parses a string containing a number literal into a number. |
| + * |
| + * The input string should be either one accepted by [int.parse] (with no |
|
floitsch
2013/11/25 12:50:53
won't work in dartdoc: []()
Not sure if much bett
Lasse Reichstein Nielsen
2013/11/25 13:05:16
That is better. Done.
|
| + * radix) or by [double.parse]. |
| + * If it would be accepted by [int.parse], |
| + * the result is the integer that `int.parse` would return. |
| + * Otherwise the result is the same result that `double.parse` would return. |
| + * |
| + * If the input is acceptable by neither `int.parse` nor `double.parse`, |
| + * the result is found by calling [onError] with the input. |
| + * If no [onError] is suppled, a [FormatException] is thrown. |
|
floitsch
2013/11/25 12:50:53
supplied
Lasse Reichstein Nielsen
2013/11/25 13:05:16
Replaced by your wording above.
|
| + * |
| + * For any number `n`, this function satisfies `n == num.parse(n.toString())`. |
|
floitsch
2013/11/25 12:50:53
any number `n`, not NaN, ...
or use identical(n,
Lasse Reichstein Nielsen
2013/11/25 13:05:16
Used identical. That's what the test tests too.
|
| + */ |
| + static num parse(String input, [num onError(String input)]) { |
| + String source = input.trim(); |
| + // TODO(lrn): Optimize to detect format and result type in one check. |
| + num result = int.parse(source, onError: _returnNull); |
| + if (result != null) return result; |
| + result = double.parse(source, _returnNull); |
| + if (result != null) return result; |
| + if (onError == null) throw new FormatException(input); |
| + return onError(input); |
| + } |
| + |
| + /** Helper function for [parse]. */ |
| + static _returnNull(_) => null; |
| } |