| 1 | package String::Splitter; | ||||||||||
| 2 | |||||||||||
| 3 | 3 | ▶ | use Carp; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 4 | |||||||||||
| 5 | 3 | ▶ | use utf8; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 6 | 3 | ▶ | use warnings; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 7 | 3 | ▶ | use strict; | ||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||
| 8 | |||||||||||
| 9 | =encoding UTF-8 | ||||||||||
| 10 | |||||||||||
| 11 | =head1 NAME | ||||||||||
| 12 | |||||||||||
| 13 | String::Splitter - Find all possible string splits and unique substrings. | ||||||||||
| 14 | |||||||||||
| 15 | =head1 VERSION | ||||||||||
| 16 | |||||||||||
| 17 | Version 0.4 | ||||||||||
| 18 | |||||||||||
| 19 | =cut | ||||||||||
| 20 | |||||||||||
| 21 | 3 | our $VERSION = '0.4'; | |||||||||
| 22 | |||||||||||
| 23 | =head1 SYNOPSIS | ||||||||||
| 24 | |||||||||||
| 25 | Find all possible string splits and unique substrings. | ||||||||||
| 26 | |||||||||||
| 27 | use String::Splitter; | ||||||||||
| 28 | |||||||||||
| 29 | my $ss = String::Splitter->new(); | ||||||||||
| 30 | |||||||||||
| 31 | my $all_splits = $ss->all_splits("ABCD"); | ||||||||||
| 32 | |||||||||||
| 33 | # $all_splits == [ | ||||||||||
| 34 | # [ 'A', 'B', 'C', 'D' ], | ||||||||||
| 35 | # [ 'AB', 'C', 'D' ], | ||||||||||
| 36 | # [ 'A', 'B', 'CD' ], | ||||||||||
| 37 | # [ 'ABC', 'D' ], | ||||||||||
| 38 | # [ 'A', 'BC', 'D' ], | ||||||||||
| 39 | # [ 'AB', 'CD' ], | ||||||||||
| 40 | # [ 'A', 'BCD' ], | ||||||||||
| 41 | # [ 'ABCD' ] | ||||||||||
| 42 | # ] | ||||||||||
| 43 | |||||||||||
| 44 | my $all_substrings = $ss->all_substrings("ABCA"); | ||||||||||
| 45 | |||||||||||
| 46 | # $all_substrings == [ | ||||||||||
| 47 | # 'A', | ||||||||||
| 48 | # 'ABC', | ||||||||||
| 49 | # 'BC', | ||||||||||
| 50 | # 'ABCA', | ||||||||||
| 51 | # 'B', | ||||||||||
| 52 | # 'BCA', | ||||||||||
| 53 | # 'C', | ||||||||||
| 54 | # 'CA', | ||||||||||
| 55 | # 'AB' | ||||||||||
| 56 | # ]; | ||||||||||
| 57 | |||||||||||
| 58 | |||||||||||
| 59 | =head2 UTF SUPPORT | ||||||||||
| 60 | |||||||||||
| 61 | Module is utf8 safe. You can | ||||||||||
| 62 | |||||||||||
| 63 | my $results = $ss->all_splits("☺☻"); | ||||||||||
| 64 | |||||||||||
| 65 | to get | ||||||||||
| 66 | |||||||||||
| 67 | [ | ||||||||||
| 68 | [ '☺', '☻' ], | ||||||||||
| 69 | [ '☺☻' ] | ||||||||||
| 70 | ] | ||||||||||
| 71 | |||||||||||
| 72 | =head2 MEMORY WARNING | ||||||||||
| 73 | |||||||||||
| 74 | Amount of possible splits is equal to | ||||||||||
| 75 | |||||||||||
| 76 | 2 ** ( length($string) -1) | ||||||||||
| 77 | |||||||||||
| 78 | so be careful with length as this grows REALLY fast!! | ||||||||||
| 79 | |||||||||||
| 80 | =head1 FUNCTIONS | ||||||||||
| 81 | |||||||||||
| 82 | =head2 new | ||||||||||
| 83 | |||||||||||
| 84 | Creates new object. | ||||||||||
| 85 | |||||||||||
| 86 | =cut | ||||||||||
| 87 | |||||||||||
| 88 | 1 | ▶ | sub new { | ||||||||
|
Subroutine
new: called CC=1 SCAR=0.0
Pod
documented
| |||||||||||
| 89 | 1 | my ($class) = @_; | |||||||||
| 90 | 1 | my $self = {}; | |||||||||
| 91 | |||||||||||
| 92 | 1 | return bless $self, $class; | |||||||||
| 93 | } | ||||||||||
| 94 | |||||||||||
| 95 | =head2 all_splits | ||||||||||
| 96 | |||||||||||
| 97 | my $results = $ss->all_splits("ABCD"); | ||||||||||
| 98 | |||||||||||
| 99 | Returns ArrayRef of ArrayRefs with all possible splits. | ||||||||||
| 100 | |||||||||||
| 101 | C<< Carp::confess >> will be called if param is missing or zero length. | ||||||||||
| 102 | |||||||||||
| 103 | =cut | ||||||||||
| 104 | |||||||||||
| 105 | 5 | ▶ | sub all_splits { | ||||||||
|
Subroutine
all_splits: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 106 | 5 | my ( $self, $string ) = @_; | |||||||||
| 107 | |||||||||||
| 108 | 5 | ▶ | confess 'Missing $string param' unless defined $string; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 109 | 4 | ▶ | confess 'Zero length $string param' unless length $string; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 110 | |||||||||||
| 111 | 3 | $self->_generate_split_points( [], 0, length $string ); | |||||||||
| 112 | |||||||||||
| 113 | 3 | my @results; | |||||||||
| 114 | 3 | for my $pattern ( @{ $self->{'patterns'} } ) { | |||||||||
| 115 | 11 | my $s = $string; | |||||||||
| 116 | 11 | my @split; | |||||||||
| 117 | 11 | for my $amount ( @{$pattern} ) { | |||||||||
| 118 | 24 | push @split, substr $s, 0, $amount, ''; | |||||||||
| 119 | } | ||||||||||
| 120 | 11 | push @results, \@split; | |||||||||
| 121 | } | ||||||||||
| 122 | |||||||||||
| 123 | 3 | delete $self->{'patterns'}; | |||||||||
| 124 | |||||||||||
| 125 | 3 | return \@results; | |||||||||
| 126 | } | ||||||||||
| 127 | |||||||||||
| 128 | =head2 all_substrings | ||||||||||
| 129 | |||||||||||
| 130 | my $results = $ss->unique_substrings("AABCDAA"); | ||||||||||
| 131 | |||||||||||
| 132 | Returns ArrayRef of all possible unique substrings. | ||||||||||
| 133 | |||||||||||
| 134 | C<< Carp::confess >> will be called if param is missing or zero length. | ||||||||||
| 135 | |||||||||||
| 136 | =cut | ||||||||||
| 137 | |||||||||||
| 138 | 4 | ▶ | sub all_substrings { | ||||||||
|
Subroutine
all_substrings: called CC=5 SCAR=16.1
Pod
documented
| |||||||||||
| 139 | 4 | my ( $self, $string ) = @_; | |||||||||
| 140 | |||||||||||
| 141 | 4 | ▶ | confess 'Missing $string param' unless defined $string; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 142 | 3 | ▶ | confess 'Zero length $string param' unless length $string; | ||||||||
|
Branch: 1 branch
| |||||||||||
| 143 | |||||||||||
| 144 | 2 | my %results; | |||||||||
| 145 | 2 | for my $i ( 0 .. length $string ) { | |||||||||
| 146 | 8 | for my $j ( 0 .. length $string ) { | |||||||||
| 147 | 34 | $results{ substr $string, $i, $j } = 1; | |||||||||
| 148 | } | ||||||||||
| 149 | } | ||||||||||
| 150 | |||||||||||
| 151 | 2 | delete $results{''}; | |||||||||
| 152 | |||||||||||
| 153 | 2 | return [ keys %results ]; | |||||||||
| 154 | } | ||||||||||
| 155 | |||||||||||
| 156 | # generate all possible substring lengths | ||||||||||
| 157 | # exmaple for 4 char string | ||||||||||
| 158 | # | ||||||||||
| 159 | # [ | ||||||||||
| 160 | # [ 1, 1, 1, 1 ], | ||||||||||
| 161 | # [ 2, 1, 1 ], | ||||||||||
| 162 | # [ 1, 1, 2 ], | ||||||||||
| 163 | # [ 3, 1 ], | ||||||||||
| 164 | # [ 1, 2, 1 ], | ||||||||||
| 165 | # [ 2, 2 ], | ||||||||||
| 166 | # [ 1, 3 ], | ||||||||||
| 167 | # [ 4, ] | ||||||||||
| 168 | # ] | ||||||||||
| 169 | # | ||||||||||
| 170 | # saves them in $self->{'patterns'} | ||||||||||
| 171 | |||||||||||
| 172 | 51 | ▶ | sub _generate_split_points { | ||||||||
|
Subroutine
_generate_split_points: called CC=3 SCAR=11.0
| |||||||||||
| 173 | 51 | my ( $self, $chunks, $length, $remaining ) = @_; | |||||||||
| 174 | |||||||||||
| 175 | 51 | ▶ | if ( $length == $remaining ) { | ||||||||
|
Branch: 1 branch
| |||||||||||
| 176 | 11 | $chunks->[0] = $remaining; | |||||||||
| 177 | 11 | push @{ $self->{'patterns'} }, [ @{$chunks} ]; | |||||||||
| 178 | 11 | return; | |||||||||
| 179 | } | ||||||||||
| 180 | |||||||||||
| 181 | 40 | for ( 1 .. $remaining ) { | |||||||||
| 182 | 48 | $self->_generate_split_points( [ @{$chunks}, $length ], | |||||||||
| 183 | $_, $remaining - $length ); | ||||||||||
| 184 | } | ||||||||||
| 185 | } | ||||||||||
| 186 | |||||||||||
| 187 | =head1 AUTHOR | ||||||||||
| 188 | |||||||||||
| 189 | Pawel (bbkr) Pabian, C<< <cpan at bbkr.org> >> | ||||||||||
| 190 | |||||||||||
| 191 | Private website: L<http://bbkr.org> | ||||||||||
| 192 | |||||||||||
| 193 | Company website: L<http://implix.com> | ||||||||||
| 194 | |||||||||||
| 195 | |||||||||||
| 196 | =head1 BUGS | ||||||||||
| 197 | |||||||||||
| 198 | Please report any bugs or feature requests to C<bug-string-splitter at rt.cpan.org>, or through | ||||||||||
| 199 | the web interface at L<http://rt.cpan.org/NoAuth/ReportBug.html?Queue=String-Splitter>. I will be notified, and then you'll | ||||||||||
| 200 | automatically be notified of progress on your bug as I make changes. | ||||||||||
| 201 | |||||||||||
| 202 | |||||||||||
| 203 | =head1 SUPPORT | ||||||||||
| 204 | |||||||||||
| 205 | You can find documentation for this module with the perldoc command. | ||||||||||
| 206 | |||||||||||
| 207 | perldoc String::Splitter | ||||||||||
| 208 | |||||||||||
| 209 | |||||||||||
| 210 | You can also look for information at: | ||||||||||
| 211 | |||||||||||
| 212 | =over 4 | ||||||||||
| 213 | |||||||||||
| 214 | =item * RT: CPAN's request tracker | ||||||||||
| 215 | |||||||||||
| 216 | L<http://rt.cpan.org/NoAuth/Bugs.html?Dist=String-Splitter> | ||||||||||
| 217 | |||||||||||
| 218 | =item * AnnoCPAN: Annotated CPAN documentation | ||||||||||
| 219 | |||||||||||
| 220 | L<http://annocpan.org/dist/String-Splitter> | ||||||||||
| 221 | |||||||||||
| 222 | =item * CPAN Ratings | ||||||||||
| 223 | |||||||||||
| 224 | L<http://cpanratings.perl.org/d/String-Splitter> | ||||||||||
| 225 | |||||||||||
| 226 | =item * Search CPAN | ||||||||||
| 227 | |||||||||||
| 228 | L<http://search.cpan.org/dist/String-Splitter> | ||||||||||
| 229 | |||||||||||
| 230 | =back | ||||||||||
| 231 | |||||||||||
| 232 | |||||||||||
| 233 | =head1 ACKNOWLEDGEMENTS | ||||||||||
| 234 | |||||||||||
| 235 | |||||||||||
| 236 | =head1 COPYRIGHT & LICENSE | ||||||||||
| 237 | |||||||||||
| 238 | Copyright 2008 Pawel bbkr Pabian, all rights reserved. | ||||||||||
| 239 | |||||||||||
| 240 | This program is free software; you can redistribute it and/or modify it | ||||||||||
| 241 | under the same terms as Perl itself. | ||||||||||
| 242 | |||||||||||
| 243 | |||||||||||
| 244 | =cut | ||||||||||
| 245 | |||||||||||
| 246 | 3 | 1; # End of String::Splitter | |||||||||