From a86a82b39c2bdeabc25ecce9d68928e4c6ec53f3 Mon Sep 17 00:00:00 2001 From: John Keiser Date: Fri, 12 Jun 2020 14:33:20 -0700 Subject: [PATCH] Rename minify class to minifier so the minify() method is cleared up --- benchmark/minifiercompetition.cpp | 2 +- doc/basics.md | 4 ++-- include/simdjson/dom/array.h | 2 +- include/simdjson/dom/document.h | 2 +- include/simdjson/dom/element.h | 2 +- include/simdjson/dom/object.h | 2 +- include/simdjson/inline/array.h | 4 ++-- include/simdjson/inline/element.h | 4 ++-- include/simdjson/inline/object.h | 6 +++--- include/simdjson/minify.h | 12 +++++++----- src/implementation.cpp | 2 +- tests/basictests.cpp | 18 +++++++++--------- tests/readme_examples.cpp | 6 +++--- 13 files changed, 34 insertions(+), 32 deletions(-) diff --git a/benchmark/minifiercompetition.cpp b/benchmark/minifiercompetition.cpp index 189979ddc..2a8c1611a 100644 --- a/benchmark/minifiercompetition.cpp +++ b/benchmark/minifiercompetition.cpp @@ -139,7 +139,7 @@ int main(int argc, char *argv[]) { char *mini_buffer = simdjson::internal::allocate_padded_buffer(p.size() + 1); size_t minisize; - auto minierror = minify_string(p.data(), p.size(),mini_buffer, minisize); + auto minierror = minify(p.data(), p.size(),mini_buffer, minisize); if (!minierror) { std::cerr << minierror << std::endl; exit(1); } mini_buffer[minisize] = '\0'; diff --git a/doc/basics.md b/doc/basics.md index 88aa1475a..562364804 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -171,7 +171,7 @@ And another one: Minifying JSON strings without parsing ---------------------- -In some cases, you may have valid JSON strings that you do not wish to parse but that you wish to minify. That is, you wish to remove all unnecessary spaces. We have a fast function for this purpose (`minify_string`). This function does not validate your content, and it does not parse it. Instead, it assumes that your string is valid UTF-8. It is much faster than parsing the string and re-serializing it in minified form. Usage is relatively simple. You must pass an input pointer with a length parameter, as well as an output pointer and an output length parameter (by reference). The output length parameter is not read, but written to. The output pointer should point to a valid memory region that is slightly overallocated (by `simdjson::SIMDJSON_PADDING`) compared to the original string length. The input pointer and input length are read, but not written to. +In some cases, you may have valid JSON strings that you do not wish to parse but that you wish to minify. That is, you wish to remove all unnecessary spaces. We have a fast function for this purpose (`minify`). This function does not validate your content, and it does not parse it. Instead, it assumes that your string is valid UTF-8. It is much faster than parsing the string and re-serializing it in minified form. Usage is relatively simple. You must pass an input pointer with a length parameter, as well as an output pointer and an output length parameter (by reference). The output length parameter is not read, but written to. The output pointer should point to a valid memory region that is slightly overallocated (by `simdjson::SIMDJSON_PADDING`) compared to the original string length. The input pointer and input length are read, but not written to. ```C++ // Starts with a valid JSON document as a string. @@ -182,7 +182,7 @@ In some cases, you may have valid JSON strings that you do not wish to parse but // including some padding (simdjson::SIMDJSON_PADDING). std::unique_ptr buffer{new(std::nothrow) char[length + simdjson::SIMDJSON_PADDING]}; size_t new_length{}; // It will receive the minified length. - auto error = simdjson::minify_string(some_string, length, buffer.get(), new_length); + auto error = simdjson::minify(some_string, length, buffer.get(), new_length); // The buffer variable now has "[1,2,3,4]" and new_length has value 9. ``` diff --git a/include/simdjson/dom/array.h b/include/simdjson/dom/array.h index 091b596e8..44ef4bb46 100644 --- a/include/simdjson/dom/array.h +++ b/include/simdjson/dom/array.h @@ -92,7 +92,7 @@ private: friend class element; friend struct simdjson_result; template - friend class simdjson::minify; + friend class simdjson::minifier; }; /** diff --git a/include/simdjson/dom/document.h b/include/simdjson/dom/document.h index d8c1303dc..7b8f1e57a 100644 --- a/include/simdjson/dom/document.h +++ b/include/simdjson/dom/document.h @@ -68,7 +68,7 @@ public: private: inline error_code allocate(size_t len) noexcept; template - friend class simdjson::minify; + friend class simdjson::minifier; friend class parser; }; // class document diff --git a/include/simdjson/dom/element.h b/include/simdjson/dom/element.h index 964bac7d9..80b6a3958 100644 --- a/include/simdjson/dom/element.h +++ b/include/simdjson/dom/element.h @@ -255,7 +255,7 @@ private: friend class array; friend struct simdjson_result; template - friend class simdjson::minify; + friend class simdjson::minifier; }; /** diff --git a/include/simdjson/dom/object.h b/include/simdjson/dom/object.h index 1dad97593..332183bc6 100644 --- a/include/simdjson/dom/object.h +++ b/include/simdjson/dom/object.h @@ -168,7 +168,7 @@ private: friend class element; friend struct simdjson_result; template - friend class simdjson::minify; + friend class simdjson::minifier; }; /** diff --git a/include/simdjson/inline/array.h b/include/simdjson/inline/array.h index 78370e43a..da5081425 100644 --- a/include/simdjson/inline/array.h +++ b/include/simdjson/inline/array.h @@ -121,7 +121,7 @@ inline std::ostream& operator<<(std::ostream& out, const array &value) { } // namespace dom template<> -inline std::ostream& minify::print(std::ostream& out) { +inline std::ostream& minifier::print(std::ostream& out) { out << '['; auto iter = value.begin(); auto end = value.end(); @@ -137,7 +137,7 @@ inline std::ostream& minify::print(std::ostream& out) { #if SIMDJSON_EXCEPTIONS template<> -inline std::ostream& minify>::print(std::ostream& out) { +inline std::ostream& minifier>::print(std::ostream& out) { if (value.error()) { throw simdjson_error(value.error()); } return out << minify(value.first); } diff --git a/include/simdjson/inline/element.h b/include/simdjson/inline/element.h index 38fea5f4b..c77be566b 100644 --- a/include/simdjson/inline/element.h +++ b/include/simdjson/inline/element.h @@ -300,7 +300,7 @@ inline std::ostream& operator<<(std::ostream& out, element_type type) { } // namespace dom template<> -inline std::ostream& minify::print(std::ostream& out) { +inline std::ostream& minifier::print(std::ostream& out) { using tape_type=internal::tape_type; size_t depth = 0; constexpr size_t MAX_DEPTH = 16; @@ -426,7 +426,7 @@ inline std::ostream& minify::print(std::ostream& out) { #if SIMDJSON_EXCEPTIONS template<> -inline std::ostream& minify>::print(std::ostream& out) { +inline std::ostream& minifier>::print(std::ostream& out) { if (value.error()) { throw simdjson_error(value.error()); } return out << minify(value.first); } diff --git a/include/simdjson/inline/object.h b/include/simdjson/inline/object.h index d274e6096..bbd69818c 100644 --- a/include/simdjson/inline/object.h +++ b/include/simdjson/inline/object.h @@ -219,7 +219,7 @@ inline std::ostream& operator<<(std::ostream& out, const key_value_pair &value) } // namespace dom template<> -inline std::ostream& minify::print(std::ostream& out) { +inline std::ostream& minifier::print(std::ostream& out) { out << '{'; auto pair = value.begin(); auto end = value.end(); @@ -233,14 +233,14 @@ inline std::ostream& minify::print(std::ostream& out) { } template<> -inline std::ostream& minify::print(std::ostream& out) { +inline std::ostream& minifier::print(std::ostream& out) { return out << '"' << internal::escape_json_string(value.key) << "\":" << value.value; } #if SIMDJSON_EXCEPTIONS template<> -inline std::ostream& minify>::print(std::ostream& out) { +inline std::ostream& minifier>::print(std::ostream& out) { if (value.error()) { throw simdjson_error(value.error()); } return out << minify(value.first); } diff --git a/include/simdjson/minify.h b/include/simdjson/minify.h index 7392e979e..5d4522931 100644 --- a/include/simdjson/minify.h +++ b/include/simdjson/minify.h @@ -24,8 +24,7 @@ namespace simdjson { * @param dst_len the number of bytes written. Output only. * @return the error code, or SUCCESS if there was no error. */ -WARN_UNUSED error_code minify_string(const char *buf, size_t len, char *dst, size_t &dst_len) noexcept; - +WARN_UNUSED error_code minify(const char *buf, size_t len, char *dst, size_t &dst_len) noexcept; /** * Minifies a JSON element or document, printing the smallest possible valid JSON. @@ -36,14 +35,14 @@ WARN_UNUSED error_code minify_string(const char *buf, size_t len, char *dst, siz * */ template -class minify { +class minifier { public: /** * Create a new minifier. * * @param _value The document or element to minify. */ - inline minify(const T &_value) noexcept : value{_value} {} + inline minifier(const T &_value) noexcept : value{_value} {} /** * Minify JSON to a string. @@ -58,6 +57,9 @@ private: const T &value; }; +template +inline minifier minify(const T &value) noexcept { return minifier(value); } + /** * Minify JSON to an output stream. * @@ -66,7 +68,7 @@ private: * @throw if there is an error with the underlying output stream. simdjson itself will not throw. */ template -inline std::ostream& operator<<(std::ostream& out, minify formatter) { return formatter.print(out); } +inline std::ostream& operator<<(std::ostream& out, minifier formatter) { return formatter.print(out); } } // namespace simdjson diff --git a/src/implementation.cpp b/src/implementation.cpp index eb4574813..1c5c516de 100644 --- a/src/implementation.cpp +++ b/src/implementation.cpp @@ -134,7 +134,7 @@ const implementation *detect_best_supported_implementation_on_first_use::set_bes SIMDJSON_DLLIMPORTEXPORT const internal::available_implementation_list available_implementations{}; SIMDJSON_DLLIMPORTEXPORT internal::atomic_ptr active_implementation{&internal::detect_best_supported_implementation_on_first_use_singleton}; -WARN_UNUSED error_code minify_string(const char *buf, size_t len, char *dst, size_t &dst_len) noexcept { +WARN_UNUSED error_code minify(const char *buf, size_t len, char *dst, size_t &dst_len) noexcept { return active_implementation->minify((const uint8_t *)buf, len, (uint8_t *)dst, dst_len); } diff --git a/tests/basictests.cpp b/tests/basictests.cpp index 9d72faeb8..07bd8da4e 100644 --- a/tests/basictests.cpp +++ b/tests/basictests.cpp @@ -1714,7 +1714,7 @@ namespace type_tests { -namespace minify_string_tests { +namespace minify_tests { bool check_minification(const char * input, size_t length, const char * expected, size_t expected_length) { std::unique_ptr buffer{new(std::nothrow) char[length + simdjson::SIMDJSON_PADDING]}; @@ -1723,7 +1723,7 @@ namespace minify_string_tests { return false; } size_t newlength{}; - auto error = simdjson::minify_string(input, length, buffer.get(), newlength); + auto error = simdjson::minify(input, length, buffer.get(), newlength); if(error != simdjson::SUCCESS) { std::cerr << "error " << error << std::endl; return false; @@ -1742,28 +1742,28 @@ namespace minify_string_tests { return true; } - bool test_minify_string() { + bool test_minify() { std::cout << "Running " << __func__ << std::endl; const std::string test = R"({ "foo" : 1, "bar" : [ 1, 2, 3 ], "baz": { "a": 1, "b": 2, "c": 3 } })"; const std::string minified(R"({"foo":1,"bar":[1,2,3],"baz":{"a":1,"b":2,"c":3}})"); return check_minification(test.c_str(), test.size(), minified.c_str(), minified.size()); } - bool test_minify_string_array() { + bool test_minify_array() { std::cout << "Running " << __func__ << std::endl; std::string test("[ 1, 2, 3]"); std::string minified("[1,2,3]"); return check_minification(test.c_str(), test.size(), minified.c_str(), minified.size()); } - bool test_minify_string_object() { + bool test_minify_object() { std::cout << "Running " << __func__ << std::endl; std::string test(R"({ "foo " : 1, "b ar" : [ 1, 2, 3 ], "baz": { "a": 1, "b": 2, "c": 3 } })"); std::string minified(R"({"foo ":1,"b ar":[1,2,3],"baz":{"a":1,"b":2,"c":3}})"); return check_minification(test.c_str(), test.size(), minified.c_str(), minified.size()); } bool run() { - return test_minify_string() && - test_minify_string_array() && - test_minify_string_object(); + return test_minify() && + test_minify_array() && + test_minify_object(); } } @@ -2024,7 +2024,7 @@ int main(int argc, char *argv[]) { printf("unsupported CPU\n"); } std::cout << "Running basic tests." << std::endl; - if (minify_string_tests::run() && + if (minify_tests::run() && parse_api_tests::run() && dom_api_tests::run() && type_tests::run() && diff --git a/tests/readme_examples.cpp b/tests/readme_examples.cpp index 3a22ba06d..b4c0bfc89 100644 --- a/tests/readme_examples.cpp +++ b/tests/readme_examples.cpp @@ -239,12 +239,12 @@ void performance_3() { SIMDJSON_POP_DISABLE_WARNINGS #endif -void minify_string() { +void minify() { const char * some_string = "[ 1, 2, 3, 4] "; size_t length = strlen(some_string); std::unique_ptr buffer{new(std::nothrow) char[length + simdjson::SIMDJSON_PADDING]}; size_t new_length{}; - auto error = simdjson::minify_string(some_string, length, buffer.get(), new_length); + auto error = simdjson::minify(some_string, length, buffer.get(), new_length); if(error != simdjson::SUCCESS) { std::cerr << "error " << error << std::endl; abort(); @@ -269,6 +269,6 @@ int main() { basics_dom_2(); basics_dom_3(); basics_dom_4(); - minify_string(); + minify(); return 0; }