Skip to content

Commit 1a845fb

Browse files
peffgitster
authored andcommitted
grep: mark "haystack" buffers as const
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, and make our expectations more clear (we will not take ownership of the memory, nor will we ever write to it). These spots don't all necessarily have to be converted at the same time, but some of them are dependent on others, because we pass pointers-to-pointers in a few cases. And none of this should change any behavior, since we're just adding "const" qualifiers (and likewise, the compiler will let us know if we missed any spots). So it's relatively low-risk to just do this all at once. Signed-off-by: Jeff King <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent f84e79f commit 1a845fb

File tree

1 file changed

+35
-26
lines changed

1 file changed

+35
-26
lines changed

grep.c

Lines changed: 35 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -867,7 +867,7 @@ void free_grep_patterns(struct grep_opt *opt)
867867
free_pattern_expr(opt->pattern_expression);
868868
}
869869

870-
static char *end_of_line(char *cp, unsigned long *left)
870+
static const char *end_of_line(const char *cp, unsigned long *left)
871871
{
872872
unsigned long l = *left;
873873
while (l && *cp != '\n') {
@@ -908,7 +908,8 @@ static void show_name(struct grep_opt *opt, const char *name)
908908
opt->output(opt, opt->null_following_name ? "\0" : "\n", 1);
909909
}
910910

911-
static int patmatch(struct grep_pat *p, char *line, char *eol,
911+
static int patmatch(struct grep_pat *p,
912+
const char *line, const char *eol,
912913
regmatch_t *match, int eflags)
913914
{
914915
int hit;
@@ -922,9 +923,9 @@ static int patmatch(struct grep_pat *p, char *line, char *eol,
922923
return hit;
923924
}
924925

925-
static void strip_timestamp(char *bol, char **eol_p)
926+
static void strip_timestamp(const char *bol, const char **eol_p)
926927
{
927-
char *eol = *eol_p;
928+
const char *eol = *eol_p;
928929

929930
while (bol < --eol) {
930931
if (*eol != '>')
@@ -943,7 +944,8 @@ static struct {
943944
{ "reflog ", 7 },
944945
};
945946

946-
static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
947+
static int match_one_pattern(struct grep_pat *p,
948+
const char *bol, const char *eol,
947949
enum grep_context ctx,
948950
regmatch_t *pmatch, int eflags)
949951
{
@@ -1023,8 +1025,9 @@ static int match_one_pattern(struct grep_pat *p, char *bol, char *eol,
10231025
return hit;
10241026
}
10251027

1026-
static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
1027-
char *eol, enum grep_context ctx, ssize_t *col,
1028+
static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x,
1029+
const char *bol, const char *eol,
1030+
enum grep_context ctx, ssize_t *col,
10281031
ssize_t *icol, int collect_hits)
10291032
{
10301033
int h = 0;
@@ -1091,15 +1094,17 @@ static int match_expr_eval(struct grep_opt *opt, struct grep_expr *x, char *bol,
10911094
return h;
10921095
}
10931096

1094-
static int match_expr(struct grep_opt *opt, char *bol, char *eol,
1097+
static int match_expr(struct grep_opt *opt,
1098+
const char *bol, const char *eol,
10951099
enum grep_context ctx, ssize_t *col,
10961100
ssize_t *icol, int collect_hits)
10971101
{
10981102
struct grep_expr *x = opt->pattern_expression;
10991103
return match_expr_eval(opt, x, bol, eol, ctx, col, icol, collect_hits);
11001104
}
11011105

1102-
static int match_line(struct grep_opt *opt, char *bol, char *eol,
1106+
static int match_line(struct grep_opt *opt,
1107+
const char *bol, const char *eol,
11031108
ssize_t *col, ssize_t *icol,
11041109
enum grep_context ctx, int collect_hits)
11051110
{
@@ -1131,7 +1136,8 @@ static int match_line(struct grep_opt *opt, char *bol, char *eol,
11311136
return hit;
11321137
}
11331138

1134-
static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
1139+
static int match_next_pattern(struct grep_pat *p,
1140+
const char *bol, const char *eol,
11351141
enum grep_context ctx,
11361142
regmatch_t *pmatch, int eflags)
11371143
{
@@ -1152,7 +1158,8 @@ static int match_next_pattern(struct grep_pat *p, char *bol, char *eol,
11521158
return 1;
11531159
}
11541160

1155-
static int next_match(struct grep_opt *opt, char *bol, char *eol,
1161+
static int next_match(struct grep_opt *opt,
1162+
const char *bol, const char *eol,
11561163
enum grep_context ctx, regmatch_t *pmatch, int eflags)
11571164
{
11581165
struct grep_pat *p;
@@ -1208,7 +1215,8 @@ static void show_line_header(struct grep_opt *opt, const char *name,
12081215
}
12091216
}
12101217

1211-
static void show_line(struct grep_opt *opt, char *bol, char *eol,
1218+
static void show_line(struct grep_opt *opt,
1219+
const char *bol, const char *eol,
12121220
const char *name, unsigned lno, ssize_t cno, char sign)
12131221
{
12141222
int rest = eol - bol;
@@ -1297,7 +1305,8 @@ static inline void grep_attr_unlock(void)
12971305
pthread_mutex_unlock(&grep_attr_mutex);
12981306
}
12991307

1300-
static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bol, char *eol)
1308+
static int match_funcname(struct grep_opt *opt, struct grep_source *gs,
1309+
const char *bol, const char *eol)
13011310
{
13021311
xdemitconf_t *xecfg = opt->priv;
13031312
if (xecfg && !xecfg->find_func) {
@@ -1324,10 +1333,10 @@ static int match_funcname(struct grep_opt *opt, struct grep_source *gs, char *bo
13241333
}
13251334

13261335
static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
1327-
char *bol, unsigned lno)
1336+
const char *bol, unsigned lno)
13281337
{
13291338
while (bol > gs->buf) {
1330-
char *eol = --bol;
1339+
const char *eol = --bol;
13311340

13321341
while (bol > gs->buf && bol[-1] != '\n')
13331342
bol--;
@@ -1346,7 +1355,7 @@ static void show_funcname_line(struct grep_opt *opt, struct grep_source *gs,
13461355
static int is_empty_line(const char *bol, const char *eol);
13471356

13481357
static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
1349-
char *bol, char *end, unsigned lno)
1358+
const char *bol, const char *end, unsigned lno)
13501359
{
13511360
unsigned cur = lno, from = 1, funcname_lno = 0, orig_from;
13521361
int funcname_needed = !!opt->funcname, comment_needed = 0;
@@ -1366,8 +1375,8 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
13661375

13671376
/* Rewind. */
13681377
while (bol > gs->buf && cur > from) {
1369-
char *next_bol = bol;
1370-
char *eol = --bol;
1378+
const char *next_bol = bol;
1379+
const char *eol = --bol;
13711380

13721381
while (bol > gs->buf && bol[-1] != '\n')
13731382
bol--;
@@ -1398,7 +1407,7 @@ static void show_pre_context(struct grep_opt *opt, struct grep_source *gs,
13981407

13991408
/* Back forward. */
14001409
while (cur < lno) {
1401-
char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
1410+
const char *eol = bol, sign = (cur == funcname_lno) ? '=' : '-';
14021411

14031412
while (*eol != '\n')
14041413
eol++;
@@ -1426,12 +1435,12 @@ static int should_lookahead(struct grep_opt *opt)
14261435
static int look_ahead(struct grep_opt *opt,
14271436
unsigned long *left_p,
14281437
unsigned *lno_p,
1429-
char **bol_p)
1438+
const char **bol_p)
14301439
{
14311440
unsigned lno = *lno_p;
1432-
char *bol = *bol_p;
1441+
const char *bol = *bol_p;
14331442
struct grep_pat *p;
1434-
char *sp, *last_bol;
1443+
const char *sp, *last_bol;
14351444
regoff_t earliest = -1;
14361445

14371446
for (p = opt->pattern_list; p; p = p->next) {
@@ -1533,8 +1542,8 @@ static int is_empty_line(const char *bol, const char *eol)
15331542

15341543
static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int collect_hits)
15351544
{
1536-
char *bol;
1537-
char *peek_bol = NULL;
1545+
const char *bol;
1546+
const char *peek_bol = NULL;
15381547
unsigned long left;
15391548
unsigned lno = 1;
15401549
unsigned last_hit = 0;
@@ -1616,7 +1625,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
16161625
bol = gs->buf;
16171626
left = gs->size;
16181627
while (left) {
1619-
char *eol;
1628+
const char *eol;
16201629
int hit;
16211630
ssize_t cno;
16221631
ssize_t col = -1, icol = -1;
@@ -1700,7 +1709,7 @@ static int grep_source_1(struct grep_opt *opt, struct grep_source *gs, int colle
17001709
}
17011710
if (show_function && (!peek_bol || peek_bol < bol)) {
17021711
unsigned long peek_left = left;
1703-
char *peek_eol = eol;
1712+
const char *peek_eol = eol;
17041713

17051714
/*
17061715
* Trailing empty lines are not interesting.

0 commit comments

Comments
 (0)