1919#ifndef XY_H
2020#define XY_H
2121
22- #define _XY_Version "v0.1.6.0 -2025/08/18"
22+ #define _XY_Version "v0.1.6.1 -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
@@ -165,7 +165,13 @@ xy_malloc0 (size_t size)
165165 ******************************************************/
166166
167167/**
168- * 将str中所有的pat字符串替换成replace,返回一个全新的字符串
168+ * @brief 将 str 中所有的 pat 字符串替换成 replace,返回一个全新的字符串;也可用作删除、缩小、扩张
169+ *
170+ * @param str 原字符串
171+ * @param pat 要替换的字符串
172+ * @param replace 替换成的字符串
173+ *
174+ * @return 替换后的新字符串
169175 */
170176static char *
171177xy_str_gsub (const char * str , const char * pat , const char * replace )
@@ -231,6 +237,15 @@ xy_2strjoin (const char *str1, const char *str2)
231237 return ret ;
232238}
233239
240+
241+ /**
242+ * @brief 将多个字符串连接成一个字符串
243+ *
244+ * @param count 连接的字符串数量
245+ * @param ... 连接的字符串
246+ *
247+ * @return 拼接的新字符串
248+ */
234249static char *
235250xy_strjoin (unsigned int count , ...)
236251{
@@ -285,6 +300,14 @@ xy_strjoin (unsigned int count, ...)
285300 return ret ;
286301}
287302
303+
304+ /**
305+ * @brief 复制一个字符串,返回复制的新字符串
306+ *
307+ * @param str 要复制的字符串
308+ *
309+ * @return 复制的新字符串
310+ */
288311static char *
289312xy_strdup (const char * str )
290313{
@@ -684,14 +707,16 @@ _xy_log_brkt (int level, const char *prompt1, const char *prompt2, const char *c
684707/******************************************************
685708 * System
686709 ******************************************************/
710+
687711/**
688- * 执行cmd,返回某行输出结果,并对已经遍历过的行执行iter_func
712+ * @brief 执行cmd,返回某行输出结果,并对已经遍历过的行执行iter_func
689713 *
690714 * @param cmd 要执行的命令
691715 * @param n 指定命令执行输出的结果行中的某一行,0 表示最后一行,n (n>0) 表示第n行
692- * 该函数会返回这一行的内容
693716 * @param iter_func 对遍历时经过的行的内容,进行函数调用
694717 *
718+ * @return 该函数会返回 参数 n 指定的该行的内容
719+ *
695720 * @note 返回的字符串最后面一般有换行符号
696721 *
697722 * 由于目标行会被返回出来,所以 iter_func() 并不执行目标行,只会执行遍历过的行
@@ -745,8 +770,7 @@ xy_run (const char *cmd, unsigned long n)
745770 ******************************************************/
746771
747772 /**
748- * 该函数同 just 中的 os_family(),只区分 windows, unix
749- *
773+ * @note 该函数同 just 中的 os_family(),只区分 windows, unix
750774 * @return 返回 "windows" 或 "unix"
751775 */
752776#define xy_os_family _xy_os_family ()
@@ -761,7 +785,7 @@ _xy_os_family ()
761785
762786
763787/**
764- * 该函数返回所在 os family 的对应字符串
788+ * @brief 该函数返回所在 os family 的对应字符串
765789 */
766790static const char *
767791xy_os_depend_str (const char * str_for_win , const char * str_for_unix )
@@ -773,6 +797,12 @@ xy_os_depend_str (const char *str_for_win, const char *str_for_unix)
773797}
774798
775799
800+ /**
801+ * @brief 返回当前操作系统的 HOME 目录
802+ *
803+ * @note Windows 上返回 %USERPROFILE%,Linux 等返回 $HOME
804+ * @warning Windows 上要警惕 Documents 等目录被移动位置的情况,避免使用本函数的 HOME 路径直接拼接 Documents,应参考 _xy_win_documents() 的实现
805+ */
776806#define xy_os_home _xy_os_home ()
777807static char *
778808_xy_os_home ()
@@ -786,6 +816,12 @@ _xy_os_home ()
786816}
787817
788818
819+ /**
820+ * @brief 返回 Windows 上的 Documents 目录
821+ *
822+ * @note 警告,不可使用 HOME 目录直接拼接,若用户移动了 Documents,将导致错误
823+ * @warning 非 Windows 返回 NULL
824+ */
789825static char *
790826_xy_win_documents ()
791827{
@@ -806,7 +842,11 @@ _xy_win_documents ()
806842#define xy_win_powershell_profile _xy_win_powershell_profile ()
807843#define xy_win_powershellv5_profile _xy_win_powershellv5_profile ()
808844
809- // 更新 PowerShell 配置文件路径函数
845+ /**
846+ * @brief 返回 Windows 上 pwsh (>=v5) 的配置文件路径
847+ *
848+ * @warning 非 Windows 返回 NULL
849+ */
810850static char *
811851_xy_win_powershell_profile ()
812852{
@@ -822,6 +862,11 @@ _xy_win_powershell_profile ()
822862}
823863
824864
865+ /**
866+ * @brief 返回 Windows 上自带的 powershell (v5) 的配置文件路径
867+ *
868+ * @warning 非 Windows 返回 NULL
869+ */
825870static char *
826871_xy_win_powershellv5_profile ()
827872{
@@ -856,8 +901,8 @@ xy_file_exist (const char *path)
856901}
857902
858903/**
859- * @note xy_file_exist() 和 xy_dir_exist() 两个函数在所有平台默认都支持使用 '~',
860- * 但实现中都没有调用 xy_normalize_path(),以防万一,调用前可能需要用户手动调用它
904+ * @note ` xy_file_exist()` 和 ` xy_dir_exist()` 两个函数在所有平台默认都支持使用 '~',
905+ * 但实现中都没有调用 ` xy_normalize_path()` ,以防万一,调用前可能需要用户手动调用它
861906 */
862907static bool
863908xy_dir_exist (const char * path )
@@ -923,6 +968,11 @@ xy_normalize_path (const char *path)
923968 new = xy_2strjoin (xy_os_home , xy_str_delete_prefix (new , "~" ));
924969 }
925970
971+ // 在特殊情况下上面的替换最终可能导致形如 `/foo//bar/` 之类的东西
972+ // 建议进行去重处理以防万一
973+ //new = xy_str_gsub (new, "\\", "/");
974+ //new = xy_str_gsub (new, "//", "/");
975+
926976 if (xy_on_windows )
927977 return xy_str_gsub (new , "/" , "\\" );
928978 else
0 commit comments