Skip to content

Conversation

@jdeisenberg
Copy link
Contributor

Implement String.to_integer/1 and String.to_float/1. A successful conversion returns a tuple in the form {value, remainder} where remainder is the part of the string following the valid number. An unsuccessful conversion returns :error.

Unlike Erlang's :string.to_float/1 function, String.to_float/1 will accept an integer as valid and return it as an integer value.

@meh
Copy link
Contributor

meh commented May 10, 2013

Are those unrelated commits?

@jdeisenberg
Copy link
Contributor Author

Yes, I did a pull request earlier that got rejected [the h(Module...)], and the merge when I did the latest pull. Sorry about that. Should I delete this request, do a rebase, and resubmit the request?

@meh
Copy link
Contributor

meh commented May 10, 2013

You can rebase and force push.

@jdeisenberg
Copy link
Contributor Author

Unfortunately, I have no idea what that involves in terms of git commands.(Git is a "know everything before doing anything" system.) I did a rebase on the branch, and it just said "noop" in the file that it generated.

@meh
Copy link
Contributor

meh commented May 10, 2013

Assuming master is updated to latest upstream, do the following:

git checkout master git branch -M string_to_int_float old git checkout -b string_to_int_float git cherry-pick old git push -f
@jdeisenberg
Copy link
Contributor Author

OK; thanks. I feel like I have just chanted a mystical voodoo spell; I hope the repository doesn't die a horrible death as a result :)

@meh
Copy link
Contributor

meh commented May 10, 2013

Still nope, looks like master wasn't clean, make sure it doesn't have those commits.

@jdeisenberg
Copy link
Contributor Author

In that case, I think my best course of action is to find some way to delete this request, destroy my fork of elixir, create a new one, and start all over.

@meh
Copy link
Contributor

meh commented May 10, 2013

It cannot be destroyed, only closed and a new one opened.

Assuming upstream is a remote to this repository and not your fork.

git checkout master git fetch upstream git reset --hard upstream/master git branch -D string_to_int_float git checkout -b string_to_int_float git cherry-pick old git push -f
@jdeisenberg
Copy link
Contributor Author

OK; I did that. If it didn't work, then I'll just close this request, delete my fork, and I promise to never, ever again make more trouble by issuing a pull request to this or any other project.

Given a string that starts with a valid integer, String.to_integer/1 returns a tuple {result, remainder} where remainder is the character string following the valid integer. If the input is not valid as specified by Erlang's :string.to_integer/1, the function returns :error. Given a string that starts with a valid float, String.to_float/1 returns a tuple {result, remainder} where remainder is the character string following the valid float. If the input is not valid as specified by Erlang's :string.to_float/1, the function then calls String.to_integer/1 and returns that value, which is either an {integer result, remainder} or :error. This latter behavior allows you to say String.to_float("34") without generating an error.
@meh
Copy link
Contributor

meh commented May 10, 2013

It worked :)

/me is a git wizard

@jdeisenberg
Copy link
Contributor Author

Thank you! You saved me at least the five hours it would have taken me to figure it out.

@devinus
Copy link
Contributor

devinus commented May 10, 2013

Isn't it generally against Elixir policy to simply wrap Erlang APIs? What does this give us over :string.to_(float|integer)?

@devinus
Copy link
Contributor

devinus commented May 10, 2013

One thing I don't like is that String.to_float can return an integer when you asked it to return a float. This could potentially break somebody expecting the return value to be a float.

@jdeisenberg
Copy link
Contributor Author

Devinus: I implemented the results of the discussion at #1029, where there appeared to be agreement that it might be useful.

@devinus
Copy link
Contributor

devinus commented May 10, 2013

@jdeisenberg Okay, can we at least ensure String.to_float returns a float?

Added code to make String.to_float("34") return {34.0, ""}
@josevalim
Copy link
Member

Thanks @jdeisenberg! The pull request looks good but we need tests before we can merge this.

After this is merged, we should probably break it apart into our own implementation (using pattern matching on binaries).

@devinus
Copy link
Contributor

devinus commented May 11, 2013

@jdeisenberg @josevalim Let me make an example of why String.to_float should always return floats:

float1 = String.to_float("12,0") float2 = String.to_float("12.0") float1 === float
@jdeisenberg
Copy link
Contributor Author

@josevalim: I'll write some tests tomorrow (right now while I am tired would be a really bad idea); @devinus: I've already put in that patch.

@devinus
Copy link
Contributor

devinus commented May 11, 2013

@jdeisenberg awesome

@josevalim regarding writing it our own, does :string do this as nifs?

@jdeisenberg
Copy link
Contributor Author

Tests added (see comment above).

josevalim pushed a commit that referenced this pull request May 15, 2013
String.to_integer and String.to_float
@josevalim josevalim merged commit d8e374d into elixir-lang:master May 15, 2013
@jdeisenberg
Copy link
Contributor Author

OK, I pushed that change on the current branch, but am not sure how to re-activate the pull request, unless I have to start yet another branch. In which case I am stuck, because I can't undo anything in git.

@meh
Copy link
Contributor

meh commented May 16, 2013

You have to make a new pull request after one has been merged.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

5 participants