Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
28 changes: 26 additions & 2 deletions lib/xy.h
Original file line number Diff line number Diff line change
Expand Up @@ -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)
{
Expand All @@ -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;
}
Expand Down
30 changes: 15 additions & 15 deletions src/chsrc-main.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}
Expand Down Expand Up @@ -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;
Expand Down
12 changes: 6 additions & 6 deletions src/framework/dish.c
Original file line number Diff line number Diff line change
Expand Up @@ -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;
}

Expand Down Expand Up @@ -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);
Expand All @@ -266,7 +266,7 @@ dish_select_source_by_mirror_code (Dish_t *dish, char *mirror_code)
for (int i=0; i<size; i++)
{
src = sources[i];
if (xy_streql (src.mirror->code, mirror_code))
if (xy_streql_ci (src.mirror->code, mirror_code))
{
exist = true;
break;
Expand All @@ -279,7 +279,7 @@ dish_select_source_by_mirror_code (Dish_t *dish, char *mirror_code)
for (int i=0; i<xy_seq_len(ProgStore.mirror_sites); i++)
{
MirrorSite_t *mir = xy_seq_at (ProgStore.mirror_sites, i);
if (xy_streql_ic (mir->code, mirror_code))
if (xy_streql_ci (mir->code, mirror_code))
{
mirror_site_exist = true;
break;
Expand Down
8 changes: 4 additions & 4 deletions src/framework/waiter.c
Original file line number Diff line number Diff line change
Expand Up @@ -142,23 +142,23 @@ 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"
: "Support following Programming Languages (same line indicates these dishes are compatible)\n";
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"
: "Support following Operating Systems (same line indicates these dishes are compatible)\n";
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"
Expand Down Expand Up @@ -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);
}

/**
Expand Down
23 changes: 21 additions & 2 deletions test/xy.c
Original file line number Diff line number Diff line change
Expand Up @@ -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"));
Expand Down