mirror of
https://github.com/libgit2/libgit2.git
synced 2026-06-22 06:26:26 +00:00
path: provide a helper to compute len with trailing slashes
We may need to strip multiple slashes at the end of a path; provide a method to do so.
This commit is contained in:
@@ -419,6 +419,16 @@ int git_fs_path_to_dir(git_str *path)
|
||||
return git_str_oom(path) ? -1 : 0;
|
||||
}
|
||||
|
||||
size_t git_fs_path_dirlen(const char *path)
|
||||
{
|
||||
size_t len = strlen(path);
|
||||
|
||||
while (len > 1 && path[len - 1] == '/')
|
||||
len--;
|
||||
|
||||
return len;
|
||||
}
|
||||
|
||||
void git_fs_path_string_to_dir(char *path, size_t size)
|
||||
{
|
||||
size_t end = strlen(path);
|
||||
|
||||
@@ -86,6 +86,12 @@ extern int git_fs_path_to_dir(git_str *path);
|
||||
*/
|
||||
extern void git_fs_path_string_to_dir(char *path, size_t size);
|
||||
|
||||
/**
|
||||
* Provides the length of the given path string with no trailing
|
||||
* slashes.
|
||||
*/
|
||||
size_t git_fs_path_dirlen(const char *path);
|
||||
|
||||
/**
|
||||
* Taken from git.git; returns nonzero if the given path is "." or "..".
|
||||
*/
|
||||
|
||||
@@ -766,3 +766,15 @@ void test_path__validate_current_user_ownership(void)
|
||||
cl_git_fail(git_fs_path_owner_is_current_user(&is_cur, "/path/does/not/exist"));
|
||||
#endif
|
||||
}
|
||||
|
||||
void test_path__dirlen(void)
|
||||
{
|
||||
cl_assert_equal_sz(13, git_fs_path_dirlen("/foo/bar/asdf"));
|
||||
cl_assert_equal_sz(13, git_fs_path_dirlen("/foo/bar/asdf/"));
|
||||
cl_assert_equal_sz(13, git_fs_path_dirlen("/foo/bar/asdf//"));
|
||||
cl_assert_equal_sz(3, git_fs_path_dirlen("foo////"));
|
||||
cl_assert_equal_sz(3, git_fs_path_dirlen("foo"));
|
||||
cl_assert_equal_sz(1, git_fs_path_dirlen("/"));
|
||||
cl_assert_equal_sz(1, git_fs_path_dirlen("////"));
|
||||
cl_assert_equal_sz(0, git_fs_path_dirlen(""));
|
||||
}
|
||||
|
||||
Reference in New Issue
Block a user