From a76c67c19f5248c93ff9890d56ae1caed77a8936 Mon Sep 17 00:00:00 2001 From: Daniel Lemire Date: Mon, 22 Jun 2020 15:57:54 -0400 Subject: [PATCH] Fixing... --- doc/basics.md | 2 +- include/simdjson/implementation.h | 14 +++++++++++++- tests/readme_examples.cpp | 14 ++++++++++++++ 3 files changed, 28 insertions(+), 2 deletions(-) diff --git a/doc/basics.md b/doc/basics.md index 0aaf5d8d..7a32da92 100644 --- a/doc/basics.md +++ b/doc/basics.md @@ -200,7 +200,7 @@ The simdjson library has fast functions to validate UTF-8 strings. They are many bool is_ok = simdjson::validate_utf8(some_string, length); ``` -Though it does not validate the JSON input, it will detect when the document ends with an unterminated string. E.g., it would refuse to minify the string `"this string is not terminated` because of the missing final quote. +The UTF-8 validation function merely checks that the input is valid UTF-8: it works with strings in general, not just JSON strings. C++17 Support diff --git a/include/simdjson/implementation.h b/include/simdjson/implementation.h index f4146fcf..13ef72b7 100644 --- a/include/simdjson/implementation.h +++ b/include/simdjson/implementation.h @@ -26,7 +26,19 @@ WARN_UNUSED bool validate_utf8(const char * buf, size_t length) noexcept; * @param p the string_view to validate. * @return true if the string is valid UTF-8. */ -WARN_UNUSED bool validate_utf8(std::string_view& p) noexcept; +really_inline WARN_UNUSED bool validate_utf8(const std::string_view& p) noexcept { + return validate_utf8(p.data(), p.size()); +} + +/** + * Validate the UTF-8 string. + * + * @param p the string_view to validate. + * @return true if the string is valid UTF-8. + */ +really_inline WARN_UNUSED bool validate_utf8(const std::string& p) noexcept { + return validate_utf8(p.data(), p.size()); +} namespace dom { class document; diff --git a/tests/readme_examples.cpp b/tests/readme_examples.cpp index 1c2617ca..692b9492 100644 --- a/tests/readme_examples.cpp +++ b/tests/readme_examples.cpp @@ -272,6 +272,20 @@ bool is_correct() { return is_ok; } +bool is_correct_string_view() { + const char * some_string = "[ 1, 2, 3, 4] "; + size_t length = strlen(some_string); + std::string_view v(some_string, length); + bool is_ok = simdjson::validate_utf8(v); + return is_ok; +} + +bool is_correct_string() { + const std::string some_string = "[ 1, 2, 3, 4] "; + bool is_ok = simdjson::validate_utf8(some_string); + return is_ok; +} + int main() { basics_dom_1(); basics_dom_2();