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