1package Text::Diff3::Text; 2use 5.006; 3use strict; 4use warnings; 5use base qw(Text::Diff3::ListMixin Text::Diff3::Base); 6 7use version; our $VERSION = '0.08'; 8 9sub text { return $_[0]->{text} } 10sub list { return $_[0]->{text} } # interface to ListMixin 11 12sub first_index { return 1 } 13 14sub last_index { 15 my($self) = @_; 16 return $#{$self->{text}} + $self->first_index; 17} 18 19sub range { 20 my($self) = @_; 21 return ($self->first_index .. $self->last_index); 22} 23 24sub at { 25 my($self, $x) = @_; 26 $x -= $self->first_index; 27 return $x < 0 || $x > $#{$self->{text}} ? undef : $self->{text}[$x]; 28} 29 30sub as_string_range { 31 my($self, @range) = @_; 32 my $t = q{}; 33 for (@range) { 34 my $line = $self->at($_); 35 if (defined $line) { 36 $t .= $line . "\n"; 37 } 38 } 39 return $t; 40} 41 42sub as_string_at { 43 my($self, $x) = @_; 44 my $line = $self->at($x); 45 return defined($line) ? $line . "\n" : q{}; 46} 47 48sub eq_at { 49 my($self, $x, $other) = @_; 50 my $this = $self->at($x); 51 return 0 if (defined $this) ^ (defined $other); 52 return ! (defined $this) || $this eq $other; 53} 54 55sub initialize { 56 my($self, @arg) = @_; 57 $self->SUPER::initialize(@arg); 58 my($f, $s) = @arg; 59 if (ref $s eq 'ARRAY') { 60 $self->{text} = $s; 61 } else { 62 if (ref $s) { 63 $s = ${$s}; 64 } 65 chomp $s; 66 $self->{text} = [split /\n/msx, $s, -1]; 67 } 68 return $self; 69} 70 711; 72 73__END__ 74 75=pod 76 77=head1 NAME 78 79Text::Diff3::Text - line number scheme free text buffer 80 81=head1 VERSION 82 830.08 84 85=head1 SYNOPSIS 86 87 use Text::Diff3; 88 my $f = Text::Diff3::Factory->new; 89 my $t0 = $f->create_text([ map{chomp;$_} <F0> ]); # do not dup internally. 90 my $t1 = $f->create_text($string); # make array references. 91 # follows four take same output. 92 print $_, "\n" for @{$t0->text}; 93 print $t0->as_string_at($_) for $t0->range; 94 print $t0->as_string_range($t0->ragne); 95 print $t0->as_string_at($_) for $t0->first_index .. $t0->last_index; 96 print $t0->as_string_range($t0->first_index .. $t0->last_index); 97 for ($t0->first_index .. $t0->last_index) { 98 my $line = $t0->at($_); 99 print $line, "\n" if defined($line); 100 } 101 # string compare 102 if ($t0->eq_at($i, $string)) { .... } 103 # get string size 104 my $length = $t0->size; 105 106=head1 DESCRIPTION 107 108This is a wrapper for a Perl's array reference, improving line number 109scheme free and limiting fetching from last element by minus index. 110Normally line number starts 1 in compatible with diff command tools. 111But you can change it another value like as 0 override first index 112methods. 113 114=over 115 116=item create 117 118Author recommends you to create an instance of text by using with 119a factory as follows. 120 121 use SomeFactory; 122 my $f = SomeFactory->new; 123 my $t = $f->create_text( string or arrayref ); 124 125Text::Diff3::Factory is a class to packaging several classes 126for the build-in diff processor. 127 128When pass a string, it is split by /\n/ before store the line buffers. 129When pass an array reference, it simply assigned text properties 130without duplication. In the later case, the side effects will happen 131if you use same reference at another place. 132 133=item C<< $obj->text >> 134 135Returns the line buffer attribute. It is an array reference. 136 137=item C<< $obj->list >> 138 139Same as the text property, which is an interface property 140for ListMixin. 141 142=item C<< $obj->first_index >> 143 144Returns first-index accessible by the `at' method. 145 146=item C<< $obj->last_index >> 147 148Returns last-index accessible by the `at' method. 149 150=item C<< $obj->range >> 151 152Returns a range between fist-index and last-index. 153 154=item C<< $obj->at >> 155 156Returns a line specified by a line number. 157If line number is out of range, it returns undef. 158 159=item C<< $obj->as_string_at($x) >> 160 161This is short cut for line accessing through `at'. 162If line number is out of range, it returns '', in otherwise returns line."\n". 163 164=item C<< $obj->as_string_range(@range) >> 165 166Contatinents lines in the given line number array. 167 168=item C<< $obj->eq_at($x, $other) >> 169 170This is short cut for comparison line and other string. 171 172=item C<< $obj->initialize >> 173 174Makes initial state. 175 176=back 177 178=head1 COMPATIBILITY 179 180Use new function style interfaces introduced from version 0.08. 181This module remained for backward compatibility before version 0.07. 182This module is no longer maintenance after version 0.08. 183 184=head1 AUTHOR 185 186MIZUTANI Tociyuki C<< <tociyuki@gmail.com> >>. 187 188=head1 LICENSE AND COPYRIGHT 189 190Copyright (C) 2010 MIZUTANI Tociyuki 191 192This program is free software; you can redistribute it and/or modify 193it under the terms of the GNU General Public License as published by 194the Free Software Foundation; either version 2, or (at your option) 195any later version. 196 197=cut 198 199