@@ -168,26 +168,35 @@ my module sprintf {
168
168
169
169
method directive :sym <b >($/ ) {
170
170
my $ next := next_argument($/ );
171
+ my int $ i := 0 ;
171
172
CATCH {
172
173
bad-type-for-directive($ next , ' b' );
173
174
}
174
175
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' );
178
185
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 ;
181
188
}
182
189
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
+ }
187
196
}
188
-
189
197
make $ int ;
190
198
}
199
+
191
200
method directive :sym <c >($/ ) {
192
201
my $ next := next_argument($/ );
193
202
CATCH {
0 commit comments