| 1 |
# Copyright (C) 2008-2010 Modestas Vainius <[email protected]> |
| 2 |
# |
| 3 |
# This program is free software: you can redistribute it and/or modify |
| 4 |
# it under the terms of the GNU General Public License as published by |
| 5 |
# the Free Software Foundation, either version 3 of the License, or |
| 6 |
# (at your option) any later version. |
| 7 |
# |
| 8 |
# This program is distributed in the hope that it will be useful, |
| 9 |
# but WITHOUT ANY WARRANTY; without even the implied warranty of |
| 10 |
# MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the |
| 11 |
# GNU General Public License for more details. |
| 12 |
# |
| 13 |
# You should have received a copy of the GNU General Public License |
| 14 |
# along with this program. If not, see <http://www.gnu.org/licenses/> |
| 15 |
|
| 16 |
package Debian::PkgKde::SymbolsHelper::String; |
| 17 |
|
| 18 |
use strict; |
| 19 |
use warnings; |
| 20 |
|
| 21 |
use overload '""' => \&get_string; |
| 22 |
|
| 23 |
sub new { |
| 24 |
my ($class, $str) = @_; |
| 25 |
return bless { str => $str }, $class; |
| 26 |
} |
| 27 |
|
| 28 |
sub init_string2_by_re { |
| 29 |
my ($self, $str2, $re, $values) = @_; |
| 30 |
my @str2 = split(//, $self->get_string()); |
| 31 |
my $offset = 0; |
| 32 |
while ($str2 =~ m/$re/g) { |
| 33 |
my $key = $1; |
| 34 |
my $i = pos($str2) - length($&) - $offset; |
| 35 |
$str2[$i] = "$&"; |
| 36 |
my $count = $i + length($values->{$key}); |
| 37 |
for ($i++; $i < $count; $i++) { |
| 38 |
$str2[$i] = undef; |
| 39 |
} |
| 40 |
$offset += length($&) - length($values->{$key}); |
| 41 |
} |
| 42 |
$self->{str2} = \@str2; |
| 43 |
} |
| 44 |
|
| 45 |
sub substr { |
| 46 |
my ($self, $offset, $length, $repl1, $repl2) = @_; |
| 47 |
if (defined $repl2 || exists $self->{str2}) { |
| 48 |
# If str2 has not been created yet, create it |
| 49 |
if (!exists $self->{str2}) { |
| 50 |
$self->{str2} = [ split(//, $self->{str}) ]; |
| 51 |
} |
| 52 |
# Keep offset information intact with $repl1 |
| 53 |
my @repl2; |
| 54 |
my $edit_str2 = 1; |
| 55 |
if (defined $repl2) { |
| 56 |
@repl2 = map { undef } split(//, $repl1); |
| 57 |
$repl2[0] = $repl2; |
| 58 |
} elsif ($length != length($repl1)) { |
| 59 |
if (!defined $repl2) { |
| 60 |
for (my $i = 0; $i < length($repl1); $i++) { |
| 61 |
if ($i < $length) { |
| 62 |
push @repl2, $self->{str2}[$offset+$i]; |
| 63 |
} else { |
| 64 |
push @repl2, undef; |
| 65 |
} |
| 66 |
} |
| 67 |
} |
| 68 |
} else { |
| 69 |
$edit_str2 = 0; |
| 70 |
} |
| 71 |
splice @{$self->{str2}}, $offset, $length, @repl2 if $edit_str2; |
| 72 |
} |
| 73 |
substr($self->{str}, $offset, $length) = $repl1; |
| 74 |
} |
| 75 |
|
| 76 |
sub get_string { |
| 77 |
return shift()->{str}; |
| 78 |
} |
| 79 |
|
| 80 |
sub has_string2 { |
| 81 |
return exists shift()->{str2}; |
| 82 |
} |
| 83 |
|
| 84 |
sub get_string2_char { |
| 85 |
my ($self, $index) = @_; |
| 86 |
return $self->{str2}->[$index]; |
| 87 |
} |
| 88 |
|
| 89 |
sub get_string2 { |
| 90 |
my $self = shift; |
| 91 |
if (defined $self->{str2}) { |
| 92 |
my $str = ""; |
| 93 |
foreach my $s (@{$self->{str2}}) { |
| 94 |
$str .= $s if defined $s; |
| 95 |
} |
| 96 |
return $str; |
| 97 |
} |
| 98 |
return $self->get_string(); |
| 99 |
} |
| 100 |
|
| 101 |
1; |