refactor(lib): 重命名字符串替换函数、优化空指针处理

This commit is contained in:
Mikachu2333
2025-10-07 22:13:25 +08:00
parent 1ac8b892e2
commit 990b8d0df3

View File

@@ -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;
}