From 189658406480e2d39d34995383d760ddcfb51d85 Mon Sep 17 00:00:00 2001 From: Aoran Zeng Date: Thu, 13 Aug 2026 11:41:46 +0800 Subject: [PATCH] =?UTF-8?q?`XySeq=5Ft`=20=E5=BA=8F=E5=8F=B7=E4=BB=8E0?= =?UTF-8?q?=E5=BC=80=E5=A7=8B?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- lib/xy.h | 9 +++++---- src/chsrc-main.c | 35 ++++++++++++++++------------------- test/xy.c | 5 +++-- 3 files changed, 24 insertions(+), 25 deletions(-) diff --git a/lib/xy.h b/lib/xy.h index 63da16c7..7e4286c8 100644 --- a/lib/xy.h +++ b/lib/xy.h @@ -1759,7 +1759,7 @@ xy_seq_last (XySeq_t *seq) /** * @flavor Ruby: Array#at * - * @note 序号从1开始 + * @note 序号从0开始 (为了保持C语言的惯性) * * @return 如果seq中并没有第n个数据,则返回NULL * @@ -1771,12 +1771,13 @@ xy_seq_at (XySeq_t *seq, int n) { xy_cant_be_null (seq); - if (0 == n) xy_throw ("The index must begin from 1, not 0"); + if (n < 0) xy_throw ("The XySeq_t index must be non-negative"); + if (n >= seq->length) xy_throw ("The XySeq_t index is out of range"); - if (1 == n) return seq->first_item ? seq->first_item->data : NULL; + if (0 == n) return seq->first_item ? seq->first_item->data : NULL; XySeqItem_t *it = seq->first_item; - for (uint32_t i = 1; i < n && it; i++) + for (size_t i = 0; i < n && it; i++) { it = it->next; } diff --git a/src/chsrc-main.c b/src/chsrc-main.c index c5aa4b63..ef916307 100644 --- a/src/chsrc-main.c +++ b/src/chsrc-main.c @@ -121,7 +121,7 @@ cli_print_all_mirror_sites () println ("--------- -------------- ------------------------------------- ---------------------"); } - for (int i=1; i<=xy_seq_len(ProgStore.mirror_sites); i++) + for (size_t i=0; icode, mir->abbr, mir->site, mir->name); @@ -172,28 +172,25 @@ iterate_aliases (const char *aliases, bool (*callback)(const char *alias, void * /** - * 用于 cli_print_targets_for_menu 的回调函数,打印每个别名 + * 用于 cli_print_targets_for_menu() 的回调函数,打印每个别名 */ bool -callback_print_alias (const char *alias, void *user_data) +callback_print_alias (const char *alias, void *DUMMY) { printf ("%s ", alias); return false; // 继续遍历,不停止 } -void -callback_print_targets (void *data, void *DUMMY) -{ - Target_t *target = (Target_t *) data; - // 使用通用的别名遍历函数打印所有别名 - iterate_aliases (target->aliases, callback_print_alias, NULL); - br(); // 每个target换行 -} - void cli_print_targets_for_menu (XySeq_t *menu) { - xy_seq_each (menu, callback_print_targets, NULL); + for (size_t i=0; ialiases, callback_print_alias, NULL); + br(); // 每个target换行 + } br(); // 最后额外换行 } @@ -265,7 +262,7 @@ cli_print_menu (char *menu) void cli_print_target_available_sources (Source_t sources[], size_t size) { - for (int i=0;ichefs && xy_seq_len(target->chefs) > 0) { printf ("%s", bdblue(msg)); - for (size_t i = 1; i <= xy_seq_len(target->chefs); i++) + for (size_t i=0; ichefs); i++) { - if (i > 1) printf (", "); + if (i > 0) printf (", "); Contributor_t *chef = xy_seq_at (target->chefs, i); printf ("%s <%s>", chef->name ? chef->name : "Unknown", @@ -470,9 +467,9 @@ cli_print_target_maintain_info (Target_t *target, const char *input_target_name) if (target->sauciers && xy_seq_len(target->sauciers) > 0) { printf ("%s", bdblue(msg)); - for (size_t i = 1; i <= xy_seq_len(target->sauciers); i++) + for (size_t i=0; isauciers); i++) { - if (i > 1) printf (", "); + if (i > 0) printf (", "); Contributor_t *saucier = xy_seq_at (target->sauciers, i); printf ("%s <%s>", saucier->name, saucier->email ); } @@ -708,7 +705,7 @@ get_target (const char *input, TargetOp code, char *option) chsrc_log (bdyellow(CHINESE ? zh_msg : en_msg)); - for (size_t i = 1; i <= sub_count; i++) + for (size_t i=0; isub_targets, i); if (!sub_target->inited) diff --git a/test/xy.c b/test/xy.c index 99e31c93..37b3ee98 100644 --- a/test/xy.c +++ b/test/xy.c @@ -182,8 +182,9 @@ main (int argc, char const *argv[]) XySeq_t *seq = xy_seq_new (); xy_seq_push (seq, "Hello"); xy_seq_push (seq, "World"); - assert_str ("Hello", xy_seq_at (seq, 1)); - assert_str ("World", xy_seq_at (seq, 2)); + assert_str ("Hello", xy_seq_at (seq, 0)); + assert_str ("World", xy_seq_at (seq, 1)); + // assert_str ("World", xy_seq_at (seq, 2)); // out-of-rage throw xy_seq_each (seq, callback_print_str_for_seq, NULL); br(); xy_seq_pop (seq); assert (1 == xy_seq_len (seq));