From dff708634cdcab44c55c177f4ed3519e2fb5d663 Mon Sep 17 00:00:00 2001 From: John Kerl Date: Thu, 13 Oct 2016 21:53:11 -0400 Subject: [PATCH] comment-strip neaten --- c/lib/mlrutil.c | 27 --------------------------- c/lib/mlrutil.h | 4 ---- c/mapping/mapper_put_or_filter.c | 14 ++------------ c/todo.txt | 1 + c/unit_test/test_mlrutil.c | 26 -------------------------- 5 files changed, 3 insertions(+), 69 deletions(-) diff --git a/c/lib/mlrutil.c b/c/lib/mlrutil.c index 8e187ad5c..131eafa7e 100644 --- a/c/lib/mlrutil.c +++ b/c/lib/mlrutil.c @@ -515,30 +515,3 @@ 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 e3f2305ef..8f67c4c16 100644 --- a/c/lib/mlrutil.h +++ b/c/lib/mlrutil.h @@ -159,8 +159,4 @@ 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/mapping/mapper_put_or_filter.c b/c/mapping/mapper_put_or_filter.c index 8eba7f68f..7437e1eca 100644 --- a/c/mapping/mapper_put_or_filter.c +++ b/c/mapping/mapper_put_or_filter.c @@ -17,7 +17,6 @@ // ---------------------------------------------------------------- typedef struct _mapper_put_or_filter_state_t { char* mlr_dsl_expression; - char* comment_stripped_mlr_dsl_expression; mlr_dsl_ast_t* past; mlr_dsl_cst_t* pcst; @@ -55,7 +54,6 @@ static mapper_t* mapper_put_or_filter_alloc( char* mlr_dsl_expression, int print_ast, int trace_stack_allocation, - char* comment_stripped_mlr_dsl_expression, mlr_dsl_ast_t* past, int put_output_disabled, // mlr put -q int do_final_filter, // mlr filter @@ -227,7 +225,6 @@ static mapper_t* shared_parse_cli(int* pargi, int argc, char** argv, slls_t* expression_filenames = slls_alloc(); slls_t* expression_strings = slls_alloc(); char* mlr_dsl_expression = NULL; - char* comment_stripped_mlr_dsl_expression = NULL; int put_output_disabled = FALSE; int do_final_filter = FALSE; int negate_final_filter = FALSE; @@ -343,14 +340,11 @@ static mapper_t* shared_parse_cli(int* pargi, int argc, char** argv, slls_free(expression_filenames); slls_free(expression_strings); - //comment_stripped_mlr_dsl_expression = alloc_comment_strip(mlr_dsl_expression); - comment_stripped_mlr_dsl_expression = mlr_strdup_or_die(mlr_dsl_expression); - // Linked list of mlr_dsl_ast_node_t*. - mlr_dsl_ast_t* past = mlr_dsl_parse(comment_stripped_mlr_dsl_expression, trace_parse); + mlr_dsl_ast_t* past = mlr_dsl_parse(mlr_dsl_expression, trace_parse); if (past == NULL) { fprintf(stderr, "%s %s: syntax error on DSL parse of '%s'\n", - argv[0], verb, comment_stripped_mlr_dsl_expression); + argv[0], verb, mlr_dsl_expression); return NULL; } @@ -364,7 +358,6 @@ static mapper_t* shared_parse_cli(int* pargi, int argc, char** argv, *pargi = argi; return mapper_put_or_filter_alloc(mlr_dsl_expression, print_ast, trace_stack_allocation, - comment_stripped_mlr_dsl_expression, past, put_output_disabled, do_final_filter, negate_final_filter, type_inferencing, oosvar_flatten_separator, flush_every_record, pwriter_opts, pmain_writer_opts); @@ -375,7 +368,6 @@ static mapper_t* mapper_put_or_filter_alloc( char* mlr_dsl_expression, int print_ast, int trace_stack_allocation, - char* comment_stripped_mlr_dsl_expression, mlr_dsl_ast_t* past, int put_output_disabled, // mlr put -q int do_final_filter, // mlr filter @@ -389,7 +381,6 @@ static mapper_t* mapper_put_or_filter_alloc( mapper_put_or_filter_state_t* pstate = mlr_malloc_or_die(sizeof(mapper_put_or_filter_state_t)); // Retain the string contents along with any in-pointers from the AST/CST pstate->mlr_dsl_expression = mlr_dsl_expression; - pstate->comment_stripped_mlr_dsl_expression = comment_stripped_mlr_dsl_expression; pstate->past = past; pstate->pcst = mlr_dsl_cst_alloc(past, print_ast, trace_stack_allocation, type_inferencing, flush_every_record, do_final_filter, negate_final_filter); @@ -416,7 +407,6 @@ static void mapper_put_or_filter_free(mapper_t* pmapper) { mapper_put_or_filter_state_t* pstate = pmapper->pvstate; free(pstate->mlr_dsl_expression); - free(pstate->comment_stripped_mlr_dsl_expression); mlhmmv_free(pstate->poosvars); bind_stack_free(pstate->pbind_stack); loop_stack_free(pstate->ploop_stack); diff --git a/c/todo.txt b/c/todo.txt index a2d83575e..194aadb8d 100644 --- a/c/todo.txt +++ b/c/todo.txt @@ -119,6 +119,7 @@ DSL: * variadic min & max * substr func * ENV @ LHS (putenv), and why (popens) +* # in the lexer so #-strings are OK now (show an example) VERBS: * most-frequent/least-frequent diff --git a/c/unit_test/test_mlrutil.c b/c/unit_test/test_mlrutil.c index 2efe6da20..337f0d0b6 100644 --- a/c/unit_test/test_mlrutil.c +++ b/c/unit_test/test_mlrutil.c @@ -141,31 +141,6 @@ static char * test_unbackslash() { 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")); - mu_assert_lf(streq(alloc_comment_strip("#one\nt#wo\nth#ree\nfou#r#"), "\nt\nth\nfou")); - return 0; -} - // ================================================================ static char * all_tests() { mu_run_test(test_canonical_mod); @@ -175,7 +150,6 @@ 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; }