From 100444225380d3f5ca29424ea54703d308c7c651 Mon Sep 17 00:00:00 2001 From: Rusty Russell Date: Tue, 18 Jan 2011 11:14:46 +1030 Subject: [PATCH] strsplit: remove nump argument You can use talloc_array_length() to get the length of a tallocated array. --- ccan/grab_file/grab_file.h | 4 ++-- ccan/grab_file/test/run-grab.c | 2 +- ccan/str_talloc/_info | 2 +- ccan/str_talloc/str_talloc.c | 9 ++++----- ccan/str_talloc/str_talloc.h | 13 ++++++------- ccan/str_talloc/test/run.c | 20 ++++++-------------- ccan/tdb/tools/replay_trace.c | 6 +++--- tools/ccanlint/ccanlint.c | 6 +++--- tools/ccanlint/file_analysis.c | 7 ++++--- tools/ccanlint/tests/tests_coverage.c | 2 +- tools/ccanlint/tests/tests_pass_valgrind.c | 6 +++--- tools/depends.c | 20 ++++++++++++-------- tools/doc_extract-core.c | 12 ++++++------ tools/doc_extract.c | 5 ++--- tools/doc_extract.h | 2 +- tools/namespacize.c | 2 +- 16 files changed, 56 insertions(+), 62 deletions(-) diff --git a/ccan/grab_file/grab_file.h b/ccan/grab_file/grab_file.h index b2bd31ad..5d6d018c 100644 --- a/ccan/grab_file/grab_file.h +++ b/ccan/grab_file/grab_file.h @@ -25,7 +25,7 @@ * all = grab_fd(NULL, 0, NULL); * if (!all) * return NULL; - * lines = strsplit(NULL, all, "\n", NULL); + * lines = strsplit(NULL, all, "\n"); * talloc_free(all); * return lines; * } @@ -52,7 +52,7 @@ void *grab_fd(const void *ctx, int fd, size_t *size); * all = grab_file(NULL, filename, NULL); * if (!all) * return NULL; - * lines = strsplit(NULL, all, "\n", NULL); + * lines = strsplit(NULL, all, "\n"); * talloc_free(all); * return lines; * } diff --git a/ccan/grab_file/test/run-grab.c b/ccan/grab_file/test/run-grab.c index 67be97c1..deb466e9 100644 --- a/ccan/grab_file/test/run-grab.c +++ b/ccan/grab_file/test/run-grab.c @@ -18,7 +18,7 @@ main(int argc, char *argv[]) struct stat st; str = grab_file(NULL, "test/run-grab.c", NULL); - split = strsplit(NULL, str, "\n", NULL); + split = strsplit(NULL, str, "\n"); length = strlen(split[0]); ok1(!strcmp(split[0], "/* This is test for grab_file() function")); for (i = 1; split[i]; i++) diff --git a/ccan/str_talloc/_info b/ccan/str_talloc/_info index f6214ce7..4edee27b 100644 --- a/ccan/str_talloc/_info +++ b/ccan/str_talloc/_info @@ -25,7 +25,7 @@ * textfile = grab_file(NULL, argv[1], NULL); * if (!textfile) * err(1, "Failed reading %s", argv[1]); - * lines = strsplit(textfile, textfile, "\n", NULL); + * lines = strsplit(textfile, textfile, "\n"); * * // Join them back together with two linefeeds. * printf("%s", strjoin(textfile, lines, "\n\n")); diff --git a/ccan/str_talloc/str_talloc.c b/ccan/str_talloc/str_talloc.c index e2d12df8..3bcb1f2a 100644 --- a/ccan/str_talloc/str_talloc.c +++ b/ccan/str_talloc/str_talloc.c @@ -11,8 +11,7 @@ #include #include -char **strsplit(const void *ctx, const char *string, const char *delims, - unsigned int *nump) +char **strsplit(const void *ctx, const char *string, const char *delims) { char **lines = NULL; unsigned int max = 64, num = 0; @@ -30,9 +29,9 @@ char **strsplit(const void *ctx, const char *string, const char *delims, lines = talloc_realloc(ctx, lines, char *, max*=2 + 1); } lines[num] = NULL; - if (nump) - *nump = num; - return lines; + + /* Shrink, so talloc_get_size works */ + return talloc_realloc(ctx, lines, char *, num+1); } char *strjoin(const void *ctx, char *strings[], const char *delim) diff --git a/ccan/str_talloc/str_talloc.h b/ccan/str_talloc/str_talloc.h index 3c65f9f8..337422de 100644 --- a/ccan/str_talloc/str_talloc.h +++ b/ccan/str_talloc/str_talloc.h @@ -8,7 +8,6 @@ * @ctx: the context to tallocate from (often NULL) * @string: the string to split * @delims: delimiters where lines should be split. - * @nump: optional pointer to place resulting number of lines * * This function splits a single string into multiple strings. The * original string is untouched: an array is allocated (using talloc) @@ -16,8 +15,9 @@ * in empty substrings. By definition, no delimiters will appear in * the substrings. * - * The final char * in the array will be NULL, so you can use this or - * @nump to find the array length. + * The final char * in the array will be NULL, talloc_array_length() of the + * returned value is 1 greater than the number of valid elements in + * the array. * * Example: * #include @@ -29,7 +29,7 @@ * unsigned int i, long_lines = 0; * * // Can only fail on out-of-memory. - * lines = strsplit(NULL, string, "\n", NULL); + * lines = strsplit(NULL, string, "\n"); * for (i = 0; lines[i] != NULL; i++) * if (strlen(lines[i]) > 80) * long_lines++; @@ -37,8 +37,7 @@ * return long_lines; * } */ -char **strsplit(const void *ctx, const char *string, const char *delims, - unsigned int *nump); +char **strsplit(const void *ctx, const char *string, const char *delims); /** * strjoin - Join an array of substrings into one long string @@ -56,7 +55,7 @@ char **strsplit(const void *ctx, const char *string, const char *delims, * { * char **lines, *ret; * - * lines = strsplit(NULL, string, "\n", NULL); + * lines = strsplit(NULL, string, "\n"); * ret = strjoin(NULL, lines, "-- EOL\n"); * talloc_free(lines); * return ret; diff --git a/ccan/str_talloc/test/run.c b/ccan/str_talloc/test/run.c index c8cc6217..6b7fe14e 100644 --- a/ccan/str_talloc/test/run.c +++ b/ccan/str_talloc/test/run.c @@ -4,34 +4,26 @@ #include #include -/* FIXME: ccanize */ #define ARRAY_SIZE(arr) (sizeof(arr) / sizeof(arr[0])) static char *substrings[] = { "far", "bar", "baz", "b", "ba", "z", "ar", NULL }; int main(int argc, char *argv[]) { - unsigned int n; char **split, *str; void *ctx; - plan_tests(19); - split = strsplit(NULL, "hello world", " ", &n); - ok1(n == 3); + plan_tests(16); + split = strsplit(NULL, "hello world", " "); + ok1(talloc_array_length(split) == 4); ok1(!strcmp(split[0], "hello")); ok1(!strcmp(split[1], "")); ok1(!strcmp(split[2], "world")); ok1(split[3] == NULL); talloc_free(split); - split = strsplit(NULL, "hello world", " ", NULL); - ok1(!strcmp(split[0], "hello")); - ok1(!strcmp(split[1], "")); - ok1(!strcmp(split[2], "world")); - ok1(split[3] == NULL); - talloc_free(split); - - split = strsplit(NULL, "hello world", "o ", NULL); + split = strsplit(NULL, "hello world", "o "); + ok1(talloc_array_length(split) == 6); ok1(!strcmp(split[0], "hell")); ok1(!strcmp(split[1], "")); ok1(!strcmp(split[2], "")); @@ -40,7 +32,7 @@ int main(int argc, char *argv[]) ok1(split[5] == NULL); ctx = split; - split = strsplit(ctx, "hello world", "o ", NULL); + split = strsplit(ctx, "hello world", "o "); ok1(talloc_parent(split) == ctx); talloc_free(ctx); diff --git a/ccan/tdb/tools/replay_trace.c b/ccan/tdb/tools/replay_trace.c index b42fb7b1..0e799a68 100644 --- a/ccan/tdb/tools/replay_trace.c +++ b/ccan/tdb/tools/replay_trace.c @@ -872,11 +872,11 @@ static struct op *load_tracefile(char *filename[], if (!contents) err(1, "Reading %s", filename[file]); - lines = strsplit(contents, contents, "\n", NULL); + lines = strsplit(contents, contents, "\n"); if (!lines[0]) errx(1, "%s is empty", filename[file]); - words = strsplit(lines, lines[0], " ", NULL); + words = strsplit(lines, lines[0], " "); if (!streq(words[1], "tdb_open")) fail(filename[file], 1, "does not start with tdb_open"); @@ -887,7 +887,7 @@ static struct op *load_tracefile(char *filename[], for (i = 1; lines[i]; i++) { const struct op_table *opt; - words = strsplit(lines, lines[i], " ", NULL); + words = strsplit(lines, lines[i], " "); if (!words[0] || !words[1]) fail(filename[file], i+1, "Expected seqnum number and op"); diff --git a/tools/ccanlint/ccanlint.c b/tools/ccanlint/ccanlint.c index 3ffe38b2..15d131e2 100644 --- a/tools/ccanlint/ccanlint.c +++ b/tools/ccanlint/ccanlint.c @@ -238,7 +238,7 @@ static void init_tests(void) /* Resolve dependencies. */ foreach_ptr(list, &compulsory_tests, &normal_tests) { list_for_each(list, c, list) { - char **deps = strsplit(NULL, c->needs, " ", NULL); + char **deps = strsplit(NULL, c->needs, " "); unsigned int i; for (i = 0; deps[i]; i++) { @@ -410,8 +410,8 @@ static void add_info_options(struct ccan_file *info, bool mark_fails) continue; for (i = 0; i < d->num_lines; i++) { - char **words = collapse(strsplit(d, d->lines[i], " \t", - NULL), NULL); + char **words = collapse(strsplit(d, d->lines[i], " \t"), + NULL); if (!words[0]) continue; diff --git a/tools/ccanlint/file_analysis.c b/tools/ccanlint/file_analysis.c index b941f94e..985f259b 100644 --- a/tools/ccanlint/file_analysis.c +++ b/tools/ccanlint/file_analysis.c @@ -54,9 +54,10 @@ const char *get_ccan_file_contents(struct ccan_file *f) char **get_ccan_file_lines(struct ccan_file *f) { if (!f->lines) - f->lines = strsplit(f, get_ccan_file_contents(f), - "\n", &f->num_lines); + f->lines = strsplit(f, get_ccan_file_contents(f), "\n"); + /* FIXME: is f->num_lines necessary? */ + f->num_lines = talloc_array_length(f->lines) - 1; return f->lines; } @@ -64,7 +65,7 @@ struct list_head *get_ccan_file_docs(struct ccan_file *f) { if (!f->doc_sections) { get_ccan_file_lines(f); - f->doc_sections = extract_doc_sections(f->lines, f->num_lines); + f->doc_sections = extract_doc_sections(f->lines); } return f->doc_sections; } diff --git a/tools/ccanlint/tests/tests_coverage.c b/tools/ccanlint/tests/tests_coverage.c index ec9774da..a2b9ee32 100644 --- a/tools/ccanlint/tests/tests_coverage.c +++ b/tools/ccanlint/tests/tests_coverage.c @@ -54,7 +54,7 @@ static unsigned int score_coverage(float covered, unsigned total) static void analyze_coverage(struct manifest *m, bool full_gcov, const char *output, struct score *score) { - char **lines = strsplit(score, output, "\n", NULL); + char **lines = strsplit(score, output, "\n"); float covered_lines = 0.0; unsigned int i, total_lines = 0; bool lines_matter = false; diff --git a/tools/ccanlint/tests/tests_pass_valgrind.c b/tools/ccanlint/tests/tests_pass_valgrind.c index 4365f2a6..4c4338b6 100644 --- a/tools/ccanlint/tests/tests_pass_valgrind.c +++ b/tools/ccanlint/tests/tests_pass_valgrind.c @@ -52,11 +52,11 @@ static bool blank_line(const char *line) static char *get_leaks(const char *output, char **errs) { char *leaks = talloc_strdup(output, ""); - unsigned int i, num; - char **lines = strsplit(output, output, "\n", &num); + unsigned int i; + char **lines = strsplit(output, output, "\n"); *errs = talloc_strdup(output, ""); - for (i = 0; i < num; i++) { + for (i = 0; i < talloc_array_length(lines) - 1; i++) { if (strstr(lines[i], " lost ")) { /* A leak... */ if (strstr(lines[i], " definitely lost ")) { diff --git a/tools/depends.c b/tools/depends.c index 4bac1b06..ee39a12f 100644 --- a/tools/depends.c +++ b/tools/depends.c @@ -12,8 +12,8 @@ #include #include -static char ** __attribute__((format(printf, 3, 4))) -lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...) +static char ** __attribute__((format(printf, 2, 3))) +lines_from_cmd(const void *ctx, char *format, ...) { va_list ap; char *cmd, *buffer; @@ -32,7 +32,7 @@ lines_from_cmd(const void *ctx, unsigned int *num, char *format, ...) err(1, "Reading from '%s'", cmd); pclose(p); - return strsplit(ctx, buffer, "\n", num); + return strsplit(ctx, buffer, "\n"); } /* Be careful about trying to compile over running programs (parallel make). @@ -80,9 +80,11 @@ static char **get_one_deps(const void *ctx, const char *dir, } cmd = talloc_asprintf(ctx, "%s depends", *infofile); - deps = lines_from_cmd(cmd, num, "%s", cmd); + deps = lines_from_cmd(cmd, "%s", cmd); if (!deps) err(1, "Could not run '%s'", cmd); + /* FIXME: Do we need num arg? */ + *num = talloc_array_length(deps) - 1; return deps; } @@ -120,7 +122,7 @@ static char **get_one_safe_deps(const void *ctx, char **infofile) { char **deps, **lines, *raw, *fname; - unsigned int i, n = 0; + unsigned int i, n; fname = talloc_asprintf(ctx, "%s/_info", dir); raw = grab_file(fname, fname, NULL); @@ -128,9 +130,9 @@ static char **get_one_safe_deps(const void *ctx, errx(1, "Could not open %s", fname); /* Replace \n by actual line breaks, and split it. */ - lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n", &n); + lines = strsplit(raw, replace(raw, raw, "\\n", "\n"), "\n"); - deps = talloc_array(ctx, char *, n+1); + deps = talloc_array(ctx, char *, talloc_array_length(lines)); for (n = i = 0; lines[i]; i++) { char *str; @@ -222,9 +224,11 @@ char **get_libs(const void *ctx, const char *dir, } cmd = talloc_asprintf(ctx, "%s libs", *infofile); - libs = lines_from_cmd(cmd, num, "%s", cmd); + libs = lines_from_cmd(cmd, "%s", cmd); if (!libs) err(1, "Could not run '%s'", cmd); + /* FIXME: Do we need num arg? */ + *num = talloc_array_length(libs) - 1; return libs; } diff --git a/tools/doc_extract-core.c b/tools/doc_extract-core.c index d7490598..781ce2bd 100644 --- a/tools/doc_extract-core.c +++ b/tools/doc_extract-core.c @@ -15,14 +15,14 @@ #include "doc_extract.h" #include "tools.h" -static char **grab_doc(char **lines, unsigned int num, unsigned int **linemap) +static char **grab_doc(char **lines, unsigned int **linemap) { char **ret; - unsigned int i; + unsigned int i, num; bool printing = false; - ret = talloc_array(NULL, char *, num+1); - *linemap = talloc_array(ret, unsigned int, num); + ret = talloc_array(NULL, char *, talloc_array_length(lines)); + *linemap = talloc_array(ret, unsigned int, talloc_array_length(lines)); num = 0; for (i = 0; lines[i]; i++) { @@ -129,10 +129,10 @@ static void add_line(struct doc_section *curr, const char *line) curr->lines[curr->num_lines++] = talloc_strdup(curr->lines, line); } -struct list_head *extract_doc_sections(char **rawlines, unsigned int num) +struct list_head *extract_doc_sections(char **rawlines) { unsigned int *linemap; - char **lines = grab_doc(rawlines, num, &linemap); + char **lines = grab_doc(rawlines, &linemap); const char *function = NULL; struct doc_section *curr = NULL; unsigned int i; diff --git a/tools/doc_extract.c b/tools/doc_extract.c index d7617a07..5a48b79f 100644 --- a/tools/doc_extract.c +++ b/tools/doc_extract.c @@ -27,16 +27,15 @@ int main(int argc, char *argv[]) type = argv[1]; for (i = 2; i < argc; i++) { char *file, **lines; - unsigned int num; struct list_head *list; struct doc_section *d; file = grab_file(NULL, argv[i], NULL); if (!file) err(1, "Reading file %s", argv[i]); - lines = strsplit(file, file, "\n", &num); + lines = strsplit(file, file, "\n"); - list = extract_doc_sections(lines, num); + list = extract_doc_sections(lines); if (list_empty(list)) errx(1, "No documentation in file %s", argv[i]); talloc_free(file); diff --git a/tools/doc_extract.h b/tools/doc_extract.h index 24d076c9..1f4650a1 100644 --- a/tools/doc_extract.h +++ b/tools/doc_extract.h @@ -13,5 +13,5 @@ struct doc_section { char **lines; }; -struct list_head *extract_doc_sections(char **rawlines, unsigned int num); +struct list_head *extract_doc_sections(char **rawlines); #endif /* _DOC_EXTRACT_CORE_H */ diff --git a/tools/namespacize.c b/tools/namespacize.c index 545b28bb..5ac85f50 100644 --- a/tools/namespacize.c +++ b/tools/namespacize.c @@ -445,7 +445,7 @@ static struct replace *read_replacement_file(const char *depdir) return NULL; } - for (line = strsplit(file, file, "\n", NULL); *line; line++) + for (line = strsplit(file, file, "\n"); *line; line++) add_replace(&repl, *line); return repl; } -- 2.39.2