| 1 | package Data::Unixish::count; | ||||||||||
| 2 | |||||||||||
| 3 | 2 | ▶ | use 5.010001; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | 2 | ▶ | use locale; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 5 | 2 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 2 | ▶ | use syntax 'each_on_array'; # to support perl < 5.12 | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 2 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | 2 | ▶ | use Log::ger; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 9 | |||||||||||
| 10 | 2 | ▶ | use Data::Unixish::Util qw(%common_args); | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 11 | |||||||||||
| 12 | 2 | our $AUTHORITY = 'cpan:PERLANCAR'; # AUTHORITY | |||||||||
| 13 | 2 | our $DATE = '2025-02-24'; # DATE | |||||||||
| 14 | 2 | our $DIST = 'Data-Unixish'; # DIST | |||||||||
| 15 | 2 | our $VERSION = '1.574'; # VERSION | |||||||||
| 16 | |||||||||||
| 17 | 2 | our %SPEC; | |||||||||
| 18 | |||||||||||
| 19 | 7 | ▶ | sub _pattern_to_re { | ||||||||
|
Subroutine
_pattern_to_re: called CC=6 SCAR=23.5
| |||||||||||
| 20 | 7 | my $args = shift; | |||||||||
| 21 | |||||||||||
| 22 | 7 | my $re; | |||||||||
| 23 | 7 | ▶ | my $pattern = $args->{pattern}; defined $pattern or die "Please specify pattern"; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 24 | 7 | ▶ | if ($args->{fixed_string}) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 25 | 7 | ▶ | $re = $args->{ignore_case} ? qr/\Q$pattern/i : qr/\Q$pattern/; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 26 | } else { | ||||||||||
| 27 | 0 | ▶ | eval { $re = $args->{ignore_case} ? qr/$pattern/i : qr/$pattern/ }; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 28 | 0 | ▶ | die "Invalid pattern: $@" if $@; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 29 | } | ||||||||||
| 30 | |||||||||||
| 31 | 7 | $re; | |||||||||
| 32 | } | ||||||||||
| 33 | |||||||||||
| 34 | $SPEC{count} = { | ||||||||||
| 35 | 2 | v => 1.1, | |||||||||
| 36 | summary => 'Count substrings (or regex pattern matches) in a string', | ||||||||||
| 37 | description => <<'MARKDOWN', | ||||||||||
| 38 | |||||||||||
| 39 | MARKDOWN | ||||||||||
| 40 | args => { | ||||||||||
| 41 | %common_args, | ||||||||||
| 42 | pattern => { | ||||||||||
| 43 | summary => 'Pattern or substring', | ||||||||||
| 44 | schema => ['str*'], | ||||||||||
| 45 | req => 1, | ||||||||||
| 46 | pos => 0, | ||||||||||
| 47 | }, | ||||||||||
| 48 | fixed_string => { | ||||||||||
| 49 | summary => 'Interpret pattern as fixed string instead of regular expression', | ||||||||||
| 50 | schema => 'true*', | ||||||||||
| 51 | cmdline_aliases => {F=>{}}, | ||||||||||
| 52 | }, | ||||||||||
| 53 | ignore_case => { | ||||||||||
| 54 | summary => 'Whether to ignore case', | ||||||||||
| 55 | schema => 'bool*', | ||||||||||
| 56 | cmdline_aliases => {i=>{}}, | ||||||||||
| 57 | }, | ||||||||||
| 58 | }, | ||||||||||
| 59 | tags => [qw/itemfunc text regex/], | ||||||||||
| 60 | }; | ||||||||||
| 61 | 1 | ▶ | sub count { | ||||||||
|
Subroutine
count: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||
| 62 | 1 | my %args = @_; | |||||||||
| 63 | 1 | my ($in, $out) = ($args{in}, $args{out}); | |||||||||
| 64 | |||||||||||
| 65 | # we don't call _count_item() to optimize | ||||||||||
| 66 | 1 | my $re = _pattern_to_re(\%args); | |||||||||
| 67 | 1 | ▶ | while (my ($index, $item) = each @$in) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 68 | 6 | my $n = 0; | |||||||||
| 69 | 6 | ▶ | $n++ while $item =~ /$re/g; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 70 | 6 | push @$out, $n; | |||||||||
| 71 | } | ||||||||||
| 72 | |||||||||||
| 73 | 1 | [200, "OK"]; | |||||||||
| 74 | } | ||||||||||
| 75 | |||||||||||
| 76 | 6 | ▶ | sub _count_item { | ||||||||
|
Subroutine
_count_item: called CC=2 SCAR=6.9
| |||||||||||
| 77 | 6 | my ($item, $args) = @_; | |||||||||
| 78 | |||||||||||
| 79 | 6 | my $re = _pattern_to_re($args); | |||||||||
| 80 | 6 | my $n = 0; | |||||||||
| 81 | 6 | ▶ | $n++ while $item =~ /$re/g; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 82 | 6 | $n; | |||||||||
| 83 | } | ||||||||||
| 84 | |||||||||||
| 85 | 2 | 1; | |||||||||
| 86 | # ABSTRACT: Count substrings (or regex pattern matches) in a string | ||||||||||
| 87 | |||||||||||
| 88 | __END__ | ||||||||||