@@ -16,58 +16,27 @@ static std::string join(Iterator begin, Iterator end, const std::string & separa
16
16
17
17
static std::string repeat (const std::string & str, size_t n);
18
18
19
- static std::string build_repetition (const std::string & item_rule, int min_items, int max_items, const std::string & separator_rule = " " , bool item_rule_is_literal = false ) {
20
- if (separator_rule.empty ()) {
21
- if (min_items == 0 && max_items == 1 ) {
22
- return item_rule + " ?" ;
23
- } else if (min_items == 1 && max_items == std::numeric_limits<int >::max ()) {
24
- return item_rule + " +" ;
25
- }
26
- }
19
+ static std::string build_repetition (const std::string & item_rule, int min_items, int max_items, const std::string & separator_rule = " " ) {
20
+ auto has_max = max_items != std::numeric_limits<int >::max ();
27
21
28
- std::string result;
29
- if (min_items > 0 ) {
30
- if (item_rule_is_literal && separator_rule.empty ()) {
31
- result = " \" " + repeat (std::string (item_rule.begin () + 1 , item_rule.end () - 1 ), min_items) + " \" " ;
32
- } else {
33
- std::vector<std::string> items (min_items, item_rule);
34
- result = join (items.begin (), items.end (), separator_rule.empty () ? " " : " " + separator_rule + " " );
35
- }
22
+ if (min_items == 0 && max_items == 1 ) {
23
+ return item_rule + " ?" ;
36
24
}
37
25
38
- std::function<std::string (int , bool )> opt_repetitions = [&](int up_to_n, bool prefix_with_sep) -> std::string {
39
- auto content = prefix_with_sep && !separator_rule.empty () ? separator_rule + " " + item_rule : item_rule;
40
-
41
- if (up_to_n == 0 ) {
42
- return " " ;
43
- } else if (up_to_n == 1 ) {
44
- return " (" + content + " )?" ;
45
- } else if (!separator_rule.empty () && !prefix_with_sep) {
46
- return " (" + content + " " + opt_repetitions (up_to_n - 1 , true ) + " )?" ;
26
+ if (separator_rule.empty ()) {
27
+ if (min_items == 1 && !has_max) {
28
+ return item_rule + " +" ;
29
+ } else if (min_items == 0 && !has_max) {
30
+ return item_rule + " *" ;
47
31
} else {
48
- std::string res = repeat (" (" + content + " " , up_to_n);
49
- // strip trailing space
50
- res = res.substr (0 , res.length () - 1 );
51
- res += repeat (" )?" , up_to_n);
52
- return res;
32
+ return item_rule + " {" + std::to_string (min_items) + " ," + (has_max ? std::to_string (max_items) : " " ) + " }" ;
53
33
}
54
- };
55
-
56
- if (min_items > 0 && max_items != min_items) {
57
- result += " " ;
58
34
}
59
35
60
- if (max_items != std::numeric_limits<int >::max ()) {
61
- result += opt_repetitions (max_items - min_items, min_items > 0 );
62
- } else {
63
- std::string item_operator = " (" + (separator_rule.empty () ? " " : separator_rule + " " ) + item_rule + " )" ;
64
- if (min_items == 0 && !separator_rule.empty ()) {
65
- result = " (" + item_rule + " " + item_operator + " *)?" ;
66
- } else {
67
- result += item_operator + " *" ;
68
- }
36
+ auto result = item_rule + " " + build_repetition (" (" + separator_rule + " " + item_rule + " )" , min_items == 0 ? 0 : min_items - 1 , has_max ? max_items - 1 : max_items);
37
+ if (min_items == 0 ) {
38
+ result = " (" + result + " )?" ;
69
39
}
70
-
71
40
return result;
72
41
}
73
42
@@ -78,30 +47,24 @@ struct BuiltinRule {
78
47
std::vector<std::string> deps;
79
48
};
80
49
81
- const std::string _up_to_15_digits = build_repetition(" [0-9]" , 0 , 15 );
82
-
83
50
std::unordered_map<std::string, BuiltinRule> PRIMITIVE_RULES = {
84
51
{" boolean" , {" (\" true\" | \" false\" ) space" , {}}},
85
- {" decimal-part" , {" [0-9] " + _up_to_15_digits , {}}},
86
- {" integral-part" , {" [0-9 ] | [1-9] " + _up_to_15_digits , {}}},
52
+ {" decimal-part" , {" [0-9]{1,16} " , {}}},
53
+ {" integral-part" , {" [0] | [1-9] [0-9]{0,15} " , {}}},
87
54
{" number" , {" (\" -\" ? integral-part) (\" .\" decimal-part)? ([eE] [-+]? integral-part)? space" , {" integral-part" , " decimal-part" }}},
88
55
{" integer" , {" (\" -\" ? integral-part) space" , {" integral-part" }}},
89
56
{" value" , {" object | array | string | number | boolean | null" , {" object" , " array" , " string" , " number" , " boolean" , " null" }}},
90
57
{" object" , {" \" {\" space ( string \" :\" space value (\" ,\" space string \" :\" space value)* )? \" }\" space" , {" string" , " value" }}},
91
58
{" array" , {" \" [\" space ( value (\" ,\" space value)* )? \" ]\" space" , {" value" }}},
92
- {" uuid" , {" \"\\\"\" [0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F] "
93
- " \" -\" [0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F] "
94
- " \" -\" [0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F] "
95
- " \" -\" [0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F] "
96
- " \" -\" [0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F][0-9a-fA-F] \"\\\"\" space" , {}}},
97
- {" char" , {" [^\"\\\\ ] | \"\\\\\" ([\"\\\\ /bfnrt] | \" u\" [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F] [0-9a-fA-F])" , {}}},
59
+ {" uuid" , {" \"\\\"\" [0-9a-fA-F]{8} \" -\" [0-9a-fA-F]{4} \" -\" [0-9a-fA-F]{4} \" -\" [0-9a-fA-F]{4} \" -\" [0-9a-fA-F]{12} \"\\\"\" space" , {}}},
60
+ {" char" , {" [^\"\\\\ ] | \"\\\\\" ([\"\\\\ /bfnrt] | \" u\" [0-9a-fA-F]{4})" , {}}},
98
61
{" string" , {" \"\\\"\" char* \"\\\"\" space" , {" char" }}},
99
62
{" null" , {" \" null\" space" , {}}},
100
63
};
101
64
102
65
std::unordered_map<std::string, BuiltinRule> STRING_FORMAT_RULES = {
103
- {" date" , {" [0-9] [0-9] [0-9] [0-9] \" -\" ( \" 0\" [1-9] | \" 1\" [0-2] ) \" -\" ( \" 0\" [1-9] | [1-2] [0-9] | \" 3\" [0-1] )" , {}}},
104
- {" time" , {" ([01] [0-9] | \" 2\" [0-3]) \" :\" [0-5] [0-9] \" :\" [0-5] [0-9] ( \" .\" [0-9] [0-9] [0-9] )? ( \" Z\" | ( \" +\" | \" -\" ) ( [01] [0-9] | \" 2\" [0-3] ) \" :\" [0-5] [0-9] )" , {}}},
66
+ {" date" , {" [0-9]{4} \" -\" ( \" 0\" [1-9] | \" 1\" [0-2] ) \" -\" ( \" 0\" [1-9] | [1-2] [0-9] | \" 3\" [0-1] )" , {}}},
67
+ {" time" , {" ([01] [0-9] | \" 2\" [0-3]) \" :\" [0-5] [0-9] \" :\" [0-5] [0-9] ( \" .\" [0-9]{3} )? ( \" Z\" | ( \" +\" | \" -\" ) ( [01] [0-9] | \" 2\" [0-3] ) \" :\" [0-5] [0-9] )" , {}}},
105
68
{" date-time" , {" date \" T\" time" , {" date" , " time" }}},
106
69
{" date-string" , {" \"\\\"\" date \"\\\"\" space" , {" date" }}},
107
70
{" time-string" , {" \"\\\"\" time \"\\\"\" space" , {" time" }}},
@@ -385,8 +348,7 @@ class SchemaConverter {
385
348
sub_is_literal ? " \" " + sub + " \" " : sub,
386
349
min_times,
387
350
max_times,
388
- " " ,
389
- sub_is_literal
351
+ " "
390
352
);
391
353
seq.back ().second = false ;
392
354
} else {
0 commit comments