git.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Jonathan Lebon <jonathan.lebon@gmail.com>
To: git@vger.kernel.org
Cc: peff@peff.net, Jonathan Lebon <jonathan.lebon@gmail.com>
Subject: [PATCH 2/4] diff-highlight: factor out prefix/suffix functions
Date: Mon,  2 Nov 2015 21:05:32 -0500	[thread overview]
Message-ID: <1446516334-27652-3-git-send-email-jonathan.lebon@gmail.com> (raw)
In-Reply-To: <1446516334-27652-1-git-send-email-jonathan.lebon@gmail.com>

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@gmail.com>
---
 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

  parent reply	other threads:[~2015-11-03  2:06 UTC|newest]

Thread overview: 17+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2015-11-03  2:05 [PATCH 0/4] diff-highlight: make a few improvements Jonathan Lebon
2015-11-03  2:05 ` [PATCH 1/4] diff-highlight: add `less -r` to cmd in README Jonathan Lebon
2015-11-03  2:41   ` Junio C Hamano
2015-11-03  3:12     ` Jonathan Lebon
2015-11-03 21:14   ` Jeff King
2015-11-03 21:51     ` Junio C Hamano
2015-11-03 21:55       ` Jeff King
2015-11-03  2:05 ` Jonathan Lebon [this message]
2015-11-03 21:17   ` [PATCH 2/4] diff-highlight: factor out prefix/suffix functions Jeff King
2015-11-03  2:05 ` [PATCH 3/4] diff-highlight: match up lines before highlighting Jonathan Lebon
2015-11-03 21:44   ` Jeff King
2015-11-03 22:03     ` Jeff King
2015-11-17  5:18       ` Jonathan Lebon
2015-11-17 22:50         ` Jeff King
2015-11-03  2:05 ` [PATCH 4/4] diff-highlight: add maxhunksize config option Jonathan Lebon
2015-11-03 21:46   ` Jeff King
2015-11-03 22:07 ` [PATCH 0/4] diff-highlight: make a few improvements Jeff King

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1446516334-27652-3-git-send-email-jonathan.lebon@gmail.com \
    --to=jonathan.lebon@gmail.com \
    --cc=git@vger.kernel.org \
    --cc=peff@peff.net \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).