diff options
author | Junio C Hamano <gitster@pobox.com> | 2022-09-06 04:33:40 +0300 |
---|---|---|
committer | Junio C Hamano <gitster@pobox.com> | 2022-09-06 04:33:40 +0300 |
commit | 5784d201dad27453d8b83dd370ce702a26ae8577 (patch) | |
tree | 7f5d33336cdcebda93b2d2bf18fbebaf7bfa1c24 /t/helper | |
parent | b5d2e9924f0f4b605e7452922e7a247864f31192 (diff) | |
parent | c333c2ce651d5b363ec67d33559ebd9a717ced68 (diff) |
Merge branch 'rs/test-mergesort'
Optimization of a test-helper command.
* rs/test-mergesort:
test-mergesort: use mem_pool for sort input
test-mergesort: read sort input all at once
Diffstat (limited to 't/helper')
-rw-r--r-- | t/helper/test-mergesort.c | 40 |
1 files changed, 27 insertions, 13 deletions
diff --git a/t/helper/test-mergesort.c b/t/helper/test-mergesort.c index 202e54a7ff..335e5bb3a9 100644 --- a/t/helper/test-mergesort.c +++ b/t/helper/test-mergesort.c @@ -22,21 +22,35 @@ static int compare_strings(const struct line *x, const struct line *y) static int sort_stdin(void) { - struct line *line, *p = NULL, *lines = NULL; + struct line *lines; + struct line **tail = &lines; struct strbuf sb = STRBUF_INIT; - - while (!strbuf_getline(&sb, stdin)) { - line = xmalloc(sizeof(struct line)); - line->text = strbuf_detach(&sb, NULL); - if (p) { - line->next = p->next; - p->next = line; - } else { - line->next = NULL; - lines = line; - } - p = line; + struct mem_pool lines_pool; + char *p; + + strbuf_read(&sb, 0, 0); + + /* + * Split by newline, but don't create an item + * for the empty string after the last separator. + */ + if (sb.len && sb.buf[sb.len - 1] == '\n') + strbuf_setlen(&sb, sb.len - 1); + + mem_pool_init(&lines_pool, 0); + p = sb.buf; + for (;;) { + char *eol = strchr(p, '\n'); + struct line *line = mem_pool_alloc(&lines_pool, sizeof(*line)); + line->text = p; + *tail = line; + tail = &line->next; + if (!eol) + break; + *eol = '\0'; + p = eol + 1; } + *tail = NULL; sort_lines(&lines, compare_strings); |