Skip to content

Commit

Permalink
Make sure that "x" creates lowercase hex characters
Browse files Browse the repository at this point in the history
  • Loading branch information
lizmat committed Apr 9, 2019
1 parent 4ae023c commit af7b833
Showing 1 changed file with 15 additions and 12 deletions.
27 changes: 15 additions & 12 deletions src/core/Rakudo/Internals/Sprintf.pm6
Expand Up @@ -124,14 +124,17 @@ class Rakudo::Internals::Sprintf {
sub has_zero($/) { $<flags>.contains("0") } sub has_zero($/) { $<flags>.contains("0") }


# helper sub for processing formats for integer values # helper sub for processing formats for integer values
sub handle-integer-numeric($/, Int:D $base, Str:D $hash) { sub handle-integer-numeric($/, Int:D $base, Str:D $hash, Bool:D $lc) {
# set up any prefixes # set up any prefixes
my str $prefix = has_plus($/) ?? "+" !! ""; my str $prefix = has_plus($/) ?? "+" !! "";
$prefix = $prefix ~ $hash if has_hash($/); $prefix = $prefix ~ $hash if has_hash($/);


# handle precision / zero filling # set up value / lowercasing
my int $size = +$<size>; my int $size = +$<size>;
my str $value = "\@args.AT-POS({$*DIRECTIVES_SEEN++}).Int.base($base)"; my str $value = "\@args.AT-POS({$*DIRECTIVES++}).Int.base($base)";
$value = "$value.lc" if $lc;

# handle precision / zero filling
if $<precision> -> $precision { if $<precision> -> $precision {
$value = "pad-zeroes-str($precision,$value)"; $value = "pad-zeroes-str($precision,$value)";
} }
Expand All @@ -154,12 +157,12 @@ class Rakudo::Internals::Sprintf {


# show numeric value in binary # show numeric value in binary
method directive:sym<b>($/) { method directive:sym<b>($/) {
handle-integer-numeric($/, 2, "0$<sym>") handle-integer-numeric($/, 2, "0b", False)
} }


# show character representation of codepoint value # show character representation of codepoint value
method directive:sym<c>($/) { method directive:sym<c>($/) {
my str $value = "\@args.AT-POS({$*DIRECTIVES_SEEN++}).chr"; my str $value = "\@args.AT-POS({$*DIRECTIVES++}).chr";


# handle justification # handle justification
if +$<size> -> $size { if +$<size> -> $size {
Expand All @@ -175,7 +178,7 @@ class Rakudo::Internals::Sprintf {
method directive:sym<d>($/) { method directive:sym<d>($/) {


# handle precision / plus prefixing # handle precision / plus prefixing
my str $value = "\@args.AT-POS({$*DIRECTIVES_SEEN++}).Int.Str"; my str $value = "\@args.AT-POS({$*DIRECTIVES++}).Int.Str";
if $<precision> -> $precision { if $<precision> -> $precision {
$value = has_plus($/) $value = has_plus($/)
?? "prefix-plus(pad-zeroes-int($precision,$value))" ?? "prefix-plus(pad-zeroes-int($precision,$value))"
Expand Down Expand Up @@ -203,17 +206,17 @@ class Rakudo::Internals::Sprintf {


# show numeric value in octal with Perl 6 roundtrippability # show numeric value in octal with Perl 6 roundtrippability
method directive:sym<o>($/) { method directive:sym<o>($/) {
handle-integer-numeric($/, 8, "0o") handle-integer-numeric($/, 8, "0o", False)
} }


# show numeric value in octal using Perl 5 semantics # show numeric value in octal using Perl 5 semantics
method directive:sym<O>($/) { method directive:sym<O>($/) {
handle-integer-numeric($/, 8, "0") handle-integer-numeric($/, 8, "0", False)
} }


# show string # show string
method directive:sym<s>($/) { method directive:sym<s>($/) {
my str $value = "\@args.AT-POS({$*DIRECTIVES_SEEN++}).Str"; my str $value = "\@args.AT-POS({$*DIRECTIVES++}).Str";


# handle precision # handle precision
if $<precision> -> $precision { if $<precision> -> $precision {
Expand All @@ -234,7 +237,7 @@ class Rakudo::Internals::Sprintf {
method directive:sym<u>($/) { method directive:sym<u>($/) {


# handle unsigned check # handle unsigned check
my str $value = "unsigned-int(\@args.AT-POS({$*DIRECTIVES_SEEN++}))"; my str $value = "unsigned-int(\@args.AT-POS({$*DIRECTIVES++}))";
# handle zero padding / left / right justification # handle zero padding / left / right justification
if +$<size> -> $size { if +$<size> -> $size {
if has_minus($/) { if has_minus($/) {
Expand All @@ -253,7 +256,7 @@ class Rakudo::Internals::Sprintf {


# show numeric value in octal # show numeric value in octal
method directive:sym<x>($/) { method directive:sym<x>($/) {
handle-integer-numeric($/, 16, "0$<sym>") handle-integer-numeric($/, 16, "0x", ~$<sym> eq 'x')
} }
} }


Expand Down Expand Up @@ -374,7 +377,7 @@ class Rakudo::Internals::Sprintf {


# Create callable for given uncached format string # Create callable for given uncached format string
sub create-format($format --> Callable:D) { sub create-format($format --> Callable:D) {
my $*DIRECTIVES_SEEN = 0; my $*DIRECTIVES = 0;
if Syntax.parse($format, actions => Actions) -> $parsed { if Syntax.parse($format, actions => Actions) -> $parsed {
my @directives; my @directives;
my @parts = $parsed<statement>.map: { my @parts = $parsed<statement>.map: {
Expand Down

0 comments on commit af7b833

Please sign in to comment.