/usr/share/perl5/App/Asciio/Undo.pm is in asciio 1.02.71-1.
This file is owned by root:root, with mode 0o644.
The actual contents of the file can be viewed below.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 | package App::Asciio ;
$|++ ;
use strict;
use warnings;
use Data::TreeDumper ;
#~ use Compress::LZF ':compress';
use Compress::Bzip2 qw(:all :utilities :gzip);
#-----------------------------------------------------------------------------
sub pop_undo_buffer
{
my ($self, $number_of_steps) = @_;
pop @{$self->{DO_STACK}} for(1 .. $number_of_steps) ;
}
#-----------------------------------------------------------------------------
sub redo
{
my ($self, $number_of_steps) = @_;
$self->{DO_STACK_POINTER} += $number_of_steps ;
if($self->{DO_STACK_POINTER} >= @{$self->{DO_STACK}})
{
$self->{DO_STACK_POINTER} = @{$self->{DO_STACK}} - 1 ;
}
$self->do($self->{DO_STACK_POINTER}) ;
}
sub undo
{
my ($self, $number_of_steps) = @_;
(my $new_stack_pointer = $self->{DO_STACK_POINTER}) -= $number_of_steps ;
$new_stack_pointer = 0 if($new_stack_pointer < 0) ;
$self->{DO_STACK} ||= [] ;
if($self->{DO_STACK_POINTER} == @{$self->{DO_STACK}})
{
$self->create_undo_snapshot() ;
}
$self->{DO_STACK_POINTER} = $new_stack_pointer ;
$self->do($new_stack_pointer) ;
}
sub do
{
my ($self, $stack_pointer) = @_;
my $new_self = $self->{DO_STACK}[$stack_pointer] ;
if(defined $new_self)
{
my ($do_stack_pointer, $do_stack) = ($self->{DO_STACK_POINTER}, $self->{DO_STACK}) ;
my $decompressed_new_self = decompress $new_self ;
$decompressed_new_self .= "\n\n" ; # important line or eval would complain about syntax errors !!!
my $VAR1 ;
eval $decompressed_new_self ;
if($@)
{
use File::Slurp ;
write_file('undo_error.pl', $decompressed_new_self ) ;
die "Can't undo! $@\n" ;
}
else
{
$self->load_self($VAR1) ;
($self->{DO_STACK_POINTER}, $self->{DO_STACK}) = ($do_stack_pointer, $do_stack) ;
$self->update_display() ;
}
}
}
#-----------------------------------------------------------------------------
sub create_undo_snapshot
{
my ($self) = @_;
#TODO: delta, serialize and compress, use the same huffman table for extra compression
my $serialized_self ;
{
local $self->{DO_STACK} = undef ;
$serialized_self = $self->serialize_self() ;
}
#~ my $previous_serialized_self = '' ;
#~ {
#~ local $self->{DO_STACK} = undef ;
#~ my $xxx= $self->serialize_self(1) ;
#~ use File::Slurp ;
#~ write_file("test/undo_$self->{DO_STACK_POINTER}.txt", $xxx) ;
# diff serialize 1 + bzip 2 => 500-1000 bytes vs 4000-5000 bytes with no diff and compress
#~ }
my $compressed_self = compress $serialized_self ;
splice(@{$self->{DO_STACK}}, min($self->{DO_STACK_POINTER}, scalar(@{$self->{DO_STACK}}))) ; # new do branch
push @{$self->{DO_STACK}}, $compressed_self ;
$self->{DO_STACK_POINTER} = @{$self->{DO_STACK}} ;
#~ print 'stack: ' . scalar(@{$self->{DO_STACK}}) . ' size: ' . length($serialized_self) . ' compressed: ' . length($compressed_self) . "\n" ;
}
#-----------------------------------------------------------------------------
use Algorithm::Diff qw(diff LCS traverse_sequences) ;
sub test_diff
{
# This example produces traditional 'diff' output:
my @seq1 = ("line 1", "line 2", "line3", "line 4", "line 5", 'line 6') ;
my @seq2 = ("line mod1", "line 2", "line 2B", "line3", "line 5") ;
my @diff_lines = get_diff_lines(\@seq1, \@seq2) ;
for my $diff_line (@diff_lines)
{
print DumpTree $diff_line ;
my (
$number_of_errors
, $number_of_match
, $synchronized_a
, $synchronized_b
, $error_string
) = CompareStrings($diff_line->{REFERENCE}, $diff_line->{NEW}) ;
my $undefined_line = '' ;
$undefined_line = '** reference line did not exist! **' unless defined $diff_line->{REFERENCE} ;
$undefined_line = '** new line did not exist! **' unless defined $diff_line->{NEW} ;
print <<ERRORS ;
line = $diff_line->{LINE}
number_of_match = $number_of_match
number_of_errors = $number_of_errors
$undefined_line
$synchronized_a
$synchronized_b
$error_string
ERRORS
}
}
sub get_diff_lines
{
my ($seq1, $seq2) = @_ ;
my $diff = Algorithm::Diff->new($seq1, $seq2 );
my @diff_lines ;
$diff->Base(1);
my $line = 1 ;
while($diff->Next())
{
unless($diff->Same())
{
my ($reference_line) = $diff->Items(1) ;
my ($new_line) = $diff->Items(2) ;
push @diff_lines, {LINE => $line, REFERENCE => $reference_line , NEW => $new_line} ;
}
$line++ ;
}
return @diff_lines ;
}
sub CompareStrings($$)
{
=head2 CompareStrings
Returns the following list:
=over 2
=item 1 number_of_errors
=item 2 number_of_match
=item 3 synchronized_a
=item 4 synchronized_b
=item 5 error_string
=back
=cut
my ($a_string, $b_string) = @_ ;
my ($a, $b) ;
# handle cases were one or both strings are not defined
if(!defined $a_string && ! defined $b_string)
{
return (0, 0, '', '', '') ;
}
elsif(!defined $a_string)
{
return (length($b_string), 0, '', $b_string, '^' x length($b_string)) ;
}
elsif(!defined $b_string)
{
return (length($a_string), 0, $a_string, '', '^' x length($a_string)) ;
}
my @a = split //, $a_string ;
my @b = split //, $b_string ;
my @match_indexes = Algorithm::Diff::_longestCommonSubsequence( \@a, \@b) ;
#print Dumper(\@match_indexes), "\n" ;
#my @LCS = LCS( \@a, \@b ) ;
#print Dumper(\@LCS), "\n" ;
my $previous = -1 ;
my $last_match_in_B = -1 ;
# build $a a character at a time. Synchronize strings before adding current character
for(0 .. $#match_indexes)
{
if(defined $previous)
{
if(defined $match_indexes[$_])
{
if($match_indexes[$_] == $previous + 1)
{
# match
$b .= $b[$match_indexes[$_]] ;
$last_match_in_B = $match_indexes[$_] ;
}
else
{
# match but extra letters in B, synchronize A
$a .= ' ' x ($match_indexes[$_] - ($previous + 1)) ;
$b .= join '', @b[$previous + 1 .. $match_indexes[$_]] ;
$last_match_in_B = $match_indexes[$_] ;
}
}
#else
# letter in A doesn't match in B
}
else
{
if(defined $match_indexes[$_])
{
# match
# synchronize B
my $number_of_skipped_characters_in_B = ($match_indexes[$_] - 1) - ($last_match_in_B) ;
$b .= ' ' x (length($a) - (length($b) + $number_of_skipped_characters_in_B)) ;
$b .= join '', @b[$last_match_in_B + 1 .. $match_indexes[$_]] ;
$last_match_in_B = $match_indexes[$_] ;
# synchronize A if needed
$a .= ' ' x (length($b) - (length($a) + 1)) ; # +1 as current character will be appended to $a
}
#else
# letter in A doesn't match in B
}
$a .= $a[$_] ;
$previous = $match_indexes[$_] ;
}
my $trailers_in_A = scalar(@a) - scalar(@match_indexes) ;
$a .= join '', @a[-$trailers_in_A .. -1] ;
my $trailers_in_B = scalar(@b) - ($last_match_in_B + 1) ;
$b .= join '', @b[-$trailers_in_B .. -1] ;
my $error_string = $a ^ $b ;
my $number_of_matches = $error_string =~ tr[\0][\0] ;
my $number_of_errors = length($error_string) - $number_of_matches ;
# show were the strings are different
$error_string =~ tr[\0][^]c ;
$error_string =~ tr[\0][ ] ;
return ($number_of_errors, $number_of_matches, $a, $b, $error_string) ;
}
#-----------------------------------------------------------------------------
1 ;
|