From 07ce4c217ed8a111482a4149e7f15cd9aab711e2 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Tue, 5 Sep 2023 10:30:44 +0800 Subject: [PATCH] Add `xy_str_start_with()` and `xy_str_delete_prefix()` --- test_xy.c | 14 +++++++++++++- xy.h | 32 ++++++++++++++++++++++++++++++++ 2 files changed, 45 insertions(+), 1 deletion(-) diff --git a/test_xy.c b/test_xy.c index bc5d1ab..c4f9aa5 100644 --- a/test_xy.c +++ b/test_xy.c @@ -2,7 +2,7 @@ * File : test_xy.c * Authors : Aoran Zeng * Created on : <2023-08-30> -* Last modified : <2023-09-04> +* Last modified : <2023-09-05> * * test_xy: * @@ -31,11 +31,23 @@ main (int argc, char const *argv[]) putb(xy_str_end_with("abcdef", "abcdef")); // true putb(xy_str_end_with("abcdef", "")); // true + putb(xy_str_start_with("abcdef", "abcdefg")); // false + putb(xy_str_start_with("abcdef", "abc")); // true + putb(xy_str_start_with("abcdef", "abcde")); // true + putb(xy_str_start_with("abcdef", "abcdef")); // true + putb(xy_str_start_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 + + puts(xy_str_delete_prefix("abcdefg", "cdef")); // 不变 + puts(xy_str_delete_prefix("abcdefg", "0abcde"));// 不变 + puts(xy_str_delete_prefix("abcdefg", "")); // 不变 + puts(xy_str_delete_prefix("abcdefg", "abc")); // defg + xy_success("成功:输出成功内容"); xy_info("信息: 输出信息内容"); xy_warn("警告:输出警告内容"); diff --git a/xy.h b/xy.h index 15dff91..2736615 100644 --- a/xy.h +++ b/xy.h @@ -301,6 +301,38 @@ xy_str_end_with (const char* str, const char* suffix) return true; } +bool +xy_str_start_with (const char* str, const char* prefix) +{ + size_t len1 = strlen(str); + size_t len2 = strlen(prefix); + + if (0==len2) return true; // 空字符串直接返回 + if (len1 < len2) return false; + + const char* cur1 = str; + const char* cur2 = prefix; + + for (int i=0; i