From 60a2385df29359828c5754cbc2c4a864f1ba5461 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Wed, 18 Nov 2026 19:14:53 +0800 Subject: [PATCH 1/5] =?UTF-8?q?feat(cli):=20=E5=91=BD=E4=BB=A4=E3=80=81?= =?UTF-8?q?=E9=80=89=E9=A1=B9=E5=92=8C=E9=95=9C=E5=83=8F=E4=BB=A3=E5=8F=B7?= =?UTF-8?q?=E5=BF=BD=E7=95=A5=E5=A4=A7=E5=B0=8F=E5=86=99?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 29 +++++------ src/chsrc-main.c | 110 ++++++++++++++++++++--------------------- src/framework/dish.c | 10 ++-- src/framework/waiter.c | 6 +-- test/cli.pl | 8 +++ test/fw.c | 2 +- test/xy.c | 3 ++ 7 files changed, 87 insertions(+), 81 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index e5becd618..4158f56d0 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -658,30 +658,25 @@ xy_streql (const char *str1, const char *str2) } static bool -xy_streql_ic (const char *str1, const char *str2) +xy_str_start_with_ic (const char *str, const char *prefix) { - if (NULL == str1 || NULL == str2) - { - return false; - } - - size_t len1 = strlen (str1); - size_t len2 = strlen (str2); - if (len1 != len2) - { - return false; - } + if (!str || !prefix) + return false; - for (size_t i = 0; i < len1; i++) + while (*prefix) { - if (tolower (str1[i]) != tolower (str2[i])) - { - return false; - } + if (tolower ((unsigned char)*str++) != tolower ((unsigned char)*prefix++)) + return false; } return true; } +static bool +xy_streql_ic (const char *str1, const char *str2) +{ + return str1 && str2 && strlen (str1) == strlen (str2) && xy_str_start_with_ic (str1, str2); +} + /** * @flavor Ruby: String#end_with? diff --git a/src/chsrc-main.c b/src/chsrc-main.c index e0779de13..f003af478 100644 --- a/src/chsrc-main.c +++ b/src/chsrc-main.c @@ -149,22 +149,22 @@ main (int argc, char const *argv[]) { if (xy_str_start_with (argv[i], "-")) { - if (xy_streql (argv[i], "-ipv6")) + if (xy_streql_ic (argv[i], "-ipv6")) { ProgMode.Ipv6Mode = true; } - else if (xy_streql (argv[i], "-local")) + else if (xy_streql_ic (argv[i], "-local")) { ProgMode.Scope = ProjectScope; char *msg = CHINESE ? " -local 选项已弃用,请使用 -scope=project" : " -local is deprecated, please use -scope=project"; chsrc_warn (msg); } - else if ( xy_streql (argv[i], "-scope") - || xy_str_start_with (argv[i], "-scope=")) + else if ( xy_streql_ic (argv[i], "-scope") + || xy_str_start_with_ic (argv[i], "-scope=")) { const char *scope = NULL; - if (xy_streql (argv[i], "-scope")) + if (xy_streql_ic (argv[i], "-scope")) { if (i + 1 > argc) { @@ -212,28 +212,28 @@ main (int argc, char const *argv[]) return Exit_Unknown; } } - else if (xy_streql (argv[i], "-en") || xy_streql (argv[i], "-english")) + else if (xy_streql_ic (argv[i], "-en") || xy_streql_ic (argv[i], "-english")) { ProgMode.EnglishMode = true; } - else if (xy_streql (argv[i], "-dry")) + else if (xy_streql_ic (argv[i], "-dry")) { ProgMode.DryRunMode = true; } - else if (xy_streql (argv[i], "-no-color") || xy_streql (argv[i], "-no-colour")) + else if (xy_streql_ic (argv[i], "-no-color") || xy_streql_ic (argv[i], "-no-colour")) { ProgMode.NoColorMode = true; xy.enable_color = false; } - else if ( xy_streql (argv[i], "-h") - || xy_streql (argv[i], "-help") - || xy_streql (argv[i], "--help")) + else if ( xy_streql_ic (argv[i], "-h") + || xy_streql_ic (argv[i], "-help") + || xy_streql_ic (argv[i], "--help")) { command = "help"; /* 交到下方处理 */ } - else if ( xy_streql (argv[i], "-v") - || xy_streql (argv[i], "-version") - || xy_streql (argv[i], "--version")) + else if ( xy_streql_ic (argv[i], "-v") + || xy_streql_ic (argv[i], "-version") + || xy_streql_ic (argv[i], "--version")) { command = "version"; /* 交到下方处理 */ } @@ -257,31 +257,31 @@ main (int argc, char const *argv[]) } /* chsrc help */ - if ( xy_streql (command, "h") - || xy_streql (command, "-h") - || xy_streql (command, "help") - || xy_streql (command, "-help") - || xy_streql (command, "--help")) + if ( xy_streql_ic (command, "h") + || xy_streql_ic (command, "-h") + || xy_streql_ic (command, "help") + || xy_streql_ic (command, "-help") + || xy_streql_ic (command, "--help")) { cli_print_help (); return Exit_OK; } /* chsrc -v */ - else if ( xy_streql (command, "-v") - || xy_streql (command, "-version") - || xy_streql (command, "--version") - || xy_streql (command, "ver") - || xy_streql (command, "version")) + else if ( xy_streql_ic (command, "-v") + || xy_streql_ic (command, "-version") + || xy_streql_ic (command, "--version") + || xy_streql_ic (command, "ver") + || xy_streql_ic (command, "version")) { cli_print_version (); return Exit_OK; } /* chsrc list */ - else if ( xy_streql (command, "list") - || xy_streql (command, "l") - || xy_streql (command, "ls")) + else if ( xy_streql_ic (command, "list") + || xy_streql_ic (command, "l") + || xy_streql_ic (command, "ls")) { if (argc < cli_arg_Dish_pos) { @@ -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_ic (dish_name, "mirrors") + || xy_streql_ic (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_ic (dish_name, "dishes") + || xy_streql_ic (dish_name, "dish") + || xy_streql_ic (dish_name, "dishs")) /* 防止有人错误拼写 */ { cli_print_supported_dishes (); return Exit_OK; } - else if (xy_streql (dish_name, "os")) + else if (xy_streql_ic (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_ic (dish_name, "lang") + || xy_streql_ic (dish_name, "pl") + || xy_streql_ic (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_ic (dish_name, "ware") + || xy_streql_ic (dish_name, "software")) { cli_print_menu ("wr"); return Exit_OK; @@ -345,12 +345,12 @@ main (int argc, char const *argv[]) #define MSG_ZH_USE_LIST_TARGETS "使用 chsrc list dishes 查看所有支持的菜品" /* chsrc measure */ - else if ( xy_streql (command, "measure") - || xy_streql (command, "mea") - || xy_streql (command, "m") - || xy_streql (command, "cesu") - || xy_streql (command, "ce") - || xy_streql (command, "c")) + else if ( xy_streql_ic (command, "measure") + || xy_streql_ic (command, "mea") + || xy_streql_ic (command, "m") + || xy_streql_ic (command, "cesu") + || xy_streql_ic (command, "ce") + || xy_streql_ic (command, "c")) { if (argc < cli_arg_Dish_pos) { @@ -376,8 +376,8 @@ main (int argc, char const *argv[]) /* chsrc get */ - else if ( xy_streql (command, "get") - || xy_streql (command, "g")) + else if ( xy_streql_ic (command, "get") + || xy_streql_ic (command, "g")) { if (argc < cli_arg_Dish_pos) { @@ -400,8 +400,8 @@ main (int argc, char const *argv[]) } /* chsrc set */ - else if ( xy_streql (command, "set") - || xy_streql (command, "s")) + else if ( xy_streql_ic (command, "set") + || xy_streql_ic (command, "s")) { if (argc < cli_arg_Dish_pos) { @@ -431,9 +431,9 @@ main (int argc, char const *argv[]) } /* chsrc reset */ - else if ( xy_streql (command, "reset") - || xy_streql (command, "rest") - || xy_streql (command, "r")) + else if ( xy_streql_ic (command, "reset") + || xy_streql_ic (command, "rest") + || xy_streql_ic (command, "r")) { if (argc < cli_arg_Dish_pos) { @@ -458,10 +458,10 @@ main (int argc, char const *argv[]) } /* chsrc issue */ - else if ( xy_streql (command, "issue") - || xy_streql (command, "issues") - || xy_streql (command, "isue") - || xy_streql (command, "i")) + else if ( xy_streql_ic (command, "issue") + || xy_streql_ic (command, "issues") + || xy_streql_ic (command, "isue") + || xy_streql_ic (command, "i")) { cli_print_issues (); return Exit_OK; diff --git a/src/framework/dish.c b/src/framework/dish.c index e95002459..1f3945aeb 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_ic (mirror_code, "upstream")) return dish->sources_n > 0; - if (xy_streql (mirror_code, "first")) + if (xy_streql_ic (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_ic (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_ic ("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_ic (src.mirror->code, mirror_code)) { exist = true; break; diff --git a/src/framework/waiter.c b/src/framework/waiter.c index a2009f31f..2a48fd971 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_ic (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_ic (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_ic (menu, "wr")) { char *msg = CHINESE ? "支持对以下软件换源 (同一行表示这几个菜品兼容)\n" diff --git a/test/cli.pl b/test/cli.pl index d13019684..c5c45e2b7 100644 --- a/test/cli.pl +++ b/test/cli.pl @@ -96,6 +96,14 @@ like `$CHSRC list mirrors`, $list_str, 'chsrc list mirrors'; like `$CHSRC list os`, qr/netbsd\s*openbsd/, 'chsrc list os'; like `$CHSRC list ware`, qr/brew\s*homebrew/, 'chsrc list ware'; +like `$CHSRC LiSt MiRrOrS`, $list_str, 'mixed-case command and list keyword'; +like `$CHSRC --VeRsIoN -EN`, qr/License GPLv3\+/, 'mixed-case flags'; +like `$CHSRC SeT -DRY -No-Color -SCOPE=PROJECT NpM FiRsT`, + qr/npm config --location project set registry https:/, 'mixed-case scope and first mirror'; +like `$CHSRC SeT -DRY -No-Color -ScOpE PrOjEcT NpM NpMmIrRoR`, + qr/npm config --location project set registry https:/, 'mixed-case separate scope and named mirror'; +like `$CHSRC SeT -DRY -No-Color NpM https://example.invalid/MixedCase/Index`, + qr{npm config set registry https://example\.invalid/MixedCase/Index}, 'URL path retains case'; =begin comment diff --git a/test/fw.c b/test/fw.c index bcceb18b9..794e40ca3 100644 --- a/test/fw.c +++ b/test/fw.c @@ -17,7 +17,7 @@ int main (int argc, char const *argv[]) { - if (argc == 2 && xy_streql (argv[1], "--version")) + if (argc == 2 && xy_streql_ic (argv[1], "--version")) { say (Chsrc_Version); return 0; diff --git a/test/xy.c b/test/xy.c index 8ffe2153d..138ee5beb 100644 --- a/test/xy.c +++ b/test/xy.c @@ -94,6 +94,9 @@ main (int argc, char const *argv[]) assert (xy_str_start_with ("abcdef", "abcde")); assert (xy_str_start_with ("abcdef", "abcdef")); assert (xy_str_start_with ("abcdef", "")); + assert (xy_str_start_with_ic ("-ScOpE=project", "-scope=")); + assert (!xy_str_start_with_ic ("-scope", "-scope=")); + assert (!xy_str_start_with_ic (NULL, "-scope=")); assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "cdef")); assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "cdefgh")); From d78be2f8ed0bd3f814f514ee1b253cefdbcf28a9 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Sat, 28 Nov 2026 21:05:28 +0800 Subject: [PATCH 2/5] =?UTF-8?q?fix(cli):=20=E6=81=A2=E5=A4=8D=E5=91=BD?= =?UTF-8?q?=E4=BB=A4=E5=92=8C=E9=80=89=E9=A1=B9=E7=9A=84=E5=A4=A7=E5=B0=8F?= =?UTF-8?q?=E5=86=99=E9=99=90=E5=88=B6?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- src/chsrc-main.c | 88 ++++++++++++++++++++++++------------------------ test/cli.pl | 35 +++++++++++++++---- test/fw.c | 2 +- 3 files changed, 73 insertions(+), 52 deletions(-) diff --git a/src/chsrc-main.c b/src/chsrc-main.c index f003af478..321e0c78e 100644 --- a/src/chsrc-main.c +++ b/src/chsrc-main.c @@ -149,22 +149,22 @@ main (int argc, char const *argv[]) { if (xy_str_start_with (argv[i], "-")) { - if (xy_streql_ic (argv[i], "-ipv6")) + if (xy_streql (argv[i], "-ipv6")) { ProgMode.Ipv6Mode = true; } - else if (xy_streql_ic (argv[i], "-local")) + else if (xy_streql (argv[i], "-local")) { ProgMode.Scope = ProjectScope; char *msg = CHINESE ? " -local 选项已弃用,请使用 -scope=project" : " -local is deprecated, please use -scope=project"; chsrc_warn (msg); } - else if ( xy_streql_ic (argv[i], "-scope") - || xy_str_start_with_ic (argv[i], "-scope=")) + else if ( xy_streql (argv[i], "-scope") + || xy_str_start_with (argv[i], "-scope=")) { const char *scope = NULL; - if (xy_streql_ic (argv[i], "-scope")) + if (xy_streql (argv[i], "-scope")) { if (i + 1 > argc) { @@ -212,28 +212,28 @@ main (int argc, char const *argv[]) return Exit_Unknown; } } - else if (xy_streql_ic (argv[i], "-en") || xy_streql_ic (argv[i], "-english")) + else if (xy_streql (argv[i], "-en") || xy_streql (argv[i], "-english")) { ProgMode.EnglishMode = true; } - else if (xy_streql_ic (argv[i], "-dry")) + else if (xy_streql (argv[i], "-dry")) { ProgMode.DryRunMode = true; } - else if (xy_streql_ic (argv[i], "-no-color") || xy_streql_ic (argv[i], "-no-colour")) + else if (xy_streql (argv[i], "-no-color") || xy_streql (argv[i], "-no-colour")) { ProgMode.NoColorMode = true; xy.enable_color = false; } - else if ( xy_streql_ic (argv[i], "-h") - || xy_streql_ic (argv[i], "-help") - || xy_streql_ic (argv[i], "--help")) + else if ( xy_streql (argv[i], "-h") + || xy_streql (argv[i], "-help") + || xy_streql (argv[i], "--help")) { command = "help"; /* 交到下方处理 */ } - else if ( xy_streql_ic (argv[i], "-v") - || xy_streql_ic (argv[i], "-version") - || xy_streql_ic (argv[i], "--version")) + else if ( xy_streql (argv[i], "-v") + || xy_streql (argv[i], "-version") + || xy_streql (argv[i], "--version")) { command = "version"; /* 交到下方处理 */ } @@ -257,31 +257,31 @@ main (int argc, char const *argv[]) } /* chsrc help */ - if ( xy_streql_ic (command, "h") - || xy_streql_ic (command, "-h") - || xy_streql_ic (command, "help") - || xy_streql_ic (command, "-help") - || xy_streql_ic (command, "--help")) + if ( xy_streql (command, "h") + || xy_streql (command, "-h") + || xy_streql (command, "help") + || xy_streql (command, "-help") + || xy_streql (command, "--help")) { cli_print_help (); return Exit_OK; } /* chsrc -v */ - else if ( xy_streql_ic (command, "-v") - || xy_streql_ic (command, "-version") - || xy_streql_ic (command, "--version") - || xy_streql_ic (command, "ver") - || xy_streql_ic (command, "version")) + else if ( xy_streql (command, "-v") + || xy_streql (command, "-version") + || xy_streql (command, "--version") + || xy_streql (command, "ver") + || xy_streql (command, "version")) { cli_print_version (); return Exit_OK; } /* chsrc list */ - else if ( xy_streql_ic (command, "list") - || xy_streql_ic (command, "l") - || xy_streql_ic (command, "ls")) + else if ( xy_streql (command, "list") + || xy_streql (command, "l") + || xy_streql (command, "ls")) { if (argc < cli_arg_Dish_pos) { @@ -345,12 +345,12 @@ main (int argc, char const *argv[]) #define MSG_ZH_USE_LIST_TARGETS "使用 chsrc list dishes 查看所有支持的菜品" /* chsrc measure */ - else if ( xy_streql_ic (command, "measure") - || xy_streql_ic (command, "mea") - || xy_streql_ic (command, "m") - || xy_streql_ic (command, "cesu") - || xy_streql_ic (command, "ce") - || xy_streql_ic (command, "c")) + else if ( xy_streql (command, "measure") + || xy_streql (command, "mea") + || xy_streql (command, "m") + || xy_streql (command, "cesu") + || xy_streql (command, "ce") + || xy_streql (command, "c")) { if (argc < cli_arg_Dish_pos) { @@ -376,8 +376,8 @@ main (int argc, char const *argv[]) /* chsrc get */ - else if ( xy_streql_ic (command, "get") - || xy_streql_ic (command, "g")) + else if ( xy_streql (command, "get") + || xy_streql (command, "g")) { if (argc < cli_arg_Dish_pos) { @@ -400,8 +400,8 @@ main (int argc, char const *argv[]) } /* chsrc set */ - else if ( xy_streql_ic (command, "set") - || xy_streql_ic (command, "s")) + else if ( xy_streql (command, "set") + || xy_streql (command, "s")) { if (argc < cli_arg_Dish_pos) { @@ -431,9 +431,9 @@ main (int argc, char const *argv[]) } /* chsrc reset */ - else if ( xy_streql_ic (command, "reset") - || xy_streql_ic (command, "rest") - || xy_streql_ic (command, "r")) + else if ( xy_streql (command, "reset") + || xy_streql (command, "rest") + || xy_streql (command, "r")) { if (argc < cli_arg_Dish_pos) { @@ -458,10 +458,10 @@ main (int argc, char const *argv[]) } /* chsrc issue */ - else if ( xy_streql_ic (command, "issue") - || xy_streql_ic (command, "issues") - || xy_streql_ic (command, "isue") - || xy_streql_ic (command, "i")) + else if ( xy_streql (command, "issue") + || xy_streql (command, "issues") + || xy_streql (command, "isue") + || xy_streql (command, "i")) { cli_print_issues (); return Exit_OK; diff --git a/test/cli.pl b/test/cli.pl index c5c45e2b7..25c41f709 100644 --- a/test/cli.pl +++ b/test/cli.pl @@ -96,13 +96,34 @@ like `$CHSRC list mirrors`, $list_str, 'chsrc list mirrors'; like `$CHSRC list os`, qr/netbsd\s*openbsd/, 'chsrc list os'; like `$CHSRC list ware`, qr/brew\s*homebrew/, 'chsrc list ware'; -like `$CHSRC LiSt MiRrOrS`, $list_str, 'mixed-case command and list keyword'; -like `$CHSRC --VeRsIoN -EN`, qr/License GPLv3\+/, 'mixed-case flags'; -like `$CHSRC SeT -DRY -No-Color -SCOPE=PROJECT NpM FiRsT`, - qr/npm config --location project set registry https:/, 'mixed-case scope and first mirror'; -like `$CHSRC SeT -DRY -No-Color -ScOpE PrOjEcT NpM NpMmIrRoR`, - qr/npm config --location project set registry https:/, 'mixed-case separate scope and named mirror'; -like `$CHSRC SeT -DRY -No-Color NpM https://example.invalid/MixedCase/Index`, +like `$CHSRC list MiRrOrS`, $list_str, 'mixed-case mirror list'; +like `$CHSRC list OS`, qr/netbsd\s*openbsd/, 'uppercase OS list'; +like `$CHSRC list SoFtWaRe`, qr/brew\s*homebrew/, 'mixed-case software list'; +like `$CHSRC list LaNgUaGe`, qr/js\s*javascript\s*node\s*nodejs/, 'mixed-case language list'; +like `$CHSRC list DiShEs`, qr/brew\s*homebrew/, 'mixed-case dish list'; +like `$CHSRC list -no-color NpM`, qr/npmmirror/, 'mixed-case dish name'; + +for my $option (qw(-IPv6 -LoCaL -ScOpE -ScOpE=project -EN -English -DRY + -No-Color -No-Colour -H -Help --Help -V -Version --Version)) { + my $output = `$CHSRC version -en -no-color $option 2>&1`; + isnt $? >> 8, 0, "mixed-case option $option fails"; + like $output, qr/Unknown option: \Q$option\E/, "mixed-case option $option is rejected"; +} + +for my $command (qw(HeLp LiSt GeT SeT ReSeT MeAsUrE VeRsIoN IsSuE + H L LS G S R REST M MEA CESU CE C VER I ISSUES ISUE)) { + my $output = `$CHSRC $command -en -no-color 2>&1`; + isnt $? >> 8, 0, "mixed-case command $command fails"; + like $output, qr/Unknown command `\Q$command\E`/, "mixed-case command $command is rejected"; +} + +like `$CHSRC set -dry -no-color -scope=project NpM FiRsT`, + qr/npm config --location project set registry https:/, 'mixed-case first mirror'; +like `$CHSRC set -dry -no-color -scope project NpM NpMmIrRoR`, + qr{npm config --location project set registry https://registry\.npmmirror\.com}, 'mixed-case named mirror'; +like `$CHSRC set -dry -no-color NpM UpStReAm`, + qr{npm config set registry https://registry\.npmjs\.org}, 'mixed-case upstream mirror'; +like `$CHSRC set -dry -no-color NpM https://example.invalid/MixedCase/Index`, qr{npm config set registry https://example\.invalid/MixedCase/Index}, 'URL path retains case'; diff --git a/test/fw.c b/test/fw.c index 794e40ca3..bcceb18b9 100644 --- a/test/fw.c +++ b/test/fw.c @@ -17,7 +17,7 @@ int main (int argc, char const *argv[]) { - if (argc == 2 && xy_streql_ic (argv[1], "--version")) + if (argc == 2 && xy_streql (argv[1], "--version")) { say (Chsrc_Version); return 0; From d334f98577b7173ac9e3bf7881a92ddad810222b Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Sat, 28 Nov 2026 21:05:57 +0800 Subject: [PATCH 3/5] =?UTF-8?q?refactor(xy):=20=E7=94=A8=E5=AD=97=E7=AC=A6?= =?UTF-8?q?=E4=B8=B2=E8=BD=AC=E5=B0=8F=E5=86=99=E6=9B=BF=E4=BB=A3=E5=BF=BD?= =?UTF-8?q?=E7=95=A5=E5=A4=A7=E5=B0=8F=E5=86=99=E7=9A=84=E5=89=8D=E7=BC=80?= =?UTF-8?q?=E5=88=A4=E6=96=AD?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 47 ++++++++++++++++++++++++++++++++++++++--------- test/xy.c | 22 +++++++++++++++++++--- 2 files changed, 57 insertions(+), 12 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 4158f56d0..cef8b7178 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -657,24 +657,53 @@ xy_streql (const char *str1, const char *str2) return strcmp (str1, str2) == 0 ? true : false; } -static bool -xy_str_start_with_ic (const char *str, const char *prefix) +/** + * @brief 将字符串转为小写,返回新字符串 + * + * @param str 可为NULL + * + * @return 转换后的新字符串;输入为NULL时返回NULL + * + * @memory SAFE + * return caller-free + */ +static char * +xy_str2lower (const char *str) { - if (!str || !prefix) - return false; + if (!str) + return NULL; - while (*prefix) + char *lower = xy_strdup (str); + for (char *cur = lower; *cur; cur++) { - if (tolower ((unsigned char)*str++) != tolower ((unsigned char)*prefix++)) - return false; + *cur = (char) tolower ((unsigned char)*cur); } - return true; + return lower; } static bool xy_streql_ic (const char *str1, const char *str2) { - return str1 && str2 && strlen (str1) == strlen (str2) && xy_str_start_with_ic (str1, str2); + if (NULL == str1 || NULL == str2) + { + return false; + } + + size_t len1 = strlen (str1); + size_t len2 = strlen (str2); + if (len1 != len2) + { + return false; + } + + for (size_t i = 0; i < len1; i++) + { + if (tolower ((unsigned char)str1[i]) != tolower ((unsigned char)str2[i])) + { + return false; + } + } + return true; } diff --git a/test/xy.c b/test/xy.c index 138ee5beb..8e0e9786b 100644 --- a/test/xy.c +++ b/test/xy.c @@ -81,6 +81,25 @@ main (int argc, char const *argv[]) assert (xy_streql_ic ("abc", "abc")); assert (false == xy_streql ("abc", "abC")); assert (true == xy_streql_ic ("abc", "abC")); + assert (xy_streql_ic ("", "")); + assert (!xy_streql_ic ("abc", "ABCD")); + assert (!xy_streql_ic ("abcd", "ABC")); + assert (!xy_streql_ic ("abc", "ABD")); + assert (!xy_streql_ic (NULL, "abc")); + assert (!xy_streql_ic ("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")); @@ -94,9 +113,6 @@ main (int argc, char const *argv[]) assert (xy_str_start_with ("abcdef", "abcde")); assert (xy_str_start_with ("abcdef", "abcdef")); assert (xy_str_start_with ("abcdef", "")); - assert (xy_str_start_with_ic ("-ScOpE=project", "-scope=")); - assert (!xy_str_start_with_ic ("-scope", "-scope=")); - assert (!xy_str_start_with_ic (NULL, "-scope=")); assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "cdef")); assert_str ("abcdefg", xy_str_delete_suffix ("abcdefg", "cdefgh")); From c4713e6b2efd5a1ac31bf1e96aa8c009a1140176 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Sat, 28 Nov 2026 21:06:44 +0800 Subject: [PATCH 4/5] =?UTF-8?q?=E9=87=8D=E5=91=BD=E5=90=8D=20`xy=5Fstreql?= =?UTF-8?q?=5Fci`?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 2 +- src/chsrc-main.c | 30 +++++++++++++++--------------- src/framework/dish.c | 12 ++++++------ src/framework/waiter.c | 8 ++++---- test/xy.c | 16 ++++++++-------- 5 files changed, 34 insertions(+), 34 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index cef8b7178..05131331e 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -682,7 +682,7 @@ xy_str2lower (const char *str) } 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) { diff --git a/src/chsrc-main.c b/src/chsrc-main.c index 321e0c78e..224dabf80 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_ic (dish_name, "mirrors") - || xy_streql_ic (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_ic (dish_name, "dishes") - || xy_streql_ic (dish_name, "dish") - || xy_streql_ic (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_ic (dish_name, "os")) + else if (xy_streql_ci (dish_name, "os")) { cli_print_menu ("os"); return Exit_OK; } - else if ( xy_streql_ic (dish_name, "lang") - || xy_streql_ic (dish_name, "pl") - || xy_streql_ic (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_ic (dish_name, "ware") - || xy_streql_ic (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 1f3945aeb..1c8763dc6 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_ic (mirror_code, "upstream")) + if (xy_streql_ci (mirror_code, "upstream")) return dish->sources_n > 0; - if (xy_streql_ic (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_ic (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_ic ("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 2a48fd971..0327d01f0 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_ic (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_ic (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_ic (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 8e0e9786b..e7d224c5c 100644 --- a/test/xy.c +++ b/test/xy.c @@ -78,15 +78,15 @@ 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 (xy_streql_ic ("", "")); - assert (!xy_streql_ic ("abc", "ABCD")); - assert (!xy_streql_ic ("abcd", "ABC")); - assert (!xy_streql_ic ("abc", "ABD")); - assert (!xy_streql_ic (NULL, "abc")); - assert (!xy_streql_ic ("abc", NULL)); + 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 (""); From 07d735bf920edbc1d48adc41e4822b36765ccf50 Mon Sep 17 00:00:00 2001 From: Mikachu2333 Date: Sun, 29 Nov 2026 21:51:05 +0800 Subject: [PATCH 5/5] =?UTF-8?q?=E5=9B=9E=E9=80=80cli=E6=B5=8B=E8=AF=95?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- test/cli.pl | 29 ----------------------------- 1 file changed, 29 deletions(-) diff --git a/test/cli.pl b/test/cli.pl index 25c41f709..d13019684 100644 --- a/test/cli.pl +++ b/test/cli.pl @@ -96,35 +96,6 @@ like `$CHSRC list mirrors`, $list_str, 'chsrc list mirrors'; like `$CHSRC list os`, qr/netbsd\s*openbsd/, 'chsrc list os'; like `$CHSRC list ware`, qr/brew\s*homebrew/, 'chsrc list ware'; -like `$CHSRC list MiRrOrS`, $list_str, 'mixed-case mirror list'; -like `$CHSRC list OS`, qr/netbsd\s*openbsd/, 'uppercase OS list'; -like `$CHSRC list SoFtWaRe`, qr/brew\s*homebrew/, 'mixed-case software list'; -like `$CHSRC list LaNgUaGe`, qr/js\s*javascript\s*node\s*nodejs/, 'mixed-case language list'; -like `$CHSRC list DiShEs`, qr/brew\s*homebrew/, 'mixed-case dish list'; -like `$CHSRC list -no-color NpM`, qr/npmmirror/, 'mixed-case dish name'; - -for my $option (qw(-IPv6 -LoCaL -ScOpE -ScOpE=project -EN -English -DRY - -No-Color -No-Colour -H -Help --Help -V -Version --Version)) { - my $output = `$CHSRC version -en -no-color $option 2>&1`; - isnt $? >> 8, 0, "mixed-case option $option fails"; - like $output, qr/Unknown option: \Q$option\E/, "mixed-case option $option is rejected"; -} - -for my $command (qw(HeLp LiSt GeT SeT ReSeT MeAsUrE VeRsIoN IsSuE - H L LS G S R REST M MEA CESU CE C VER I ISSUES ISUE)) { - my $output = `$CHSRC $command -en -no-color 2>&1`; - isnt $? >> 8, 0, "mixed-case command $command fails"; - like $output, qr/Unknown command `\Q$command\E`/, "mixed-case command $command is rejected"; -} - -like `$CHSRC set -dry -no-color -scope=project NpM FiRsT`, - qr/npm config --location project set registry https:/, 'mixed-case first mirror'; -like `$CHSRC set -dry -no-color -scope project NpM NpMmIrRoR`, - qr{npm config --location project set registry https://registry\.npmmirror\.com}, 'mixed-case named mirror'; -like `$CHSRC set -dry -no-color NpM UpStReAm`, - qr{npm config set registry https://registry\.npmjs\.org}, 'mixed-case upstream mirror'; -like `$CHSRC set -dry -no-color NpM https://example.invalid/MixedCase/Index`, - qr{npm config set registry https://example\.invalid/MixedCase/Index}, 'URL path retains case'; =begin comment