diff --git a/lib/xy.h b/lib/xy.h index e5becd61..05131331 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -657,8 +657,32 @@ xy_streql (const char *str1, const char *str2) return strcmp (str1, str2) == 0 ? true : false; } +/** + * @brief 将字符串转为小写,返回新字符串 + * + * @param str 可为NULL + * + * @return 转换后的新字符串;输入为NULL时返回NULL + * + * @memory SAFE + * return caller-free + */ +static char * +xy_str2lower (const char *str) +{ + if (!str) + return NULL; + + char *lower = xy_strdup (str); + for (char *cur = lower; *cur; cur++) + { + *cur = (char) tolower ((unsigned char)*cur); + } + return lower; +} + static bool -xy_streql_ic (const char *str1, const char *str2) +xy_streql_ci (const char *str1, const char *str2) { if (NULL == str1 || NULL == str2) { @@ -674,7 +698,7 @@ xy_streql_ic (const char *str1, const char *str2) for (size_t i = 0; i < len1; i++) { - if (tolower (str1[i]) != tolower (str2[i])) + if (tolower ((unsigned char)str1[i]) != tolower ((unsigned char)str2[i])) { return false; } diff --git a/src/chsrc-main.c b/src/chsrc-main.c index e0779de1..224dabf8 100644 --- a/src/chsrc-main.c +++ b/src/chsrc-main.c @@ -181,19 +181,19 @@ main (int argc, char const *argv[]) { scope = argv[i] + 7; } - if (xy_streql_ic (scope, "project")) + if (xy_streql_ci (scope, "project")) { ProgMode.Scope = ProjectScope; } - else if (xy_streql_ic (scope, "user")) + else if (xy_streql_ci (scope, "user")) { ProgMode.Scope = UserScope; } - else if (xy_streql_ic (scope, "system")) + else if (xy_streql_ci (scope, "system")) { ProgMode.Scope = SystemScope; } - else if (xy_streql_ic (scope, "default")) + else if (xy_streql_ci (scope, "default")) { ProgMode.Scope = ImplementationDefinedScope; } @@ -292,37 +292,37 @@ main (int argc, char const *argv[]) else { dish_name = argv[cli_arg_Dish_pos]; - if ( xy_streql (dish_name, "mirrors") - || xy_streql (dish_name, "mirror")) + if ( xy_streql_ci (dish_name, "mirrors") + || xy_streql_ci (dish_name, "mirror")) { cli_print_all_mirror_sites (); return Exit_OK; } - else if ( xy_streql (dish_name, "dishes") - || xy_streql (dish_name, "dish") - || xy_streql (dish_name, "dishs")) /* 防止有人错误拼写 */ + else if ( xy_streql_ci (dish_name, "dishes") + || xy_streql_ci (dish_name, "dish") + || xy_streql_ci (dish_name, "dishs")) /* 防止有人错误拼写 */ { cli_print_supported_dishes (); return Exit_OK; } - else if (xy_streql (dish_name, "os")) + else if (xy_streql_ci (dish_name, "os")) { cli_print_menu ("os"); return Exit_OK; } - else if ( xy_streql (dish_name, "lang") - || xy_streql (dish_name, "pl") - || xy_streql (dish_name, "language")) + else if ( xy_streql_ci (dish_name, "lang") + || xy_streql_ci (dish_name, "pl") + || xy_streql_ci (dish_name, "language")) { cli_print_menu ("pl"); return Exit_OK; } - else if ( xy_streql (dish_name, "ware") - || xy_streql (dish_name, "software")) + else if ( xy_streql_ci (dish_name, "ware") + || xy_streql_ci (dish_name, "software")) { cli_print_menu ("wr"); return Exit_OK; diff --git a/src/framework/dish.c b/src/framework/dish.c index e9500245..1c8763dc 100644 --- a/src/framework/dish.c +++ b/src/framework/dish.c @@ -107,15 +107,15 @@ dish_has_source_from_mirror (Dish_t *dish, const char *mirror_code) if (!dish || !dish->sources || !mirror_code) return false; - if (xy_streql (mirror_code, "upstream")) + if (xy_streql_ci (mirror_code, "upstream")) return dish->sources_n > 0; - if (xy_streql (mirror_code, "first")) + if (xy_streql_ci (mirror_code, "first")) return dish->sources_n > 1; for (int i=0; i < dish->sources_n; i++) { - if (xy_streql (dish->sources[i].mirror->code, mirror_code)) + if (xy_streql_ci (dish->sources[i].mirror->code, mirror_code)) return true; } @@ -250,7 +250,7 @@ dish_select_source_by_mirror_code (Dish_t *dish, char *mirror_code) chsrc_succ (xy_strcat (4, name, msg1, dish_name, msg2)); } - if (xy_streql ("first", mirror_code)) + if (xy_streql_ci ("first", mirror_code)) { char *msg = ENGLISH ? "Will use the first speedy source measured by maintainers" : "将使用维护团队测速第一的源"; say (msg); @@ -266,7 +266,7 @@ dish_select_source_by_mirror_code (Dish_t *dish, char *mirror_code) for (int i=0; icode, mirror_code)) + if (xy_streql_ci (src.mirror->code, mirror_code)) { exist = true; break; @@ -279,7 +279,7 @@ dish_select_source_by_mirror_code (Dish_t *dish, char *mirror_code) for (int i=0; icode, mirror_code)) + if (xy_streql_ci (mir->code, mirror_code)) { mirror_site_exist = true; break; diff --git a/src/framework/waiter.c b/src/framework/waiter.c index a2009f31..0327d01f 100644 --- a/src/framework/waiter.c +++ b/src/framework/waiter.c @@ -142,7 +142,7 @@ cli_print_supported_dishes () void cli_print_menu (char *menu) { - if (xy_streql (menu, "pl")) + if (xy_streql_ci (menu, "pl")) { char *msg = CHINESE ? "支持对以下编程语言生态换源 (同一行表示这几个菜品兼容)\n" @@ -150,7 +150,7 @@ cli_print_menu (char *menu) say (bdgreen(msg)); cli_print_dishes_for_menu (ProgStore.pl); } - else if (xy_streql (menu, "os")) + else if (xy_streql_ci (menu, "os")) { char *msg = CHINESE ? "支持对以下操作系统换源 (同一行表示这几个菜品兼容)\n" @@ -158,7 +158,7 @@ cli_print_menu (char *menu) say (bdgreen(msg)); cli_print_dishes_for_menu (ProgStore.os); } - else if (xy_streql (menu, "wr")) + else if (xy_streql_ci (menu, "wr")) { char *msg = CHINESE ? "支持对以下软件换源 (同一行表示这几个菜品兼容)\n" @@ -501,7 +501,7 @@ bool callback_match_alias (const char *alias, void *user_data) { const char *input = (const char *)user_data; - return xy_streql_ic (input, alias); + return xy_streql_ci (input, alias); } /** diff --git a/test/xy.c b/test/xy.c index 8ffe2153..e7d224c5 100644 --- a/test/xy.c +++ b/test/xy.c @@ -78,9 +78,28 @@ main (int argc, char const *argv[]) xy_error_brkt ("xy.h", "错误", "西安肉丸胡辣汤里没有肉丸"); assert (xy_streql ("abc", "abc")); - assert (xy_streql_ic ("abc", "abc")); + assert (xy_streql_ci ("abc", "abc")); assert (false == xy_streql ("abc", "abC")); - assert (true == xy_streql_ic ("abc", "abC")); + assert (true == xy_streql_ci ("abc", "abC")); + assert (xy_streql_ci ("", "")); + assert (!xy_streql_ci ("abc", "ABCD")); + assert (!xy_streql_ci ("abcd", "ABC")); + assert (!xy_streql_ci ("abc", "ABD")); + assert (!xy_streql_ci (NULL, "abc")); + assert (!xy_streql_ci ("abc", NULL)); + + assert (NULL == xy_str2lower (NULL)); + char *lower_empty = xy_str2lower (""); + assert_str ("", lower_empty); + free (lower_empty); + + char mixed_case[] = "NpMmIrRoR-123 镜像站"; + char *lower = xy_str2lower (mixed_case); + assert_str ("npmmirror-123 镜像站", lower); + assert_str ("NpMmIrRoR-123 镜像站", mixed_case); + assert (xy_str_start_with (lower, "npmmirror-")); + assert (!xy_str_start_with (lower, "tuna")); + free (lower); assert (false == xy_str_end_with ("abcdef", "abcdefg")); assert (xy_str_end_with ("abcdef", "def"));