t/helper/test-oidmap.c: avoid using `strtok()`

Apply similar treatment as in the previous commit to remove usage of `strtok()` from the "oidmap" test helper. Signed-off-by: Taylor Blau <me@ttaylorr.com> Signed-off-by: Junio C Hamano <gitster@pobox.com>

Taylor Blau committed Apr 24, 2023 at 18:20 UTC deeabc1ff07fed102e90f9037aed749b16abd9a6
1 file changed +14 -6
t/helper/test-oidmap.c
+14 -6
@@ -4,6 +4,7 @@
4 #include "oidmap.h"
5 #include "setup.h"
6 #include "strbuf.h"
7 +#include "string-list.h"
8
9 /* key is an oid and value is a name (could be a refname for example) */
10 struct test_entry {
@@ -25,6 +26,7 @@ struct test_entry {
26 */
27 int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
28 {
29 + struct string_list parts = STRING_LIST_INIT_NODUP;
30 struct strbuf line = STRBUF_INIT;
31 struct oidmap map = OIDMAP_INIT;
32
@@ -35,19 +37,24 @@ int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
37
38 /* process commands from stdin */
39 while (strbuf_getline(&line, stdin) != EOF) {
38 - char *cmd, *p1 = NULL, *p2 = NULL;
40 + char *cmd, *p1, *p2;
41 struct test_entry *entry;
42 struct object_id oid;
43
44 /* break line into command and up to two parameters */
43 - cmd = strtok(line.buf, DELIM);
45 + string_list_setlen(&parts, 0);
46 + string_list_split_in_place(&parts, line.buf, DELIM, 2);
47 + string_list_remove_empty_items(&parts, 0);
48 +
49 /* ignore empty lines */
45 - if (!cmd || *cmd == '#')
50 + if (!parts.nr)
51 + continue;
52 + if (!*parts.items[0].string || *parts.items[0].string == '#')
53 continue;
54
48 - p1 = strtok(NULL, DELIM);
49 - if (p1)
50 - p2 = strtok(NULL, DELIM);
55 + cmd = parts.items[0].string;
56 + p1 = parts.nr >= 1 ? parts.items[1].string : NULL;
57 + p2 = parts.nr >= 2 ? parts.items[2].string : NULL;
58
59 if (!strcmp("put", cmd) && p1 && p2) {
60
@@ -108,6 +115,7 @@ int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
115 }
116 }
117
118 + string_list_clear(&parts, 0);
119 strbuf_release(&line);
120 oidmap_free(&map, 1);
121 return 0;