Skip to content

Commit

Permalink
squash - keep this just for temporary reference
Browse files Browse the repository at this point in the history
  • Loading branch information
rgerhards committed Jun 7, 2021
1 parent 87e4de1 commit d6ee727
Show file tree
Hide file tree
Showing 5 changed files with 78 additions and 13 deletions.
40 changes: 28 additions & 12 deletions src/parser.c
Original file line number Diff line number Diff line change
Expand Up @@ -1646,7 +1646,7 @@ PARSER_Parse(OpQuotedString)


struct data_QuotedString {
int dashIsNull;
int dashIsEmpty;
int quotesOptional;
int supportEscape;
};
Expand All @@ -1661,35 +1661,51 @@ 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;
}
}

fprintf(stderr, "start loop %zd, char %c\n", i, c[i]);
/* 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] != '"')
if(hadQuote && (i == npb->strLen || c[i] != '"'))
goto done;

/* success, persist */
*parsed = i + 1 - *offs; /* "eat" terminal double quote */
const size_t charsFound = i - *offs + (hadQuote ? 1 : 0);
fprintf(stderr, "charsFound %zd, i %zd, offs %zd\n", charsFound, i, *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:
Expand All @@ -1711,8 +1727,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 {
Expand Down
2 changes: 2 additions & 0 deletions tests/Makefile.am
Original file line number Diff line number Diff line change
Expand Up @@ -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 \
Expand Down
27 changes: 27 additions & 0 deletions tests/quote-string-dash-empty.sh
Original file line number Diff line number Diff line change
@@ -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

2 changes: 1 addition & 1 deletion tests/quote-string-escape.sh
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
20 changes: 20 additions & 0 deletions tests/quote-string-quote-optional.sh
Original file line number Diff line number Diff line change
@@ -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": "line2"}'


cleanup_tmp_files

0 comments on commit d6ee727

Please sign in to comment.