From f7b2a3a64f329bfb1ff3fd49414fe00d5a38007a Mon Sep 17 00:00:00 2001 From: Rainer Gerhards Date: Mon, 7 Jun 2021 14:27:44 +0200 Subject: [PATCH] squash - keep this just for temporary reference --- src/parser.c | 39 +++++++++++++++++++--------- tests/Makefile.am | 2 ++ tests/quote-string-dash-empty.sh | 27 +++++++++++++++++++ tests/quote-string-escape.sh | 2 +- tests/quote-string-quote-optional.sh | 20 ++++++++++++++ 5 files changed, 77 insertions(+), 13 deletions(-) create mode 100755 tests/quote-string-dash-empty.sh create mode 100755 tests/quote-string-quote-optional.sh diff --git a/src/parser.c b/src/parser.c index a644bd6..e3e17fe 100644 --- a/src/parser.c +++ b/src/parser.c @@ -1646,7 +1646,7 @@ PARSER_Parse(OpQuotedString) struct data_QuotedString { - int dashIsNull; + int dashIsEmpty; int quotesOptional; int supportEscape; }; @@ -1661,35 +1661,50 @@ PARSER_Parse(QuotedString) const char *c; struct data_QuotedString *const data = (struct data_QuotedString*) pdata; size_t i; + int hadQuote = 0; assert(npb->str != NULL); assert(offs != NULL); assert(parsed != NULL); c = npb->str; i = *offs; - if(i + 2 > npb->strLen) - goto done; /* needs at least 2 characters */ + if(i + 1 > npb->strLen) + goto done; /* needs at least 1 characters (with quotesQptional...) */ - if(c[i] != '"') - goto done; - ++i; + if(c[i] == '"') { + hadQuote = 1; + ++i; + } else { + if(!data->quotesOptional) { + goto done; + } + } /* search end of string */ - while(i < npb->strLen && c[i] != '"') { + while(i < npb->strLen && + ( (hadQuote && c[i] != '"') || (!hadQuote && c[i] != ' ') ) + ) { +fprintf(stderr, "in loop %zd, char %c\n", i, c[i]); if(data->supportEscape && c[i] == '\\' && (i < npb->strLen)) { i++; /* next char is escaped */ } i++; } - if(i == npb->strLen || c[i] != '"') +fprintf(stderr, "i %zd, strLen %zd, hadQuote %d, c:%c\n", i, npb->strLen, hadQuote, c[i]); + if(hadQuote && (i == npb->strLen || c[i] != '"')) goto done; /* success, persist */ - *parsed = i + 1 - *offs; /* "eat" terminal double quote */ + const size_t charsFound = i + 1 - *offs; + *parsed = charsFound; /* "eat" terminal double quote */ /* create JSON value to save quoted string contents */ if(value != NULL) { - *value = json_object_new_string_len(npb->str+(*offs), *parsed); + if(charsFound == 3 && data->dashIsEmpty && !strncmp(npb->str+(*offs), "\"-\"", 3)) { + *value = json_object_new_string_len("", 0); + } else { + *value = json_object_new_string_len(npb->str+(*offs), *parsed); + } } r = 0; /* success */ done: @@ -1711,8 +1726,8 @@ PARSER_Construct(QuotedString) struct json_object *const val = json_object_iter_peek_value(&it); if(!strcasecmp(key, "option.quotesOptional")) { data->quotesOptional = json_object_get_boolean(val); - } else if(!strcasecmp(key, "option.dashIsNull")) { - data->dashIsNull = json_object_get_boolean(val); + } else if(!strcasecmp(key, "option.dashIsEmpty")) { + data->dashIsEmpty = json_object_get_boolean(val); } else if(!strcasecmp(key, "option.supportEscape")) { data->supportEscape = json_object_get_boolean(val); } else { diff --git a/tests/Makefile.am b/tests/Makefile.am index a881ed3..8f8ec45 100644 --- a/tests/Makefile.am +++ b/tests/Makefile.am @@ -64,6 +64,8 @@ TESTS_SHELLSCRIPTS = \ strict_prefix_matching_1.sh \ strict_prefix_matching_2.sh \ quote-string-escape.sh \ + quote-string-dash-empty.sh \ + quote-string-quote-optional.sh \ field_string.sh \ field_string_perm_chars.sh \ field_string_lazy_matching.sh \ diff --git a/tests/quote-string-dash-empty.sh b/tests/quote-string-dash-empty.sh new file mode 100755 index 0000000..0446139 --- /dev/null +++ b/tests/quote-string-dash-empty.sh @@ -0,0 +1,27 @@ +#!/bin/bash +# added 2021-06-07 by Rainer Gerhards +# This file is part of the liblognorm project, released under ASL 2.0 +. $srcdir/exec.sh +no_solaris10 +test_def $0 "quoted string with dash" + +add_rule 'version=2' +add_rule 'rule=:% + {"type":"quoted-string", "name":"str", "option.dashIsEmpty":True} + %' + +execute '"-"' +assert_output_json_eq '{ "str": ""}' + +reset_rules +add_rule 'version=2' +add_rule 'rule=:% + {"type":"quoted-string", "name":"str"} + %' + +execute '"-"' +assert_output_json_eq '{ "str": "\"-\""}' + + +cleanup_tmp_files + diff --git a/tests/quote-string-escape.sh b/tests/quote-string-escape.sh index a52ae7b..44b89b6 100755 --- a/tests/quote-string-escape.sh +++ b/tests/quote-string-escape.sh @@ -11,7 +11,7 @@ add_rule 'rule=:% %' execute '"word1\"word2"' -assert_output_json_eq '{ "b": "word2", "a": "word1" }' +assert_output_json_eq '{ "str": "\"word1\\\"word2\""}' cleanup_tmp_files diff --git a/tests/quote-string-quote-optional.sh b/tests/quote-string-quote-optional.sh new file mode 100755 index 0000000..ac9cc61 --- /dev/null +++ b/tests/quote-string-quote-optional.sh @@ -0,0 +1,20 @@ +#!/bin/bash +# added 2021-06-07 by Rainer Gerhards +# This file is part of the liblognorm project, released under ASL 2.0 +. $srcdir/exec.sh +no_solaris10 +test_def $0 "quoted string with quotesOptional" + +add_rule 'version=2' +add_rule 'rule=:% + {"type":"quoted-string", "name":"str", "option.quotesOptional":True} + %' + +execute '"line 1"' +assert_output_json_eq '{ "str": "\"line 1\""}' + +execute 'line2' +assert_output_json_eq '{ "str": "\"line 2\""}' + + +cleanup_tmp_files