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 <l.s.r@web.de> Signed-off-by: Junio C Hamano <gitster@pobox.com>

René Scharfe committed Aug 9, 2017 at 17:54 UTC 881529c84656e7ff41379c0684df0ff9a796d444
2 files changed +5 -8
apply.c
+5 -7
@@ -79,7 +79,6 @@ int init_apply_state(struct apply_state *state,
79 {
80 memset(state, 0, sizeof(*state));
81 state->prefix = prefix;
82 - state->prefix_length = state->prefix ? strlen(state->prefix) : 0;
82 state->lock_file = lock_file;
83 state->newfd = -1;
84 state->apply = 1;
@@ -795,11 +794,11 @@ static int guess_p_value(struct apply_state *state, const char *nameline)
794 * Does it begin with "a/$our-prefix" and such? Then this is
795 * very likely to apply to our directory.
796 */
798 - if (!strncmp(name, state->prefix, state->prefix_length))
797 + if (starts_with(name, state->prefix))
798 val = count_slashes(state->prefix);
799 else {
800 cp++;
802 - if (!strncmp(cp, state->prefix, state->prefix_length))
801 + if (starts_with(cp, state->prefix))
802 val = count_slashes(state->prefix) + 1;
803 }
804 }
@@ -2078,10 +2077,9 @@ static int use_patch(struct apply_state *state, struct patch *p)
2077 int i;
2078
2079 /* 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)
2083 return 0;
2084 }
2085
apply.h
-1
@@ -35,7 +35,6 @@ enum apply_verbosity {
35
36 struct apply_state {
37 const char *prefix;
38 - int prefix_length;
38
39 /* These are lock_file related */
40 struct lock_file *lock_file;