xy.h + xy_str_swap 帮助释放内存

This commit is contained in:
Mikachu2333
2025-10-06 01:37:28 +08:00
parent 0b4215f257
commit 5771a308a6

View File

@@ -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返回一个全新的字符串也可用作删除、缩小、扩张
*