|
1 | 1 | #include "json-schema-to-grammar.h" |
| 2 | +#include "common.h" |
| 3 | + |
2 | 4 | #include <algorithm> |
3 | 5 | #include <fstream> |
4 | 6 | #include <map> |
|
11 | 13 |
|
12 | 14 | using json = nlohmann::ordered_json; |
13 | 15 |
|
14 | | -static std::string repeat(const std::string & str, size_t n); |
15 | | - |
16 | 16 | static std::string build_repetition(const std::string & item_rule, int min_items, int max_items, const std::string & separator_rule = "") { |
17 | 17 | auto has_max = max_items != std::numeric_limits<int>::max(); |
18 | 18 |
|
@@ -125,8 +125,8 @@ static void _build_min_max_int(int min_value, int max_value, std::stringstream & |
125 | 125 | if (sub_len > 0) { |
126 | 126 | auto from_sub = from.substr(i + 1); |
127 | 127 | auto to_sub = to.substr(i + 1); |
128 | | - auto sub_zeros = repeat("0", sub_len); |
129 | | - auto sub_nines = repeat("9", sub_len); |
| 128 | + auto sub_zeros = string_repeat("0", sub_len); |
| 129 | + auto sub_nines = string_repeat("9", sub_len); |
130 | 130 |
|
131 | 131 | auto to_reached = false; |
132 | 132 | out << "("; |
@@ -185,8 +185,8 @@ static void _build_min_max_int(int min_value, int max_value, std::stringstream & |
185 | 185 | auto max_digits = max_s.length(); |
186 | 186 |
|
187 | 187 | for (auto digits = min_digits; digits < max_digits; digits++) { |
188 | | - uniform_range(min_s, repeat("9", digits)); |
189 | | - min_s = "1" + repeat("0", digits); |
| 188 | + uniform_range(min_s, string_repeat("9", digits)); |
| 189 | + min_s = "1" + string_repeat("0", digits); |
190 | 190 | out << " | "; |
191 | 191 | } |
192 | 192 | uniform_range(min_s, max_s); |
@@ -315,49 +315,6 @@ std::unordered_map<char, std::string> GRAMMAR_LITERAL_ESCAPES = { |
315 | 315 | std::unordered_set<char> NON_LITERAL_SET = {'|', '.', '(', ')', '[', ']', '{', '}', '*', '+', '?'}; |
316 | 316 | std::unordered_set<char> ESCAPED_IN_REGEXPS_BUT_NOT_IN_LITERALS = {'^', '$', '.', '[', ']', '(', ')', '|', '{', '}', '*', '+', '?'}; |
317 | 317 |
|
318 | | -template <typename Iterator> |
319 | | -std::string join(Iterator begin, Iterator end, const std::string & separator) { |
320 | | - std::ostringstream result; |
321 | | - if (begin != end) { |
322 | | - result << *begin; |
323 | | - for (Iterator it = begin + 1; it != end; ++it) { |
324 | | - result << separator << *it; |
325 | | - } |
326 | | - } |
327 | | - return result.str(); |
328 | | -} |
329 | | - |
330 | | -static std::vector<std::string> split(const std::string & str, const std::string & delimiter) { |
331 | | - std::vector<std::string> tokens; |
332 | | - size_t start = 0; |
333 | | - size_t end = str.find(delimiter); |
334 | | - |
335 | | - while (end != std::string::npos) { |
336 | | - tokens.push_back(str.substr(start, end - start)); |
337 | | - start = end + delimiter.length(); |
338 | | - end = str.find(delimiter, start); |
339 | | - } |
340 | | - |
341 | | - tokens.push_back(str.substr(start)); |
342 | | - |
343 | | - return tokens; |
344 | | -} |
345 | | - |
346 | | -static std::string repeat(const std::string & str, size_t n) { |
347 | | - if (n == 0) { |
348 | | - return ""; |
349 | | - } |
350 | | - |
351 | | - std::string result; |
352 | | - result.reserve(str.length() * n); |
353 | | - |
354 | | - for (size_t i = 0; i < n; ++i) { |
355 | | - result += str; |
356 | | - } |
357 | | - |
358 | | - return result; |
359 | | -} |
360 | | - |
361 | 318 | static std::string replacePattern(const std::string & input, const std::regex & regex, const std::function<std::string(const std::smatch &)> & replacement) { |
362 | 319 | std::smatch match; |
363 | 320 | std::string result; |
@@ -416,7 +373,7 @@ class SchemaConverter { |
416 | 373 | for (size_t i = 0; i < alt_schemas.size(); i++) { |
417 | 374 | rules.push_back(visit(alt_schemas[i], name + (name.empty() ? "alternative-" : "-") + std::to_string(i))); |
418 | 375 | } |
419 | | - return join(rules.begin(), rules.end(), " | "); |
| 376 | + return string_join(rules, " | "); |
420 | 377 | } |
421 | 378 |
|
422 | 379 | std::string _visit_pattern(const std::string & pattern, const std::string & name) { |
@@ -479,7 +436,7 @@ class SchemaConverter { |
479 | 436 | for (const auto & item : ret) { |
480 | 437 | results.push_back(to_rule(item)); |
481 | 438 | } |
482 | | - return std::make_pair(join(results.begin(), results.end(), " "), false); |
| 439 | + return std::make_pair(string_join(results, " "), false); |
483 | 440 | }; |
484 | 441 |
|
485 | 442 | while (i < length) { |
@@ -537,7 +494,7 @@ class SchemaConverter { |
537 | 494 | } |
538 | 495 | curly_brackets += '}'; |
539 | 496 | i++; |
540 | | - auto nums = split(curly_brackets.substr(1, curly_brackets.length() - 2), ","); |
| 497 | + auto nums = string_split(curly_brackets.substr(1, curly_brackets.length() - 2), ","); |
541 | 498 | int min_times = 0; |
542 | 499 | int max_times = std::numeric_limits<int>::max(); |
543 | 500 | try { |
@@ -852,7 +809,7 @@ class SchemaConverter { |
852 | 809 | return; |
853 | 810 | } |
854 | 811 | std::string pointer = ref.substr(ref.find('#') + 1); |
855 | | - std::vector<std::string> tokens = split(pointer, "/"); |
| 812 | + std::vector<std::string> tokens = string_split(pointer, "/"); |
856 | 813 | for (size_t i = 1; i < tokens.size(); ++i) { |
857 | 814 | std::string sel = tokens[i]; |
858 | 815 | if (target.is_null() || !target.contains(sel)) { |
@@ -903,7 +860,7 @@ class SchemaConverter { |
903 | 860 | for (const auto & v : schema["enum"]) { |
904 | 861 | enum_values.push_back(_generate_constant_rule(v)); |
905 | 862 | } |
906 | | - return _add_rule(rule_name, "(" + join(enum_values.begin(), enum_values.end(), " | ") + ") space"); |
| 863 | + return _add_rule(rule_name, "(" + string_join(enum_values, " | ") + ") space"); |
907 | 864 | } else if ((schema_type.is_null() || schema_type == "object") |
908 | 865 | && (schema.contains("properties") || |
909 | 866 | (schema.contains("additionalProperties") && schema["additionalProperties"] != true))) { |
@@ -1017,10 +974,10 @@ class SchemaConverter { |
1017 | 974 |
|
1018 | 975 | void check_errors() { |
1019 | 976 | if (!_errors.empty()) { |
1020 | | - throw std::runtime_error("JSON schema conversion failed:\n" + join(_errors.begin(), _errors.end(), "\n")); |
| 977 | + throw std::runtime_error("JSON schema conversion failed:\n" + string_join(_errors, "\n")); |
1021 | 978 | } |
1022 | 979 | if (!_warnings.empty()) { |
1023 | | - fprintf(stderr, "WARNING: JSON schema conversion was incomplete: %s\n", join(_warnings.begin(), _warnings.end(), "; ").c_str()); |
| 980 | + fprintf(stderr, "WARNING: JSON schema conversion was incomplete: %s\n", string_join(_warnings, "; ").c_str()); |
1024 | 981 | } |
1025 | 982 | } |
1026 | 983 |
|
|
0 commit comments