Skip to content

Commit 881529c

Browse files
rscharfegitster
authored andcommitted
apply: remove prefix_length member from apply_state
Use a NULL-and-NUL check to see if we have a prefix and consistently use C string functions on it instead of storing its length in a member of struct apply_state. This avoids strlen() calls and simplifies the code. Signed-off-by: Rene Scharfe <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 95d6787 commit 881529c

File tree

2 files changed

+5
-8
lines changed

2 files changed

+5
-8
lines changed

apply.c

Lines changed: 5 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -79,7 +79,6 @@ int init_apply_state(struct apply_state *state,
7979
{
8080
memset(state, 0, sizeof(*state));
8181
state->prefix = prefix;
82-
state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
8382
state->lock_file = lock_file;
8483
state->newfd = -1;
8584
state->apply = 1;
@@ -795,11 +794,11 @@ static int guess_p_value(struct apply_state *state, const char *nameline)
795794
* Does it begin with "a/$our-prefix" and such? Then this is
796795
* very likely to apply to our directory.
797796
*/
798-
if (!strncmp(name, state->prefix, state->prefix_length))
797+
if (starts_with(name, state->prefix))
799798
val = count_slashes(state->prefix);
800799
else {
801800
cp++;
802-
if (!strncmp(cp, state->prefix, state->prefix_length))
801+
if (starts_with(cp, state->prefix))
803802
val = count_slashes(state->prefix) + 1;
804803
}
805804
}
@@ -2078,10 +2077,9 @@ static int use_patch(struct apply_state *state, struct patch *p)
20782077
int i;
20792078

20802079
/* Paths outside are not touched regardless of "--include" */
2081-
if (0 < state->prefix_length) {
2082-
int pathlen = strlen(pathname);
2083-
if (pathlen <= state->prefix_length ||
2084-
memcmp(state->prefix, pathname, state->prefix_length))
2080+
if (state->prefix && *state->prefix) {
2081+
const char *rest;
2082+
if (!skip_prefix(pathname, state->prefix, &rest) || !*rest)
20852083
return 0;
20862084
}
20872085

apply.h

Lines changed: 0 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -35,7 +35,6 @@ enum apply_verbosity {
3535

3636
struct apply_state {
3737
const char *prefix;
38-
int prefix_length;
3938

4039
/* These are lock_file related */
4140
struct lock_file *lock_file;

0 commit comments

Comments
 (0)