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

added parseInt to stdlib #213

Merged
merged 1 commit into from
Jul 6, 2016
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
7 changes: 7 additions & 0 deletions doc/docs/stdlib.html
Original file line number Diff line number Diff line change
Expand Up @@ -178,6 +178,13 @@ <h4>std.format(str, vals)</h4>
<p>Example: <code>"Hello %(name)s, age %(age)d" % {age: 25, name: "Foo"}</code> yields
<code>"Hello Foo, age 25"</code>. </p>

<h4>std.parseInt(str)</h4>

<p>Parses a signed decimal integer from the input string</p>

<p>Example: <code>std.parseInt("123")</code> yields <code>123</code>.</p>

<p>Example: <code>std.parseInt("-123")</code> yields <code>-123</code>.</p>

<h4>std.escapeStringBash(str)</h4>

Expand Down
13 changes: 13 additions & 0 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,19 @@ limitations under the License.

stringChars(str)::
std.makeArray(std.length(str), function(i) str[i]),

parseInt(str)::
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;
local toDigits(str) =
[std.codepoint(char) - std.codepoint("0") for char in std.stringChars(str)];
if str[0] == "-" then
-std.foldl(addDigit, toDigits(str[1:]), 0)
else
std.foldl(addDigit, toDigits(str), 0),

split(str, c)::
if std.type(str) != "string" then
Expand Down
2 changes: 1 addition & 1 deletion test_suite/error.equality_function.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RUNTIME ERROR: Cannot test equality of functions
std.jsonnet:987:17-41 function <anonymous>
std.jsonnet:1000:17-41 function <anonymous>
error.equality_function.jsonnet:17:1-32
10 changes: 5 additions & 5 deletions test_suite/error.inside_equals_array.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RUNTIME ERROR: foobar
error.inside_equals_array.jsonnet:18:18-31 thunk <array_element>
std.jsonnet:967:41-44 thunk <b>
std.jsonnet:967:33-44 function <anonymous>
std.jsonnet:967:33-44 function <aux>
std.jsonnet:970:29-44 function <anonymous>
std.jsonnet:971:21-32
std.jsonnet:980:41-44 thunk <b>
std.jsonnet:980:33-44 function <anonymous>
std.jsonnet:980:33-44 function <aux>
std.jsonnet:983:29-44 function <anonymous>
std.jsonnet:984:21-32
10 changes: 5 additions & 5 deletions test_suite/error.inside_equals_object.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
RUNTIME ERROR: foobar
error.inside_equals_object.jsonnet:18:22-35 object <b>
std.jsonnet:981:62-65 thunk <b>
std.jsonnet:981:54-65 function <anonymous>
std.jsonnet:981:54-65 function <aux>
std.jsonnet:984:29-44 function <anonymous>
std.jsonnet:985:21-32
std.jsonnet:994:62-65 thunk <b>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:994:54-65 function <aux>
std.jsonnet:997:29-44 function <anonymous>
std.jsonnet:998:21-32
8 changes: 4 additions & 4 deletions test_suite/error.invariant.equality.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: Object assertion failed.
error.invariant.equality.jsonnet:17:10-14 thunk <object_assert>
std.jsonnet:981:54-57 thunk <a>
std.jsonnet:981:54-65 function <anonymous>
std.jsonnet:981:54-65 function <anonymous>
std.jsonnet:985:21-32
std.jsonnet:994:54-57 thunk <a>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:998:21-32
8 changes: 4 additions & 4 deletions test_suite/error.obj_assert.fail1.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: Object assertion failed.
error.obj_assert.fail1.jsonnet:20:23-28 thunk <object_assert>
std.jsonnet:981:54-57 thunk <a>
std.jsonnet:981:54-65 function <anonymous>
std.jsonnet:981:54-65 function <anonymous>
std.jsonnet:985:21-32
std.jsonnet:994:54-57 thunk <a>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:998:21-32
8 changes: 4 additions & 4 deletions test_suite/error.obj_assert.fail2.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
RUNTIME ERROR: foo was not equal to bar
error.obj_assert.fail2.jsonnet:20:32-64 thunk <object_assert>
std.jsonnet:981:54-57 thunk <a>
std.jsonnet:981:54-65 function <anonymous>
std.jsonnet:981:54-65 function <anonymous>
std.jsonnet:985:21-32
std.jsonnet:994:54-57 thunk <a>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:994:54-65 function <anonymous>
std.jsonnet:998:21-32
2 changes: 1 addition & 1 deletion test_suite/error.sanity.jsonnet.golden
Original file line number Diff line number Diff line change
@@ -1,3 +1,3 @@
RUNTIME ERROR: Assertion failed. 1 != 2
std.jsonnet:635:13-55 function <anonymous>
std.jsonnet:648:13-55 function <anonymous>
error.sanity.jsonnet:17:1-21
2 changes: 2 additions & 0 deletions test_suite/stdlib.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -372,4 +372,6 @@ std.assertEqual(std.manifestJsonEx({
|||
) &&

std.assertEqual(std.parseInt("01234567890"), 1234567890) &&
std.assertEqual(std.parseInt("-01234567890"), -1234567890) &&
true