dir: create function count_slashes()

Similar functions exist in apply.c and builtin/show-branch.c for counting the number of slashes in a string. Also in the later patches, we introduce a third caller for the same. Hence, we unify it now by cleaning the existing functions and declaring a common function count_slashes in dir.h and implementing it in dir.c to remove this code duplication. Mentored-by: Christian Couder <christian.couder@gmail.com> Mentored-by: Stefan Beller <sbeller@google.com> Signed-off-by: Junio C Hamano <gitster@pobox.com> Signed-off-by: Prathamesh Chavan <pc44800@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Prathamesh Chavan committed Jun 8, 2017 at 23:38 UTC e0556a928fdba489307d13d8d4c4d2a461fbc3ad
4 files changed +15 -21
apply.c
-11
@@ -762,17 +762,6 @@ static char *find_name_traditional(struct apply_state *state,
762 return find_name_common(state, line, def, p_value, line + len, 0);
763 }
764
765 -static int count_slashes(const char *cp)
766 -{
767 - int cnt = 0;
768 - char ch;
769 -
770 - while ((ch = *cp++))
771 - if (ch == '/')
772 - cnt++;
773 - return cnt;
774 -}
775 -
765 /*
766 * Given the string after "--- " or "+++ ", guess the appropriate
767 * p_value for the given patch.
builtin/show-branch.c
+3 -10
@@ -5,6 +5,7 @@
5 #include "color.h"
6 #include "argv-array.h"
7 #include "parse-options.h"
8 +#include "dir.h"
9
10 static const char* show_branch_usage[] = {
11 N_("git show-branch [-a | --all] [-r | --remotes] [--topo-order | --date-order]\n"
@@ -421,14 +422,6 @@ static int append_tag_ref(const char *refname, const struct object_id *oid,
422
423 static const char *match_ref_pattern = NULL;
424 static int match_ref_slash = 0;
424 -static int count_slash(const char *s)
425 -{
426 - int cnt = 0;
427 - while (*s)
428 - if (*s++ == '/')
429 - cnt++;
430 - return cnt;
431 -}
425
426 static int append_matching_ref(const char *refname, const struct object_id *oid,
427 int flag, void *cb_data)
@@ -438,7 +431,7 @@ static int append_matching_ref(const char *refname, const struct object_id *oid,
431 * refs/tags/v0.99.9a and friends.
432 */
433 const char *tail;
441 - int slash = count_slash(refname);
434 + int slash = count_slashes(refname);
435 for (tail = refname; *tail && match_ref_slash < slash; )
436 if (*tail++ == '/')
437 slash--;
@@ -529,7 +522,7 @@ static void append_one_rev(const char *av)
522 int saved_matches = ref_name_cnt;
523
524 match_ref_pattern = av;
532 - match_ref_slash = count_slash(av);
525 + match_ref_slash = count_slashes(av);
526 for_each_ref(append_matching_ref, NULL);
527 if (saved_matches == ref_name_cnt &&
528 ref_name_cnt < MAX_REVS)
dir.c
+9
@@ -49,6 +49,15 @@ static enum path_treatment read_directory_recursive(struct dir_struct *dir,
49 int check_only, const struct pathspec *pathspec);
50 static int get_dtype(struct dirent *de, const char *path, int len);
51
52 +int count_slashes(const char *s)
53 +{
54 + int cnt = 0;
55 + while (*s)
56 + if (*s++ == '/')
57 + cnt++;
58 + return cnt;
59 +}
60 +
61 int fspathcmp(const char *a, const char *b)
62 {
63 return ignore_case ? strcasecmp(a, b) : strcmp(a, b);
dir.h
+3
@@ -196,6 +196,9 @@ struct dir_struct {
196 unsigned unmanaged_exclude_files;
197 };
198
199 +/*Count the number of slashes for string s*/
200 +extern int count_slashes(const char *s);
201 +
202 /*
203 * The ordering of these constants is significant, with
204 * higher-numbered match types signifying "closer" (i.e. more