Skip to content
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

stdlib should have a parseInt function #212

Closed
andrewmchen opened this issue Jul 5, 2016 · 10 comments
Closed

stdlib should have a parseInt function #212

andrewmchen opened this issue Jul 5, 2016 · 10 comments

Comments

@andrewmchen
Copy link
Contributor

It'd be nice to have something where I can do
stdlib.parseInt("16") == 16

I wrote this function myself here but would like to contribute it upstream.

local ASCII_0 = 48;
{
  parseInt(string)::
    local toNumber(digits) = 
      local aux(aggregate, base, digits) = 
        local length = std.length(digits);
        if length == 0 then
          aggregate
        else if digits[length-1] < 0 || digits[length-1] > 9 then
          error("parseInt got string which does not match regex [0-9]+")
        else
          aux(aggregate + base * digits[length-1], base * 10, digits[0:length-1]) tailstrict;
      aux(0, 1, digits);
    toNumber([std.codepoint(char) - ASCII_0 for char in std.stringChars(string)]),
}
@sparkprime
Copy link
Contributor

This is useful, please submit a PR!

@sparkprime
Copy link
Contributor

In fact if you do it today it'll get in the release I'm planning to do tomorrow.

@igorpeshansky
Copy link
Contributor

Would the following be simpler?

local ASCII_0 = 48;
{
  parseInt(string)::
    local addDigit(aggregate, digit) =
      if digit < 0 || digit > 9 then
        error("parseInt got string which does not match regex [0-9]+")
      else
        10 * aggregate + digit;
    std.foldl(addDigit, [std.codepoint(char) - ASCII_0 for char in std.stringChars(string)], 0),
}

@sparkprime
Copy link
Contributor

Igor has a good point

@andrewmchen
Copy link
Contributor Author

Thanks for the suggestion. I'll make this change in the PR.

As a quick aside is there an easy way to regenerate the golden validation without looking through ./run_tests.sh

@sparkprime
Copy link
Contributor

The easiest way I know is, from test_suite:

./refresh_golden.sh stdlib.jsonnet

which can take a list of files

@sparkprime
Copy link
Contributor

Well, don't do it on a test that does not have a golden file, like stdlib.jsonnet :) It might be worth writing a script to refresh all of them at once (even if only some of them need it). However I suppose this would make people sloppy and not actually checking that the changes are valid.

@igorpeshansky
Copy link
Contributor

Note that neither your variant nor mine handles negative numbers or numbers with leading whitespace...

@andrewmchen
Copy link
Contributor Author

andrewmchen commented Jul 5, 2016

I have negative numbers in the pr. I didn't consider leading whitespace though.

Is the whitespace fix necessary though? For example it looks like in Java any extra whitespace throws NumberFormatException.

@sparkprime
Copy link
Contributor

I think it's fine as it is the PR. We can always add a trim() or whatever.

In future, we might want to parse floating point, or even arbitrary JSON as well.

sbarzowski pushed a commit to sbarzowski/jsonnet that referenced this issue Jun 10, 2024
* Refactor handling of --{ext,tla}-* flags

* better helper function names for --{ext,tla}-* flags
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

3 participants