Skip to content

Commit

Permalink
Merge pull request #92 from MtFmT-Lib/str_replace
Browse files Browse the repository at this point in the history
ADD: 字符串的retain
  • Loading branch information
XiangYyang authored Aug 6, 2023
2 parents 564e5ea + 5c66401 commit 972866f
Show file tree
Hide file tree
Showing 6 changed files with 388 additions and 152 deletions.
106 changes: 37 additions & 69 deletions inc/mm_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -46,43 +46,6 @@ typedef enum tagMStringReplaceOption
MStringReplaceOption_All,
} MStringReplaceOption;

/**
* @brief 字符串替换信息
*
*/
typedef struct tagMStringReplaceTarget
{
/**
* @brief 替换选项
*
*/
MStringReplaceOption opt;

/**
* @brief 模式串
*
*/
const char* substr;

/**
* @brief 模式串长度
*
*/
usize_t substr_len;

/**
* @brief 替换为的目标字串
*
*/
const char* replace_to;

/**
* @brief 目标串长度
*
*/
usize_t replace_to_len;
} MStringReplaceTarget;

/**
* @brief 字符串匹配信息
*
Expand Down Expand Up @@ -202,7 +165,8 @@ mstr_create(MString* str, const char* content);
* @param[out] str: 目标字符串
* @param[inout] other: 需要移动的字符串
*
* @attention 该函数转移other的所有权, other不需要再次释放
* @attention 该函数转移other的所有权, other不需要再次释放,
* 且会释放掉str
*/
MSTR_EXPORT_API(void)
mstr_move_from(MString* str, MString* other);
Expand All @@ -214,11 +178,21 @@ mstr_move_from(MString* str, MString* other);
* @param[in] other: 需要移动的字符串
*
* @attention 该函数会清空释放原有的字符串并创建一个新的,
* other需要再次释放
* other需要再次释放, 且会释放掉原有的str
*/
MSTR_EXPORT_API(mstr_result_t)
mstr_copy_from(MString* str, const MString* other);

/**
* @brief 保留 sz 个char数的内存区
*
* @param[inout] str: 字符串
* @param[in] new_size: 需要保留的大小
*
*/
MSTR_EXPORT_API(mstr_result_t)
mstr_reserve(MString* str, usize_t new_size);

/**
* @brief 拼接字符串
*
Expand Down Expand Up @@ -321,6 +295,18 @@ MSTR_EXPORT_API(const char*) mstr_c_str(MString* str);
MSTR_EXPORT_API(mstr_bool_t)
mstr_equal(const MString* a, const MString* b);

/**
* @brief 判断两个字符串是否相等(cstr)
*
* @param[in] a: 字符串a
* @param[in] b: 字符串b
* @param[in] b_cnt: 字符串b的字符数
*
* @return mstr_bool_t: 字符串相等情况
*/
MSTR_EXPORT_API(mstr_bool_t)
mstr_equal_cstr(const MString* a, const mstr_char_t* b, usize_t b_cnt);

/**
* @brief 判断字符串是否以某个字串开始
*
Expand Down Expand Up @@ -408,34 +394,6 @@ mstr_find(
usize_t pattern_cnt
);

/**
* @brief 进行字符串替换
*
* @param[inout] str: 字符串
* @param[in] target: 需要替换的目标
* @param[in] target_cnt: 需要替换的目标的数组大小
*/
MSTR_EXPORT_API(mstr_result_t)
mstr_replace_multi(
MString* str, const MStringReplaceTarget* target, usize_t target_cnt
);

/**
* @brief 设置替换的目标
*
* @param[out] rep: 替换目标的结构
* @param[in] opt: 替换选项
* @param[in] patt: 模式串
* @param[in] target: 替换为的目标
*/
MSTR_EXPORT_API(void)
mstr_replace_set_target(
MStringReplaceTarget* rep,
MStringReplaceOption opt,
const char* pattern,
const char* target
);

/**
* @brief 从字符串中移除所有匹配substr的字符
*
Expand All @@ -456,11 +414,21 @@ mstr_retain(
* @brief 进行字符串替换(单个目标)
*
* @param[inout] str: 字符串
* @param[in] target: 需要替换的子串
* @param[in] opt: 替换模式
* @param[in] pattern: 需要替换的子串模式
* @param[in] pattern_cnt: 模式串的字符计数
* @param[in] replace_to: 需要替换为的结果
* @param[in] replace_to_cnt_cnt: 结果的字符计数
*/
MSTR_EXPORT_API(mstr_result_t)
mstr_replace(MString* str, const char* pattern, const char* replace_to);
mstr_replace(
MString* str,
MStringReplaceOption opt,
const char* pattern,
usize_t pattern_cnt,
const char* replace_to,
usize_t replace_to_cnt
);

/**
* @brief 取得迭代器
Expand Down
27 changes: 19 additions & 8 deletions inc/mm_string.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -353,11 +353,7 @@ class string final
template <std::size_t N>
bool operator==(const value_t (&str)[N]) const noexcept
{
MString obj;
mstr_create(&obj, str);
bool result = !!mstr_equal(&this_obj, &obj);
mstr_free(&obj);
return result;
return !!mstr_equal_cstr(&this_obj, str, N - 1);
}

/**
Expand Down Expand Up @@ -403,6 +399,22 @@ class string final
return !(pthis == str);
}

/**
* @brief 保留足够的内存
*
* @param new_size: 需要保留的内存字节数
*/
result<unit_t, mstr_result_t> reserve(usize_t new_size) noexcept
{
mstr_result_t code = mstr_reserve(&this_obj, new_size);
if (MSTR_SUCC(code)) {
return unit_t();
}
else {
return code;
}
}

/**
* @brief 判断字符串是否以另一个字串开始(c_str buffer)
*
Expand Down Expand Up @@ -467,8 +479,7 @@ class string final
* @brief 放入一个字符
*
*/
result<unit_t, mstr_result_t> push(mstr_codepoint_t uni_char
) noexcept
result<unit_t, mstr_result_t> push(unicode_t uni_char) noexcept
{
mstr_result_t code = mstr_append(&this_obj, uni_char);
if (MSTR_SUCC(code)) {
Expand All @@ -484,7 +495,7 @@ class string final
*
*/
result<unit_t, mstr_result_t> push(
mstr_codepoint_t ch, std::size_t repeat
unicode_t ch, std::size_t repeat
) noexcept
{
mstr_result_t code = mstr_repeat_append(&this_obj, ch, repeat);
Expand Down
Loading

0 comments on commit 972866f

Please sign in to comment.