From 0b4215f257ead0f6214c6045b0d6d844c304085b Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 00:53:00 +0800 Subject: [PATCH 01/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20core.c=20=E4=B8=AD?= =?UTF-8?q?=E6=9C=AA=E6=89=8B=E5=8A=A8=E9=87=8A=E6=94=BE=E7=9A=84=E5=86=85?= =?UTF-8?q?=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/framework/core.c | 7 +++++++ 1 file changed, 7 insertions(+) diff --git a/src/framework/core.c b/src/framework/core.c index ff247c6..ddbb9b0 100644 --- a/src/framework/core.c +++ b/src/framework/core.c @@ -863,6 +863,9 @@ measure_speed_for_every_source (Source_t sources[], int size, double speed_recor char *curl_result = measure_speed_for_url (url); double speed = parse_and_say_curl_result (curl_result); speed_records[i] = speed; + + /* 释放 url 内存 */ + if (url) free (url); } else { @@ -1519,6 +1522,7 @@ chsrc_run_as_bash_file (const char *script_content) char *cmd = xy_2strcat ("bash ", tmpfile); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); remove (tmpfile); + free (tmpfile); /* 释放 tmpfile 路径内存 */ } @@ -1539,6 +1543,7 @@ chsrc_run_as_sh_file (const char *script_content) char *cmd = xy_2strcat ("sh ", tmpfile); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); remove (tmpfile); + free (tmpfile); } @@ -1558,6 +1563,7 @@ chsrc_run_as_pwsh_file (const char *script_content) char *cmd = xy_2strcat ("pwsh ", tmpfile); chsrc_run (cmd, RunOpt_Dont_Abort_On_Failure); remove (tmpfile); + free (tmpfile); } @@ -1849,6 +1855,7 @@ chsrc_overwrite_file (const char *str, const char *filename) size_t ret = fwrite (str, len, 1, f); if (ret != 1) { + fclose (f); char *msg = ENGLISH ? xy_2strcat ("Write failed to ", file) : xy_2strcat ("写入文件失败: ", file); chsrc_error2 (msg); From 5771a308a6cedfddd78ae95bb5c42bae2ba8207e Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 01:37:28 +0800 Subject: [PATCH 02/16] =?UTF-8?q?xy.h=20+=20`xy=5Fstr=5Fswap`=20=E5=B8=AE?= =?UTF-8?q?=E5=8A=A9=E9=87=8A=E6=94=BE=E5=86=85=E5=AD=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 21 +++++++++++++++++++++ 1 file changed, 21 insertions(+) diff --git a/lib/xy.h b/lib/xy.h index 08dda24..8582027 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -208,6 +208,27 @@ xy_malloc0 (size_t size) * String ******************************************************/ +/** + * @brief 替换字符串指针并自动释放旧内存 + * + * @param old_ptr 指向要被替换的字符串指针的指针 (char **) + * @param new_str 新的字符串指针 + */ +static inline void +xy_str_swap (char **old_ptr, char *new_str) +{ + if (old_ptr && *old_ptr) + { + char *temp = *old_ptr; + *old_ptr = new_str; + free (temp); + } + else if (old_ptr) + { + *old_ptr = new_str; + } +} + /** * @brief 将 str 中所有的 pat 字符串替换成 replace,返回一个全新的字符串;也可用作删除、缩小、扩张 * From b2a844b6763abe260dee35a6ddf518241f9a0300 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 01:53:27 +0800 Subject: [PATCH 03/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Ffile=5Fto=5Fstr?= =?UTF-8?q?=20=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/lib/xy.h b/lib/xy.h index 8582027..66316b4 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -745,7 +745,7 @@ xy_file_read (const char *path) buf[read_bytes] = '\0'; char *formatted_str = xy_str_gsub (buf, "\r\n", "\n"); - formatted_str = xy_str_gsub (formatted_str, "\r", "\n"); + xy_str_swap (&formatted_str, xy_str_gsub (formatted_str, "\r", "\n")); free (buf); From 437729a5cfe5b757cf628afaf234dfa0c3a399c3 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 01:54:13 +0800 Subject: [PATCH 04/16] =?UTF-8?q?=E6=94=B9=E8=BF=9B=20xy=5Ffile=5Fexist=20?= =?UTF-8?q?=E7=9A=84=E5=8F=98=E9=87=8F=E5=91=BD=E5=90=8D=EF=BC=8C=E4=BF=AE?= =?UTF-8?q?=E5=A4=8D=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 12 +++++++++--- 1 file changed, 9 insertions(+), 3 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 66316b4..4582631 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1164,13 +1164,19 @@ _xy_win_powershellv5_profile () static bool xy_file_exist (const char *path) { - const char *new_path = path; + char *expanded_path = NULL; + const char *check_path = path; + if (xy_str_start_with (path, "~")) { - new_path = xy_2strcat (xy_os_home, path + 1); + expanded_path = xy_2strcat (xy_os_home, path + 1); + check_path = expanded_path; } + // 0 即 F_OK - return (0==access (new_path, 0)) ? true : false; + bool result = (0 == access (check_path, 0)) ? true : false; + if (expanded_path) free (expanded_path); + return result; } /** From a3600bf05f8721767bc3116c9d320a661586e3d8 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 01:55:12 +0800 Subject: [PATCH 05/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Fdir=5Fexist=20?= =?UTF-8?q?=E7=9A=84=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 21 ++++++++++++--------- 1 file changed, 12 insertions(+), 9 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 4582631..bbbd450 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1186,12 +1186,14 @@ xy_file_exist (const char *path) static bool xy_dir_exist (const char *path) { + char *allocated_dir = NULL; const char *dir = path; if (xy.on_windows) { if (xy_str_start_with (path, "~")) { - dir = xy_2strcat (xy_os_home, path + 1); + allocated_dir = xy_2strcat (xy_os_home, path + 1); + dir = allocated_dir; } } @@ -1201,29 +1203,30 @@ xy_dir_exist (const char *path) // 也可以用 opendir() #include DWORD attr = GetFileAttributesA (dir); + bool result = false; if (attr == INVALID_FILE_ATTRIBUTES) { // Q: 我们应该报错吗? - return false; + result = false; } else if (attr & FILE_ATTRIBUTE_DIRECTORY) { - return true; + result = true; } else { - return false; + result = false; } + if (allocated_dir) free (allocated_dir); + return result; #endif } else { int status = system (xy_2strcat ("test -d ", dir)); - - if (0==status) - return true; - else - return false; + bool result = (0==status); + if (allocated_dir) free (allocated_dir); + return result; } return false; From 2a9d13bfc83132b44f4098ec9e314b7a05646666 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 01:56:15 +0800 Subject: [PATCH 06/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Fnormalize=5Fpat?= =?UTF-8?q?h=20=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index bbbd450..8b0592f 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1249,13 +1249,14 @@ xy_normalize_path (const char *path) if (xy_str_start_with (new, "~")) { - new = xy_2strcat (xy_os_home, xy_str_delete_prefix (new, "~")); + xy_str_swap (&new, xy_2strcat (xy_os_home, xy_str_delete_prefix (new, "~"))); } if (xy.on_windows) - return xy_str_gsub (new, "/", "\\"); - else - return new; + { + xy_str_swap (&new, xy_str_gsub (new, "/", "\\")); + } + return new; } From e64778911a9f90579a8f638eca019168d16896cf Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 01:57:18 +0800 Subject: [PATCH 07/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Fparent=5Fdir=20?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 11 ++++++----- 1 file changed, 6 insertions(+), 5 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 8b0592f..52fc421 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1273,10 +1273,10 @@ xy_parent_dir (const char *path) char *dir = xy_normalize_path (path); /* 不管是否为Windows,全部统一使用 / 作为路径分隔符,方便后续处理 */ - dir = xy_str_gsub (dir, "\\", "/"); + xy_str_swap (&dir, xy_str_gsub (dir, "\\", "/")); if (xy_str_end_with (dir, "/")) - dir = xy_str_delete_suffix (dir, "/"); + xy_str_swap (&dir, xy_str_delete_suffix (dir, "/")); char *last = NULL; @@ -1290,9 +1290,10 @@ xy_parent_dir (const char *path) /* Windows上重新使用 \ 作为路径分隔符 */ if (xy.on_windows) - return xy_str_gsub (dir, "/", "\\"); - else - return dir; + { + xy_str_swap (&dir, xy_str_gsub (dir, "/", "\\")); + } + return dir; } From 547931c4e4ebcfebf3cc18adfbb72b2e4555d6f9 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 01:57:47 +0800 Subject: [PATCH 08/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Fdetect=5Fos=20?= =?UTF-8?q?=E5=86=85=E5=AD=98=E6=B3=84=E9=9C=B2?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 5 +++-- 1 file changed, 3 insertions(+), 2 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 52fc421..1a916a6 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1351,9 +1351,10 @@ xy_detect_os () fp = popen ("uname -s", "r"); if (!fp) { - if (opendir ("/etc/rc.d")) + DIR *bsd_dir = opendir ("/etc/rc.d"); + if (bsd_dir) { - closedir (d); + closedir (bsd_dir); xy.on_bsd = true; return; } From 5588beb5422ac79212ac28fb66064123082ce8cc Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 03:00:24 +0800 Subject: [PATCH 09/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Fstr=5Fdelete=5F?= =?UTF-8?q?prefix=20=E8=AF=AF=E9=87=8A=E6=94=BE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 9 ++++----- 1 file changed, 4 insertions(+), 5 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 1a916a6..3b22431 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -583,12 +583,11 @@ xy_str_delete_prefix (const char *str, const char *prefix) { char *new = xy_strdup (str); bool yes = xy_str_start_with (str, prefix); - if (!yes) - return new; - + if (!yes) return new; size_t len = strlen (prefix); - char *cur = new + len; - return cur; + char *ret = xy_strdup (new + len); + free (new); + return ret; } /** From 41783b8531f5d2c50eb69be9f4553054f9cef475 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 03:00:59 +0800 Subject: [PATCH 10/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Fstr=5Fstrip=20?= =?UTF-8?q?=E8=AF=AF=E9=87=8A=E6=94=BE=E7=9A=84=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 28 +++++++++++++++------------- 1 file changed, 15 insertions(+), 13 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 3b22431..ff21a82 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -614,23 +614,25 @@ xy_str_delete_suffix (const char *str, const char *suffix) static char * xy_str_strip (const char *str) { - char *new = xy_strdup (str); + if (!str) + return xy_strdup (""); - while (strchr ("\n\r\v\t\f ", new[0])) - { - new += 1; - } + const char *start = str; + while (*start && strchr ("\n\r\v\t\f ", *start)) + start++; - size_t len = strlen (new); + if ('\0' == *start) + return xy_strdup (""); - char *last = new + len - 1; + const char *end = start + strlen (start) - 1; + while (end >= start && strchr ("\n\r\v\t\f ", *end)) + end--; - while (strchr ("\n\r\v\t\f ", *last)) - { - *last = '\0'; - last -= 1; - } - return new; + size_t len = (size_t) (end - start + 1); + char *ret = xy_malloc0 (len + 1); + memcpy (ret, start, len); + ret[len] = '\0'; + return ret; } typedef struct From 948d55185f7d6ce5aa654c2ad2a9d0d00cc19fd6 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Mon, 6 Oct 2025 03:01:53 +0800 Subject: [PATCH 11/16] =?UTF-8?q?=E4=BF=AE=E5=A4=8D=20xy=5Fnormalize=5Fpat?= =?UTF-8?q?h=20=E5=86=85=E5=AD=98=E6=B3=84=E6=BC=8F=E9=97=AE=E9=A2=98?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 5 ++++- 1 file changed, 4 insertions(+), 1 deletion(-) diff --git a/lib/xy.h b/lib/xy.h index ff21a82..f7d1325 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1250,7 +1250,10 @@ xy_normalize_path (const char *path) if (xy_str_start_with (new, "~")) { - xy_str_swap (&new, xy_2strcat (xy_os_home, xy_str_delete_prefix (new, "~"))); + char *tmp = xy_str_delete_prefix (new, "~"); + char *joined = xy_2strcat (xy_os_home, tmp); + free (tmp); + xy_str_swap (&new, joined); } if (xy.on_windows) From 6f312a66c0f436febda356b15abe2ce42d44f53e Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Tue, 7 Oct 2025 18:41:15 +0800 Subject: [PATCH 12/16] =?UTF-8?q?fix(lib):=20=E4=BC=98=E5=8C=96=E5=AD=97?= =?UTF-8?q?=E7=AC=A6=E4=B8=B2=E5=89=8D=E7=BC=80=E5=88=A0=E9=99=A4=E9=80=BB?= =?UTF-8?q?=E8=BE=91=E5=B9=B6=E4=BF=AE=E5=A4=8D=E5=86=85=E5=AD=98=E6=B3=84?= =?UTF-8?q?=E6=BC=8F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 重构 `xy_str_delete_prefix` 函数以避免不必要的内存分配和释放, 直接使用原字符串进行处理。同时修复 `xy_dir_exist` 函数中 system 调用后未释放临时命令字符串的内存泄漏问题。 --- lib/xy.h | 19 ++++++++++++------- 1 file changed, 12 insertions(+), 7 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index f7d1325..77cd60f 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -581,13 +581,16 @@ xy_str_start_with (const char *str, const char *prefix) static char * xy_str_delete_prefix (const char *str, const char *prefix) { - char *new = xy_strdup (str); bool yes = xy_str_start_with (str, prefix); - if (!yes) return new; - size_t len = strlen (prefix); - char *ret = xy_strdup (new + len); - free (new); - return ret; + if (!yes) + { + return xy_strdup(str); + } + else + { + size_t len = strlen (prefix); + return xy_strdup (str + len); + } } /** @@ -1224,7 +1227,9 @@ xy_dir_exist (const char *path) } else { - int status = system (xy_2strcat ("test -d ", dir)); + char *tmp_cmd = xy_2strcat ("test -d ", dir); + int status = system (tmp_cmd); + free (tmp_cmd); bool result = (0==status); if (allocated_dir) free (allocated_dir); return result; From 16602d0e7f8a90b6755d5a79f6ac4fc323401338 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Tue, 7 Oct 2025 18:41:33 +0800 Subject: [PATCH 13/16] =?UTF-8?q?refactor(lib):=20=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=9B=BF=E6=8D=A2=E5=87=BD?= =?UTF-8?q?=E6=95=B0?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit 将函数名从 `xy_str_swap` 更改为 `xy_str_replace`,以更准确地反映其实际功能。 该函数并非简单交换两个字符串,而是用新字符串替换旧字符串的内容。 此更改涉及多个文件中的调用点,包括路径处理和文件读取相关的逻辑。 --- lib/xy.h | 14 +++++++------- 1 file changed, 7 insertions(+), 7 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 77cd60f..9e4e822 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_swap (char **old_ptr, char *new_str) +xy_str_replace (char **old_ptr, char *new_str) { if (old_ptr && *old_ptr) { @@ -749,7 +749,7 @@ xy_file_read (const char *path) buf[read_bytes] = '\0'; char *formatted_str = xy_str_gsub (buf, "\r\n", "\n"); - xy_str_swap (&formatted_str, xy_str_gsub (formatted_str, "\r", "\n")); + xy_str_replace (&formatted_str, xy_str_gsub (formatted_str, "\r", "\n")); free (buf); @@ -1258,12 +1258,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_swap (&new, joined); + xy_str_replace (&new, joined); } if (xy.on_windows) { - xy_str_swap (&new, xy_str_gsub (new, "/", "\\")); + xy_str_replace (&new, xy_str_gsub (new, "/", "\\")); } return new; } @@ -1282,10 +1282,10 @@ xy_parent_dir (const char *path) char *dir = xy_normalize_path (path); /* 不管是否为Windows,全部统一使用 / 作为路径分隔符,方便后续处理 */ - xy_str_swap (&dir, xy_str_gsub (dir, "\\", "/")); + xy_str_replace (&dir, xy_str_gsub (dir, "\\", "/")); if (xy_str_end_with (dir, "/")) - xy_str_swap (&dir, xy_str_delete_suffix (dir, "/")); + xy_str_replace (&dir, xy_str_delete_suffix (dir, "/")); char *last = NULL; @@ -1300,7 +1300,7 @@ xy_parent_dir (const char *path) /* Windows上重新使用 \ 作为路径分隔符 */ if (xy.on_windows) { - xy_str_swap (&dir, xy_str_gsub (dir, "/", "\\")); + xy_str_replace (&dir, xy_str_gsub (dir, "/", "\\")); } return dir; } From c44377f2d981628d092300520f33479b49f2535a Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Tue, 7 Oct 2025 19:05:51 +0800 Subject: [PATCH 14/16] =?UTF-8?q?build(lib):=20=E6=9B=B4=E6=96=B0=E7=89=88?= =?UTF-8?q?=E6=9C=AC=E5=8F=B7=E5=92=8C=E6=9C=80=E5=90=8E=E4=BF=AE=E6=94=B9?= =?UTF-8?q?=E6=97=A5=E6=9C=9F?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 4 ++-- 1 file changed, 2 insertions(+), 2 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 9e4e822..d4caedb 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -9,7 +9,7 @@ * | BingChunMoLi * | * Created On : <2023-08-28> - * Last Modified : <2025-10-06> + * Last Modified : <2025-10-07> * * * xy: 襄阳、咸阳 @@ -23,7 +23,7 @@ #ifndef XY_H #define XY_H -#define _XY_Version "v0.2.1.0-2025/10/06" +#define _XY_Version "v0.2.1.1-2025/10/07" #define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h" #define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h" From 1ac8b892e2a4c99f94fcd7b149cbff0b48cdbd89 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Tue, 7 Oct 2025 19:10:59 +0800 Subject: [PATCH 15/16] =?UTF-8?q?style(lib):=20=E8=B0=83=E6=95=B4=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E5=8F=82=E6=95=B0=E6=8B=AC=E5=8F=B7=E5=89=8D=E7=A9=BA?= =?UTF-8?q?=E6=A0=BC?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 12 ++++++------ 1 file changed, 6 insertions(+), 6 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index d4caedb..f8c12c1 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -491,15 +491,15 @@ xy_streql (const char *str1, const char *str2) } static bool -xy_streql_ic(const char *str1, const char *str2) +xy_streql_ic (const char *str1, const char *str2) { if (NULL == str1 || NULL == str2) { return false; } - size_t len1 = strlen(str1); - size_t len2 = strlen(str2); + size_t len1 = strlen (str1); + size_t len2 = strlen (str2); if (len1 != len2) { return false; @@ -507,7 +507,7 @@ xy_streql_ic(const char *str1, const char *str2) for (size_t i = 0; i < len1; i++) { - if (tolower(str1[i]) != tolower(str2[i])) + if (tolower (str1[i]) != tolower (str2[i])) { return false; } @@ -584,7 +584,7 @@ xy_str_delete_prefix (const char *str, const char *prefix) bool yes = xy_str_start_with (str, prefix); if (!yes) { - return xy_strdup(str); + return xy_strdup (str); } else { @@ -1329,7 +1329,7 @@ xy_detect_os () if (fp) { char buf[256] = {0}; - fread (buf, 1, sizeof(buf) - 1, fp); + fread (buf, 1, sizeof (buf) - 1, fp); fclose (fp); if (strstr (buf, "Android")) { From 990b8d0df380536d63fe634c9ffaadba828e5826 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Tue, 7 Oct 2025 22:13:25 +0800 Subject: [PATCH 16/16] =?UTF-8?q?refactor(lib):=20=E9=87=8D=E5=91=BD?= =?UTF-8?q?=E5=90=8D=E5=AD=97=E7=AC=A6=E4=B8=B2=E6=9B=BF=E6=8D=A2=E5=87=BD?= =?UTF-8?q?=E6=95=B0=E3=80=81=E4=BC=98=E5=8C=96=E7=A9=BA=E6=8C=87=E9=92=88?= =?UTF-8?q?=E5=A4=84=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; }