Add xy_str_end_with()

This commit is contained in:
Aoran Zeng 2023-09-04 21:15:16 +08:00
parent b9c57d54d2
commit 28eb3fae31
2 changed files with 29 additions and 0 deletions

View File

@ -24,6 +24,13 @@ main (int argc, char const *argv[])
puts(xy_strjoin(4, "水落鱼梁浅,", "天寒梦泽深。", "羊公碑字在,", "读罢泪沾襟。"));
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", ""));
xy_success("成功:输出成功内容");
xy_info("信息: 输出信息内容");
xy_warn("警告:输出警告内容");

22
xy.h
View File

@ -71,6 +71,7 @@
#define putf(n) printf("%f\n", n)
#define puti(n) printf("%d\n", n)
#define putb(n) if(n) puts("true"); else puts("false");
#define xy_arylen(x) (sizeof(x) / sizeof(x[0]))
@ -279,4 +280,25 @@ xy_str_to_quietcmd (const char* cmd)
return ret;
}
bool
xy_str_end_with (const char* str, const char* suffix)
{
size_t len1 = strlen(str);
size_t len2 = strlen(suffix);
if (0==len2) return true; // 空字符串直接返回
if (len1 < len2) return false;
const char* cur1 = str + len1 - 1;
const char* cur2 = suffix + len2 - 1;
for (int i=0; i<len2; i++)
{
if (*cur1 != *cur2) return false;
cur1--; cur2--;
}
return true;
}
#endif