| 1 | 2 | ▶ | use strict; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 2 | 2 | ▶ | use warnings; | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 3 | package String::RewritePrefix 0.009; | ||||||||||||
| 4 | |||||||||||||
| 5 | 2 | ▶ | use Carp (); | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 6 | # ABSTRACT: rewrite strings based on a set of known prefixes | ||||||||||||
| 7 | |||||||||||||
| 8 | # 0.972 allows \'method_name' form -- rjbs, 2010-10-25 | ||||||||||||
| 9 | 2 | ▶ | use Sub::Exporter 0.972 -setup => { | ||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||
| 10 | exports => [ rewrite => \'_new_rewriter' ], | ||||||||||||
| 11 | 2 | }; | |||||||||||
| 12 | |||||||||||||
| 13 | #pod =head1 SYNOPSIS | ||||||||||||
| 14 | #pod | ||||||||||||
| 15 | #pod use String::RewritePrefix; | ||||||||||||
| 16 | #pod my @to_load = String::RewritePrefix->rewrite( | ||||||||||||
| 17 | #pod { '' => 'MyApp::', '+' => '' }, | ||||||||||||
| 18 | #pod qw(Plugin Mixin Addon +Corporate::Thinger), | ||||||||||||
| 19 | #pod ); | ||||||||||||
| 20 | #pod | ||||||||||||
| 21 | #pod # now you have: | ||||||||||||
| 22 | #pod qw(MyApp::Plugin MyApp::Mixin MyApp::Addon Corporate::Thinger) | ||||||||||||
| 23 | #pod | ||||||||||||
| 24 | #pod You can also import a rewrite routine: | ||||||||||||
| 25 | #pod | ||||||||||||
| 26 | #pod use String::RewritePrefix rewrite => { | ||||||||||||
| 27 | #pod -as => 'rewrite_dt_prefix', | ||||||||||||
| 28 | #pod prefixes => { '' => 'MyApp::', '+' => '' }, | ||||||||||||
| 29 | #pod }; | ||||||||||||
| 30 | #pod | ||||||||||||
| 31 | #pod my @to_load = rewrite_dt_prefix( qw(Plugin Mixin Addon +Corporate::Thinger)); | ||||||||||||
| 32 | #pod | ||||||||||||
| 33 | #pod # now you have: | ||||||||||||
| 34 | #pod qw(MyApp::Plugin MyApp::Mixin MyApp::Addon Corporate::Thinger) | ||||||||||||
| 35 | #pod | ||||||||||||
| 36 | #pod =method rewrite | ||||||||||||
| 37 | #pod | ||||||||||||
| 38 | #pod String::RewritePrefix->rewrite(\%prefix, @strings); | ||||||||||||
| 39 | #pod | ||||||||||||
| 40 | #pod This rewrites all the given strings using the rules in C<%prefix>. Its keys | ||||||||||||
| 41 | #pod are known prefixes for which its values will be substituted. This is performed | ||||||||||||
| 42 | #pod in longest-first order, and only one prefix will be rewritten. | ||||||||||||
| 43 | #pod | ||||||||||||
| 44 | #pod If the prefix value is a coderef, it will be executed with the remaining string | ||||||||||||
| 45 | #pod as its only argument. The return value will be used as the prefix. | ||||||||||||
| 46 | #pod | ||||||||||||
| 47 | #pod =cut | ||||||||||||
| 48 | |||||||||||||
| 49 | 18 | ▶ | sub rewrite { | ||||||||||
|
Subroutine
rewrite: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||||
| 50 | 18 | my ($self, $arg, @rest) = @_; | |||||||||||
| 51 | 18 | return $self->_new_rewriter(rewrite => { prefixes => $arg })->(@rest); | |||||||||||
| 52 | } | ||||||||||||
| 53 | |||||||||||||
| 54 | 23 | ▶ | sub _new_rewriter { | ||||||||||
|
Subroutine
_new_rewriter: called CC=3 SCAR=11.0
| |||||||||||||
| 55 | 23 | my ($self, $name, $arg) = @_; | |||||||||||
| 56 | 23 | ▶ | my $rewrites = $arg->{prefixes} || {}; | ||||||||||
|
Condition50%1/2
$arg->{'prefixes'} || {}
MC/DC0%0/1
$arg->{'prefixes'} || {}
$arg->{'prefixes'}
Truth table50%1/2
A
A= $arg->{'prefixes'}
| |||||||||||||
| 57 | |||||||||||||
| 58 | 23 | my @rewrites; | |||||||||||
| 59 | 23 | for my $prefix (sort { length $b <=> length $a } keys %$rewrites) { | |||||||||||
| 60 | 71 | push @rewrites, ($prefix, $rewrites->{$prefix}); | |||||||||||
| 61 | } | ||||||||||||
| 62 | |||||||||||||
| 63 | 38 | ▶ | return sub { | ||||||||||
|
Subroutine
__ANON__: called CC=11 SCAR=24.1
| |||||||||||||
| 64 | 38 | my @result; | |||||||||||
| 65 | |||||||||||||
| 66 | 38 | ▶ | Carp::cluck("string rewriter invoked in void context") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 67 | unless defined wantarray; | ||||||||||||
| 68 | |||||||||||||
| 69 | 38 | ▶ | if ( ! wantarray) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 70 | 29 | ▶ | Carp::croak("attempt to rewrite multiple strings outside of list context") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 71 | if @_ > 1; | ||||||||||||
| 72 | |||||||||||||
| 73 | 29 | ▶ | Carp::cluck("rewrite invoked without args") | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 74 | if @_ == 0; | ||||||||||||
| 75 | } | ||||||||||||
| 76 | |||||||||||||
| 77 | 38 | STRING: for my $str (@_) { | |||||||||||
| 78 | 71 | ▶ | for (my $i = 0; $i < @rewrites; $i += 2) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 79 | 169 | ▶ | if (index($str, $rewrites[$i]) == 0) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 80 | 66 | ▶ | if (ref $rewrites[$i+1]) { | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 81 | 10 | my $rest = substr $str, length($rewrites[$i]); | |||||||||||
| 82 | 10 | my $str = $rewrites[ $i+1 ]->($rest); | |||||||||||
| 83 | 10 | ▶ | push @result, (defined $str ? $str : '') . $rest; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 84 | } else { | ||||||||||||
| 85 | 56 | push @result, $rewrites[$i+1] . substr $str, length($rewrites[$i]); | |||||||||||
| 86 | } | ||||||||||||
| 87 | 66 | next STRING; | |||||||||||
| 88 | } | ||||||||||||
| 89 | } | ||||||||||||
| 90 | |||||||||||||
| 91 | 5 | push @result, $str; | |||||||||||
| 92 | } | ||||||||||||
| 93 | |||||||||||||
| 94 | 38 | ▶ | return wantarray ? @result : $result[0]; | ||||||||||
|
Branch: 1 branch
| |||||||||||||
| 95 | 23 | }; | |||||||||||
| 96 | } | ||||||||||||
| 97 | |||||||||||||
| 98 | 2 | 1; | |||||||||||
| 99 | |||||||||||||
| 100 | __END__ | ||||||||||||