Skip to content

Commit 40b30f2

Browse files
pushkarscriptsgitster
authored andcommitted
path: factor out skip_slashes() in normalize_path_copy_len()
Extract skip_slashes() to avoid repeating the same is_dir_sep() loop in multiple places inside normalize_path_copy_len(). Keep the dot-component handling inline to preserve the original control flow and readability, as suggested in review. No functional changes. Behavior verified with t0060-path-utils.sh. Signed-off-by: Pushkar Singh <pushkarkumarsingh1970@gmail.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>
1 parent 7c02d39 commit 40b30f2

1 file changed

Lines changed: 13 additions & 6 deletions

File tree

path.c

Lines changed: 13 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1112,6 +1112,14 @@ const char *remove_leading_path(const char *in, const char *prefix)
11121112
* end with a '/', then the callers need to be fixed up accordingly.
11131113
*
11141114
*/
1115+
1116+
static const char *skip_slashes(const char *p)
1117+
{
1118+
while (is_dir_sep(*p))
1119+
p++;
1120+
return p;
1121+
}
1122+
11151123
int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
11161124
{
11171125
char *dst0;
@@ -1129,8 +1137,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
11291137
}
11301138
dst0 = dst;
11311139

1132-
while (is_dir_sep(*src))
1133-
src++;
1140+
src = skip_slashes(src);
11341141

11351142
for (;;) {
11361143
char c = *src;
@@ -1150,8 +1157,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
11501157
} else if (is_dir_sep(src[1])) {
11511158
/* (2) */
11521159
src += 2;
1153-
while (is_dir_sep(*src))
1154-
src++;
1160+
src = skip_slashes(src);
11551161
continue;
11561162
} else if (src[1] == '.') {
11571163
if (!src[2]) {
@@ -1161,8 +1167,7 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
11611167
} else if (is_dir_sep(src[2])) {
11621168
/* (4) */
11631169
src += 3;
1164-
while (is_dir_sep(*src))
1165-
src++;
1170+
src = skip_slashes(src);
11661171
goto up_one;
11671172
}
11681173
}
@@ -1182,6 +1187,8 @@ int normalize_path_copy_len(char *dst, const char *src, int *prefix_len)
11821187

11831188
up_one:
11841189
/*
1190+
* strip the last component
1191+
*
11851192
* dst0..dst is prefix portion, and dst[-1] is '/';
11861193
* go up one level.
11871194
*/

0 commit comments

Comments
 (0)