diff mbox series

[2/2] grep: mark "haystack" buffers as const

Message ID YUk4vnMQHApY99Lb@coredump.intra.peff.net (mailing list archive)
State New, archived
Headers show
Series [1/2] grep: stop modifying buffer in strip_timestamp | expand

Commit Message

Jeff King Sept. 21, 2021, 1:43 a.m. UTC
When we're grepping in a buffer, we don't need to modify it. So we can
take "const char *" buffers, rather than "char *". This can avoid some
awkward casts in our callers.

Signed-off-by: Jeff King <peff@peff.net>
---
This step should be quite safe, because we're not changing any behavior,
and the compiler will alert us if we missed any spots.

There may be further cleanup possible that the compiler doesn't tell us
about (i.e., other callers which have "char *" but could themselves
tighten to "const char *"), since that implicit conversion is OK without
a cast.

 grep.c   | 12 ++++++++----
 grep.h   |  3 ++-
 pretty.c |  6 +++---
 3 files changed, 13 insertions(+), 8 deletions(-)

Comments

Jeff King Sept. 21, 2021, 2:05 a.m. UTC | #1
On Mon, Sep 20, 2021 at 09:43:26PM -0400, Jeff King wrote:

> When we're grepping in a buffer, we don't need to modify it. So we can
> take "const char *" buffers, rather than "char *". This can avoid some
> awkward casts in our callers.

Sorry, this patch should have touched strip_timestamp(), too. I had
originally done it in the earlier patch, but because we pass a
pointer-to-pointer, the compiler got mad about the mis-matched type from
the caller. So I reverted it there, but forgot to add it back in here.

At any rate, these are mostly for illustration. They'd need a little
rebasing if not put on top of your patches anyway.

-Peff
diff mbox series

Patch

diff --git a/grep.c b/grep.c
index 3b372ec29d..2cb65d191f 100644
--- a/grep.c
+++ b/grep.c
@@ -908,7 +908,8 @@  static void show_name(struct grep_opt *opt, const char *name)
 	opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
 }
 
-static int patmatch(struct grep_pat *p, char *line, char *eol,
+static int patmatch(struct grep_pat *p,
+		    const char *line, const char *eol,
 		    regmatch_t *match, int eflags)
 {
 	int hit;
@@ -943,7 +944,8 @@  static struct {
 	{ "reflog ", 7 },
 };
 
-static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
+static int match_one_pattern(struct grep_pat *p,
+			     const char *bol, const char *eol,
 			     enum grep_context ctx,
 			     regmatch_t *pmatch, int eflags)
 {
@@ -1141,7 +1143,8 @@  static int match_line(struct grep_opt *opt, char *bol, char *eol,
 	return hit;
 }
 
-static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
+static int match_next_pattern(struct grep_pat *p,
+			      const char *bol, const char *eol,
 			      enum grep_context ctx,
 			      regmatch_t *pmatch, int eflags)
 {
@@ -1162,7 +1165,8 @@  static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
 	return 1;
 }
 
-int grep_next_match(struct grep_opt *opt, char *bol, char *eol,
+int grep_next_match(struct grep_opt *opt,
+		    const char *bol, const char *eol,
 		    enum grep_context ctx, regmatch_t *pmatch,
 		    enum grep_header_field field, int eflags)
 {
diff --git a/grep.h b/grep.h
index b82e5de982..a1880899ba 100644
--- a/grep.h
+++ b/grep.h
@@ -190,7 +190,8 @@  void append_header_grep_pattern(struct grep_opt *, enum grep_header_field, const
 void compile_grep_patterns(struct grep_opt *opt);
 void free_grep_patterns(struct grep_opt *opt);
 int grep_buffer(struct grep_opt *opt, char *buf, unsigned long size);
-int grep_next_match(struct grep_opt *opt, char *bol, char *eol,
+int grep_next_match(struct grep_opt *opt,
+		    const char *bol, const char *eol,
 		    enum grep_context ctx, regmatch_t *pmatch,
 		    enum grep_header_field field, int eflags);
 
diff --git a/pretty.c b/pretty.c
index 943a2d2ee2..be4efd9364 100644
--- a/pretty.c
+++ b/pretty.c
@@ -432,7 +432,7 @@  const char *show_ident_date(const struct ident_split *ident,
 }
 
 static inline void strbuf_add_with_color(struct strbuf *sb, const char *color,
-					 char *buf, size_t buflen)
+					 const char *buf, size_t buflen)
 {
 	strbuf_addstr(sb, color);
 	strbuf_add(sb, buf, buflen);
@@ -445,7 +445,7 @@  static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
 				   int color, enum grep_context ctx,
 				   enum grep_header_field field)
 {
-	char *buf, *eol;
+	const char *buf, *eol;
 	const char *line_color, *match_color;
 	regmatch_t match;
 	int eflags = 0;
@@ -455,7 +455,7 @@  static void append_line_with_color(struct strbuf *sb, struct grep_opt *opt,
 		return;
 	}
 
-	buf = (char *)line;
+	buf = line;
 	eol = buf + linelen;
 
 	line_color = opt->colors[GREP_COLOR_SELECTED];