path: Also accept / as directory separator on Windows
This adds helper functions to determine the first or last directory separator in a string and to check if a given character is a separator. Paths starting with a separator are now also considered absolute on Windows as these are rooted at the current drive. Note that it's fine to use DIRECTORY_SEPARATOR when combining strings as Windows API calls accept both forward and backward slashes as separators. Co-authored-by: Michał Skalski <[email protected]> References #3684.
This commit is contained in:
co-authored by
Michał Skalski
parent
3de65f8d67
commit
e6a6fc33b6
@@ -766,6 +766,97 @@ START_TEST(test_strreplace)
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/*******************************************************************************
|
||||
* path_first/last_separator
|
||||
*/
|
||||
|
||||
static struct {
|
||||
char *path;
|
||||
int len;
|
||||
int first;
|
||||
int last;
|
||||
} separator_data[] = {
|
||||
{NULL, -1, -1, -1},
|
||||
{"", -1, -1, -1},
|
||||
{".", -1, -1, -1},
|
||||
{"..", -1, -1, -1},
|
||||
#ifdef WIN32
|
||||
{"C:\\", -1, 2, 2},
|
||||
{"C:/", -1, 2, 2},
|
||||
{"X:\\\\", -1, 2, 3},
|
||||
{"d:\\f", -1, 2, 2},
|
||||
{"d:\\f", 2, -1, -1},
|
||||
{"C:\\foo\\", -1, 2, 6},
|
||||
{"foo\\bar", -1, 3, 3},
|
||||
{"foo\\\\bar", -1, 3, 4},
|
||||
{"\\foo\\bar", -1, 0, 4},
|
||||
{"\\\\foo\\bar", -1, 0, 5},
|
||||
{"\\\\foo\\bar", 4, 0, 1},
|
||||
{"foo\\bar/baz", -1, 3, 7},
|
||||
#endif /* WIN32 */
|
||||
{"/", -1, 0, 0},
|
||||
{"//", -1, 0, 1},
|
||||
{"foo", -1, -1, -1},
|
||||
{"f/", -1, 1, 1},
|
||||
{"foo/", -1, 3, 3},
|
||||
{"foo/", 2, -1, -1},
|
||||
{"foo//", -1, 3, 4},
|
||||
{"/foo", -1, 0, 0},
|
||||
{"/foo/", -1, 0, 4},
|
||||
{"/foo/", 3, 0, 0},
|
||||
{"//foo/", -1, 0, 5},
|
||||
{"foo/bar", -1, 3, 3},
|
||||
{"foo/bar", 1, -1, -1},
|
||||
{"foo/bar", 2, -1, -1},
|
||||
{"foo/bar", 3, -1, -1},
|
||||
{"foo/bar", 4, 3, 3},
|
||||
{"foo/bar", 5, 3, 3},
|
||||
{"foo//bar", -1, 3, 4},
|
||||
{"/foo/bar", -1, 0, 4},
|
||||
{"/foo/bar/", -1, 0, 8},
|
||||
{"/foo/bar/", 0, -1, -1},
|
||||
{"/foo/bar/", 1, 0, 0},
|
||||
{"/foo/bar/", 2, 0, 0},
|
||||
{"/foo/bar/", 3, 0, 0},
|
||||
{"/foo/bar/", 4, 0, 0},
|
||||
{"/foo/bar/", 5, 0, 4},
|
||||
{"/foo/bar/", 7, 0, 4},
|
||||
{"/foo/bar/", 8, 0, 4},
|
||||
{"/foo/bar/", 9, 0, 8},
|
||||
};
|
||||
|
||||
START_TEST(test_path_first_separator)
|
||||
{
|
||||
char *pos;
|
||||
|
||||
pos = path_first_separator(separator_data[_i].path, separator_data[_i].len);
|
||||
if (separator_data[_i].first >= 0)
|
||||
{
|
||||
ck_assert_int_eq(pos-separator_data[_i].path, separator_data[_i].first);
|
||||
}
|
||||
else
|
||||
{
|
||||
ck_assert(!pos);
|
||||
}
|
||||
}
|
||||
END_TEST
|
||||
|
||||
START_TEST(test_path_last_separator)
|
||||
{
|
||||
char *pos;
|
||||
|
||||
pos = path_last_separator(separator_data[_i].path, separator_data[_i].len);
|
||||
if (separator_data[_i].last >= 0)
|
||||
{
|
||||
ck_assert_int_eq(pos-separator_data[_i].path, separator_data[_i].last);
|
||||
}
|
||||
else
|
||||
{
|
||||
ck_assert(!pos);
|
||||
}
|
||||
}
|
||||
END_TEST
|
||||
|
||||
/*******************************************************************************
|
||||
* path_dirname/basename/absolute
|
||||
*/
|
||||
@@ -782,6 +873,7 @@ static struct {
|
||||
{"..", ".", "..", FALSE},
|
||||
#ifdef WIN32
|
||||
{"C:\\", "C:", "C:", TRUE},
|
||||
{"C:/", "C:", "C:", TRUE},
|
||||
{"X:\\\\", "X:", "X:", TRUE},
|
||||
{"foo", ".", "foo", FALSE},
|
||||
{"f\\", ".", "f", FALSE},
|
||||
@@ -789,16 +881,20 @@ static struct {
|
||||
{"foo\\\\", ".", "foo", FALSE},
|
||||
{"d:\\f", "d:", "f", TRUE},
|
||||
{"C:\\f\\", "C:", "f", TRUE},
|
||||
{"C:\\f\\", "C:", "f", TRUE},
|
||||
{"C:\\foo", "C:", "foo", TRUE},
|
||||
{"C:\\foo\\", "C:", "foo", TRUE},
|
||||
{"C:\\foo/", "C:", "foo", TRUE},
|
||||
{"foo\\bar", "foo", "bar", FALSE},
|
||||
{"foo\\\\bar", "foo", "bar", FALSE},
|
||||
{"C:\\foo\\bar", "C:\\foo", "bar", TRUE},
|
||||
{"C:\\foo\\bar\\", "C:\\foo", "bar", TRUE},
|
||||
{"C:\\foo\\bar\\baz", "C:\\foo\\bar", "baz", TRUE},
|
||||
{"\\foo\\bar", "\\foo", "bar", FALSE},
|
||||
{"C:\\foo/bar\\baz", "C:\\foo/bar", "baz", TRUE},
|
||||
{"C:/foo/bar/baz", "C:/foo/bar", "baz", TRUE},
|
||||
{"\\foo\\bar", "\\foo", "bar", TRUE},
|
||||
{"\\\\foo\\bar", "\\\\foo", "bar", TRUE},
|
||||
#else /* !WIN32 */
|
||||
#endif /* WIN32 */
|
||||
{"/", "/", "/", TRUE},
|
||||
{"//", "/", "/", TRUE},
|
||||
{"foo", ".", "foo", FALSE},
|
||||
@@ -815,7 +911,6 @@ static struct {
|
||||
{"/foo/bar", "/foo", "bar", TRUE},
|
||||
{"/foo/bar/", "/foo", "bar", TRUE},
|
||||
{"/foo/bar/baz", "/foo/bar", "baz", TRUE},
|
||||
#endif
|
||||
};
|
||||
|
||||
START_TEST(test_path_dirname)
|
||||
@@ -1207,6 +1302,11 @@ Suite *utils_suite_create()
|
||||
tcase_add_loop_test(tc, test_strreplace, 0, countof(strreplace_data));
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
tc = tcase_create("path_first/last_separator");
|
||||
tcase_add_loop_test(tc, test_path_first_separator, 0, countof(separator_data));
|
||||
tcase_add_loop_test(tc, test_path_last_separator, 0, countof(separator_data));
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
tc = tcase_create("path_dirname");
|
||||
tcase_add_loop_test(tc, test_path_dirname, 0, countof(path_data));
|
||||
suite_add_tcase(s, tc);
|
||||
|
||||
Reference in New Issue
Block a user