@@ -71,6 +71,13 @@ my module sprintf {
71
71
make nqp :: join (' ' , @ statements );
72
72
}
73
73
74
+ sub panic ($ payload , $ directive ) {
75
+ my $ ex := nqp ::newexception();
76
+ nqp ::setmessage($ ex , " Directive $ directive not applicable for type " ~ $ payload . HOW . name ($ payload ));
77
+ nqp ::setpayload($ ex , $ payload );
78
+ nqp :: throw ($ ex );
79
+ }
80
+
74
81
sub infix_x ($ s , $ n ) {
75
82
my @ strings ;
76
83
my int $ i := 0 ;
@@ -146,7 +153,11 @@ my module sprintf {
146
153
}
147
154
148
155
method directive :sym <b >($/ ) {
149
- my $ int := intify(next_argument($/ ));
156
+ my $ next := next_argument($/ );
157
+ CATCH {
158
+ panic($ next , ' b' );
159
+ }
160
+ my $ int := intify($ next );
150
161
$ int := nqp ::base_I($ int , 2 );
151
162
my $ pre := ($ < sym > eq ' b' ?? ' 0b' !! ' 0B' ) if $ int ne ' 0' && has_flag($/ , ' hash' );
152
163
if nqp :: chars ($ < precision > ) {
@@ -159,11 +170,19 @@ my module sprintf {
159
170
make $ int ;
160
171
}
161
172
method directive :sym <c >($/ ) {
162
- make nqp :: chr (next_argument($/ ))
173
+ my $ next := next_argument($/ );
174
+ CATCH {
175
+ panic($ next , ' c' );
176
+ }
177
+ make nqp :: chr ($ next )
163
178
}
164
179
165
180
method directive :sym <d >($/ ) {
166
- my $ int := intify(next_argument($/ ));
181
+ my $ next := next_argument($/ );
182
+ CATCH {
183
+ panic($ next , ' d' );
184
+ }
185
+ my $ int := intify($ next );
167
186
my $ pad := padding_char($/ );
168
187
my $ sign := nqp ::islt_I($ int , $ zero ) ?? ' -'
169
188
!! has_flag($/ , ' plus' ) ?? ' +'
@@ -321,28 +340,44 @@ my module sprintf {
321
340
}
322
341
323
342
method directive :sym <e >($/ ) {
324
- my $ float := next_argument($/ );
343
+ my $ next := next_argument($/ );
344
+ CATCH {
345
+ panic($ next , ' e' );
346
+ }
347
+ my $ float := $ next ;
325
348
my $ precision := $ < precision > ?? $ < precision > . made !! 6 ;
326
349
my $ pad := padding_char($/ );
327
350
my $ size := $ < size > ?? $ < size > . made !! 0 ;
328
351
make scientific($ float , $ < sym > , $ precision , $ size , $ pad , $/ );
329
352
}
330
353
method directive :sym <f >($/ ) {
331
- my $ int := next_argument($/ );
354
+ my $ next := next_argument($/ );
355
+ CATCH {
356
+ panic($ next , ' f' );
357
+ }
358
+ my $ int := $ next ;
332
359
my $ precision := $ < precision > ?? $ < precision > . made !! 6 ;
333
360
my $ pad := padding_char($/ );
334
361
my $ size := $ < size > ?? $ < size > . made !! 0 ;
335
362
make fixed-point($ int , $ precision , $ size , $ pad , $/ );
336
363
}
337
364
method directive :sym <g >($/ ) {
338
- my $ float := next_argument($/ );
365
+ my $ next := next_argument($/ );
366
+ CATCH {
367
+ panic($ next , ' g' );
368
+ }
369
+ my $ float := $ next ;
339
370
my $ precision := $ < precision > ?? $ < precision > . made !! 6 ;
340
371
my $ pad := padding_char($/ );
341
372
my $ size := $ < size > ?? $ < size > . made !! 0 ;
342
373
make shortest($ float , $ < sym > eq ' G' ?? ' E' !! ' e' , $ precision , $ size , $ pad , $/ );
343
374
}
344
375
method directive :sym <o >($/ ) {
345
- my $ int := intify(next_argument($/ ));
376
+ my $ next := next_argument($/ );
377
+ CATCH {
378
+ panic($ next , ' o' );
379
+ }
380
+ my $ int := intify($ next );
346
381
$ int := nqp ::base_I($ int , 8 );
347
382
my $ pre := ' 0' if $ int ne ' 0' && has_flag($/ , ' hash' );
348
383
if nqp :: chars ($ < precision > ) {
@@ -356,7 +391,11 @@ my module sprintf {
356
391
}
357
392
358
393
method directive :sym <s >($/ ) {
359
- my $ string := next_argument($/ );
394
+ my $ next := next_argument($/ );
395
+ CATCH {
396
+ panic($ next , ' s' );
397
+ }
398
+ my $ string := $ next ;
360
399
if nqp :: chars ($ < precision > ) && nqp :: chars ($ string ) > $ < precision > . made {
361
400
$ string := nqp :: substr ($ string , 0 , $ < precision > . made );
362
401
}
@@ -365,7 +404,11 @@ my module sprintf {
365
404
# XXX: Should we emulate an upper limit, like 2**64?
366
405
# XXX: Should we emulate p5 behaviour for negative values passed to %u ?
367
406
method directive :sym <u >($/ ) {
368
- my $ int := intify(next_argument($/ ));
407
+ my $ next := next_argument($/ );
408
+ CATCH {
409
+ panic($ next , ' u' );
410
+ }
411
+ my $ int := intify($ next );
369
412
if nqp ::islt_I($ int , $ zero ) {
370
413
my $ err := nqp ::getstderr();
371
414
nqp ::printfh($ err , " negative value '"
@@ -378,7 +421,11 @@ my module sprintf {
378
421
make nqp ::tostr_I($ int )
379
422
}
380
423
method directive :sym <x >($/ ) {
381
- my $ int := intify(next_argument($/ ));
424
+ my $ next := next_argument($/ );
425
+ CATCH {
426
+ panic($ next , ' x' );
427
+ }
428
+ my $ int := intify($ next );
382
429
$ int := nqp ::base_I($ int , 16 );
383
430
my $ pre := ' 0X' if $ int ne ' 0' && has_flag($/ , ' hash' );
384
431
if nqp :: chars ($ < precision > ) {
0 commit comments