Skip to content

Commit 0836f3e

Browse files
author
Mikachu2333
authored
Merge branch 'dev' into feat/improve_comments
2 parents 60d2e1c + 4d81389 commit 0836f3e

2 files changed

Lines changed: 34 additions & 39 deletions

File tree

lib/xy.h

Lines changed: 25 additions & 39 deletions
Original file line numberDiff line numberDiff line change
@@ -984,60 +984,46 @@ xy_normalize_path (const char *path)
984984
{
985985
char *new = xy_str_strip (path); // 防止开发者多写了空白符
986986

987-
if (xy_on_windows)
987+
if (xy_str_start_with (new, "~"))
988988
{
989-
if (xy_str_start_with (new, "~/"))
990-
{
991-
// 或 %USERPROFILE%
992-
new = xy_strjoin (3, xy_os_home, "\\",
993-
xy_str_delete_prefix (new, "~/"));
994-
}
995-
new = xy_str_gsub (new, "/", "\\");
996-
}
997-
else
998-
{
999-
if (xy_str_start_with (new, "~/"))
1000-
{
1001-
new = xy_strjoin (3, xy_os_home, "/",
1002-
xy_str_delete_prefix (new, "~/"));
1003-
}
989+
new = xy_strjoin (3, xy_os_home, "/",
990+
xy_str_delete_prefix (new, "~"));
1004991
}
1005992

1006-
return new;
1007-
}
993+
new = xy_str_gsub (new, "\\", "/");
994+
new = xy_str_gsub (new, "//", "/");
1008995

996+
if (xy_on_windows)
997+
return xy_str_gsub (new, "/", "\\");
998+
else
999+
return new;
1000+
}
10091001

10101002
/**
1011-
* @warning 如果传入的路径以 `\` 或者 `/` 结尾,本函数将直接去除斜杠
1012-
* 并且不会进一步处理为传入路径的父目录
1013-
* @bug TODO 存在问题,后续修复
1003+
* @note 总是返回不含末尾斜杠的父目录路径
10141004
*/
10151005
static char *
10161006
xy_parent_dir (const char *path)
10171007
{
10181008
char *dir = xy_normalize_path (path);
1009+
dir = xy_str_gsub (dir, "\\", "/");
1010+
if (xy_str_end_with (dir, "/"))
1011+
dir = xy_str_delete_suffix (dir, "/");
1012+
10191013
char *last = NULL;
1020-
if (xy_on_windows)
1014+
1015+
last = strrchr (dir, '/');
1016+
if (!last)
10211017
{
1022-
last = strrchr (dir, '\\');
1023-
if (!last)
1024-
{
1025-
/* current dir */
1026-
return ".";
1027-
}
1028-
*last = '\0';
1018+
/* current dir */
1019+
return ".";
10291020
}
1021+
*last = '\0';
1022+
1023+
if (xy_on_windows)
1024+
return xy_str_gsub (dir, "/", "\\");
10301025
else
1031-
{
1032-
last = strrchr (dir, '/');
1033-
if (!last)
1034-
{
1035-
/* current dir */
1036-
return ".";
1037-
}
1038-
*last = '\0';
1039-
}
1040-
return dir;
1026+
return dir;
10411027
}
10421028

10431029

test/xy.c

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -104,6 +104,13 @@ main (int argc, char const *argv[])
104104
assert (xy_file_exist (xy_win_powershell_profile));
105105
assert (true == xy_file_exist (xy_win_powershellv5_profile));
106106
assert (xy_dir_exist ("C:\\Users"));
107+
assert_str (xy_normalize_path ("C:\\a bc\\def\\"), "C:\\a bc\\def\\");
108+
assert_str (xy_normalize_path ("a/b c/d"), "a\\b c\\d");
109+
assert_str (xy_normalize_path ("a/b c/d/"), "a\\b c\\d\\");
110+
assert_str (xy_parent_dir ("a/b c/d"), "a\\b c");
111+
assert_str (xy_parent_dir ("a/b c\\d/"), "a\\b c");
112+
assert_str (xy_parent_dir (xy_normalize_path ("~/")), "C:\\Users");
113+
assert_str (xy_parent_dir (xy_normalize_path ("~")), "C:\\Users");
107114
}
108115
else
109116
{
@@ -116,6 +123,8 @@ main (int argc, char const *argv[])
116123
assert (xy_file_exist (xy_bashrc));
117124
}
118125
assert (xy_dir_exist ("/etc"));
126+
assert_str (xy_normalize_path ("a\\b c\\d"), "a/b c/d");
127+
assert_str (xy_normalize_path ("a\\b c\\d\\"), "a/b c/d/");
119128
}
120129

121130
println (xy_normalize_path (" \n ~/haha/test/123 \n\r "));

0 commit comments

Comments
 (0)