Skip to content

Commit 395b782

Browse files
committed
Merge branch 'jc/strbuf-split' into jch
Arrays of strbuf is often a wrong data structure to use, and strbuf_split*() family of functions that create them often have better alternatives. Update several code paths and replace strbuf_split*(). * jc/strbuf-split: trace2: do not use strbuf_split*() trace2: trim_trailing_newline followed by trim is a no-op sub-process: do not use strbuf_split*() environment: do not use strbuf_split*() config: do not use strbuf_split() notes: do not use strbuf_split*() merge-tree: do not use strbuf_split*() clean: do not use strbuf_split*() [part 2] clean: do not pass the whole structure when it is not necessary clean: do not use strbuf_split*() [part 1] clean: do not pass strbuf by value wt-status: avoid strbuf_split*()
2 parents bde64a6 + 838fe56 commit 395b782

File tree

8 files changed

+129
-166
lines changed

8 files changed

+129
-166
lines changed

builtin/clean.c

Lines changed: 36 additions & 38 deletions
Original file line numberDiff line numberDiff line change
@@ -478,43 +478,39 @@ static int find_unique(const char *choice, struct menu_stuff *menu_stuff)
478478
*/
479479
static int parse_choice(struct menu_stuff *menu_stuff,
480480
int is_single,
481-
struct strbuf input,
481+
char *input,
482482
int **chosen)
483483
{
484-
struct strbuf **choice_list, **ptr;
484+
struct string_list choice = STRING_LIST_INIT_NODUP;
485+
struct string_list_item *item;
485486
int nr = 0;
486487
int i;
487488

488-
if (is_single) {
489-
choice_list = strbuf_split_max(&input, '\n', 0);
490-
} else {
491-
char *p = input.buf;
492-
do {
493-
if (*p == ',')
494-
*p = ' ';
495-
} while (*p++);
496-
choice_list = strbuf_split_max(&input, ' ', 0);
497-
}
489+
string_list_split_in_place_f(&choice, input,
490+
is_single ? "\n" : ", ", -1,
491+
STRING_LIST_SPLIT_TRIM);
498492

499-
for (ptr = choice_list; *ptr; ptr++) {
500-
char *p;
501-
int choose = 1;
493+
for_each_string_list_item(item, &choice) {
494+
const char *string;
495+
int choose;
502496
int bottom = 0, top = 0;
503497
int is_range, is_number;
504498

505-
strbuf_trim(*ptr);
506-
if (!(*ptr)->len)
499+
string = item->string;
500+
if (!*string)
507501
continue;
508502

509503
/* Input that begins with '-'; unchoose */
510-
if (*(*ptr)->buf == '-') {
504+
if (string[0] == '-') {
511505
choose = 0;
512-
strbuf_remove((*ptr), 0, 1);
506+
string++;
507+
} else {
508+
choose = 1;
513509
}
514510

515511
is_range = 0;
516512
is_number = 1;
517-
for (p = (*ptr)->buf; *p; p++) {
513+
for (const char *p = string; *p; p++) {
518514
if ('-' == *p) {
519515
if (!is_range) {
520516
is_range = 1;
@@ -532,27 +528,27 @@ static int parse_choice(struct menu_stuff *menu_stuff,
532528
}
533529

534530
if (is_number) {
535-
bottom = atoi((*ptr)->buf);
531+
bottom = atoi(string);
536532
top = bottom;
537533
} else if (is_range) {
538-
bottom = atoi((*ptr)->buf);
534+
bottom = atoi(string);
539535
/* a range can be specified like 5-7 or 5- */
540-
if (!*(strchr((*ptr)->buf, '-') + 1))
536+
if (!*(strchr(string, '-') + 1))
541537
top = menu_stuff->nr;
542538
else
543-
top = atoi(strchr((*ptr)->buf, '-') + 1);
544-
} else if (!strcmp((*ptr)->buf, "*")) {
539+
top = atoi(strchr(string, '-') + 1);
540+
} else if (!strcmp(string, "*")) {
545541
bottom = 1;
546542
top = menu_stuff->nr;
547543
} else {
548-
bottom = find_unique((*ptr)->buf, menu_stuff);
544+
bottom = find_unique(string, menu_stuff);
549545
top = bottom;
550546
}
551547

552548
if (top <= 0 || bottom <= 0 || top > menu_stuff->nr || bottom > top ||
553549
(is_single && bottom != top)) {
554550
clean_print_color(CLEAN_COLOR_ERROR);
555-
printf(_("Huh (%s)?\n"), (*ptr)->buf);
551+
printf(_("Huh (%s)?\n"), string);
556552
clean_print_color(CLEAN_COLOR_RESET);
557553
continue;
558554
}
@@ -561,7 +557,7 @@ static int parse_choice(struct menu_stuff *menu_stuff,
561557
(*chosen)[i-1] = choose;
562558
}
563559

564-
strbuf_list_free(choice_list);
560+
string_list_clear(&choice, 0);
565561

566562
for (i = 0; i < menu_stuff->nr; i++)
567563
nr += (*chosen)[i];
@@ -631,7 +627,7 @@ static int *list_and_choose(struct menu_opts *opts, struct menu_stuff *stuff)
631627

632628
nr = parse_choice(stuff,
633629
opts->flags & MENU_OPTS_SINGLETON,
634-
choice,
630+
choice.buf,
635631
&chosen);
636632

637633
if (opts->flags & MENU_OPTS_SINGLETON) {
@@ -679,12 +675,13 @@ static int filter_by_patterns_cmd(void)
679675
{
680676
struct dir_struct dir = DIR_INIT;
681677
struct strbuf confirm = STRBUF_INIT;
682-
struct strbuf **ignore_list;
683-
struct string_list_item *item;
684678
struct pattern_list *pl;
685679
int changed = -1, i;
686680

687681
for (;;) {
682+
struct string_list ignore_list = STRING_LIST_INIT_NODUP;
683+
struct string_list_item *item;
684+
688685
if (!del_list.nr)
689686
break;
690687

@@ -702,14 +699,15 @@ static int filter_by_patterns_cmd(void)
702699
break;
703700

704701
pl = add_pattern_list(&dir, EXC_CMDL, "manual exclude");
705-
ignore_list = strbuf_split_max(&confirm, ' ', 0);
706702

707-
for (i = 0; ignore_list[i]; i++) {
708-
strbuf_trim(ignore_list[i]);
709-
if (!ignore_list[i]->len)
710-
continue;
703+
string_list_split_in_place_f(&ignore_list, confirm.buf, " ", -1,
704+
STRING_LIST_SPLIT_TRIM);
711705

712-
add_pattern(ignore_list[i]->buf, "", 0, pl, -(i+1));
706+
for (i = 0; i < ignore_list.nr; i++) {
707+
item = &ignore_list.items[i];
708+
if (!*item->string)
709+
continue;
710+
add_pattern(item->string, "", 0, pl, -(i+1));
713711
}
714712

715713
changed = 0;
@@ -730,7 +728,7 @@ static int filter_by_patterns_cmd(void)
730728
clean_print_color(CLEAN_COLOR_RESET);
731729
}
732730

733-
strbuf_list_free(ignore_list);
731+
string_list_clear(&ignore_list, 0);
734732
dir_clear(&dir);
735733
}
736734

builtin/merge-tree.c

Lines changed: 16 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -619,32 +619,34 @@ int cmd_merge_tree(int argc,
619619
"--merge-base", "--stdin");
620620
line_termination = '\0';
621621
while (strbuf_getline_lf(&buf, stdin) != EOF) {
622-
struct strbuf **split;
622+
struct string_list split = STRING_LIST_INIT_NODUP;
623623
const char *input_merge_base = NULL;
624624

625-
split = strbuf_split(&buf, ' ');
626-
if (!split[0] || !split[1])
625+
string_list_split_in_place_f(&split, buf.buf, " ", -1,
626+
STRING_LIST_SPLIT_TRIM);
627+
628+
if (split.nr < 2)
627629
die(_("malformed input line: '%s'."), buf.buf);
628-
strbuf_rtrim(split[0]);
629-
strbuf_rtrim(split[1]);
630630

631631
/* parse the merge-base */
632-
if (!strcmp(split[1]->buf, "--")) {
633-
input_merge_base = split[0]->buf;
632+
if (!strcmp(split.items[1].string, "--")) {
633+
input_merge_base = split.items[0].string;
634634
}
635635

636-
if (input_merge_base && split[2] && split[3] && !split[4]) {
637-
strbuf_rtrim(split[2]);
638-
strbuf_rtrim(split[3]);
639-
real_merge(&o, input_merge_base, split[2]->buf, split[3]->buf, prefix);
640-
} else if (!input_merge_base && !split[2]) {
641-
real_merge(&o, NULL, split[0]->buf, split[1]->buf, prefix);
636+
if (input_merge_base && split.nr == 4) {
637+
real_merge(&o, input_merge_base,
638+
split.items[2].string, split.items[3].string,
639+
prefix);
640+
} else if (!input_merge_base && split.nr == 2) {
641+
real_merge(&o, NULL,
642+
split.items[0].string, split.items[1].string,
643+
prefix);
642644
} else {
643645
die(_("malformed input line: '%s'."), buf.buf);
644646
}
645647
maybe_flush_or_die(stdout, "stdout");
646648

647-
strbuf_list_free(split);
649+
string_list_clear(&split, 0);
648650
}
649651
strbuf_release(&buf);
650652

builtin/notes.c

Lines changed: 12 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -376,18 +376,19 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
376376

377377
while (strbuf_getline_lf(&buf, stdin) != EOF) {
378378
struct object_id from_obj, to_obj;
379-
struct strbuf **split;
379+
struct string_list split = STRING_LIST_INIT_NODUP;
380380
int err;
381381

382-
split = strbuf_split(&buf, ' ');
383-
if (!split[0] || !split[1])
382+
string_list_split_in_place_f(&split, buf.buf, " ", -1,
383+
STRING_LIST_SPLIT_TRIM);
384+
if (split.nr < 2)
384385
die(_("malformed input line: '%s'."), buf.buf);
385-
strbuf_rtrim(split[0]);
386-
strbuf_rtrim(split[1]);
387-
if (repo_get_oid(the_repository, split[0]->buf, &from_obj))
388-
die(_("failed to resolve '%s' as a valid ref."), split[0]->buf);
389-
if (repo_get_oid(the_repository, split[1]->buf, &to_obj))
390-
die(_("failed to resolve '%s' as a valid ref."), split[1]->buf);
386+
if (repo_get_oid(the_repository, split.items[0].string, &from_obj))
387+
die(_("failed to resolve '%s' as a valid ref."),
388+
split.items[0].string);
389+
if (repo_get_oid(the_repository, split.items[1].string, &to_obj))
390+
die(_("failed to resolve '%s' as a valid ref."),
391+
split.items[1].string);
391392

392393
if (rewrite_cmd)
393394
err = copy_note_for_rewrite(c, &from_obj, &to_obj);
@@ -397,11 +398,11 @@ static int notes_copy_from_stdin(int force, const char *rewrite_cmd)
397398

398399
if (err) {
399400
error(_("failed to copy notes from '%s' to '%s'"),
400-
split[0]->buf, split[1]->buf);
401+
split.items[0].string, split.items[1].string);
401402
ret = 1;
402403
}
403404

404-
strbuf_list_free(split);
405+
string_list_clear(&split, 0);
405406
}
406407

407408
if (!rewrite_cmd) {

config.c

Lines changed: 10 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -629,31 +629,28 @@ int git_config_parse_parameter(const char *text,
629629
config_fn_t fn, void *data)
630630
{
631631
const char *value;
632-
struct strbuf **pair;
632+
struct string_list pair = STRING_LIST_INIT_DUP;
633633
int ret;
634634
struct key_value_info kvi = KVI_INIT;
635635

636636
kvi_from_param(&kvi);
637637

638-
pair = strbuf_split_str(text, '=', 2);
639-
if (!pair[0])
638+
string_list_split(&pair, text, "=", 1);
639+
if (!pair.nr)
640640
return error(_("bogus config parameter: %s"), text);
641641

642-
if (pair[0]->len && pair[0]->buf[pair[0]->len - 1] == '=') {
643-
strbuf_setlen(pair[0], pair[0]->len - 1);
644-
value = pair[1] ? pair[1]->buf : "";
645-
} else {
642+
if (pair.nr == 1)
646643
value = NULL;
647-
}
644+
else
645+
value = pair.items[1].string;
648646

649-
strbuf_trim(pair[0]);
650-
if (!pair[0]->len) {
651-
strbuf_list_free(pair);
647+
if (!*pair.items[0].string) {
648+
string_list_clear(&pair, 0);
652649
return error(_("bogus config parameter: %s"), text);
653650
}
654651

655-
ret = config_parse_pair(pair[0]->buf, value, &kvi, fn, data);
656-
strbuf_list_free(pair);
652+
ret = config_parse_pair(pair.items[0].string, value, &kvi, fn, data);
653+
string_list_clear(&pair, 0);
657654
return ret;
658655
}
659656

environment.c

Lines changed: 12 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -175,10 +175,10 @@ int have_git_dir(void)
175175
const char *get_git_namespace(void)
176176
{
177177
static const char *namespace;
178-
179178
struct strbuf buf = STRBUF_INIT;
180-
struct strbuf **components, **c;
181179
const char *raw_namespace;
180+
struct string_list components = STRING_LIST_INIT_DUP;
181+
struct string_list_item *item;
182182

183183
if (namespace)
184184
return namespace;
@@ -190,12 +190,17 @@ const char *get_git_namespace(void)
190190
}
191191

192192
strbuf_addstr(&buf, raw_namespace);
193-
components = strbuf_split(&buf, '/');
193+
194+
string_list_split(&components, buf.buf, "/", -1);
194195
strbuf_reset(&buf);
195-
for (c = components; *c; c++)
196-
if (strcmp((*c)->buf, "/") != 0)
197-
strbuf_addf(&buf, "refs/namespaces/%s", (*c)->buf);
198-
strbuf_list_free(components);
196+
197+
for_each_string_list_item(item, &components) {
198+
if (item->string[0])
199+
strbuf_addf(&buf, "refs/namespaces/%s/", item->string);
200+
}
201+
string_list_clear(&components, 0);
202+
203+
strbuf_trim_trailing_dir_sep(&buf);
199204
if (check_refname_format(buf.buf, 0))
200205
die(_("bad git namespace path \"%s\""), raw_namespace);
201206
strbuf_addch(&buf, '/');

sub-process.c

Lines changed: 6 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -30,23 +30,20 @@ struct subprocess_entry *subprocess_find_entry(struct hashmap *hashmap, const ch
3030

3131
int subprocess_read_status(int fd, struct strbuf *status)
3232
{
33-
struct strbuf **pair;
34-
char *line;
3533
int len;
3634

3735
for (;;) {
36+
char *line;
37+
const char *value;
38+
3839
len = packet_read_line_gently(fd, NULL, &line);
3940
if ((len < 0) || !line)
4041
break;
41-
pair = strbuf_split_str(line, '=', 2);
42-
if (pair[0] && pair[0]->len && pair[1]) {
42+
if (skip_prefix(line, "status=", &value)) {
4343
/* the last "status=<foo>" line wins */
44-
if (!strcmp(pair[0]->buf, "status=")) {
45-
strbuf_reset(status);
46-
strbuf_addbuf(status, pair[1]);
47-
}
44+
strbuf_reset(status);
45+
strbuf_addstr(status, value);
4846
}
49-
strbuf_list_free(pair);
5047
}
5148

5249
return (len < 0) ? len : 0;

0 commit comments

Comments
 (0)