mirror of
https://github.com/RubyMetric/chsrc
synced 2025-11-18 23:57:43 +08:00
xy.h + xy_str_swap 帮助释放内存
This commit is contained in:
21
lib/xy.h
21
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,返回一个全新的字符串;也可用作删除、缩小、扩张
|
||||
*
|
||||
|
||||
Reference in New Issue
Block a user