From 990b8d0df380536d63fe634c9ffaadba828e5826 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Tue, 7 Oct 2025 22:13:25 +0800 Subject: [PATCH] =?UTF-8?q?refactor(lib):=20=E9=87=8D=E5=91=BD=E5=90=8D?= =?UTF-8?q?=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=9B=BF=E6=8D=A2=E5=87=BD=E6=95=B0?= =?UTF-8?q?=E3=80=81=E4=BC=98=E5=8C=96=E7=A9=BA=E6=8C=87=E9=92=88=E5=A4=84?= =?UTF-8?q?=E7=90=86?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 17 +++++++++-------- 1 file changed, 9 insertions(+), 8 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index f8c12c1..e0289a4 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -215,7 +215,7 @@ xy_malloc0 (size_t size) * @param new_str 新的字符串指针 */ static inline void -xy_str_replace (char **old_ptr, char *new_str) +xy_ptr_replace (char **old_ptr, char *new_str) { if (old_ptr && *old_ptr) { @@ -618,7 +618,8 @@ static char * xy_str_strip (const char *str) { if (!str) - return xy_strdup (""); + xy_cant_be_null (str); + const char *start = str; while (*start && strchr ("\n\r\v\t\f ", *start)) @@ -749,7 +750,7 @@ xy_file_read (const char *path) buf[read_bytes] = '\0'; char *formatted_str = xy_str_gsub (buf, "\r\n", "\n"); - xy_str_replace (&formatted_str, xy_str_gsub (formatted_str, "\r", "\n")); + xy_ptr_replace (&formatted_str, xy_str_gsub (formatted_str, "\r", "\n")); free (buf); @@ -1258,12 +1259,12 @@ xy_normalize_path (const char *path) char *tmp = xy_str_delete_prefix (new, "~"); char *joined = xy_2strcat (xy_os_home, tmp); free (tmp); - xy_str_replace (&new, joined); + xy_ptr_replace (&new, joined); } if (xy.on_windows) { - xy_str_replace (&new, xy_str_gsub (new, "/", "\\")); + xy_ptr_replace (&new, xy_str_gsub (new, "/", "\\")); } return new; } @@ -1282,10 +1283,10 @@ xy_parent_dir (const char *path) char *dir = xy_normalize_path (path); /* 不管是否为Windows,全部统一使用 / 作为路径分隔符,方便后续处理 */ - xy_str_replace (&dir, xy_str_gsub (dir, "\\", "/")); + xy_ptr_replace (&dir, xy_str_gsub (dir, "\\", "/")); if (xy_str_end_with (dir, "/")) - xy_str_replace (&dir, xy_str_delete_suffix (dir, "/")); + xy_ptr_replace (&dir, xy_str_delete_suffix (dir, "/")); char *last = NULL; @@ -1300,7 +1301,7 @@ xy_parent_dir (const char *path) /* Windows上重新使用 \ 作为路径分隔符 */ if (xy.on_windows) { - xy_str_replace (&dir, xy_str_gsub (dir, "/", "\\")); + xy_ptr_replace (&dir, xy_str_gsub (dir, "/", "\\")); } return dir; }