rust-lang / rust-lang/rfcs

Parsing string to integer accepting '0x' + hexadecimal

Open
#1,098 15 comments 32 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

T-libs
Dominant language
Markdown
Stars
6.6k
Forks
1.7k
Avg merge
16h 14m
Merged PRs (30d)
1

Description

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 of str::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 accept 0x, perhaps along with 0o and 0b 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:

  • C atoi only accepts base 10.
  • The C strtol family has a mandatory base parameter; base=0 is decimal by default but allows 0x and 0755 (octal); base=16 accepts 0x
  • Python int() is base 10 only by default, but has a base parameter; same behavior as C except 0b and 0o are also accepted
  • Ruby: '0x123'.to_i is 0, but Integer('0x123') is 291. I have no idea. The latter seems to accept the same as Python.
  • JavaScript: parseInt by default accepts 0x and, in some browsers, 0-for-octal, which ES5 says not to.

(I certainly don't propose interpreting leading zeroes as octal.)

  • One possible reason to reject alternate forms would be to give each integer a unique input representation, but of course -0 and 0, and 008 and 8 parse to the same value.

Contributor guide

No contributing guide indexed for this repository

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the discussion around FromStr::from_str, str::parse, and from_str_radix in this issue, including the examples from other languages. Done would require a settled API design and documented behavior for hexadecimal prefixes, rather than a localized implementation or named test.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
backend-api-design
Issue type
Feature
Difficulty
5/5
Estimated time
Over a week
Activity status
Stale
Clarity
Needs clarification
Newbie friendliness
25/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.