From 68cdfa8dd44131597944053d7c332f3d672a1dbe Mon Sep 17 00:00:00 2001 From: Laz Date: Sun, 11 Feb 2024 13:04:36 +0000 Subject: [PATCH] feat: add support for parsing std::wstrings --- include/rfl/parsing/Parser.hpp | 1 + include/rfl/parsing/Parser_wstring.hpp | 74 ++++++++++++++++++++++++++ tests/bson/test_wstring.cpp | 26 +++++++++ tests/bson/test_wstring.hpp | 3 ++ tests/bson/tests.cpp | 2 + tests/cbor/test_wstring.cpp | 26 +++++++++ tests/cbor/test_wstring.hpp | 3 ++ tests/cbor/tests.cpp | 2 + tests/flexbuffers/test_wstring.cpp | 26 +++++++++ tests/flexbuffers/test_wstring.hpp | 3 ++ tests/flexbuffers/tests.cpp | 2 + tests/json/test_wstring.cpp | 22 ++++++++ tests/json/test_wstring.hpp | 3 ++ tests/json/tests.cpp | 3 ++ tests/xml/test_wstring.cpp | 26 +++++++++ tests/xml/test_wstring.hpp | 3 ++ tests/xml/tests.cpp | 2 + tests/yaml/test_wstring.cpp | 26 +++++++++ tests/yaml/test_wstring.hpp | 3 ++ tests/yaml/tests.cpp | 2 + 20 files changed, 258 insertions(+) create mode 100644 include/rfl/parsing/Parser_wstring.hpp create mode 100644 tests/bson/test_wstring.cpp create mode 100644 tests/bson/test_wstring.hpp create mode 100644 tests/cbor/test_wstring.cpp create mode 100644 tests/cbor/test_wstring.hpp create mode 100644 tests/flexbuffers/test_wstring.cpp create mode 100644 tests/flexbuffers/test_wstring.hpp create mode 100644 tests/json/test_wstring.cpp create mode 100644 tests/json/test_wstring.hpp create mode 100644 tests/xml/test_wstring.cpp create mode 100644 tests/xml/test_wstring.hpp create mode 100644 tests/yaml/test_wstring.cpp create mode 100644 tests/yaml/test_wstring.hpp diff --git a/include/rfl/parsing/Parser.hpp b/include/rfl/parsing/Parser.hpp index eaecbc67..7bb7cde2 100644 --- a/include/rfl/parsing/Parser.hpp +++ b/include/rfl/parsing/Parser.hpp @@ -23,5 +23,6 @@ #include "Parser_unique_ptr.hpp" #include "Parser_variant.hpp" #include "Parser_vector_like.hpp" +#include "Parser_wstring.hpp" #endif diff --git a/include/rfl/parsing/Parser_wstring.hpp b/include/rfl/parsing/Parser_wstring.hpp new file mode 100644 index 00000000..49ac0111 --- /dev/null +++ b/include/rfl/parsing/Parser_wstring.hpp @@ -0,0 +1,74 @@ +#ifndef RFL_PARSING_PARSER_WSTRING_HPP_ +#define RFL_PARSING_PARSER_WSTRING_HPP_ + +#include + +#include "../Result.hpp" +#include "../always_false.hpp" +#include "Parent.hpp" +#include "Parser_base.hpp" + +namespace rfl { +namespace parsing { + +template + requires AreReaderAndWriter +struct Parser { + public: + using InputVarType = typename R::InputVarType; + using OutputVarType = typename W::OutputVarType; + + using ParentType = Parent; + + static Result read(const R& _r, + const InputVarType& _var) noexcept { + if (_r.is_empty(_var)) { + return std::wstring(); + } + + auto inStr = Parser::read(_r, _var); + if (auto err = inStr.error(); err.has_value()) { + return Result(err.value()); + } + + std::mbstate_t state = std::mbstate_t(); + auto val = inStr.value(); + + std::wstring outStr(L'\0', val.size()); + + // Explicitly set the size so we don't empty it when we truncate + outStr.resize(val.size()); + + auto* ptr = val.c_str(); + + // Add 1 for null terminator + auto len = std::mbsrtowcs(outStr.data(), &ptr, val.size(), &state) + 1; + outStr.resize(len); // Truncate the extra bytes + + return Result(outStr); + } + + template + static void write(const W& _w, const std::wstring& _str, + const P& _parent) noexcept { + if (_str.empty()) { + ParentType::add_value(_w, std::string(), _parent); + return; + } + + std::mbstate_t state = std::mbstate_t(); + std::string outStr('\0', _str.size()); + outStr.resize(_str.size()); + + auto* ptr = _str.c_str(); + auto len = std::wcsrtombs(outStr.data(), &ptr, _str.size(), &state) + 1; + outStr.resize(len); + + ParentType::add_value(_w, outStr, _parent); + } +}; + +} // namespace parsing +} // namespace rfl + +#endif \ No newline at end of file diff --git a/tests/bson/test_wstring.cpp b/tests/bson/test_wstring.cpp new file mode 100644 index 00000000..ba6202c0 --- /dev/null +++ b/tests/bson/test_wstring.cpp @@ -0,0 +1,26 @@ +#include "test_wstring.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +struct Test { + std::string theNormalString; + std::wstring theWiderString; +}; + +namespace test_wstring { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Test test = Test{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; + + write_and_read(test); +} +} // namespace test_wstring diff --git a/tests/bson/test_wstring.hpp b/tests/bson/test_wstring.hpp new file mode 100644 index 00000000..0f93e784 --- /dev/null +++ b/tests/bson/test_wstring.hpp @@ -0,0 +1,3 @@ +namespace test_wstring { + void test(); +} diff --git a/tests/bson/tests.cpp b/tests/bson/tests.cpp index 07067f88..8a9fe0fb 100644 --- a/tests/bson/tests.cpp +++ b/tests/bson/tests.cpp @@ -28,6 +28,7 @@ #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" #include "test_variant.hpp" +#include "test_wstring.hpp" int main() { test_readme_example::test(); @@ -60,6 +61,7 @@ int main() { test_custom_class4::test(); test_default_values::test(); test_save_load::test(); + test_wstring::test(); return 0; } diff --git a/tests/cbor/test_wstring.cpp b/tests/cbor/test_wstring.cpp new file mode 100644 index 00000000..ba6202c0 --- /dev/null +++ b/tests/cbor/test_wstring.cpp @@ -0,0 +1,26 @@ +#include "test_wstring.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +struct Test { + std::string theNormalString; + std::wstring theWiderString; +}; + +namespace test_wstring { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Test test = Test{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; + + write_and_read(test); +} +} // namespace test_wstring diff --git a/tests/cbor/test_wstring.hpp b/tests/cbor/test_wstring.hpp new file mode 100644 index 00000000..0f93e784 --- /dev/null +++ b/tests/cbor/test_wstring.hpp @@ -0,0 +1,3 @@ +namespace test_wstring { + void test(); +} diff --git a/tests/cbor/tests.cpp b/tests/cbor/tests.cpp index 07067f88..8a9fe0fb 100644 --- a/tests/cbor/tests.cpp +++ b/tests/cbor/tests.cpp @@ -28,6 +28,7 @@ #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" #include "test_variant.hpp" +#include "test_wstring.hpp" int main() { test_readme_example::test(); @@ -60,6 +61,7 @@ int main() { test_custom_class4::test(); test_default_values::test(); test_save_load::test(); + test_wstring::test(); return 0; } diff --git a/tests/flexbuffers/test_wstring.cpp b/tests/flexbuffers/test_wstring.cpp new file mode 100644 index 00000000..ba6202c0 --- /dev/null +++ b/tests/flexbuffers/test_wstring.cpp @@ -0,0 +1,26 @@ +#include "test_wstring.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +struct Test { + std::string theNormalString; + std::wstring theWiderString; +}; + +namespace test_wstring { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Test test = Test{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; + + write_and_read(test); +} +} // namespace test_wstring diff --git a/tests/flexbuffers/test_wstring.hpp b/tests/flexbuffers/test_wstring.hpp new file mode 100644 index 00000000..0f93e784 --- /dev/null +++ b/tests/flexbuffers/test_wstring.hpp @@ -0,0 +1,3 @@ +namespace test_wstring { + void test(); +} diff --git a/tests/flexbuffers/tests.cpp b/tests/flexbuffers/tests.cpp index 90095446..aab6d5a7 100644 --- a/tests/flexbuffers/tests.cpp +++ b/tests/flexbuffers/tests.cpp @@ -31,6 +31,7 @@ #include "test_unordered_multiset.hpp" #include "test_unordered_set.hpp" #include "test_variant.hpp" +#include "test_wstring.hpp" int main() { test_readme_example::test(); @@ -70,6 +71,7 @@ int main() { test_all_of::test(); test_save_load::test(); + test_wstring::test(); return 0; } diff --git a/tests/json/test_wstring.cpp b/tests/json/test_wstring.cpp new file mode 100644 index 00000000..c6db773a --- /dev/null +++ b/tests/json/test_wstring.cpp @@ -0,0 +1,22 @@ +#include "test_wstring.hpp" + +#include +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +namespace test_wstring { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + std::map homer; + homer.insert(std::make_pair("firstName", L"Homer")); + + write_and_read(homer, R"({"firstName":"Homer"})"); +} +} // namespace test_wstring diff --git a/tests/json/test_wstring.hpp b/tests/json/test_wstring.hpp new file mode 100644 index 00000000..0f93e784 --- /dev/null +++ b/tests/json/test_wstring.hpp @@ -0,0 +1,3 @@ +namespace test_wstring { + void test(); +} diff --git a/tests/json/tests.cpp b/tests/json/tests.cpp index 4b9eda91..e9af7282 100644 --- a/tests/json/tests.cpp +++ b/tests/json/tests.cpp @@ -69,6 +69,7 @@ #include "test_unordered_set.hpp" #include "test_variant.hpp" #include "test_view.hpp" +#include "test_wstring.hpp" int main() { test_readme_example::test(); @@ -150,5 +151,7 @@ int main() { test_meta_fields::test(); + test_wstring::test(); + return 0; } diff --git a/tests/xml/test_wstring.cpp b/tests/xml/test_wstring.cpp new file mode 100644 index 00000000..52499e4a --- /dev/null +++ b/tests/xml/test_wstring.cpp @@ -0,0 +1,26 @@ +#include "test_wstring.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +struct Test { + std::string theNormalString; + std::wstring theWiderString; +}; + +namespace test_wstring { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Test test = Test{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; + + write_and_read<"root">(test); +} +} // namespace test_wstring diff --git a/tests/xml/test_wstring.hpp b/tests/xml/test_wstring.hpp new file mode 100644 index 00000000..0f93e784 --- /dev/null +++ b/tests/xml/test_wstring.hpp @@ -0,0 +1,3 @@ +namespace test_wstring { + void test(); +} diff --git a/tests/xml/tests.cpp b/tests/xml/tests.cpp index e15a4023..f8671ee9 100644 --- a/tests/xml/tests.cpp +++ b/tests/xml/tests.cpp @@ -29,6 +29,7 @@ #include "test_unique_ptr2.hpp" #include "test_variant.hpp" #include "test_xml_content.hpp" +#include "test_wstring.hpp" int main() { test_readme_example::test(); @@ -62,6 +63,7 @@ int main() { test_custom_class4::test(); test_default_values::test(); test_save_load::test(); + test_wstring::test(); return 0; } diff --git a/tests/yaml/test_wstring.cpp b/tests/yaml/test_wstring.cpp new file mode 100644 index 00000000..ba6202c0 --- /dev/null +++ b/tests/yaml/test_wstring.cpp @@ -0,0 +1,26 @@ +#include "test_wstring.hpp" + +#include +#include +#include +#include +#include +#include + +#include "write_and_read.hpp" + +struct Test { + std::string theNormalString; + std::wstring theWiderString; +}; + +namespace test_wstring { +void test() { + std::cout << std::source_location::current().function_name() << std::endl; + + const Test test = Test{.theNormalString = "The normal string", + .theWiderString = L"The wider string"}; + + write_and_read(test); +} +} // namespace test_wstring diff --git a/tests/yaml/test_wstring.hpp b/tests/yaml/test_wstring.hpp new file mode 100644 index 00000000..0f93e784 --- /dev/null +++ b/tests/yaml/test_wstring.hpp @@ -0,0 +1,3 @@ +namespace test_wstring { + void test(); +} diff --git a/tests/yaml/tests.cpp b/tests/yaml/tests.cpp index 3a1bcf26..7a38771d 100644 --- a/tests/yaml/tests.cpp +++ b/tests/yaml/tests.cpp @@ -29,6 +29,7 @@ #include "test_unique_ptr.hpp" #include "test_unique_ptr2.hpp" #include "test_variant.hpp" +#include "test_wstring.hpp" int main() { test_readme_example::test(); @@ -62,6 +63,7 @@ int main() { test_default_values::test(); test_custom_constructor::test(); test_save_load::test(); + test_wstring::test(); return 0; }