File Coverage

blib/lib/Diff/LibXDiff.pm
Criterion Covered Total %
statement 53 53 100.0
branch 24 48 50.0
condition n/a
subroutine 7 7 100.0
pod 4 4 100.0
total 88 112 78.5


line stmt bran cond sub pod time code
1             package Diff::LibXDiff;
2              
3 2     2   350857 use warnings;
  2         7  
  2         88  
4 2     2   11 use strict;
  2         5  
  2         182  
5              
6             =head1 NAME
7              
8             Diff::LibXDiff - Calculate a diff with LibXDiff (via XS)
9              
10             =head1 VERSION
11              
12             Version 0.05
13              
14             =cut
15              
16             our $VERSION = '0.05';
17              
18             =head1 SYNOPSIS
19              
20             use Diff::LibXDiff;
21              
22             my $string1 = <<_END_
23             apple
24             banana
25             cherry
26             _END_
27              
28             my $string2 = <<_END_
29             apple
30             grape
31             cherry
32             lime
33             _END_
34              
35             my $diff = Diff::LibXDiff->diff( $string1, $string2 )
36             my $bin_diff = Diff::LibXDiff->bdiff( $bin_string1, $bin_string2 )
37              
38             # $diff is ...
39              
40             @@ -1,3 +1,4 @@
41             apple
42             -banana
43             +grape
44             cherry
45             +lime
46              
47             =head1 DESCRIPTION
48              
49             Diff::LibXDiff is a binding of LibXDiff (L) to Perl via XS
50              
51             LibXDiff is the basis of the diff engine for git
52              
53             =cut
54              
55             require Exporter;
56             require DynaLoader;
57              
58 2     2   2044 use Carp::Clan;
  2         4582  
  2         13  
59              
60             our @ISA = qw/Exporter DynaLoader/;
61             our @EXPORT_OK = qw//;
62              
63             bootstrap Diff::LibXDiff $VERSION;
64              
65             =head1 METHODS
66              
67             =head2 $diff = Diff::LibXDiff->diff( $string1, $string2 )
68              
69             Calculate the textual diff of $string1 and $string2 and return the result as a string
70              
71             =head2 $patched = Diff::LibXDiff->patch( $original, $patch )
72              
73             =head2 ( $patched, $rejected ) = Diff::LibXDiff->patch( $original, $patch )
74              
75             Calculate the patched string given an original string and a patch string
76              
77             If the patching algorithm cannot place a hunk, it will return a second "rejected" result (if called in list context)
78              
79             =head2 $bdiff = Diff::LibXDiff->bdiff( $bin1, $bin2 )
80              
81             Calculate the binary diff of $bin1 and $bin2 and return result as a string
82              
83             =head2 $bpatched = Diff::LibXDiff->bpatch( $original, $patch )
84              
85             Calculate the patched binary given an original string and a patch string
86              
87             =cut
88              
89             sub diff {
90 2     2 1 497 my $self = shift;
91 2         4 my ($string1, $string2) = @_;
92              
93 2 50       5 croak "string1 not defined" unless defined $string1;
94 2 50       5 croak "string2 not defined" unless defined $string2;
95 2 50       5 croak "string1 isn't a string" if ref $string1;
96 2 50       4 croak "string2 isn't a string" if ref $string2;
97              
98 2         140 my $result = _xdiff( $string1, $string2 );
99              
100 2         4 my @error = @{ $result->{error} };
  2         5  
101              
102 2 50       8 croak join '', "Unable to process the diff of string1 & string2: ", join ', ', @error if @error;
103              
104             return wantarray ?
105 2 50       11 ( $result->{result} ) :
106             $result->{result}
107             ;
108             }
109              
110             sub bdiff {
111 1     1 1 45179 my $self = shift;
112 1         11 my ($string1, $string2) = @_;
113              
114 1 50       20 croak "String1 not defined" unless defined $string1;
115 1 50       18 croak "String2 not defined" unless defined $string2;
116 1 50       17 croak "string1 isn't a string" if ref $string1;
117 1 50       19 croak "string2 isn't a string" if ref $string2;
118              
119 1         272 my $result = _xbdiff( $string1, $string2 );
120              
121 1         4 my @error = @{ $result->{error} };
  1         9  
122              
123 1 50       6 croak join '', "Unable to process the diff of string1 & string2: ", join ', ', @error if @error;
124              
125             return wantarray ?
126 1 50       18 ( $result->{result} ) :
127             $result->{result}
128             ;
129             }
130              
131             sub patch {
132 2     2 1 589 my $self = shift;
133 2         6 my ($string1, $string2) = @_;
134              
135 2 50       6 croak "string1 not defined" unless defined $string1;
136 2 50       5 croak "string2 not defined" unless defined $string2;
137 2 50       9 croak "string1 isn't a string" if ref $string1;
138 2 50       6 croak "string2 isn't a string" if ref $string2;
139              
140 2         133 my $result = _xpatch( $string1, $string2 );
141              
142 2         4 my @error = @{ $result->{error} };
  2         7  
143              
144 2 50       6 croak join '', "Unable to process the patch of string1 & string2: ", join ', ', @error if @error;
145              
146             return wantarray ?
147 2 50       17 ( $result->{result}, $result->{rejected_result} ) :
148             $result->{result};
149              
150             }
151              
152             sub bpatch {
153 1     1 1 45547 my $self = shift;
154 1         9 my ($string1, $string2) = @_;
155              
156 1 50       21 croak "string1 not defined" unless defined $string1;
157 1 50       13 croak "string2 not defined" unless defined $string2;
158 1 50       8 croak "string1 isn't a string" if ref $string1;
159 1 50       11 croak "string2 isn't a string" if ref $string2;
160              
161 1         77 my $result = _xbpatch( $string1, $string2 );
162              
163 1         5 my @error = @{ $result->{error} };
  1         4  
164              
165 1 50       9 croak join '', "Unable to process the patch of string1 & string2: ", join ', ', @error if @error;
166              
167             return wantarray ?
168 1 50       12 ( $result->{result}, $result->{rejected_result} ) :
169             $result->{result};
170             }
171              
172             =head1 AUTHOR
173              
174             Robert Krimen, C<< >>
175              
176             =head1 SEE ALSO
177              
178             L
179              
180             L
181              
182             =head1 BUGS
183              
184             Please report any bugs or feature requests to C, or through
185             the web interface at L. I will be notified, and then you'll
186             automatically be notified of progress on your bug as I make changes.
187              
188              
189              
190              
191             =head1 SUPPORT
192              
193             You can find documentation for this module with the perldoc command.
194              
195             perldoc Diff::LibXDiff
196              
197              
198             You can also look for information at:
199              
200             =over 4
201              
202             =item * RT: CPAN's request tracker
203              
204             L
205              
206             =item * AnnoCPAN: Annotated CPAN documentation
207              
208             L
209              
210             =item * CPAN Ratings
211              
212             L
213              
214             =item * Search CPAN
215              
216             L
217              
218             =back
219              
220              
221             =head1 ACKNOWLEDGEMENTS
222              
223              
224             =head1 COPYRIGHT & LICENSE
225              
226             Copyright 2009 Robert Krimen, all rights reserved.
227              
228             This program is free software; you can redistribute it and/or modify it
229             under the same terms as Perl itself.
230              
231              
232             =cut
233              
234             1; # End of Diff::LibXDiff