| 1 | ## no critic: Modules::ProhibitAutomaticExportation | ||||||||||
| 2 | |||||||||||
| 3 | package Text::sprintfn; | ||||||||||
| 4 | |||||||||||
| 5 | 2 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | 2 | require Exporter; | |||||||||
| 10 | 2 | our @ISA = qw(Exporter); | |||||||||
| 11 | 2 | our @EXPORT = qw(sprintfn printfn); | |||||||||
| 12 | |||||||||||
| 13 | 2 | our $VERSION = '0.090'; # VERSION | |||||||||
| 14 | |||||||||||
| 15 | 2 | our $distance = 10; | |||||||||
| 16 | |||||||||||
| 17 | 2 | my $re1 = qr/[^)]+/s; | |||||||||
| 18 | 2 | my $re2 = qr{(?<fmt> | |||||||||
| 19 | % | ||||||||||
| 20 | (?<pi> \d+\$ | \((?<npi>$re1)\)\$?)? | ||||||||||
| 21 | (?<flags> [ +0#-]+)? | ||||||||||
| 22 | (?<vflag> \*?[v])? | ||||||||||
| 23 | (?<width> -?\d+ | | ||||||||||
| 24 | \*\d+\$? | | ||||||||||
| 25 | \((?<nwidth>$re1)\))? | ||||||||||
| 26 | (?<dot>\.?) | ||||||||||
| 27 | (?<prec> | ||||||||||
| 28 | (?: \d+ | \* | | ||||||||||
| 29 | \((?<nprec>$re1)\) ) ) ? | ||||||||||
| 30 | (?<conv> [%csduoxefgXEGbBpniDUOF]) | ||||||||||
| 31 | )}x; | ||||||||||
| 32 | 2 | our $regex = qr{($re2|%|[^%]+)}s; | |||||||||
| 33 | |||||||||||
| 34 | # faster version, without using named capture | ||||||||||
| 35 | 2 | if (1) { | |||||||||
| 36 | 2 | $regex = qr{( #all=1 | |||||||||
| 37 | ( #fmt=2 | ||||||||||
| 38 | % | ||||||||||
| 39 | (#pi=3 | ||||||||||
| 40 | \d+\$ | \( | ||||||||||
| 41 | (#npi=4 | ||||||||||
| 42 | [^)]+)\)\$?)? | ||||||||||
| 43 | (#flags=5 | ||||||||||
| 44 | [ +0#-]+)? | ||||||||||
| 45 | (#vflag=6 | ||||||||||
| 46 | \*?[v])? | ||||||||||
| 47 | (#width=7 | ||||||||||
| 48 | -?\d+ | | ||||||||||
| 49 | \*\d+\$? | | ||||||||||
| 50 | \((#nwidth=8 | ||||||||||
| 51 | [^)]+)\))? | ||||||||||
| 52 | (#dot=9 | ||||||||||
| 53 | \.?) | ||||||||||
| 54 | (#prec=10 | ||||||||||
| 55 | (?: \d+ | \* | | ||||||||||
| 56 | \((#nprec=11 | ||||||||||
| 57 | [^)]+)\) ) ) ? | ||||||||||
| 58 | (#conv=12 | ||||||||||
| 59 | [%csduoxefgXEGbBpniDUOF]) | ||||||||||
| 60 | ) | % | [^%]+ | ||||||||||
| 61 | )}xs; | ||||||||||
| 62 | } | ||||||||||
| 63 | |||||||||||
| 64 | 29 | ▶ | sub sprintfn { | ||||||||
|
Subroutine
sprintfn: called CC=8 SCAR=20.8
Pod
documented
| |||||||||||
| 65 | 29 | my ($format, @args) = @_; | |||||||||
| 66 | |||||||||||
| 67 | 29 | my $hash; | |||||||||
| 68 | 29 | ▶ | if (ref($args[0]) eq 'HASH') { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 69 | 14 | $hash = shift(@args); | |||||||||
| 70 | } | ||||||||||
| 71 | 29 | ▶ | return sprintf($format, @args) if !$hash; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 72 | |||||||||||
| 73 | 14 | my %indexes; # key = $hash key, value = index for @args | |||||||||
| 74 | 14 | push @args, (undef) x $distance; | |||||||||
| 75 | |||||||||||
| 76 | 14 | $format =~ s{$regex}{ | |||||||||
| 77 | 56 | my ($all, $fmt, $pi, $npi, $flags, | |||||||||
| 78 | $vflag, $width, $nwidth, $dot, $prec, | ||||||||||
| 79 | $nprec, $conv) = | ||||||||||
| 80 | ($1, $2, $3, $4, $5, $6, $7, $8, $9, $10, $11, $12); | ||||||||||
| 81 | |||||||||||
| 82 | 56 | my $res; | |||||||||
| 83 | 56 | ▶ | if ($fmt) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 84 | |||||||||||
| 85 | 21 | ▶ | if (defined $npi) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 86 | 14 | my $i = $indexes{$npi}; | |||||||||
| 87 | 14 | ▶ | if (!$i) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 88 | 13 | $i = @args + 1; | |||||||||
| 89 | 13 | push @args, $hash->{$npi}; | |||||||||
| 90 | 13 | $indexes{$npi} = $i; | |||||||||
| 91 | } | ||||||||||
| 92 | 14 | $pi = "${i}\$"; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | 21 | ▶ | if (defined $nwidth) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 96 | 5 | $width = $hash->{$nwidth}; | |||||||||
| 97 | } | ||||||||||
| 98 | |||||||||||
| 99 | 21 | ▶ | if (defined $nprec) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 100 | 5 | $prec = $hash->{$nprec}; | |||||||||
| 101 | } | ||||||||||
| 102 | |||||||||||
| 103 | $res = join("", | ||||||||||
| 104 | 21 | grep {defined} ( | |||||||||
| 105 | "%", | ||||||||||
| 106 | $pi, $flags, $vflag, | ||||||||||
| 107 | $width, $dot, $prec, $conv) | ||||||||||
| 108 | ); | ||||||||||
| 109 | } else { | ||||||||||
| 110 | 35 | my $i = @args + 1; | |||||||||
| 111 | 35 | push @args, $all; | |||||||||
| 112 | 35 | $res = "\%${i}\$s"; | |||||||||
| 113 | } | ||||||||||
| 114 | 56 | $res; | |||||||||
| 115 | }xego; | ||||||||||
| 116 | |||||||||||
| 117 | # DEBUG | ||||||||||
| 118 | #use Data::Dump; dd [$format, @args]; | ||||||||||
| 119 | |||||||||||
| 120 | 14 | sprintf $format, @args; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 0 | ▶ | sub printfn { | ||||||||
|
Subroutine
printfn: not called CC=1 SCAR=6.9
Pod
documented
| |||||||||||
| 124 | 0 | print sprintfn @_; | |||||||||
| 125 | } | ||||||||||
| 126 | |||||||||||
| 127 | 2 | 1; | |||||||||
| 128 | # ABSTRACT: Drop-in replacement for sprintf(), with named parameter support | ||||||||||
| 129 | |||||||||||
| 130 | __END__ | ||||||||||