diff mbox series

[24/25] xdiff-interface: support early exit in xdiff_outf()

Message ID 20210203032811.14979-25-avarab@gmail.com (mailing list archive)
State New, archived
Headers show
Series grep: PCREv2 fixes, remove kwset.[ch] | expand

Commit Message

Ævar Arnfjörð Bjarmason Feb. 3, 2021, 3:28 a.m. UTC
Bridge the gap between the preceding "xdiff-interface: allow early
return from xdiff_emit_{line,hunk}_fn" change and the public
interface. This change was split off from the rest as it wasn't a
purely mechanical addition of "return 0".

Here we want to be able to abort early, but do so in a way that
doesn't skip the appropriate strbuf_reset() invocations.

The use of -1 as a return value in the xdiff codebase for early
return, as we'll see more of in subsequent commits.

Signed-off-by: Ævar Arnfjörð Bjarmason <avarab@gmail.com>
---
 xdiff-interface.c | 16 ++++++++++++----
 1 file changed, 12 insertions(+), 4 deletions(-)

Comments

Junio C Hamano Feb. 4, 2021, 6:16 p.m. UTC | #1
Ævar Arnfjörð Bjarmason  <avarab@gmail.com> writes:

> Bridge the gap between the preceding "xdiff-interface: allow early
> return from xdiff_emit_{line,hunk}_fn" change and the public
> interface. This change was split off from the rest as it wasn't a
> purely mechanical addition of "return 0".
>
> Here we want to be able to abort early, but do so in a way that
> doesn't skip the appropriate strbuf_reset() invocations.

Nice.

> The use of -1 as a return value in the xdiff codebase for early
> return, as we'll see more of in subsequent commits.

This is a non-sentence without a verb.  

I started reading the sentence and expected to see "The use of -1,
as opposed to something else, is because of thess deep reasons"
explained, but perhaps you forgot to conclude the sentence with such
an explanation?
diff mbox series

Patch

diff --git a/xdiff-interface.c b/xdiff-interface.c
index ef557dc4e6..d066442470 100644
--- a/xdiff-interface.c
+++ b/xdiff-interface.c
@@ -39,7 +39,8 @@  static int consume_one(void *priv_, char *s, unsigned long size)
 		unsigned long this_size;
 		ep = memchr(s, '\n', size);
 		this_size = (ep == NULL) ? size : (ep - s + 1);
-		priv->line_fn(priv->consume_callback_data, s, this_size);
+		if (priv->line_fn(priv->consume_callback_data, s, this_size))
+			return -1;
 		size -= this_size;
 		s += this_size;
 	}
@@ -50,11 +51,14 @@  static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 {
 	struct xdiff_emit_state *priv = priv_;
 	int i;
+	int stop = 0;
 
 	if (!priv->line_fn)
 		return 0;
 
 	for (i = 0; i < nbuf; i++) {
+		if (stop)
+			return -1;
 		if (mb[i].ptr[mb[i].size-1] != '\n') {
 			/* Incomplete line */
 			strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
@@ -63,17 +67,21 @@  static int xdiff_outf(void *priv_, mmbuffer_t *mb, int nbuf)
 
 		/* we have a complete line */
 		if (!priv->remainder.len) {
-			consume_one(priv, mb[i].ptr, mb[i].size);
+			stop = consume_one(priv, mb[i].ptr, mb[i].size);
 			continue;
 		}
 		strbuf_add(&priv->remainder, mb[i].ptr, mb[i].size);
-		consume_one(priv, priv->remainder.buf, priv->remainder.len);
+		stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
 		strbuf_reset(&priv->remainder);
 	}
+	if (stop)
+		return -1;
 	if (priv->remainder.len) {
-		consume_one(priv, priv->remainder.buf, priv->remainder.len);
+		stop = consume_one(priv, priv->remainder.buf, priv->remainder.len);
 		strbuf_reset(&priv->remainder);
 	}
+	if (stop)
+		return -1;
 	return 0;
 }