From a32cd6cb61a62fe8f9c4059ca2c428e945d17adc Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Wed, 16 Sep 2020 21:24:03 +0900 Subject: [PATCH] feat: enable to use u8""_toml literal in C++20 --- toml/literal.hpp | 31 ++++++++++++++++++++++++++----- 1 file changed, 26 insertions(+), 5 deletions(-) diff --git a/toml/literal.hpp b/toml/literal.hpp index 5292b3d..2c3d18c 100644 --- a/toml/literal.hpp +++ b/toml/literal.hpp @@ -11,12 +11,9 @@ inline namespace literals inline namespace toml_literals { -inline ::toml::value operator"" _toml(const char* str, std::size_t len) +// implementation +inline ::toml::value literal_internal_impl(::toml::detail::location loc) { - ::toml::detail::location - loc(/* filename = */ std::string("TOML literal encoded in a C++ code"), - /* contents = */ std::vector(str, str + len)); - // if there are some comments or empty lines, skip them. using skip_line = ::toml::detail::repeat, @@ -78,8 +75,32 @@ inline ::toml::value operator"" _toml(const char* str, std::size_t len) { throw ::toml::syntax_error(data.unwrap_err(), source_location(loc)); } + } +inline ::toml::value operator"" _toml(const char* str, std::size_t len) +{ + ::toml::detail::location loc( + std::string("TOML literal encoded in a C++ code"), + std::vector(str, str + len)); + return literal_internal_impl(std::move(loc)); +} + +// value of __cplusplus in C++2a/20 mode is not fixed yet along compilers. +// So here we use the feature test macro for `char8_t` itself. +#if defined(__cpp_char8_t) && __cpp_char8_t >= 201811L +// value of u8"" literal has been changed from char to char8_t and char8_t is +// NOT compatible to char +inline ::toml::value operator"" _toml(const char8_t* str, std::size_t len) +{ + ::toml::detail::location loc( + std::string("TOML literal encoded in a C++ code"), + std::vector(reinterpret_cast(str), + reinterpret_cast(str) + len)); + return literal_internal_impl(std::move(loc)); +} +#endif + } // toml_literals } // literals } // toml