[PATCH 2/4] diff-highlight: factor out prefix/suffix functions

[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

 



In preparation for the next patch, we factor out the functions for
finding the common prefix and suffix between two lines.

Signed-off-by: Jonathan Lebon <jonathan.lebon@xxxxxxxxx>
---
 contrib/diff-highlight/diff-highlight | 98 ++++++++++++++++++++---------------
 1 file changed, 56 insertions(+), 42 deletions(-)

diff --git a/contrib/diff-highlight/diff-highlight b/contrib/diff-highlight/diff-highlight
index ffefc31..a332f86 100755
--- a/contrib/diff-highlight/diff-highlight
+++ b/contrib/diff-highlight/diff-highlight
@@ -110,48 +110,8 @@ sub highlight_pair {
 	my @a = split_line(shift);
 	my @b = split_line(shift);
 
-	# Find common prefix, taking care to skip any ansi
-	# color codes.
-	my $seen_plusminus;
-	my ($pa, $pb) = (0, 0);
-	while ($pa < @a && $pb < @b) {
-		if ($a[$pa] =~ /$COLOR/) {
-			$pa++;
-		}
-		elsif ($b[$pb] =~ /$COLOR/) {
-			$pb++;
-		}
-		elsif ($a[$pa] eq $b[$pb]) {
-			$pa++;
-			$pb++;
-		}
-		elsif (!$seen_plusminus && $a[$pa] eq '-' && $b[$pb] eq '+') {
-			$seen_plusminus = 1;
-			$pa++;
-			$pb++;
-		}
-		else {
-			last;
-		}
-	}
-
-	# Find common suffix, ignoring colors.
-	my ($sa, $sb) = ($#a, $#b);
-	while ($sa >= $pa && $sb >= $pb) {
-		if ($a[$sa] =~ /$COLOR/) {
-			$sa--;
-		}
-		elsif ($b[$sb] =~ /$COLOR/) {
-			$sb--;
-		}
-		elsif ($a[$sa] eq $b[$sb]) {
-			$sa--;
-			$sb--;
-		}
-		else {
-			last;
-		}
-	}
+	my ($pa, $pb) = find_common_prefix(\@a, \@b);
+	my ($sa, $sb) = find_common_suffix(\@a, $pa, \@b, $pb);
 
 	if (is_pair_interesting(\@a, $pa, $sa, \@b, $pb, $sb)) {
 		return highlight_line(\@a, $pa, $sa, \@OLD_HIGHLIGHT),
@@ -173,6 +133,60 @@ sub split_line {
 		split /($COLOR+)/;
 }
 
+sub find_common_prefix {
+	my ($a, $b) = @_;
+
+	# Take care to skip any ansi color codes.
+	my $seen_plusminus;
+	my ($pa, $pb) = (0, 0);
+	while ($pa < @$a && $pb < @$b) {
+		if ($a->[$pa] =~ /$COLOR/) {
+			$pa++;
+		}
+		elsif ($b->[$pb] =~ /$COLOR/) {
+			$pb++;
+		}
+		elsif ($a->[$pa] eq $b->[$pb]) {
+			$pa++;
+			$pb++;
+		}
+		elsif (!$seen_plusminus && $a->[$pa] eq '-' && $b->[$pb] eq '+') {
+			$seen_plusminus = 1;
+			$pa++;
+			$pb++;
+		}
+		else {
+			last;
+		}
+	}
+
+	return $pa, $pb;
+}
+
+sub find_common_suffix {
+	my ($a, $pa, $b, $pb) = @_;
+
+	# Take care to skip any ansi color codes.
+	my ($sa, $sb) = ($#$a, $#$b);
+	while ($sa >= $pa && $sb >= $pb) {
+		if ($a->[$sa] =~ /$COLOR/) {
+			$sa--;
+		}
+		elsif ($b->[$sb] =~ /$COLOR/) {
+			$sb--;
+		}
+		elsif ($a->[$sa] eq $b->[$sb]) {
+			$sa--;
+			$sb--;
+		}
+		else {
+			last;
+		}
+	}
+
+	return $sa, $sb;
+}
+
 sub highlight_line {
 	my ($line, $prefix, $suffix, $theme) = @_;
 
-- 
2.6.0

--
To unsubscribe from this list: send the line "unsubscribe git" in
the body of a message to majordomo@xxxxxxxxxxxxxxx
More majordomo info at  http://vger.kernel.org/majordomo-info.html



[Index of Archives]     [Linux Kernel Development]     [Gcc Help]     [IETF Annouce]     [DCCP]     [Netdev]     [Networking]     [Security]     [V4L]     [Bugtraq]     [Yosemite]     [MIPS Linux]     [ARM Linux]     [Linux Security]     [Linux RAID]     [Linux SCSI]     [Fedora Users]