Skip to content

Commit

Permalink
Remove static 0 check optimization
Browse files Browse the repository at this point in the history
- it was premature
- moves all "0" checks for %x/%b/%o to runtime
  - so there's only a single code path
  - so we have immediate support for runtime size/precision setting
- removes the need for the code to create a lexical
  • Loading branch information
lizmat committed Sep 13, 2019
1 parent 04e3187 commit a5dc72d
Showing 1 changed file with 29 additions and 38 deletions.
67 changes: 29 additions & 38 deletions src/core.e/Rakudo/Internals/Sprintf.pm6
Expand Up @@ -136,9 +136,6 @@ class Rakudo::Internals::Sprintf {
"\@args.AT-POS($index)"
}

# helper sub to create a unique variable name
sub unique-variable(--> Str:D) { '$VAR' ~ $*INDEX++ }

# helper sub for processing formats for integer values
sub handle-integer-numeric($/,
Int:D :$base!,
Expand All @@ -150,13 +147,13 @@ class Rakudo::Internals::Sprintf {
# handle variable precision
if $<precision> && $<precision> eq '*' {
my str $digits = parameter($/) ~ ".Int";
my str $expr = parameter($/) ~ ".base($base)";
my str $expr = parameter($/) ~ ".Int.base($base)";
$expr = "$expr.lc" if $lc;

$expr = $hash && has_hash($/)
?? "prefix-hash-maybe-zero('$hash',pad-zeroes-int($digits,$expr))"
?? "prefix-hash('$hash',pad-zeroes-int($digits,$expr))"
!! $plus && has_plus($/)
?? "prefix-plus-maybe-zero(pad-zeroes-int($digits,$expr))"
?? "prefix-plus(pad-zeroes-int($digits,$expr))"
!! "pad-zeroes-int($digits,$expr)";

make +$<size>
Expand All @@ -168,13 +165,9 @@ class Rakudo::Internals::Sprintf {
}

# no variable precision, set up variable / value / lowercasing
my str $var = unique-variable;
my str $expr = $var ~ ".base($base)";
my str $expr = parameter($/) ~ ".Int.base($base)";
$expr = "$expr.lc" if $lc;

# the string to be returned when given 0
my str $zero = "0";

# handle any other precision
if $<precision> -> $precision {

Expand All @@ -185,28 +178,24 @@ class Rakudo::Internals::Sprintf {
!! $plus && has_plus($/)
?? "prefix-plus(pad-zeroes-int($digits,$expr))"
!! "pad-zeroes-int($digits,$expr)";
$zero = $zero x $digits;
}

# zero precision
else {
$expr = $hash && has_hash($/)
?? "prefix-hash('$hash',$expr)"
?? "prefix-hash('$hash',empty-zero($expr))"
!! $plus && has_plus($/)
?? "prefix-plus($expr)"
!! $expr;
$zero = '';
?? "prefix-plus(empty-zero($expr))"
!! "empty-zero($expr)";
}

# handle justification only if we need to
if +$<size> -> $size {
if has_minus($/) {
$expr = "str-left-justified($size,$expr)";
$zero = str-left-justified($size,$zero);
}
else {
$expr = "str-right-justified($size,$expr)";
$zero = str-right-justified($size,$zero);
}
}
}
Expand All @@ -221,17 +210,15 @@ class Rakudo::Internals::Sprintf {
!! $plus && has_plus($/)
?? "str-left-justified($size,prefix-plus($expr))"
!! "str-left-justified($size,$expr)";
$zero = str-left-justified($size,$zero);
}

# handle zero filling
elsif has_zero($/) {
$expr = $hash && has_hash($/)
?? "prefix-hash('$hash',pad-zeroes-int({$size - $hash.chars},$expr))"
?? "prefix-hash-zeroed('$hash',pad-zeroes-int($size,$expr))"
!! $plus && has_plus($/)
?? "prefix-plus(pad-zeroes-int({$size - $plus.chars},$expr))"
!! "pad-zeroes-int($size,$expr)";
$zero = pad-zeroes-int($size,$zero);
}

# handle right justification
Expand All @@ -241,7 +228,6 @@ class Rakudo::Internals::Sprintf {
!! $plus && has_plus($/)
?? "str-right-justified($size,prefix-plus($expr))"
!! "str-right-justified($size,$expr)";
$zero = str-right-justified($size,$zero);
}
}

Expand All @@ -255,7 +241,7 @@ class Rakudo::Internals::Sprintf {
$expr = "prefix-plus($expr)";
}

make "(my $var := " ~ parameter($/) ~ ".Int) ?? $expr !! '$zero'";
make $expr;
}

# show numeric value in binary
Expand Down Expand Up @@ -494,8 +480,13 @@ class Rakudo::Internals::Sprintf {
!! $value.Str
}

# RUNTIME make empty if value is zero
sub empty-zero(Str:D $string --> Str:D) {
$string eq '0' ?? '' !! $string
}

# RUNTIME prefix plus if value is not negative
sub prefix-plus-maybe-zero(Str:D $string --> Str:D) {
sub prefix-plus(Str:D $string --> Str:D) {
+$string
?? $string.starts-with("-")
?? $string
Expand All @@ -504,26 +495,26 @@ class Rakudo::Internals::Sprintf {
}

# RUNTIME prefix given hash properly, also if value negative
sub prefix-hash-maybe-zero(Str:D $hash, Str:D $string --> Str:D) {
sub prefix-hash(Str:D $hash, Str:D $string --> Str:D) {
+$string
?? $string.starts-with("-")
?? '-' ~ $hash ~ $string.substr(1)
!! $hash ~ $string
!! $string
}

# RUNTIME prefix plus if value is not negative
sub prefix-plus(Str:D $string --> Str:D) {
$string.starts-with("-")
?? $string
!! "+" ~ $string
}

# RUNTIME prefix given hash properly, also if value negative
sub prefix-hash(Str:D $hash, Str:D $string --> Str:D) {
$string.starts-with("-")
?? '-' ~ $hash ~ $string.substr(1)
!! $hash ~ $string
# RUNTIME prefix given hash properly for completely zeroed out string
# assumed to be already of the correct width
sub prefix-hash-zeroed(Str:D $hash, Str:D $string --> Str:D) {
+$string
?? $string.starts-with("-")
?? '-' ~ $hash ~ $string.substr(
$string.substr-eq('00',1) ?? 3 !! 1 + $string.substr-eq('0',1)
)
!! $hash ~ $string.substr(
$string.starts-with('00') ?? 2 !! +$string.starts-with('0')
)
!! $string
}

# RUNTIME pad with zeroes as integer
Expand Down Expand Up @@ -617,7 +608,7 @@ class Rakudo::Internals::Sprintf {
else {
$code = "$code check-no-arg(\@args);\n @parts[0]\n}";
}
#note $code;
note $code;
EVAL($code) but role is-hidden-from-backtrace {
method is-hidden-from-backtrace(--> True) { }
}
Expand Down

0 comments on commit a5dc72d

Please sign in to comment.