Skip to content

Commit a09c787

Browse files
committed
Merge branch 'sprintf' into master
Merge the sprintf branch from 2 years ago into master for better handling of %b spacing.
2 parents a02ff39 + aa86255 commit a09c787

File tree

2 files changed

+20
-11
lines changed

2 files changed

+20
-11
lines changed

src/HLL/sprintf.nqp

Lines changed: 19 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -168,26 +168,35 @@ my module sprintf {
168168

169169
method directive:sym<b>($/) {
170170
my $next := next_argument($/);
171+
my int $i := 0;
171172
CATCH {
172173
bad-type-for-directive($next, 'b');
173174
}
174175
my $int := intify($next);
175-
my $positive := has_flag($/, 'plus') && nqp::isgt_I($int, $zero) ?? 1 !! 0;
176-
$int := nqp::base_I($int, 2);
177-
my $pre := ($<sym> eq 'b' ?? '0b' !! '0B') if $int ne '0' && has_flag($/, 'hash');
176+
my $pad := padding_char($/);
177+
my $sign := nqp::islt_I($int, $zero) ?? '-'
178+
!! has_flag($/, 'plus') ?? '+'
179+
!! has_flag($/, 'space') ?? ' '
180+
!! '';
181+
$int := nqp::base_I(nqp::abs_I($int, $knowhow), 2);
182+
my $size := $<size> ?? $<size>.made !! 0;
183+
my $pre := '';
184+
$pre := ($<sym> eq 'b' ?? '0b' !! '0B') if $int ne '0' && has_flag($/, 'hash');
178185
if nqp::chars($<precision>) {
179-
$int := '' if $<precision>.made == 0 && $int == 0;
180-
$int := $pre ~ infix_x('0', $<precision>.made - nqp::chars($int)) ~ $int;
186+
$int := ($<precision>.made == 0 && $int == 0) ?? ''
187+
!! $sign ~ $pre ~ infix_x('0', $<precision>.made - nqp::chars($int)) ~ $int;
181188
}
182189
else {
183-
$int := $pre ~ $int
184-
}
185-
if $positive {
186-
$int := '+' ~ $int;
190+
if $pad ne ' ' && $size {
191+
my $chars_sign_pre := $sign ?? nqp::chars($pre) + 1 !! nqp::chars($pre);
192+
$int := $sign ~ $pre ~ infix_x($pad, $size - $chars_sign_pre - nqp::chars($int)) ~ $int;
193+
} else {
194+
$int := $sign ~ $pre ~ $int;
195+
}
187196
}
188-
189197
make $int;
190198
}
199+
191200
method directive:sym<c>($/) {
192201
my $next := next_argument($/);
193202
CATCH {

t/hll/06-sprintf.t

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -137,7 +137,7 @@ is(nqp::sprintf('%#3.3b', [0]), '000', '%b, width and precision but zero value')
137137
is(nqp::sprintf('%#3.4b', [0]), '0000', '%b, width and precision but zero value, overlong');
138138
is(nqp::sprintf('%.0b', [1]), '1', '%b, precision zero and value');
139139
is(nqp::sprintf('%+.0b', [1]), '+1', '%b, precision zero, plus sign and value');
140-
is(nqp::sprintf('% .0b', [1]), '1', '%b, precision zero, space char and value');
140+
is(nqp::sprintf('% .0b', [1]), ' 1', '%b, precision zero, space char and value');
141141
is(nqp::sprintf('%-.0b', [1]), '1', '%b, precision zero, hash and value');
142142
is(nqp::sprintf('%#.0b', [1]), '0b1', '%b, width, zero precision, no value');
143143
is(nqp::sprintf('%#3.0b', [1]), '0b1', '%b, width, zero precision but value');

0 commit comments

Comments
 (0)