Skip to content

Commit

Permalink
Merge pull request #135 from google/blow_stack
Browse files Browse the repository at this point in the history
Improve efficiency of std.format, side-effect fix #134
  • Loading branch information
sparkprime committed Mar 21, 2016
2 parents 0a1c051 + 57ac5eb commit 301f393
Show file tree
Hide file tree
Showing 7 changed files with 42 additions and 26 deletions.
15 changes: 5 additions & 10 deletions stdlib/std.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -321,25 +321,20 @@ limitations under the License.
};

// Parse a format string (containing none or more % format tags).
local parse_codes(str, i, out) =
local parse_codes(str, i, out, cur) =
if i >= std.length(str) then
out
out + [cur]
else
local c = str[i];
if c == "%" then
local r = parse_code(str, i + 1);
parse_codes(str, r.i, out+[r.code]) tailstrict
parse_codes(str, r.i, out+[cur, r.code], "") tailstrict
else
local last = out[std.length(out)-1];
local append = std.length(out) > 0 && std.type(last) == "string";
parse_codes(str, i + 1, if append then
std.makeArray(std.length(out),
function(i) if i < std.length(out)-1 then out[i] else last + c) tailstrict
else
std.makeArray(std.length(out) + 1,
function(i) if i < std.length(out) then out[i] else c)) tailstrict;
parse_codes(str, i + 1, out, cur + c) tailstrict;

local codes = parse_codes(str, 0, []);
local codes = parse_codes(str, 0, [], "");


///////////////////////
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:948:17-41 function <anonymous>
std.jsonnet:943: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:928:41-44 thunk <b>
std.jsonnet:928:33-44 function <anonymous>
std.jsonnet:928:33-44 function <aux>
std.jsonnet:931:29-44 function <anonymous>
std.jsonnet:932:21-32
std.jsonnet:923:41-44 thunk <b>
std.jsonnet:923:33-44 function <anonymous>
std.jsonnet:923:33-44 function <aux>
std.jsonnet:926:29-44 function <anonymous>
std.jsonnet:927: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:942:62-65 thunk <b>
std.jsonnet:942:54-65 function <anonymous>
std.jsonnet:942:54-65 function <aux>
std.jsonnet:945:29-44 function <anonymous>
std.jsonnet:946:21-32
std.jsonnet:937:62-65 thunk <b>
std.jsonnet:937:54-65 function <anonymous>
std.jsonnet:937:54-65 function <aux>
std.jsonnet:940:29-44 function <anonymous>
std.jsonnet:941: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:942:54-57 thunk <a>
std.jsonnet:942:54-65 function <anonymous>
std.jsonnet:942:54-65 function <anonymous>
std.jsonnet:946:21-32
std.jsonnet:937:54-57 thunk <a>
std.jsonnet:937:54-65 function <anonymous>
std.jsonnet:937:54-65 function <anonymous>
std.jsonnet:941: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:630:13-55 function <anonymous>
error.sanity.jsonnet:17:1-21
21 changes: 21 additions & 0 deletions test_suite/format.jsonnet
Original file line number Diff line number Diff line change
Expand Up @@ -251,4 +251,25 @@ std.assertEqual("%*.*f" % [10, 3, 1 / 3], "%10.3f" % [1 / 3]) &&
std.assertEqual("%(name)s[%(id)05d]-%(a)2x%(b)2x%(c)2x%(x)c" % { name: "foo", id: 3991, a: 17, b: 18, c: 17, x: 100 },
"foo[03991]-111211d") &&

local text = |||
Lorem ipsum dolor sit amet, consectetur adipiscing elit. In pellentesque felis mi, et iaculis
tellus consectetur pretium. Integer ultricies ullamcorper arcu quis bibendum. Vivamus luctus nec
nulla id egestas. Vestibulum lectus nibh, lobortis sed gravida ac, pellentesque sit amet eros.
Nulla urna purus, ornare at iaculis eget, pharetra sed libero. Fusce a neque malesuada,
hendrerit ex nec, suscipit lorem. Aenean orci quam, placerat sed mollis ut, faucibus nec turpis.
Vivamus consectetur auctor vehicula. Nam eu risus sit amet eros mattis finibus nec ac enim.
Quisque velit metus, tristique in urna in, dictum gravida elit.%(a)s
Aenean laoreet libero nunc. Cras molestie condimentum mollis. Nam quis leo sed enim vestibulum
dapibus faucibus eget elit. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Class
aptent taciti sociosqu ad litora torquent per conubia nostra, per inceptos himenaeos. Praesent
cursus magna at urna viverra, eget venenatis ante sodales. In vitae magna sed lacus iaculis
porttitor eu vel nisl. Lorem ipsum dolor sit amet, consectetur adipiscing elit. Ut vitae sapien
vel eros ultricies iaculis. Pellentesque et metus libero. Proin nec rhoncus est. Vivamus a
aliquam ipsum, ut vehicula nibh. Sed ac posuere dolor.
||| % { a: "a" };
std.assertEqual(std.length(text), 1244) &&
true

0 comments on commit 301f393

Please sign in to comment.