### ----------------------------------------------------
### -- Gzz::Text::Utils
### -- Licenses: LGPL-3.0-or-later
### -- Authors: Francis Grizzly Smit <grizzly@smit.id.au>
### -- File: rakulib/Gzz/Text/Utils.rakumod
### ----------------------------------------------------
### -- Chunk 4 of 8
max-width >= 0 {
                                                   $fmt ~= '.*';
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= right(sprintf($fmt, $max-width, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                               :$max-width, :$ellipsis);
                                               } else {
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                               :$max-width, :$ellipsis);
                                               }
                                           }
                                       } elsif $padding eq ' ' {
                                           if $justify eq '^' {
                                               if $width >= 0 {
                                                   if $precision >= 0 {
                                                       $fmt ~= '.*';
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   } else {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   }
                                               } else { # $width < 0 #
                                                   if $precision >= 0 {
                                                       $fmt ~= '.*';
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   } else {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   }
                                               } # $width < 0 #
                                           } else { # justify is either '-' or '' i.e. left or right #
                                               if $precision >= 0 {
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                               } else {
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                               }
                                           } # justify is either '-' or '' i.e. left or right #
                                       } elsif $padding eq '' {
                                           if $justify eq '^' {
                                               if $width >= 0 {
                                                   if $precision >= 0 {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   } else {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   }
                                               } else { # $width < 0 #
                                                   if $precision >= 0 {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $arg), $precision,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   } else {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $arg), $max-width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   }
                                               } # $width < 0 #
                                           } else { # justify is either '-' or '' i.e. left or right #
                                               $fmt ~= $justify;
                                               if $precision >= 0 {
                                                   $fmt ~= '.*';
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= centre(sprintf($fmt, $precision, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                               } else {
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= centre(sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars), :$max-width, :$ellipsis);
                                               }
                                           } # justify is either '-' or '' i.e. left or right #
                                       } else { # $padding eq something-else #
                                           if $justify eq '^' {
                                               if $width >= 0 {
                                                   if $precision >= 0 {
                                                       $fmt ~= '.*';
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   } else {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   }
                                               } else { # $width < 0 #
                                                   if $precision >= 0 {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= centre(sprintf($fmt, $arg), $precision, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                   } else {
                                                       $fmt ~= $spec-char.lc;
                                                       $result ~= sprintf($fmt, $arg);
                                                   }
                                               } # $width < 0 #
                                           } elsif $justify eq '-' {
                                               $fmt ~= $justify;
                                               if $precision >= 0 {
                                                   $fmt ~= '.*';
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= left(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                               } else {
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= left(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                               }
                                           } else { # justify is '' i.e. right #
                                               if $precision >= 0 {
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                               } else {
                                                   $fmt ~= $spec-char.lc;
                                                   $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                               }
                                           } # justify is either '-' or '' i.e. left or right #
                                       } # $padding eq something-else #
                                 } # when 'd', 'i', 'D' #
                when 'u'|'U' {
                                 $arg .=Int;
                                 BadArg.new(:msg("argument cannot be negative for char spec: $spec-char")).throw if $arg < 0;
                                 $max-width = max($max-width, $precision, 0) if $max-width > 0; #`« should not really have a both for this
                                                                                                    so munge together.
                                                                                                    Traditionally sprintf etc treat precision
                                                                                                    as max-width for Ints. »
                                 my Str:D $fmt = '%';
                                 $fmt ~= '+' if $force-sign;
                                 $fmt ~= '#' if $type-prefix;
                                 $fmt ~= 'v' if $vector;
                                 if $padding eq '0' {
                                     if $width >= 0 { # centre etc make no sense here #
                                         if $precision >= 0 {
                                             $fmt ~= $padding;
                                             $fmt ~= '*';
                                             $fmt ~= $spec-char.lc;
                                             dd $arg if $debug;
                                             $result ~= right(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                         } else {
                                             $fmt ~= $padding;
                                             $fmt ~= '*';
                                             $fmt ~= $spec-char.lc;
                                             $result ~= right(sprintf($fmt, $width, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                         }
                                     } else { # $width < 0 #
                                         if $precision >= 0 {
                                             $fmt ~= '*';
                                             $fmt ~= $spec-char.lc;
                                             $result ~= right(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                         } else {
                                             $fmt ~= $spec-char.lc;
                                             $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                         }
                                     }
                                 } elsif $padding eq ' ' {
                                     $fmt ~= '-' if $justify eq '-';
                                     $fmt ~= $padding;
                                     if $justify eq '^' {
                                         if $width >= 0 {
                                             if $precision >= 0 {
                                                 $fmt ~= '*';
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } else { # $width < 0 #
                                             if $precision >= 0 {
                                                 $fmt ~= $padding;
                                                 $fmt ~= '*';
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= right(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } # $width < 0 #
                                     } else { # justify is either '-' or '' i.e. left or right #
                                         if $precision >= 0 {
                                             $fmt ~= '*';
                                             $fmt ~= $spec-char.lc;
                                             if $justify eq '-' {
                                                 $result ~= left(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $result ~= right(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } else {
                                             $fmt ~= $spec-char.lc;
                                             if $justify eq '-' {
                                                 $result ~= left(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         }
                                     } # justify is either '-' or '' i.e. left or right #
                                 } elsif $padding eq '' { # $padding eq '' #
                                     if $justify eq '^' {
                                         if $width >= 0 {
                                             if $precision >= 0 {
                                                 $fmt ~= '*';
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $precision, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } else { # $width < 0 #
                                             if $precision >= 0 {
                                                 $fmt ~= '*';
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $precision, $arg), $precision,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $arg), $max-width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } # $width < 0 #
                                     } else { # justify is either '-' or '' i.e. left or right #
                                         if $precision >= 0 {
                                             $fmt ~= '*';
                                             $fmt ~= $spec-char.lc;
                                             if $justify eq '-' {
                                                 $result ~= left(sprintf($fmt, $precision, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $result ~= right(sprintf($fmt, $precision, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } else {
                                             $fmt ~= $spec-char.lc;
                                             if $justify eq '-' {
                                                 $result ~= left(sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $result ~= right(sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         }
                                     } # justify is either '-' or '' i.e. left or right #
                                 } else { # $padding eq something-else #
                                     if $justify eq '^' {
                                         if $width >= 0 {
                                             if $precision >= 0 {
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } else { # $width < 0 #
                                             if $precision >= 0 {
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= centre(sprintf($fmt, $arg), $max-width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $fmt ~= $spec-char.lc;
                                                 $result ~= sprintf($fmt, $arg);
                                             }
                                         } # $width < 0 #
                                     } else { # justify is either '-' or '' i.e. left or right #
                                         if $precision >= 0 {
                                             $fmt ~= $spec-char.lc;
                                             if $justify eq '-' {
                                                 $result ~= left(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } else {
                                             $fmt ~= $spec-char.lc;
                                             if $justify eq '-' {
                                                 $result ~= left(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         }
                                     } # justify is either '-' or '' i.e. left or right #
                                 }
                             } # when 'u', 'U' #
                when 'o'|'O' {
                                  $arg .=Int;
                                  $max-width = max($max-width, $precision, 0) if $max-width > 0; #`« should not really have a both for this
                                                                                                     so munge together.
                                                                                                     Traditionally sprintf etc treat precision
                                                                                                     as max-width for Ints. »
                                  my Str:D $fmt = '%';
                                  $fmt ~= '+' if $force-sign;
                                  $fmt ~= 'v' if $vector;
                                  if $padding eq '0' {
                                      if $width >= 0 { # centre etc make no sense here #
                                          if $precision >= 0 {
                                              $fmt ~= $padding;
                                              $fmt ~= '*.*';
                                              $fmt ~= $spec-char.lc;
                                              if $type-prefix {
                                                  $result ~= right('0'~ $spec-char ~ sprintf($fmt, $width - 2, $precision - 2, $arg),
                                                                          $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                              } else {
                                                  $result ~= right(sprintf($fmt, $width, $precision, $arg),
                                                                          $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                              }
                                          } else {
                                              $fmt ~= '-' if $justify eq '-';
                                              $fmt ~= $padding;
                                              $fmt ~= '*';
                                              $fmt ~= $spec-char.lc;
                                              if $type-prefix {
                                                  $result ~= right('0'~ $spec-char ~ sprintf($fmt, $width - 2, $arg),
                                                                          $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                              } else {
                                                  $result ~= right(sprintf($fmt, $width, $arg),
                                                                          $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                              }
                                          }
                                      } else { # $width < 0 #
                                          $fmt ~= '-' if $justify eq '-';
                                          $fmt ~= $padding;
                                          if $precision >= 0 {
                                              $fmt ~= '.*';
                                              $fmt ~= $spec-char.lc;
                                              if $type-prefix {
                                                  $result ~= right('0'~ $spec-char ~ sprintf($fmt, $precision - 2, $arg),
                                                                          $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                              } else {
                                                  $result ~= right(sprintf($fmt, $precision, $arg),
                                                                          $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                              }
                                          } else {
                                              $fmt ~= $spec-char.lc;
                                              $result ~= sprintf($fmt, $arg);
                                              if $type-prefix {
                                                  $result ~= right('0'~ $spec-char ~ sprintf($fmt, $arg),
                                                                          $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                              } else {
                                                  $result ~= right(sprintf($fmt, $arg),
                                                                          $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                              }
                                          }
                                      }
                                  } elsif $padding eq ' ' {
                                      $fmt ~= '0';
                                      if $justify eq '^' {
                                          if $width >= 0 {
                                              if $precision >= 0 {
                                                  $fmt ~= '.*';
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $precision - 2, $arg),
                                                                                               $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          } else { # $width < 0 #
                                              if $precision >= 0 {
                                                  $fmt ~= '.*';
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $precision - 2, $arg),
                                                                                               $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $precision, $arg),
                                                                                               $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  $fmt ~= $spec-char.lc;
                                                  $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          } # $width < 0 #
                                      } else { # justify is either '-' or '' i.e. left or right #
                                          if $precision >= 0 {
                                              $fmt ~= $spec-char.lc;
                                              if $justify eq '-' {
                                                  if $type-prefix {
                                                      $result ~= left('0' ~ $spec-char ~ sprintf($fmt, $arg),
                                                                                                        $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= left(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  if $type-prefix {
                                                      $result ~= right('0' ~ $spec-char ~ sprintf($fmt, $precision - 2, $arg),
                                                                                                         $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= right(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          } else {
                                              if $justify eq '-' {
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= left('0' ~ $spec-char ~ sprintf($fmt, $arg),
                                                                                                       $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= left(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= right('0' ~ $spec-char ~ sprintf($fmt, $arg),
                                                                                                       $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          }
                                      } # justify is either '-' or '' i.e. left or right #
                                  } elsif $padding eq '' { # $padding eq '' #
                                      if $justify eq '^' {
                                          if $width >= 0 {
                                              if $precision >= 0 {
                                                  $fmt ~= '.*';
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $precision, $arg),
                                                                                                                $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $precision, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $arg), $width);
                                                  }
                                              }
                                          } else { # $width < 0 #
                                              if $precision >= 0 {
                                                  $fmt ~= '.*';
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $precision, $arg), $max-width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $precision, $arg), $max-width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $arg), $max-width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $arg), $max-width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          } # $width < 0 #
                                      } else { # justify is either '-' or '' i.e. left or right #
                                          if $precision >= 0 {
                                              $fmt ~= '.*';
                                              $fmt ~= $spec-char.lc;
                                              if $justify eq '-' {
                                                  if $type-prefix {
                                                      $result ~= left('0' ~ $spec-char ~ sprintf($fmt, $precision - 2, $arg),
                                                                                                                   $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= left(sprintf($fmt, $precision, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  if $type-prefix {
                                                      $result ~= right('0' ~ $spec-char ~ sprintf($fmt, $precision, $arg),
                                                                                                                   $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= right(sprintf($fmt, $precision, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          } else {
                                              $fmt ~= $spec-char.lc;
                                              if $justify eq '-' {
                                                  if $type-prefix {
                                                      $result ~= left('0' ~ $spec-char ~ sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= left(sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  if $type-prefix {
                                                      $result ~= right('0' ~ $spec-char ~ sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= right(sprintf($fmt, $arg), $width,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          }
                                      } # justify is either '-' or '' i.e. left or right #
                                  } else { # $padding eq something-else #
                                      if $justify eq '^' {
                                          if $width >= 0 {
                                              if $precision >= 0 {
                                                  $fmt ~= '.*';
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $precision, $arg),
                                                                                                        $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $arg),
                                                                                                        $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          } else { # $width < 0 #
                                              if $precision >= 0 {
                                                  $fmt ~= '0';
                                                  $fmt ~= '.*';
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $precision, $arg),
                                                                                                    $max-width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $precision, $arg),
                                                                                                    $max-width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  $fmt ~= $spec-char.lc;
                                                  if $type-prefix {
                                                      $result ~= centre('0' ~ $spec-char ~ sprintf($fmt, $arg),
                                                                                                    $max-width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= centre(sprintf($fmt, $arg), $max-width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          } # $width < 0 #
                                      } else { # justify is either '-' or '' i.e. left or right #
                                          if $precision >= 0 {
                                              $fmt ~= '.*';
                                              $fmt ~= $spec-char.lc;
                                              if $justify eq '-' {
                                                  if $type-prefix {
                                                      $result ~= left('0' ~ $spec-char ~ sprintf($fmt, $precision - 2, $arg),
                                                                                                       $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= left(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  if $type-prefix {
                                                      $result ~= right('0' ~ $spec-char ~ sprintf($fmt, $precision, $arg),
                                                                                                       $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= right(sprintf($fmt, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          } else {
                                              $fmt ~= $spec-char.lc;
                                              if $justify eq '-' {
                                                  if $type-prefix {
                                                      $result ~= left('0' ~ $spec-char ~ sprintf($fmt, $arg),
                                                                                                        $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= left(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              } else {
                                                  if $type-prefix {
                                                      $result ~= right('0' ~ $spec-char ~ sprintf($fmt, $arg),
                                                                                                        $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  } else {
                                                      $result ~= right(sprintf($fmt, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                                  }
                                              }
                                          }
                                      } # justify is either '-' or '' i.e. left or right #
                                  }
                             } # when 'o', 'O' #
                when 'x'|'X' {
                                 $arg .=Int;
                                 $max-width = max($max-width, $precision, 0) if $max-width > 0; #`« should not really have a both for this
                                                                                  so munge together.
                                                                                  Traditionally sprintf etc treat precision
                                                                                  as max-width for Ints. »
                                 my Str:D $fmt = '%';
                                 $fmt ~= '+' if $force-sign;
                                 $fmt ~= 'v' if $vector;
                                 if $padding eq '0' {
                                     $fmt ~= '-' if $justify eq '-';
                                     $fmt ~= $padding;
                                     if $width >= 0 { # centre etc make no sense here #
                                         if $precision >= 0 {
                                             $fmt ~= '*.*';
                                             $fmt ~= $spec-char;
                                             if $type-prefix {
                                                 $result ~= right('0'~ $spec-char ~ sprintf($fmt, $width - 2, $precision - 2, $arg),
                                                                           $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             } else {
                                                 $result ~= right(sprintf($fmt, $width, $precision, $arg), $width, $padding,
                                                                               :number-of-chars(&internal-number-of-chars),
                                                                                                                   :$max-width, :$ellipsis);
                                             }
                                         } else {
                                             $fmt ~= '*';
                                             $fmt ~= $