From c8f98aa2c768e01742cea37aaac9f9d690ca55dd Mon Sep 17 00:00:00 2001 From: John Kerl Date: Sat, 14 May 2016 15:40:54 -0400 Subject: [PATCH] comment-strip library routine & UT --- c/lib/mlrutil.c | 27 +++++++++++++++++++++++++++ c/lib/mlrutil.h | 4 ++++ c/todo.txt | 11 ++++++----- c/unit_test/test_mlrutil.c | 24 ++++++++++++++++++++++++ 4 files changed, 61 insertions(+), 5 deletions(-) diff --git a/c/lib/mlrutil.c b/c/lib/mlrutil.c index 22bcc18c5..849cf2ee0 100644 --- a/c/lib/mlrutil.c +++ b/c/lib/mlrutil.c @@ -515,3 +515,30 @@ char* read_fp_into_memory(FILE* fp, size_t* psize) { *psize = file_size; return buffer; } + +// ---------------------------------------------------------------- +char* alloc_comment_strip(char* input) { + char* output = mlr_strdup_or_die(input); + char* q = output; + int copying = TRUE; + + for (char* p = input; *p; p++) { + if (copying) { + if (*p == '#') { + copying = FALSE; + } else { + *q = *p; + q++; + } + } else { + if (*p == '\n') { + copying = TRUE; + *q = *p; + q++; + } + } + } + + *q = 0; + return output; +} diff --git a/c/lib/mlrutil.h b/c/lib/mlrutil.h index 8f67c4c16..e3f2305ef 100644 --- a/c/lib/mlrutil.h +++ b/c/lib/mlrutil.h @@ -159,4 +159,8 @@ char* read_file_into_memory(char* filename, size_t* psize); // The caller should free the return value. char* read_fp_into_memory(FILE* fp, size_t* psize); +// Returns a copy of the input string with pound-sign to newline elided. +// Does not modify the input. The caller should free the output. +char* alloc_comment_strip(char* input); + #endif // MLRUTIL_H diff --git a/c/todo.txt b/c/todo.txt index 4b9874769..9bb5ed991 100644 --- a/c/todo.txt +++ b/c/todo.txt @@ -6,8 +6,7 @@ BUGFIXES ================================================================ TOP-OF-LIST SUMMARY -! cut over to new grammar sooner than later, and GC in the .y. but first: mapper_filter. -! oosvar keyed mean?!? solvable with `for`. +! for-variants: * for (k, v in $*) { if (k =~ "^f.*$") { $sum += v} } * for (k, v in $*) { if (k =~ "^f.*$") { $sum += $[k]} } * for (k, v in @v) @@ -15,12 +14,10 @@ TOP-OF-LIST SUMMARY * for (k1, k2, v in @v["a"]) ? for (k1, k2, v in @v[]["a"][]) ? no -- easier to do this w/ cond-block @ 2nd loop ? for (k1, k2, v in @*) - ? break/continue + ? break/continue: fully support or eliminate from the lexer/parser ! recursive PA-blocks & for-blocks -! for-srec and for-oosvar ! if-elif-elif-else ! while / do-while -x let newlines replace semicolons? no, and make it clear why: no need for \ continuations that way. ! try to eliminate need for semicolon after curly brace. for now, highlighted faqent. * comments-stripper (strdup & state-machine copy) ! need $[k] and @[k]. also need evaluators (new context mllhmv) for boundvars. @@ -28,6 +25,10 @@ x let newlines replace semicolons? no, and make it clear why: no need for \ cont * UT for empty statement for put * UT for empty statement for filter +doc: +* doc re oosvar keyed mean solvable with `for`. +* let newlines replace semicolons? no, and make it clear why: no need for \ continuations that way. + ---------------------------------------------------------------- ? substring? ? CONVFMT? IGNORECASE? MLRPATH? diff --git a/c/unit_test/test_mlrutil.c b/c/unit_test/test_mlrutil.c index 0338bbe9e..a77b12bd5 100644 --- a/c/unit_test/test_mlrutil.c +++ b/c/unit_test/test_mlrutil.c @@ -138,7 +138,30 @@ static char * test_unbackslash() { mu_assert_lf(streq(mlr_alloc_unbackslash("\\t\\\\"), "\t\\")); mu_assert_lf(streq(mlr_alloc_unbackslash("[\\132]"), "[Z]")); mu_assert_lf(streq(mlr_alloc_unbackslash("[\\x59]"), "[Y]")); + return 0; +} +// ---------------------------------------------------------------- +static char * test_alloc_comment_strip() { + mu_assert_lf(streq(alloc_comment_strip(""), "")); + mu_assert_lf(streq(alloc_comment_strip("hello"), "hello")); + mu_assert_lf(streq(alloc_comment_strip("#"), "")); + mu_assert_lf(streq(alloc_comment_strip("######"), "")); + mu_assert_lf(streq(alloc_comment_strip("#hello"), "")); + mu_assert_lf(streq(alloc_comment_strip("#hello\n"), "\n")); + mu_assert_lf(streq(alloc_comment_strip("hello#there"), "hello")); + mu_assert_lf(streq(alloc_comment_strip("hello#there\n"), "hello\n")); + mu_assert_lf(streq(alloc_comment_strip("one\ntwo\nthree\nfour"), "one\ntwo\nthree\nfour")); + mu_assert_lf(streq(alloc_comment_strip("#one\ntwo\nthree\nfour"), "\ntwo\nthree\nfour")); + mu_assert_lf(streq(alloc_comment_strip("on#e\ntwo\nthree\nfour"), "on\ntwo\nthree\nfour")); + mu_assert_lf(streq(alloc_comment_strip("one#\ntwo\nthree\nfour"), "one\ntwo\nthree\nfour")); + mu_assert_lf(streq(alloc_comment_strip("one\n#two\nthree\nfour"), "one\n\nthree\nfour")); + mu_assert_lf(streq(alloc_comment_strip("one\ntwo#\nthree\nfour"), "one\ntwo\nthree\nfour")); + mu_assert_lf(streq(alloc_comment_strip("one\ntwo\nthr#ee\nfour"), "one\ntwo\nthr\nfour")); + mu_assert_lf(streq(alloc_comment_strip("one\ntwo\nthree#\nfour"), "one\ntwo\nthree\nfour")); + mu_assert_lf(streq(alloc_comment_strip("one\ntwo\nthree\n#four"), "one\ntwo\nthree\n")); + mu_assert_lf(streq(alloc_comment_strip("one\ntwo\nthree\nfo#ur"), "one\ntwo\nthree\nfo")); + mu_assert_lf(streq(alloc_comment_strip("one\ntwo\nthree\nfour#"), "one\ntwo\nthree\nfour")); return 0; } @@ -151,6 +174,7 @@ static char * all_tests() { mu_run_test(test_scanners); mu_run_test(test_paste); mu_run_test(test_unbackslash); + mu_run_test(test_alloc_comment_strip); return 0; }