diff --git a/include/fkYAML/detail/conversions/to_string.hpp b/include/fkYAML/detail/conversions/to_string.hpp new file mode 100644 index 00000000..a74db345 --- /dev/null +++ b/include/fkYAML/detail/conversions/to_string.hpp @@ -0,0 +1,91 @@ +/** + * _______ __ __ __ _____ __ __ __ + * | __| |_/ | \_/ |/ _ \ / \/ \| | fkYAML: A C++ header-only YAML library + * | __| _ < \_ _/| ___ | _ | |___ version 0.1.2 + * |__| |_| \__| |_| |_| |_|___||___|______| https://github.com/fktn-k/fkYAML + * + * SPDX-FileCopyrightText: 2023 Kensuke Fukutani + * SPDX-License-Identifier: MIT + * + * @file + */ + +#ifndef TO__string_HPP_ +#define TO__string_HPP_ + +#include +#include +#include +#include +#include + +#include +#include +#include + +/** + * @namespace fkyaml + * @brief namespace for fkYAML library. + */ +FK_YAML_NAMESPACE_BEGIN + +/** + * @namespace detail + * @brief namespace for internal implementations of fkYAML library. + */ +namespace detail +{ + +template +inline void to_string(std::basic_string& s, ValueType); + +template <> +inline void to_string(std::string& s, std::nullptr_t /*unused*/) +{ + s = "null"; +} + +template <> +inline void to_string(std::string& s, bool b) +{ + s = b ? "true" : "false"; +} + +template +inline enable_if_t::value> to_string(std::string& s, IntegerType i) +{ + s = std::to_string(i); +} + +template +inline enable_if_t::value> to_string(std::string& s, FloatType f) +{ + if (std::isnan(f)) + { + s = ".nan"; + return; + } + + if (std::isinf(f)) + { + if (f == std::numeric_limits::infinity()) + { + s = ".inf"; + } + else + { + s = "-.inf"; + } + return; + } + + std::ostringstream oss; + oss << f; + s = oss.str(); +} + +} // namespace detail + +FK_YAML_NAMESPACE_END + +#endif /* TO__string_HPP_ */ diff --git a/include/fkYAML/detail/output/serializer.hpp b/include/fkYAML/detail/output/serializer.hpp index 82e3c258..d51566d1 100644 --- a/include/fkYAML/detail/output/serializer.hpp +++ b/include/fkYAML/detail/output/serializer.hpp @@ -18,6 +18,7 @@ #include #include +#include #include #include #include @@ -111,44 +112,20 @@ class basic_serializer } break; case node_t::NULL_OBJECT: - str += "null"; + to_string(m_tmp_str_buff, nullptr); + str += m_tmp_str_buff; break; case node_t::BOOLEAN: - if (node.to_boolean()) - { - str += "true"; - } - else - { - str += "false"; - } + to_string(m_tmp_str_buff, node.to_boolean()); + str += m_tmp_str_buff; break; case node_t::INTEGER: - str += std::to_string(node.to_integer()); + to_string(m_tmp_str_buff, node.to_integer()); + str += m_tmp_str_buff; break; case node_t::FLOAT_NUMBER: { - typename BasicNodeType::float_number_type float_val = node.to_float_number(); - if (std::isnan(float_val)) - { - str += ".nan"; - } - else if (std::isinf(float_val)) - { - if (float_val == std::numeric_limits::infinity()) - { - str += ".inf"; - } - else - { - str += "-.inf"; - } - } - else - { - std::stringstream ss; - ss << node.to_float_number(); - str += ss.str(); - } + to_string(m_tmp_str_buff, node.to_float_number()); + str += m_tmp_str_buff; break; } case node_t::STRING: @@ -183,6 +160,10 @@ class basic_serializer str += " "; } } + +private: + /** A temporal buffer for conversion from a scalar to a string. */ + std::string m_tmp_str_buff; }; } // namespace detail