From 12a094ad5d0212bb5712b071d4189b5d48f83d51 Mon Sep 17 00:00:00 2001 From: Elizabeth Mattijsen Date: Tue, 9 Apr 2019 18:53:00 +0200 Subject: [PATCH] Streamline flag handling Since we don't enforce any order in the flags, it feels like we can simplify flag checking significantly by just storing the flags in a single Match, and then just use .contains to see whether a flag is set. --- src/core/Rakudo/Internals/Sprintf.pm6 | 40 +++++++++++---------------- 1 file changed, 16 insertions(+), 24 deletions(-) diff --git a/src/core/Rakudo/Internals/Sprintf.pm6 b/src/core/Rakudo/Internals/Sprintf.pm6 index 91f6724912b..f2fe74d8773 100644 --- a/src/core/Rakudo/Internals/Sprintf.pm6 +++ b/src/core/Rakudo/Internals/Sprintf.pm6 @@ -94,13 +94,7 @@ class Rakudo::Internals::Sprintf { $=[\d+] '$' } - token flags { - | $ = ' ' - | $ = '+' - | $ = '-' - | $ = '0' - | $ = '#' - } + token flags { <[ +0#-]> } token size { \d* | $='*' @@ -121,27 +115,25 @@ class Rakudo::Internals::Sprintf { method literal($/) { make $/.Str.perl } # helper sub to check if a flag is set - sub has_flag($/, $key) { - with $ -> $flags { - return True if .AT-KEY($key) for $flags.list; - } - False - } + sub has_hash($/) { $.contains("#") } + sub has_minus($/) { $.contains("-") } + sub has_plus($/) { $.contains("+") } + sub has_zero($/) { $.contains("0") } # show numeric value in binary method directive:sym($/) { my int $size = +$; # set up any prefixes - my str $prefix = has_flag($/, 'plus') ?? "+" !! ""; - $prefix = $prefix ~ "0$" if has_flag($/, 'hash'); + my str $prefix = has_plus($/) ?? "+" !! ""; + $prefix = $prefix ~ "0$" if has_hash($/); # handle precision / zero filling my str $value = "\@args.AT-POS({$*DIRECTIVES_SEEN++}).Int.base(2)"; if $ -> $precision { $value = "pad-zeroes-str($precision,$value)"; } - elsif has_flag($/, 'zero') { + elsif has_zero($/) { $value = "pad-zeroes-str({$size - $prefix.chars},$value)"; } @@ -149,7 +141,7 @@ class Rakudo::Internals::Sprintf { $value = "'$prefix' ~ $value" if $prefix; # handle left/right justification - if has_flag($/, 'minus') { + if has_minus($/) { $value = "str-left-justified($size,$value)"; } elsif $size { @@ -180,19 +172,19 @@ class Rakudo::Internals::Sprintf { # handle precision / zero filling my str $value = "\@args.AT-POS({$*DIRECTIVES_SEEN++}).Int.Str"; if $ -> $precision { - $value = has_flag($/, 'plus') + $value = has_plus($/) ?? "prefix-plus(pad-zeroes-int($precision,$value))" !! "pad-zeroes-int($precision,$value)"; } - elsif has_flag($/, 'plus') { + elsif has_plus($/) { $value = "prefix-plus($value)"; } - elsif has_flag($/, 'zero') { + elsif has_zero($/) { $value = "pad-zeroes-int($size,$value)"; } # handle left/right justification - if has_flag($/, 'minus') { + if has_minus($/) { $value = "str-left-justified($size,$value)"; } elsif $size { @@ -213,7 +205,7 @@ class Rakudo::Internals::Sprintf { } # handle left/right justification - if has_flag($/, 'minus') { + if has_minus($/) { $value = "str-left-justified($size,$value)"; } elsif $size { @@ -231,10 +223,10 @@ class Rakudo::Internals::Sprintf { # handle zero padding / left / right justification my int $size = +$; if $size { - if has_flag($/, 'zero') { + if has_zero($/) { $value = "pad-zeroes-int($size,$value)"; } - elsif has_flag($/, 'minus') { + elsif has_minus($/) { $value = "str-left-justified($size,$value)"; } else {