| 1 | # Copyright (c) 2025-2026 Philipp Schafft | ||||||||||||||
| 2 | |||||||||||||||
| 3 | # licensed under Artistic License 2.0 (see LICENSE file) | ||||||||||||||
| 4 | |||||||||||||||
| 5 | # ABSTRACT: Compactor for superstrings | ||||||||||||||
| 6 | |||||||||||||||
| 7 | package String::Super; | ||||||||||||||
| 8 | |||||||||||||||
| 9 | 4 | ▶ | use v5.20; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 10 | 4 | ▶ | use strict; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 11 | 4 | ▶ | use warnings; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 12 | |||||||||||||||
| 13 | 4 | ▶ | use Carp; | ||||||||||||
|
Subroutine
BEGIN: called CC=1 SCAR=0.0
| |||||||||||||||
| 14 | |||||||||||||||
| 15 | 4 | our $VERSION = v0.03; | |||||||||||||
| 16 | |||||||||||||||
| 17 | |||||||||||||||
| 18 | |||||||||||||||
| 19 | 10 | ▶ | sub new { | ||||||||||||
|
Subroutine
new: called CC=3 SCAR=12.3
Pod
documented
| |||||||||||||||
| 20 | 10 | my ($pkg, %opts) = @_; | |||||||||||||
| 21 | 10 | my $self = bless { | |||||||||||||
| 22 | strings => [], | ||||||||||||||
| 23 | result => undef, | ||||||||||||||
| 24 | keep_first => undef, | ||||||||||||||
| 25 | }, $pkg; | ||||||||||||||
| 26 | |||||||||||||||
| 27 | 10 | ▶ | if (defined(my $prefix_blob = delete $opts{prefix_blob})) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 28 | 0 | $self->add_blob($prefix_blob); | |||||||||||||
| 29 | 0 | $self->{keep_first} = 1; | |||||||||||||
| 30 | } | ||||||||||||||
| 31 | |||||||||||||||
| 32 | 10 | ▶ | croak 'Stray options passed' if scalar keys %opts; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 33 | |||||||||||||||
| 34 | 10 | return $self; | |||||||||||||
| 35 | } | ||||||||||||||
| 36 | |||||||||||||||
| 37 | |||||||||||||||
| 38 | 12 | ▶ | sub add_blob { | ||||||||||||
|
Subroutine
add_blob: called CC=4 SCAR=14.0
Pod
documented
| |||||||||||||||
| 39 | 12 | my ($self, @blobs) = @_; | |||||||||||||
| 40 | 12 | my $res = scalar(@{$self->{strings}}); | |||||||||||||
| 41 | |||||||||||||||
| 42 | 12 | ▶ | croak 'Already compacted' if defined $self->{result}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 43 | |||||||||||||||
| 44 | 12 | foreach my $blob (@blobs) { | |||||||||||||
| 45 | 20 | ▶ | croak 'Provided blob is a reference' if ref $blob; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 46 | } | ||||||||||||||
| 47 | |||||||||||||||
| 48 | 12 | push(@{$self->{strings}}, @blobs); | |||||||||||||
| 49 | |||||||||||||||
| 50 | 12 | return $res; | |||||||||||||
| 51 | } | ||||||||||||||
| 52 | |||||||||||||||
| 53 | |||||||||||||||
| 54 | 2 | ▶ | sub add_utf8 { | ||||||||||||
|
Subroutine
add_utf8: called CC=3 SCAR=11.0
Pod
documented
| |||||||||||||||
| 55 | 2 | require Encode; | |||||||||||||
| 56 | |||||||||||||||
| 57 | 2 | my ($self, @strings) = @_; | |||||||||||||
| 58 | 2 | state $UTF_8 = Encode::find_encoding('UTF-8'); | |||||||||||||
| 59 | |||||||||||||||
| 60 | 2 | foreach my $string (@strings) { | |||||||||||||
| 61 | 2 | ▶ | croak 'Provided string is a reference' if ref $string; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 62 | } | ||||||||||||||
| 63 | |||||||||||||||
| 64 | 2 | return $self->add_blob(map {$UTF_8->encode($_)} @strings); | |||||||||||||
| 65 | } | ||||||||||||||
| 66 | |||||||||||||||
| 67 | |||||||||||||||
| 68 | 10 | ▶ | sub compact { | ||||||||||||
|
Subroutine
compact: called CC=9 SCAR=22.0
Pod
documented
| |||||||||||||||
| 69 | 10 | my ($self, %opts) = @_; | |||||||||||||
| 70 | |||||||||||||||
| 71 | 10 | ▶ | croak 'Stray options passed' if scalar keys %opts; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 72 | |||||||||||||||
| 73 | 10 | ▶ | return if defined $self->{result}; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 74 | |||||||||||||||
| 75 | { | ||||||||||||||
| 76 | 10 | my @data = @{$self->{strings}}; | |||||||||||||
| 77 | 10 | ▶ | my $j_start = $self->{keep_first} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 78 | |||||||||||||||
| 79 | # eliminate all strings first that are already part of other strings | ||||||||||||||
| 80 | outer: | ||||||||||||||
| 81 | 10 | ▶ | for (my $i = 0; $i < scalar(@data); $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 82 | 20 | ▶ | for (my $j = $j_start; $j < scalar(@data); $j++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 83 | 47 | ▶ | next if $i == $j; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 84 | |||||||||||||||
| 85 | 27 | ▶ | if (index($data[$i], $data[$j]) >= 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 86 | 1 | splice(@data, $j, 1); | |||||||||||||
| 87 | 1 | $i--; | |||||||||||||
| 88 | 1 | next outer; | |||||||||||||
| 89 | } | ||||||||||||||
| 90 | } | ||||||||||||||
| 91 | } | ||||||||||||||
| 92 | |||||||||||||||
| 93 | 10 | ▶ | for (my $n = 8; $n > 0; $n--) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 94 | 80 | $self->_compact_n(\@data, $n); | |||||||||||||
| 95 | } | ||||||||||||||
| 96 | |||||||||||||||
| 97 | 10 | $self->{result} = join('', @data); | |||||||||||||
| 98 | } | ||||||||||||||
| 99 | } | ||||||||||||||
| 100 | |||||||||||||||
| 101 | |||||||||||||||
| 102 | 10 | ▶ | sub result { | ||||||||||||
|
Subroutine
result: called CC=2 SCAR=7.0
Pod
documented
| |||||||||||||||
| 103 | 10 | my ($self, %opts) = @_; | |||||||||||||
| 104 | |||||||||||||||
| 105 | 10 | ▶ | croak 'Stray options passed' if scalar keys %opts; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 106 | |||||||||||||||
| 107 | 10 | $self->compact; | |||||||||||||
| 108 | |||||||||||||||
| 109 | 10 | return $self->{result}; | |||||||||||||
| 110 | } | ||||||||||||||
| 111 | |||||||||||||||
| 112 | |||||||||||||||
| 113 | 8 | ▶ | sub offset { | ||||||||||||
|
Subroutine
offset: called CC=9 SCAR=24.8
Pod
documented
| |||||||||||||||
| 114 | 8 | my ($self, @args) = @_; | |||||||||||||
| 115 | 8 | my @res; | |||||||||||||
| 116 | |||||||||||||||
| 117 | 8 | ▶ | while (scalar(@args) >= 2) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 118 | 8 | my ($key, $value) = (shift(@args), shift(@args)); | |||||||||||||
| 119 | 8 | my $d; | |||||||||||||
| 120 | |||||||||||||||
| 121 | 8 | ▶ | if ($key eq 'index') { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 122 | 8 | $d = $self->{strings}[$value]; | |||||||||||||
| 123 | } else { | ||||||||||||||
| 124 | 0 | croak 'Invalid type: '.$key; | |||||||||||||
| 125 | } | ||||||||||||||
| 126 | |||||||||||||||
| 127 | 8 | ▶ | if (!defined($d)) { | ||||||||||||
|
Branch: 2 branches
| |||||||||||||||
| 128 | 0 | croak 'Undefined value'; | |||||||||||||
| 129 | } elsif (ref($d)) { | ||||||||||||||
| 130 | 0 | croak 'Not a valid value (reference passed as blob?)'; | |||||||||||||
| 131 | } | ||||||||||||||
| 132 | |||||||||||||||
| 133 | 8 | $d = index($self->{result}, $d); | |||||||||||||
| 134 | 8 | ▶ | if ($d < 0) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 135 | 0 | croak 'Substring not found (this is most likely a bug in the callers code)'; | |||||||||||||
| 136 | } | ||||||||||||||
| 137 | |||||||||||||||
| 138 | 8 | push(@res, $d); | |||||||||||||
| 139 | } | ||||||||||||||
| 140 | |||||||||||||||
| 141 | 8 | ▶ | croak 'Stray options passed' if scalar @args; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 142 | |||||||||||||||
| 143 | 8 | ▶ | if (wantarray) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 144 | 0 | return @res; | |||||||||||||
| 145 | } else { | ||||||||||||||
| 146 | 8 | ▶ | croak 'Not exactly one result' unless scalar(@res) == 1; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 147 | 8 | return $res[0]; | |||||||||||||
| 148 | } | ||||||||||||||
| 149 | } | ||||||||||||||
| 150 | |||||||||||||||
| 151 | # ---- Private helpers ---- | ||||||||||||||
| 152 | |||||||||||||||
| 153 | 80 | ▶ | sub _compact_n { | ||||||||||||
|
Subroutine
_compact_n: called CC=7 SCAR=19.5
| |||||||||||||||
| 154 | 80 | my ($self, $data, $n) = @_; | |||||||||||||
| 155 | 80 | ▶ | my $j_start = $self->{keep_first} ? 1 : 0; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 156 | |||||||||||||||
| 157 | outer: | ||||||||||||||
| 158 | 80 | ▶ | for (my $i = 0; $i < scalar(@{$data}); $i++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 159 | 142 | my $suffix = substr($data->[$i], -$n); | |||||||||||||
| 160 | |||||||||||||||
| 161 | 142 | ▶ | next if length($suffix) != $n; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 162 | |||||||||||||||
| 163 | 67 | ▶ | for (my $j = $j_start; $j < scalar(@{$data}); $j++) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 164 | 138 | ▶ | next if $i == $j; | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 165 | |||||||||||||||
| 166 | #warn sprintf('%u, %u, -> %s, %s', $i, $j, defined($data->[$i]) ? 't' : 'f', defined($data->[$j]) ? 't' : 'f') unless defined($data->[$i]) && defined($data->[$j]); | ||||||||||||||
| 167 | 72 | ▶ | if ($suffix eq substr($data->[$j], 0, $n)) { | ||||||||||||
|
Branch: 1 branch
| |||||||||||||||
| 168 | 6 | substr($data->[$i], -$n, $n, $data->[$j]); | |||||||||||||
| 169 | 6 | splice(@{$data}, $j, 1); | |||||||||||||
| 170 | 6 | $i--; | |||||||||||||
| 171 | 6 | next outer; | |||||||||||||
| 172 | } | ||||||||||||||
| 173 | } | ||||||||||||||
| 174 | } | ||||||||||||||
| 175 | } | ||||||||||||||
| 176 | 4 | 1; | |||||||||||||
| 177 | |||||||||||||||
| 178 | __END__ | ||||||||||||||