[Proposal] Integer.parse/2 should handle leading whitespace

26 views
Skip to first unread message

Izzy Elwyn

unread,
Mar 8, 2021, 10:04:54 AM3/8/21
to elixir-lang-core
Hello, all!

I recently had an issue with consistency in the behavior of Integer.parse/2 where it handles trailing whitespace, but fails to handle leading whitespace. 

Example:
Integer.parse("12 ")
{12, " "}
Integer.parse(" 12")
:error

Because the trailing whitespace was handled, I had operated under the false assumption that the function would properly trim whitespace (both trailing and leading) and return a valid integer. For this reason, it look me a bit to track down leading whitespace as the root-cause of a production issue. 

I'd like to propose trimming whitespace as an enhancement to the functionality of Integer.parse/2 to improve consistency in its behavior. 

~ Izzy

eksperimental

unread,
Mar 8, 2021, 10:13:36 AM3/8/21
to elixir-l...@googlegroups.com
It is not documented,
but it does not trim your string, what is does is to parse the first
integers it finds, and returns the remainder of the string as the
second element of the tuple.

iex> Integer.parse("2ABC")
{2, "ABC"}



On Mon, 8 Mar 2021 07:04:54 -0800 (PST)
Izzy Elwyn <izzy....@gmail.com> wrote:

> Hello, all!
>
> I recently had an issue with consistency in the behavior of
> Integer.parse/2 where it handles trailing whitespace, but fails to
> handle leading whitespace.
>
> Example:
> Integer.parse("12 ")
> {12, " "}
> Integer.parse(" 12")
> :error
>
> Because the trailing whitespace was handled, I had operated under the
> false assumption that the function would properly trim whitespace
> (both trailing *and* leading) and return a valid integer. For this

José Valim

unread,
Mar 8, 2021, 10:13:40 AM3/8/21
to elixir-l...@googlegroups.com
Hi Izzy, thanks for the proposal.

The trailing whitespace is not handled really, it is returned as is along any trailing code. For example, it won't match if you expect `{12, ""}`. For this reason, I don't think we should silently remove the leading whitespace. You can call String.trim_leading if that's your preference.

--
You received this message because you are subscribed to the Google Groups "elixir-lang-core" group.
To unsubscribe from this group and stop receiving emails from it, send an email to elixir-lang-co...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/elixir-lang-core/c76ea4b9-68cd-4402-9747-c61e503f11f2n%40googlegroups.com.

eksperimental

unread,
Mar 8, 2021, 10:16:04 AM3/8/21
to elixir-l...@googlegroups.com
It is documented.
> If successful, returns a tuple in the form of `{integer,
> remainder_of_binary}`.

The spec could be improved to reflect this though

Xavier Noria

unread,
Mar 8, 2021, 10:17:11 AM3/8/21
to elixir-l...@googlegroups.com
It is not about whitespace, really.

That function parses from left to right. If the string starts with an integer in the given base, it returns it:

iex> Integer.parse("1")
{1, ""}

If some integer has been extracted and some character is invalid, the rest of the string is returned together with the integer so far parsed:

iex> Integer.parse("1a")
{1, "a"}

The first example is an edge case, if no such invalid character is found, the string is exhausted and you get an empty string.

Finally, if no integer is found at the start, :error is returned:

iex> Integer.parse("a")
:error

Whitespace is a particular case of this rule.
Reply all
Reply to author
Forward
0 new messages