git.delta.rocks / jrsonnet / refs/commits / 5f0f8de9f52f

difftreelog

Merge pull request #90 from CertainLach/parse-intrinsics

Yaroslav Bolyukin2022-11-23parents: #3824586 #8389283.patch.diff
in: master

1 file changed

modifiedcrates/jrsonnet-stdlib/src/std.jsonnetdiffbeforeafterboth
25 stringChars(str)::25 stringChars(str)::
26 std.makeArray(std.length(str), function(i) str[i]),26 std.makeArray(std.length(str), function(i) str[i]),
2727
28 local parse_nat(str, base) =
29 assert base > 0 && base <= 16 : 'integer base %d invalid' % base;
30 // These codepoints are in ascending order:
31 local zero_code = std.codepoint('0');
32 local upper_a_code = std.codepoint('A');
33 local lower_a_code = std.codepoint('a');
34 local addDigit(aggregate, char) =
35 local code = std.codepoint(char);
36 local digit = if code >= lower_a_code then
37 code - lower_a_code + 10
38 else if code >= upper_a_code then
39 code - upper_a_code + 10
40 else
41 code - zero_code;
42 assert digit >= 0 && digit < base : '%s is not a base %d integer' % [str, base];
43 base * aggregate + digit;
44 std.foldl(addDigit, std.stringChars(str), 0),
45
46 parseInt(str)::
47 assert std.isString(str) : 'Expected string, got ' + std.type(str);
48 assert std.length(str) > 0 && str != '-' : 'Not an integer: "%s"' % [str];
49 if str[0] == '-' then
50 -parse_nat(str[1:], 10)
51 else
52 parse_nat(str, 10),
53
54 parseOctal(str)::
55 assert std.isString(str) : 'Expected string, got ' + std.type(str);
56 assert std.length(str) > 0 : 'Not an octal number: ""';
57 parse_nat(str, 8),
58
59 parseHex(str)::
60 assert std.isString(str) : 'Expected string, got ' + std.type(str);
61 assert std.length(str) > 0 : 'Not hexadecimal: ""';
62 parse_nat(str, 16),
63
64 split(str, c):: std.splitLimit(str, c, -1),28 split(str, c):: std.splitLimit(str, c, -1),
6529
66 repeat(what, count)::30 repeat(what, count)::