99 * | Mikachu2333 <[email protected] > 1010 * |
1111 * Created On : <2023-08-28>
12- * Last Modified : <2025-08-17 >
12+ * Last Modified : <2025-08-18 >
1313 *
1414 * xy: 襄阳、咸阳
1515 * Corss-Platform C11 utilities for CLI applications in mixed
1919#ifndef XY_H
2020#define XY_H
2121
22- #define _XY_Version "v0.1.5.5 -2025/08/17 "
22+ #define _XY_Version "v0.1.6.0 -2025/08/18 "
2323#define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2424#define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2525
@@ -904,37 +904,47 @@ xy_dir_exist (const char *path)
904904}
905905
906906/**
907- * 1. 删除路径左右两边多出来的空白符
908- * 2. 将 ~/ 转换为绝对路径
907+ * @brief 规范化路径
908+ *
909+ * @details
910+ * 1. 删除路径左右两边多出来的空白符
911+ * - 防止通过间接方式得到的路径包含了空白字符 (如 grep 出来的结果)
912+ * - 防止维护者多写了空白字符
913+ * 2. 将 ~ 转换为绝对路径
914+ * 3. 在Windows上,使用标准的 \ 作为路径分隔符
909915 */
910916static char *
911917xy_normalize_path (const char * path )
912918{
913- char * new = xy_str_strip (path ); // 防止开发者多写了空白符
919+ char * new = xy_str_strip (path );
914920
915921 if (xy_str_start_with (new , "~" ))
916922 {
917- new = xy_strjoin (3 , xy_os_home , "/" ,
918- xy_str_delete_prefix (new , "~" ));
923+ new = xy_2strjoin (xy_os_home , xy_str_delete_prefix (new , "~" ));
919924 }
920925
921- new = xy_str_gsub (new , "\\" , "/" );
922- new = xy_str_gsub (new , "//" , "/" );
923-
924926 if (xy_on_windows )
925927 return xy_str_gsub (new , "/" , "\\" );
926928 else
927929 return new ;
928930}
929931
932+
930933/**
931- * @note 总是返回不含末尾斜杠的父目录路径
934+ * @brief 返回一个路径的父目录名
935+ *
936+ * @note
937+ * - 返回的是真正的 "目录名" (就像文件名一样),而不是 "路径",所以一定是不含末尾斜杠的
938+ * - 在Windows上,使用标准的 \ 作为路径分隔符
932939 */
933940static char *
934941xy_parent_dir (const char * path )
935942{
936943 char * dir = xy_normalize_path (path );
944+
945+ /* 不管是否为Windows,全部统一使用 / 作为路径分隔符,方便后续处理 */
937946 dir = xy_str_gsub (dir , "\\" , "/" );
947+
938948 if (xy_str_end_with (dir , "/" ))
939949 dir = xy_str_delete_suffix (dir , "/" );
940950
@@ -943,11 +953,12 @@ xy_parent_dir (const char *path)
943953 last = strrchr (dir , '/' );
944954 if (!last )
945955 {
946- /* current dir */
956+ /* 路径中没有一个 / 是很奇怪的,我们直接返回 . 作为当前目录 */
947957 return "." ;
948958 }
949959 * last = '\0' ;
950960
961+ /* Windows上重新使用 \ 作为路径分隔符 */
951962 if (xy_on_windows )
952963 return xy_str_gsub (dir , "/" , "\\" );
953964 else
0 commit comments