From 19199ac82c04bbf930e55f642d640343d2b042a3 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 25 Jul 2024 01:24:55 +0900 Subject: [PATCH] feat: add key_format --- include/toml11/fwd/format_fwd.hpp | 23 ++++++++++++++++++ include/toml11/impl/format_impl.hpp | 36 +++++++++++++++++++++++++++++ 2 files changed, 59 insertions(+) diff --git a/include/toml11/fwd/format_fwd.hpp b/include/toml11/fwd/format_fwd.hpp index d478d96..880b99c 100644 --- a/include/toml11/fwd/format_fwd.hpp +++ b/include/toml11/fwd/format_fwd.hpp @@ -214,6 +214,29 @@ struct table_format_info bool operator==(const table_format_info&, const table_format_info&) noexcept; bool operator!=(const table_format_info&, const table_format_info&) noexcept; +// ---------------------------------------------------------------------------- +// key + +enum class key_format : std::uint8_t +{ + bare = 0, + quoted = 1, // "" + quoted_literal = 2 // '' +}; + +std::ostream& operator<<(std::ostream& os, const key_format f); +std::string to_string(const key_format); + +struct key_format_info +{ + key_format fmt = key_format::bare; + std::int32_t spaces_before_equal = 1; + std::int32_t spaces_after_equal = 1; +}; + +bool operator==(const key_format_info&, const key_format_info&) noexcept; +bool operator!=(const key_format_info&, const key_format_info&) noexcept; + // ---------------------------------------------------------------------------- // wrapper diff --git a/include/toml11/impl/format_impl.hpp b/include/toml11/impl/format_impl.hpp index c0c6026..fcce609 100644 --- a/include/toml11/impl/format_impl.hpp +++ b/include/toml11/impl/format_impl.hpp @@ -293,5 +293,41 @@ TOML11_INLINE bool operator!=(const table_format_info& lhs, const table_format_i return !(lhs == rhs); } +// ---------------------------------------------------------------------------- +// key + +TOML11_INLINE std::ostream& operator<<(std::ostream& os, const key_format f) +{ + switch(f) + { + case key_format::bare : {os << "bare" ; break;} + case key_format::quoted : {os << "quoted" ; break;} + case key_format::quoted_literal : {os << "quoted_literal"; break;} + default: + { + os << "unknown key_format: " << static_cast(f); + break; + } + } + return os; +} +TOML11_INLINE std::string to_string(const key_format c) +{ + std::ostringstream oss; + oss << c; + return oss.str(); +} + +TOML11_INLINE bool operator==(const key_format_info& lhs, const key_format_info& rhs) noexcept +{ + return lhs.fmt == rhs.fmt && + lhs.spaces_before_equal == rhs.spaces_before_equal && + lhs.spaces_after_equal == rhs.spaces_after_equal ; +} +TOML11_INLINE bool operator!=(const key_format_info& lhs, const key_format_info& rhs) noexcept +{ + return !(lhs == rhs); +} + } // namespace toml #endif // TOML11_FORMAT_IMPL_HPP