Skip to content

Commit 2c51764

Browse files
committed
Fix sprintf with type objects
Fixes RT#132846: https://rt.perl.org/Ticket/Display.html?id=132846 - For %c, use the same intify() routine we use for other ints - For floats, add a floatify routine which will go to SprintfHandler and ask it to handle floats. It will leave Numeric:Ds as is, but everything else, it'll try to .Numeric
1 parent 51bc089 commit 2c51764

File tree

1 file changed

+13
-4
lines changed

1 file changed

+13
-4
lines changed

src/HLL/sprintf.nqp

Lines changed: 13 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -108,6 +108,15 @@ my module sprintf {
108108
}
109109
}
110110

111+
sub floatify($n) {
112+
unless $n =:= NQPMu {
113+
for @handlers {
114+
return $_.float: $n if $_.mine: $n;
115+
}
116+
}
117+
$n
118+
}
119+
111120
sub intify($number_representation) {
112121
if $number_representation =:= NQPMu {
113122
return $zero; ## missing argument is handled in method TOP
@@ -201,7 +210,7 @@ my module sprintf {
201210
CATCH {
202211
bad-type-for-directive($next, 'c');
203212
}
204-
make nqp::chr($next)
213+
make nqp::chr(intify($next))
205214
}
206215

207216
method directive:sym<d>($/) {
@@ -394,7 +403,7 @@ my module sprintf {
394403
CATCH {
395404
bad-type-for-directive($next, 'e');
396405
}
397-
my $float := $next;
406+
my $float := floatify($next);
398407
my $precision := $<precision> ?? $<precision>.made !! 6;
399408
my $pad := padding_char($/);
400409
my $size := $<size> ?? $<size>.made !! 0;
@@ -405,7 +414,7 @@ my module sprintf {
405414
CATCH {
406415
bad-type-for-directive($next, 'f');
407416
}
408-
my $int := $next;
417+
my $int := floatify($next);
409418
my $precision := $<precision> ?? $<precision>.made !! 6;
410419
my $pad := padding_char($/);
411420
my $size := $<size> ?? $<size>.made !! 0;
@@ -416,7 +425,7 @@ my module sprintf {
416425
CATCH {
417426
bad-type-for-directive($next, 'g');
418427
}
419-
my $float := $next;
428+
my $float := floatify($next);
420429
my $precision := $<precision> ?? $<precision>.made !! 6;
421430
my $pad := padding_char($/);
422431
my $size := $<size> ?? $<size>.made !! 0;

0 commit comments

Comments
 (0)