From 8d860e7d6d4b88f32feb7530161668c8e1ae8f69 Mon Sep 17 00:00:00 2001 From: adamritter <58403584+adamritter@users.noreply.github.com> Date: Fri, 2 Jun 2023 00:12:05 +0200 Subject: [PATCH] Fix some test, root path --- CMakeLists.txt | 3 +++ README.md | 1 + src/fastgron.cpp | 36 ++++++++++++++++++++++++------------ 3 files changed, 28 insertions(+), 12 deletions(-) diff --git a/CMakeLists.txt b/CMakeLists.txt index 27fe4f0..226c317 100644 --- a/CMakeLists.txt +++ b/CMakeLists.txt @@ -57,3 +57,6 @@ set_tests_properties(filter_fixed_string_text PROPERTIES PASS_REGULAR_EXPRESSION add_test(sort_and_filter_test COMMAND sh -c "echo '{\"a\":{\"c\":13,\"b\":22,\"a\":31}}' | ${CMAKE_CURRENT_BINARY_DIR}/fastgron --sort -F 3") set_tests_properties(sort_and_filter_test PROPERTIES PASS_REGULAR_EXPRESSION "json.a.a = 31;\\njson.a.c = 13;") + +add_test(root_test COMMAND sh -c "echo '[1,2,3]' | ${CMAKE_CURRENT_BINARY_DIR}/fastgron --root a") +set_tests_properties(root_test PROPERTIES PASS_REGULAR_EXPRESSION "a\\[1\\] = 2;\\na\\[2\\] = 3;") \ No newline at end of file diff --git a/README.md b/README.md index 3fe6b83..9a2d8fa 100644 --- a/README.md +++ b/README.md @@ -88,6 +88,7 @@ options: -p, -path filter path, for example .#.3.population or cities.#.population -p is optional if path starts with . and file with that name doesn't exist --no-indent don't indent output + --root root path, default is json ``` The file name can be - or missing, in that case the data is read from stdin. diff --git a/src/fastgron.cpp b/src/fastgron.cpp index e9e0bf8..4461cef 100644 --- a/src/fastgron.cpp +++ b/src/fastgron.cpp @@ -345,8 +345,9 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing { size_t orig_out_len = out_growing_string.size(); out_growing_string.reserve_extra(100); - memcpy(&out_growing_string.data[orig_out_len], &*path.data.begin(), path.size()); char *ptr = &out_growing_string.data[orig_out_len]; + memcpy(ptr, &*path.data.begin(), path.size()); + ptr += path.size(); *ptr++ = ' '; *ptr++ = '='; *ptr++ = ' '; @@ -359,7 +360,7 @@ void recursive_print_gron(ondemand::value element, growing_string &path, growing ptr += s.size(); *ptr++ = ';'; *ptr++ = '\n'; - if (can_show(out_growing_string.view())) + if (can_show(string_view(&out_growing_string.data[orig_out_len], ptr - &out_growing_string.data[orig_out_len]))) { out_growing_string.len = ptr - &out_growing_string.data[0]; } @@ -427,6 +428,7 @@ struct options bool ungron; std::string filtered_path; }; +string root = "json"; string user_agent = "fastgron/0.3.x"; bool no_indent = false; @@ -511,9 +513,18 @@ options parse_options(int argc, char *argv[]) } opts.filtered_path = argv[++i]; } + else if (strcmp(argv[i], "--root") == 0) + { + if (i + 1 >= argc) + { + cerr << "Missing argument for --root\n"; + exit(EXIT_FAILURE); + } + root = argv[++i]; + } else { - if (access(argv[i], F_OK) == -1) + if (access(argv[i], F_OK) == -1 && argv[i] != string("-")) { // Treat strings starting with . as paths if (argv[i][0] == '.') @@ -734,7 +745,8 @@ void print_help() " -u, --ungron ungron: convert gron output back to JSON\n" " -p, -path filter path, for example .#.3.population or cities.#.population\n" " -p is optional if path starts with . and file with that name doesn't exist\n" - " --no-indent don't indent output\n\n" + " --no-indent don't indent output\n" + " --root root path, default is json\n\n" "Home page with more information: https://github.com/adamritter/fastgron\n"; } @@ -841,9 +853,9 @@ std::string download(std::string url) void print_filtered_path(growing_string &path, int processed, ondemand::value element) { - if (processed == 0 && path.starts_with("json")) + if (processed == 0 && path.starts_with(root)) { - processed = 4; + processed = root.size(); } if (path.size() == processed) { @@ -1087,9 +1099,9 @@ int main(int argc, char *argv[]) data = end + 1; continue; } - if (line.starts_with("json")) + if (line.starts_with(root)) { - data = data + 4; + data = data + root.size(); } parse_json(string_view(data, end - data), builder); data = end + 1; @@ -1112,10 +1124,10 @@ int main(int argc, char *argv[]) { ondemand::document_stream docs = parser.iterate_many(json); int index = 0; - gprint("json = [];\n", batched_out); + gprint(root + " = [];\n", batched_out); for (auto doc : docs) { - growing_string path = growing_string("json[").append(to_string(index++)).append("]"); + growing_string path = growing_string(root).append("[").append(to_string(index++)).append("]"); recursive_print_gron(doc, path, batched_out); } } @@ -1124,10 +1136,10 @@ int main(int argc, char *argv[]) { ondemand::document doc = parser.iterate(json); ondemand::value val = doc; - growing_string path("json"); + growing_string path(root); if (!opts.filtered_path.empty()) { - if (opts.filtered_path.starts_with("json")) + if (opts.filtered_path.starts_with(root)) { path.erase(0); }