Skip to content

Commit 066ac19

Browse files
committed
Refactor xy_ptr_replace()
1 parent 985a634 commit 066ac19

File tree

1 file changed

+20
-24
lines changed

1 file changed

+20
-24
lines changed

lib/xy.h

Lines changed: 20 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
* | BingChunMoLi <[email protected]>
1010
* |
1111
* Created On : <2023-08-28>
12-
* Last Modified : <2025-10-15>
12+
* Last Modified : <2025-10-28>
1313
*
1414
*
1515
* xy: 襄阳、咸阳
@@ -23,7 +23,7 @@
2323
#ifndef XY_H
2424
#define XY_H
2525

26-
#define _XY_Version "v0.2.1.1-2025/10/07"
26+
#define _XY_Version "v0.2.2.0-2025/10/28"
2727
#define _XY_Maintain_URL "https://github.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2828
#define _XY_Maintain_URL2 "https://gitee.com/RubyMetric/chsrc/blob/dev/lib/xy.h"
2929

@@ -213,33 +213,31 @@ xy_malloc0 (size_t size)
213213
}
214214

215215

216-
/******************************************************
217-
* String
218-
******************************************************/
219-
220216
/**
221-
* @brief 替换字符串指针并自动释放旧内存
217+
* @brief 替换指针内容并自动释放旧内存
222218
*
223-
* @param old_ptr 指向要被替换的字符串指针的指针 (char **)
224-
* @param new_str 新的字符串指针
219+
* @param pptr 指向要被替换内存区域的指针的指针
220+
* *pptr 可为 NULL
221+
* @param new_mem 新的内存区域
225222
*/
226223
static inline void
227-
xy_ptr_replace (char **old_ptr, char *new_str)
224+
xy_ptr_replace (char **pptr, char *new_mem)
228225
{
229-
if (old_ptr && *old_ptr)
230-
{
231-
char *temp = *old_ptr;
232-
*old_ptr = new_str;
233-
free (temp);
234-
}
235-
else if (old_ptr)
236-
{
237-
*old_ptr = new_str;
238-
}
226+
xy_cant_be_null (pptr);
227+
228+
if (*pptr)
229+
free (*pptr);
230+
231+
*pptr = new_mem;
239232
}
240233

234+
235+
/******************************************************
236+
* String
237+
******************************************************/
238+
241239
/**
242-
* @brief 将 str 中所有的 pat 字符串替换成 replace,返回一个全新的字符串;也可用作删除、缩小、扩张
240+
* @brief 将 str 中所有的 pat 字符串替换成 replace,返回一个全新的字符串
243241
*
244242
* @flavor Ruby: String#gsub
245243
*
@@ -626,9 +624,7 @@ xy_str_delete_suffix (const char *str, const char *suffix)
626624
static char *
627625
xy_str_strip (const char *str)
628626
{
629-
if (!str)
630-
xy_cant_be_null (str);
631-
627+
xy_cant_be_null (str);
632628

633629
const char *start = str;
634630
while (*start && strchr ("\n\r\v\t\f ", *start))

0 commit comments

Comments
 (0)