| line | stmt | bran | cond | sub | pod | time | code | 
| 1 |  |  |  |  |  |  | package String::Splitter; | 
| 2 |  |  |  |  |  |  |  | 
| 3 | 2 |  |  | 2 |  | 120882 | use Carp; | 
|  | 2 |  |  |  |  | 6 |  | 
|  | 2 |  |  |  |  | 156 |  | 
| 4 |  |  |  |  |  |  |  | 
| 5 | 2 |  |  | 2 |  | 1308 | use utf8; | 
|  | 2 |  |  |  |  | 15 |  | 
|  | 2 |  |  |  |  | 15 |  | 
| 6 | 2 |  |  | 2 |  | 62 | use warnings; | 
|  | 2 |  |  |  |  | 10 |  | 
|  | 2 |  |  |  |  | 65 |  | 
| 7 | 2 |  |  | 2 |  | 11 | use strict; | 
|  | 2 |  |  |  |  | 4 |  | 
|  | 2 |  |  |  |  | 1322 |  | 
| 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 |  |  |  |  |  |  | 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 |  |  |  |  |  |  | sub new { | 
| 89 | 1 |  |  | 1 | 1 | 55 | my ($class) = @_; | 
| 90 | 1 |  |  |  |  | 3 | my $self = {}; | 
| 91 |  |  |  |  |  |  |  | 
| 92 | 1 |  |  |  |  | 8 | 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 |  |  |  |  |  |  | sub all_splits { | 
| 106 | 5 |  |  | 5 | 1 | 18403 | my ( $self, $string ) = @_; | 
| 107 |  |  |  |  |  |  |  | 
| 108 | 5 | 100 |  |  |  | 45 | confess 'Missing $string param'     unless defined $string; | 
| 109 | 4 | 100 |  |  |  | 25 | confess 'Zero length $string param' unless length $string; | 
| 110 |  |  |  |  |  |  |  | 
| 111 | 3 |  |  |  |  | 12 | $self->_generate_split_points( [], 0, length $string ); | 
| 112 |  |  |  |  |  |  |  | 
| 113 | 3 |  |  |  |  | 4 | my @results; | 
| 114 | 3 |  |  |  |  | 5 | for my $pattern ( @{ $self->{'patterns'} } ) { | 
|  | 3 |  |  |  |  | 7 |  | 
| 115 | 11 |  |  |  |  | 13 | my $s = $string; | 
| 116 | 11 |  |  |  |  | 12 | my @split; | 
| 117 | 11 |  |  |  |  | 13 | for my $amount ( @{$pattern} ) { | 
|  | 11 |  |  |  |  | 18 |  | 
| 118 | 24 |  |  |  |  | 674 | push @split, substr $s, 0, $amount, ''; | 
| 119 |  |  |  |  |  |  | } | 
| 120 | 11 |  |  |  |  | 28 | push @results, \@split; | 
| 121 |  |  |  |  |  |  | } | 
| 122 |  |  |  |  |  |  |  | 
| 123 | 3 |  |  |  |  | 13 | delete $self->{'patterns'}; | 
| 124 |  |  |  |  |  |  |  | 
| 125 | 3 |  |  |  |  | 12 | 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 |  |  |  |  |  |  | sub all_substrings { | 
| 139 | 4 |  |  | 4 | 1 | 4929 | my ( $self, $string ) = @_; | 
| 140 |  |  |  |  |  |  |  | 
| 141 | 4 | 100 |  |  |  | 28 | confess 'Missing $string param'     unless defined $string; | 
| 142 | 3 | 100 |  |  |  | 17 | confess 'Zero length $string param' unless length $string; | 
| 143 |  |  |  |  |  |  |  | 
| 144 | 2 |  |  |  |  | 3 | my %results; | 
| 145 | 2 |  |  |  |  | 5 | for my $i ( 0 .. length $string ) { | 
| 146 | 8 |  |  |  |  | 11 | for my $j ( 0 .. length $string ) { | 
| 147 | 34 |  |  |  |  | 74 | $results{ substr $string, $i, $j } = 1; | 
| 148 |  |  |  |  |  |  | } | 
| 149 |  |  |  |  |  |  | } | 
| 150 |  |  |  |  |  |  |  | 
| 151 | 2 |  |  |  |  | 5 | delete $results{''}; | 
| 152 |  |  |  |  |  |  |  | 
| 153 | 2 |  |  |  |  | 13 | 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 |  |  |  |  |  |  | sub _generate_split_points { | 
| 173 | 51 |  |  | 51 |  | 76 | my ( $self, $chunks, $length, $remaining ) = @_; | 
| 174 |  |  |  |  |  |  |  | 
| 175 | 51 | 100 |  |  |  | 91 | if ( $length == $remaining ) { | 
| 176 | 11 |  |  |  |  | 17 | $chunks->[0] = $remaining; | 
| 177 | 11 |  |  |  |  | 15 | push @{ $self->{'patterns'} }, [ @{$chunks} ]; | 
|  | 11 |  |  |  |  | 26 |  | 
|  | 11 |  |  |  |  | 24 |  | 
| 178 | 11 |  |  |  |  | 31 | return; | 
| 179 |  |  |  |  |  |  | } | 
| 180 |  |  |  |  |  |  |  | 
| 181 | 40 |  |  |  |  | 102 | for ( 1 .. $remaining ) { | 
| 182 | 48 |  |  |  |  | 45 | $self->_generate_split_points( [ @{$chunks}, $length ], | 
|  | 48 |  |  |  |  | 147 |  | 
| 183 |  |  |  |  |  |  | $_, $remaining - $length ); | 
| 184 |  |  |  |  |  |  | } | 
| 185 |  |  |  |  |  |  | } | 
| 186 |  |  |  |  |  |  |  | 
| 187 |  |  |  |  |  |  | =head1 AUTHOR | 
| 188 |  |  |  |  |  |  |  | 
| 189 |  |  |  |  |  |  | Pawel (bbkr) Pabian, C<<  >> | 
| 190 |  |  |  |  |  |  |  | 
| 191 |  |  |  |  |  |  | Private website: L | 
| 192 |  |  |  |  |  |  |  | 
| 193 |  |  |  |  |  |  | Company website: L | 
| 194 |  |  |  |  |  |  |  | 
| 195 |  |  |  |  |  |  |  | 
| 196 |  |  |  |  |  |  | =head1 BUGS | 
| 197 |  |  |  |  |  |  |  | 
| 198 |  |  |  |  |  |  | Please report any bugs or feature requests to C, or through | 
| 199 |  |  |  |  |  |  | the web interface at L.  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 | 
| 217 |  |  |  |  |  |  |  | 
| 218 |  |  |  |  |  |  | =item * AnnoCPAN: Annotated CPAN documentation | 
| 219 |  |  |  |  |  |  |  | 
| 220 |  |  |  |  |  |  | L | 
| 221 |  |  |  |  |  |  |  | 
| 222 |  |  |  |  |  |  | =item * CPAN Ratings | 
| 223 |  |  |  |  |  |  |  | 
| 224 |  |  |  |  |  |  | L | 
| 225 |  |  |  |  |  |  |  | 
| 226 |  |  |  |  |  |  | =item * Search CPAN | 
| 227 |  |  |  |  |  |  |  | 
| 228 |  |  |  |  |  |  | L | 
| 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 |  |  |  |  |  |  | 1;    # End of String::Splitter |