diff --git a/test_xy.c b/test_xy.c index 5e8ee2b..bc5d1ab 100644 --- a/test_xy.c +++ b/test_xy.c @@ -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("信息: 输出信息内容"); diff --git a/xy.h b/xy.h index 2a5fd4e..ec5f593 100644 --- a/xy.h +++ b/xy.h @@ -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