"GOT", but the "O" is a cute, smiling pufferfish. Index | Thread | Search

From:
Tom Jones <thj@freebsd.org>
Subject:
Re: diff: Add api for consumers to check if diff is printable
To:
gameoftrees@openbsd.org
Date:
Thu, 29 Sep 2022 10:39:53 +0100

Download raw body.

Thread
On Thu, Sep 29, 2022 at 11:31:12AM +0200, Stefan Sperling wrote:
> On Thu, Sep 29, 2022 at 10:17:52AM +0100, Tom Jones wrote:
> > Programs using the libdiff api they can need to know if the diff
> > contained anything that would be printed, or would be empty.
> > 
> > Expose the same check that the outputs do as a function call.
> > 
> > - Tom
> 
> I would prefer a name such as diff_result_contains_printable_chunks()
> to make it more obvious that this interface implements a boolean check.

Updated patch with new function name

- Tom
diff --git a/include/diff_main.h b/include/diff_main.h
index 5e816ae..7b6d039 100644
--- a/include/diff_main.h
+++ b/include/diff_main.h
@@ -251,3 +251,4 @@ struct diff_result *diff_main(const struct diff_config *config,
 			      struct diff_data *left,
 			      struct diff_data *right);
 void diff_result_free(struct diff_result *result);
+int diff_result_contains_printable_chunks(struct diff_result *result);
diff --git a/lib/diff_main.c b/lib/diff_main.c
index c531ad2..3043a9d 100644
--- a/lib/diff_main.c
+++ b/lib/diff_main.c
@@ -628,3 +628,19 @@ diff_result_free(struct diff_result *result)
 	ARRAYLIST_FREE(result->chunks);
 	free(result);
 }
+
+int
+diff_result_contains_printable_chunks(struct diff_result *result)
+{
+	struct diff_chunk *c;
+	enum diff_chunk_type t;
+
+	for (int i = 0; i < result->chunks.len; i++) {
+		c = &result->chunks.head[i];
+		t = diff_chunk_type(c);
+		if (t == CHUNK_MINUS || t == CHUNK_PLUS)
+			return 1;
+	}
+
+	return 0;
+}