comment-strip library routine & UT

This commit is contained in:
John Kerl 2016-05-14 15:40:54 -04:00
parent 5ca1a261ba
commit c8f98aa2c7
4 changed files with 61 additions and 5 deletions

View file

@ -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;
}

View file

@ -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

View file

@ -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?

View file

@ -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;
}