mirror of
https://github.com/RubyMetric/chsrc
synced 2026-01-12 07:19:01 +08:00
refactor(lib): 重命名字符串替换函数、优化空指针处理
This commit is contained in:
17
lib/xy.h
17
lib/xy.h
@@ -215,7 +215,7 @@ xy_malloc0 (size_t size)
|
|||||||
* @param new_str 新的字符串指针
|
* @param new_str 新的字符串指针
|
||||||
*/
|
*/
|
||||||
static inline void
|
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)
|
if (old_ptr && *old_ptr)
|
||||||
{
|
{
|
||||||
@@ -618,7 +618,8 @@ static char *
|
|||||||
xy_str_strip (const char *str)
|
xy_str_strip (const char *str)
|
||||||
{
|
{
|
||||||
if (!str)
|
if (!str)
|
||||||
return xy_strdup ("");
|
xy_cant_be_null (str);
|
||||||
|
|
||||||
|
|
||||||
const char *start = str;
|
const char *start = str;
|
||||||
while (*start && strchr ("\n\r\v\t\f ", *start))
|
while (*start && strchr ("\n\r\v\t\f ", *start))
|
||||||
@@ -749,7 +750,7 @@ xy_file_read (const char *path)
|
|||||||
buf[read_bytes] = '\0';
|
buf[read_bytes] = '\0';
|
||||||
|
|
||||||
char *formatted_str = xy_str_gsub (buf, "\r\n", "\n");
|
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);
|
free (buf);
|
||||||
|
|
||||||
@@ -1258,12 +1259,12 @@ xy_normalize_path (const char *path)
|
|||||||
char *tmp = xy_str_delete_prefix (new, "~");
|
char *tmp = xy_str_delete_prefix (new, "~");
|
||||||
char *joined = xy_2strcat (xy_os_home, tmp);
|
char *joined = xy_2strcat (xy_os_home, tmp);
|
||||||
free (tmp);
|
free (tmp);
|
||||||
xy_str_replace (&new, joined);
|
xy_ptr_replace (&new, joined);
|
||||||
}
|
}
|
||||||
|
|
||||||
if (xy.on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
xy_str_replace (&new, xy_str_gsub (new, "/", "\\"));
|
xy_ptr_replace (&new, xy_str_gsub (new, "/", "\\"));
|
||||||
}
|
}
|
||||||
return new;
|
return new;
|
||||||
}
|
}
|
||||||
@@ -1282,10 +1283,10 @@ xy_parent_dir (const char *path)
|
|||||||
char *dir = xy_normalize_path (path);
|
char *dir = xy_normalize_path (path);
|
||||||
|
|
||||||
/* 不管是否为Windows,全部统一使用 / 作为路径分隔符,方便后续处理 */
|
/* 不管是否为Windows,全部统一使用 / 作为路径分隔符,方便后续处理 */
|
||||||
xy_str_replace (&dir, xy_str_gsub (dir, "\\", "/"));
|
xy_ptr_replace (&dir, xy_str_gsub (dir, "\\", "/"));
|
||||||
|
|
||||||
if (xy_str_end_with (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;
|
char *last = NULL;
|
||||||
|
|
||||||
@@ -1300,7 +1301,7 @@ xy_parent_dir (const char *path)
|
|||||||
/* Windows上重新使用 \ 作为路径分隔符 */
|
/* Windows上重新使用 \ 作为路径分隔符 */
|
||||||
if (xy.on_windows)
|
if (xy.on_windows)
|
||||||
{
|
{
|
||||||
xy_str_replace (&dir, xy_str_gsub (dir, "/", "\\"));
|
xy_ptr_replace (&dir, xy_str_gsub (dir, "/", "\\"));
|
||||||
}
|
}
|
||||||
return dir;
|
return dir;
|
||||||
}
|
}
|
||||||
|
|||||||
Reference in New Issue
Block a user