Skip to content

Commit deeabc1

Browse files
ttaylorrgitster
authored andcommitted
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 <[email protected]> Signed-off-by: Junio C Hamano <[email protected]>
1 parent 826f0e3 commit deeabc1

File tree

1 file changed

+14
-6
lines changed

1 file changed

+14
-6
lines changed

t/helper/test-oidmap.c

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@
44
#include "oidmap.h"
55
#include "setup.h"
66
#include "strbuf.h"
7+
#include "string-list.h"
78

89
/* key is an oid and value is a name (could be a refname for example) */
910
struct test_entry {
@@ -25,6 +26,7 @@ struct test_entry {
2526
*/
2627
int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
2728
{
29+
struct string_list parts = STRING_LIST_INIT_NODUP;
2830
struct strbuf line = STRBUF_INIT;
2931
struct oidmap map = OIDMAP_INIT;
3032

@@ -35,19 +37,24 @@ int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
3537

3638
/* process commands from stdin */
3739
while (strbuf_getline(&line, stdin) != EOF) {
38-
char *cmd, *p1 = NULL, *p2 = NULL;
40+
char *cmd, *p1, *p2;
3941
struct test_entry *entry;
4042
struct object_id oid;
4143

4244
/* 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+
4449
/* ignore empty lines */
45-
if (!cmd || *cmd == '#')
50+
if (!parts.nr)
51+
continue;
52+
if (!*parts.items[0].string || *parts.items[0].string == '#')
4653
continue;
4754

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;
5158

5259
if (!strcmp("put", cmd) && p1 && p2) {
5360

@@ -108,6 +115,7 @@ int cmd__oidmap(int argc UNUSED, const char **argv UNUSED)
108115
}
109116
}
110117

118+
string_list_clear(&parts, 0);
111119
strbuf_release(&line);
112120
oidmap_free(&map, 1);
113121
return 0;

0 commit comments

Comments
 (0)