Skip to content

Commit 1a90373

Browse files
authored
Merge pull request #364 from dmaestro/issue/353-sprintf-octal-padding
Issue/353 sprintf octal padding
2 parents f216702 + cd8d6ae commit 1a90373

File tree

2 files changed

+6
-1
lines changed

2 files changed

+6
-1
lines changed

src/HLL/sprintf.nqp

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -418,6 +418,7 @@ my module sprintf {
418418
my $pre := '0' if $int ne '0' && has_flag($/, 'hash');
419419
if nqp::chars($<precision>) {
420420
$int := '' if $<precision>.made == 0 && $int == 0;
421+
$pre := '' if $<precision>.made > nqp::chars($int);
421422
$int := $pre ~ infix_x('0', intify($<precision>.made) - nqp::chars($int)) ~ $int;
422423
}
423424
else {

t/hll/06-sprintf.t

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,7 +22,7 @@ sub is($actual, $expected, $description) {
2222
}
2323
}
2424

25-
plan(280);
25+
plan(284);
2626

2727
is(nqp::sprintf('Walter Bishop', []), 'Walter Bishop', 'no directives' );
2828

@@ -89,6 +89,10 @@ is(nqp::sprintf('%o', [12]), '14', 'simple %o');
8989
is(nqp::sprintf('%o', [22.01]), '26', 'decimal %o');
9090
is(nqp::sprintf('%06o', [127]), '000177', '%o with 0-padding');
9191
is(nqp::sprintf('%#6o', [127]), ' 0177', '%o with space-padding and leading 0');
92+
is(nqp::sprintf('%.5o', [83]), '00123', '%o with precision');
93+
is(nqp::sprintf('%#.5o', [83]), '00123', '%o with precision, leading 0 not required');
94+
is(nqp::sprintf('%.5o', [5349]), '12345', '%o with insufficient precision');
95+
is(nqp::sprintf('%#.5o', [5349]), '012345', '%o with precision, required leading 0 added');
9296

9397
is(nqp::sprintf('%x', [0]), '0', 'simple %x');
9498
is(nqp::sprintf('%x', [12]), 'c', 'simple %x');

0 commit comments

Comments
 (0)