fixnum icon indicating copy to clipboard operation
fixnum copied to clipboard

fixnum: standardize parse methods.

Open DartBot opened this issue 9 years ago • 3 comments

<img src="https://avatars.githubusercontent.com/u/2108821?v=3" align="left" width="96" height="96"hspace="10"> Issue by rakudrama Originally opened as dart-lang/sdk#21915


Int64.parseInt, Int64.parseRadix and Int64.parseHex should be marked as deprecated and replaced with a single Int64.parse method modeled after int.parse

   static Int64 parse(String source {int radix, Int64 onError(String source)});

This method would also handle the 0x, +0x, -0x prefixes when a radix is not specified. None of the current methods can parse these.

It might be desirable to add a way to detect overflow, perhaps another named optional argument

  Int64 onOverflow(String source, Int64 value, bool is64BitUnsigned)

value is the truncated value, is64BitUnsigned is true if the value overflowed but only set the sign bit.

DartBot avatar Jun 05 '15 23:06 DartBot

Comment by lrhn

I'm not sure I'd recommend that.

I would even consider adding parseRadix, parseHex and parseDecimal to int. It's an annoying overhead on int.parse that it has to guess the format before parsing, and try trimming the string for leading/trailing whitespace even if there isn't any. If it had more specialized parsers without that overhead, I think it could be even faster than it is today. I'd also consider toStringAsRadix on int, instead of having toString take a radix argument, but it's too late to remove the argument now.

I'm all for adding an Int64.parse for convenience, but do consider keeping the specialized versions.

(Nor sure whether to accept a leading +, even int.parse doesn't do that. But then, the stated goal of int.parse is to parse the output of any int.toString() and any valid Dart integer literal. Choose your own goal here.)

DartBot avatar Jun 05 '15 23:06 DartBot

<img src="https://avatars.githubusercontent.com/u/2108821?v=3" align="left" width="48" height="48"hspace="10"> Comment by rakudrama


I'm surprised that int.parse accepts surrounding whitespace. The documentation does not describe that at all.

"I'd also consider toStringAsRadix on int, instead of having toString take a radix argument, but it's too late to remove the argument now." That is JavaScript. Dart's int interface has 'toRadixString(int radix)'.

DartBot avatar Jun 05 '15 23:06 DartBot