Skip to content

Commit

Permalink
fix format.007
Browse files Browse the repository at this point in the history
  • Loading branch information
sergot committed Mar 10, 2016
1 parent 80d4cbf commit ef1433b
Showing 1 changed file with 24 additions and 0 deletions.
24 changes: 24 additions & 0 deletions examples/format.007
Original file line number Diff line number Diff line change
Expand Up @@ -14,6 +14,29 @@ macro format(fmt, args) {
transform);
}

sub findHighestIndex(input) {
my openBracePos = input.index("{");
if openBracePos == -1 {
return -1;
}
my closeBracePos = input.suffix(openBracePos).index("}");
if closeBracePos == -1 {
return -1;
}

my index = int(input.substr(openBracePos + 1, closeBracePos - 1));

return max(index, findHighestIndex(
input.suffix(openBracePos + closeBracePos + 1)));
}

if fmt ~~ Q::Literal::Str {
my highestUsedIndex = findHighestIndex(fmt.value);
if args ~~ Q::Term::Array && args.elements.elems() <= highestUsedIndex {
args.elements[highestUsedIndex];
}
}

return quasi {
replaceAll({{{fmt}}}, "", sub transform(arg) {
return {{{args}}}[int(arg)];
Expand All @@ -23,3 +46,4 @@ macro format(fmt, args) {

say( format("{0}{1}{0}", ["abra", "cad"]) );
say( format("foo{0}bar", ["{1}"]) );
say( format("foo{1}bar", ["foo"]) );

0 comments on commit ef1433b

Please sign in to comment.