Skip to content

Commit d5988fb

Browse files
committed
squash - keep this just for temporary reference
1 parent 87e4de1 commit d5988fb

File tree

5 files changed

+78
-13
lines changed

5 files changed

+78
-13
lines changed

src/parser.c

Lines changed: 28 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1646,7 +1646,7 @@ PARSER_Parse(OpQuotedString)
16461646

16471647

16481648
struct data_QuotedString {
1649-
int dashIsNull;
1649+
int dashIsEmpty;
16501650
int quotesOptional;
16511651
int supportEscape;
16521652
};
@@ -1661,35 +1661,51 @@ PARSER_Parse(QuotedString)
16611661
const char *c;
16621662
struct data_QuotedString *const data = (struct data_QuotedString*) pdata;
16631663
size_t i;
1664+
int hadQuote = 0;
16641665

16651666
assert(npb->str != NULL);
16661667
assert(offs != NULL);
16671668
assert(parsed != NULL);
16681669
c = npb->str;
16691670
i = *offs;
1670-
if(i + 2 > npb->strLen)
1671-
goto done; /* needs at least 2 characters */
1671+
if(i + 1 > npb->strLen)
1672+
goto done; /* needs at least 1 characters (with quotesQptional...) */
16721673

1673-
if(c[i] != '"')
1674-
goto done;
1675-
++i;
1674+
if(c[i] == '"') {
1675+
hadQuote = 1;
1676+
++i;
1677+
} else {
1678+
if(!data->quotesOptional) {
1679+
goto done;
1680+
}
1681+
}
16761682

1683+
fprintf(stderr, "start loop %zd, char %c\n", i, c[i]);
16771684
/* search end of string */
1678-
while(i < npb->strLen && c[i] != '"') {
1685+
while(i < npb->strLen &&
1686+
( (hadQuote && c[i] != '"') || (!hadQuote && c[i] != ' ') )
1687+
) {
1688+
fprintf(stderr, "in loop %zd, char %c\n", i, c[i]);
16791689
if(data->supportEscape && c[i] == '\\' && (i < npb->strLen)) {
16801690
i++; /* next char is escaped */
16811691
}
16821692
i++;
16831693
}
16841694

1685-
if(i == npb->strLen || c[i] != '"')
1695+
if(hadQuote && (i == npb->strLen || c[i] != '"'))
16861696
goto done;
16871697

16881698
/* success, persist */
1689-
*parsed = i + 1 - *offs; /* "eat" terminal double quote */
1699+
const size_t charsFound = i - *offs + (hadQuote ? 1 : 0);
1700+
fprintf(stderr, "charsFound %zd, i %zd, offs %zd\n", charsFound, i, *offs);
1701+
*parsed = charsFound; /* "eat" terminal double quote */
16901702
/* create JSON value to save quoted string contents */
16911703
if(value != NULL) {
1692-
*value = json_object_new_string_len(npb->str+(*offs), *parsed);
1704+
if(charsFound == 3 && data->dashIsEmpty && !strncmp(npb->str+(*offs), "\"-\"", 3)) {
1705+
*value = json_object_new_string_len("", 0);
1706+
} else {
1707+
*value = json_object_new_string_len(npb->str+(*offs), *parsed);
1708+
}
16931709
}
16941710
r = 0; /* success */
16951711
done:
@@ -1711,8 +1727,8 @@ PARSER_Construct(QuotedString)
17111727
struct json_object *const val = json_object_iter_peek_value(&it);
17121728
if(!strcasecmp(key, "option.quotesOptional")) {
17131729
data->quotesOptional = json_object_get_boolean(val);
1714-
} else if(!strcasecmp(key, "option.dashIsNull")) {
1715-
data->dashIsNull = json_object_get_boolean(val);
1730+
} else if(!strcasecmp(key, "option.dashIsEmpty")) {
1731+
data->dashIsEmpty = json_object_get_boolean(val);
17161732
} else if(!strcasecmp(key, "option.supportEscape")) {
17171733
data->supportEscape = json_object_get_boolean(val);
17181734
} else {

tests/Makefile.am

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -64,6 +64,8 @@ TESTS_SHELLSCRIPTS = \
6464
strict_prefix_matching_1.sh \
6565
strict_prefix_matching_2.sh \
6666
quote-string-escape.sh \
67+
quote-string-dash-empty.sh \
68+
quote-string-quote-optional.sh \
6769
field_string.sh \
6870
field_string_perm_chars.sh \
6971
field_string_lazy_matching.sh \

tests/quote-string-dash-empty.sh

Lines changed: 27 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,27 @@
1+
#!/bin/bash
2+
# added 2021-06-07 by Rainer Gerhards
3+
# This file is part of the liblognorm project, released under ASL 2.0
4+
. $srcdir/exec.sh
5+
no_solaris10
6+
test_def $0 "quoted string with dash"
7+
8+
add_rule 'version=2'
9+
add_rule 'rule=:%
10+
{"type":"quoted-string", "name":"str", "option.dashIsEmpty":True}
11+
%'
12+
13+
execute '"-"'
14+
assert_output_json_eq '{ "str": ""}'
15+
16+
reset_rules
17+
add_rule 'version=2'
18+
add_rule 'rule=:%
19+
{"type":"quoted-string", "name":"str"}
20+
%'
21+
22+
execute '"-"'
23+
assert_output_json_eq '{ "str": "\"-\""}'
24+
25+
26+
cleanup_tmp_files
27+

tests/quote-string-escape.sh

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -11,7 +11,7 @@ add_rule 'rule=:%
1111
%'
1212

1313
execute '"word1\"word2"'
14-
assert_output_json_eq '{ "b": "word2", "a": "word1" }'
14+
assert_output_json_eq '{ "str": "\"word1\\\"word2\""}'
1515

1616

1717
cleanup_tmp_files

tests/quote-string-quote-optional.sh

Lines changed: 20 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,20 @@
1+
#!/bin/bash
2+
# added 2021-06-07 by Rainer Gerhards
3+
# This file is part of the liblognorm project, released under ASL 2.0
4+
. $srcdir/exec.sh
5+
no_solaris10
6+
test_def $0 "quoted string with quotesOptional"
7+
8+
add_rule 'version=2'
9+
add_rule 'rule=:%
10+
{"type":"quoted-string", "name":"str", "option.quotesOptional":True}
11+
%'
12+
13+
execute '"line 1"'
14+
assert_output_json_eq '{ "str": "\"line 1\""}'
15+
16+
execute 'line2'
17+
assert_output_json_eq '{ "str": "line2"}'
18+
19+
20+
cleanup_tmp_files

0 commit comments

Comments
 (0)