Add xy_str_delete_suffix()

This commit is contained in:
Aoran Zeng 2023-09-04 21:21:36 +08:00
parent 28eb3fae31
commit e92509d0e0
2 changed files with 25 additions and 5 deletions

View File

@ -25,11 +25,16 @@ main (int argc, char const *argv[])
puts(xy_strjoin(6, "楚山横地出,", "汉水接天回。", "冠盖非新里,", "章华即旧台。", "习池风景异,", "归路满尘埃。"));
putb(xy_str_end_with("abcdef", "abcdefg"));
putb(xy_str_end_with("abcdef", "def"));
putb(xy_str_end_with("abcdef", "bcdef"));
putb(xy_str_end_with("abcdef", "abcdef"));
putb(xy_str_end_with("abcdef", ""));
putb(xy_str_end_with("abcdef", "abcdefg")); // false
putb(xy_str_end_with("abcdef", "def")); // true
putb(xy_str_end_with("abcdef", "bcdef")); // true
putb(xy_str_end_with("abcdef", "abcdef")); // true
putb(xy_str_end_with("abcdef", "")); // true
puts(xy_str_delete_suffix("abcdefg", "cdef")); // 不变
puts(xy_str_delete_suffix("abcdefg", "cdefgh"));// 不变
puts(xy_str_delete_suffix("abcdefg", "")); // 不变
puts(xy_str_delete_suffix("abcdefg", "efg")); // abcd
xy_success("成功:输出成功内容");
xy_info("信息: 输出信息内容");

15
xy.h
View File

@ -301,4 +301,19 @@ xy_str_end_with (const char* str, const char* suffix)
return true;
}
char*
xy_str_delete_suffix (const char* str, const char* suffix)
{
char* new = xy_strdup(str);
bool yes = xy_str_end_with(str, suffix);
if (!yes) return new;
size_t len1 = strlen(str);
size_t len2 = strlen(suffix);
char* cur = new + len1 - len2;
*cur = '\0';
return new;
}
#endif