-
Notifications
You must be signed in to change notification settings - Fork 1.6k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Parsing string to integer accepting '0x' + hexadecimal #1098
Comments
I think we should expose some mechanism to parse these prefixes. I'm not convinced that An alternative to consider is an extension to |
Incidentally, we don't currently support hexadecimal float literals, but if we wanted to add support for those, that would have the same concerns that parsing prefixes on integral literals does (and may want a way to enable/disable the prefix handling when parsing strings). |
I’d prefer the "default" When this kind of thing is default/implicit, we end up with headaches like |
Hah, nice example. That's a really good point. I'm now in full agreement that it should be opt-in behavior. The simplest change here would be to follow C's lead and allow a I am still a bit partial to my "alternative" idea from before, of having a |
So,what's the decision? |
I think it would be a breaking change to do this now in existing APIs. But new APIs could be added. |
BTW with Ruby:
|
The default parser should be simple and generic. Having special cases for radix 2, 8 or 16 is terrible when we don't need it. If required, implementing another |
For future reference: use |
Why? |
Because you can use this example for example. Cheers 😃 |
Right, that misses the point of this issue. The ask was that there would be one function that will handle common prefixes. |
Ah, I see more something along the lines of parse_int::parse() |
I think this is probably what most people want:
I had a look at the |
I am the author of parse_int where you can enable the "implicit-octal" feature if needed, but it is not enabled by default. Feel free to open an issue if needed on the repo: |
In many cases, it is useful for a program that parses integers (e.g. from the command line) to accept both decimal and hexadecimal constants, by letting the user prefix the latter with
0x
. Currently,FromStr::from_str
(also the destination ofstr::parse
) only accepts base 10;from_str_radix
allows an arbitrary base to be specified, but doesn't have any special magic to parse prefixes.If I were writing
from_str
from scratch, I would suggest having it accept0x
, perhaps along with0o
and0b
like Rust source, because even if many applications don't have any particular reason to expect hexadecimal, there's very little advantage to rejecting it*. At this point, I guess that would be something of a breaking change, so a new method would be needed; still, I claim such a method belongs in the standard library for the same reason. Alternatively,from_str_radix
could accept radix 0 like some other languages' equivalents, although this seems like a hack.Other languages' behavior:
atoi
only accepts base 10.strtol
family has a mandatory base parameter; base=0 is decimal by default but allows0x
and0755
(octal); base=16 accepts0x
int()
is base 10 only by default, but has a base parameter; same behavior as C except0b
and0o
are also accepted'0x123'.to_i
is 0, butInteger('0x123')
is 291. I have no idea. The latter seems to accept the same as Python.parseInt
by default accepts0x
and, in some browsers,0
-for-octal, which ES5 says not to.(I certainly don't propose interpreting leading zeroes as octal.)
-0
and0
, and008
and8
parse to the same value.The text was updated successfully, but these errors were encountered: