diff options
-rw-r--r-- | src/eval.c | 6 | ||||
-rw-r--r-- | src/path.c | 14 | ||||
-rw-r--r-- | src/path.h | 10 | ||||
-rw-r--r-- | test/unit/path.moon | 13 |
4 files changed, 31 insertions, 12 deletions
diff --git a/src/eval.c b/src/eval.c index a2bfb19dd4..ed519ff378 100644 --- a/src/eval.c +++ b/src/eval.c @@ -12422,7 +12422,7 @@ static void f_resolve(typval_T *argvars, typval_T *rettv) p[len - 1] = NUL; /* the trailing slash breaks readlink() */ } - q = getnextcomp(p); + q = path_next_component(p); if (*q != NUL) { /* Separate the first path component in "p", and keep the * remainder (beginning with the path separator). */ @@ -12456,7 +12456,7 @@ static void f_resolve(typval_T *argvars, typval_T *rettv) /* Separate the first path component in the link value and * concatenate the remainders. */ - q = getnextcomp(vim_ispathsep(*buf) ? buf + 1 : buf); + q = path_next_component(vim_ispathsep(*buf) ? buf + 1 : buf); if (*q != NUL) { if (remain == NULL) remain = vim_strsave(q - 1); @@ -12495,7 +12495,7 @@ static void f_resolve(typval_T *argvars, typval_T *rettv) break; /* Append the first path component of "remain" to "p". */ - q = getnextcomp(remain + 1); + q = path_next_component(remain + 1); len = q - remain - (*q != NUL); cpy = vim_strnsave(p, STRLEN(p) + len); if (cpy != NULL) { diff --git a/src/path.c b/src/path.c index c01b1b9fa2..93ee6dddeb 100644 --- a/src/path.c +++ b/src/path.c @@ -93,15 +93,15 @@ char_u *path_tail_with_seperator(char_u *fname) return tail; } -/* - * get the next path component (just after the next path separator). - */ -char_u *getnextcomp(char_u *fname) +char_u *path_next_component(char_u *fname) { - while (*fname && !vim_ispathsep(*fname)) + assert(fname != NULL); + while (*fname != NUL && !vim_ispathsep(*fname)) { mb_ptr_adv(fname); - if (*fname) + } + if (*fname != NUL) { ++fname; + } return fname; } @@ -1428,7 +1428,7 @@ void simplify_filename(char_u *filename) } } else { ++components; /* simple path component */ - p = getnextcomp(p); + p = path_next_component(p); } } while (*p != NUL); } diff --git a/src/path.h b/src/path.h index 011736f215..a558fc72b3 100644 --- a/src/path.h +++ b/src/path.h @@ -33,13 +33,20 @@ char_u *path_tail(char_u *fname); /// /// Takes care of "c:/" and "//". That means `path_tail_with_seperator("dir///file.txt")` /// will return a pointer to `"///file.txt"`. -/// @param fname A file path. +/// @param fname A file path. (Must be != NULL.) /// @return /// - Pointer to the last path seperator of `fname`, if there is any. /// - `fname` if it contains no path seperator. /// - Never NULL. char_u *path_tail_with_seperator(char_u *fname); +/// Get the next path component of a path name. +/// +/// @param fname A file path. (Must be != NULL.) +/// @return Pointer to first found path seperator + 1. +/// An empty string, if `fname` doesn't contain a path seperator, +char_u *path_next_component(char_u *fname); + int vim_ispathsep(int c); int vim_ispathsep_nocolon(int c); int vim_ispathlistsep(int c); @@ -54,7 +61,6 @@ int gen_expand_wildcards(int num_pat, char_u **pat, int *num_file, char_u ***file, int flags); void addfile(garray_T *gap, char_u *f, int flags); -char_u *getnextcomp(char_u *fname); char_u *get_past_head(char_u *path); char_u *concat_str(char_u *str1, char_u *str2); void add_pathsep(char_u *p); diff --git a/test/unit/path.moon b/test/unit/path.moon index efa3a2ce74..8e153b66bf 100644 --- a/test/unit/path.moon +++ b/test/unit/path.moon @@ -9,6 +9,7 @@ typedef enum file_comparison { FileComparison path_full_compare(char_u *s1, char_u *s2, int checkname); char_u *path_tail(char_u *fname); char_u *path_tail_with_seperator(char_u *fname); +char_u *path_next_component(char_u *fname); ]] -- import constants parsed by ffi @@ -85,3 +86,15 @@ describe 'path function', -> it 'returns the whole file name if there is no seperator', -> eq 'file.txt', path_tail_with_seperator 'file.txt' + + describe 'path_next_component', -> + path_next_component = (file) -> + res = path.path_next_component (to_cstr file) + neq NULL, res + ffi.string res + + it 'returns', -> + eq 'directory/file.txt', path_next_component 'some/directory/file.txt' + + it 'returns empty string if given file contains no seperator', -> + eq '', path_next_component 'file.txt' |