From be2d2aec524a1a3819cec1b85243a5d79fceb3e2 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 20 Jun 2019 23:59:16 +0900 Subject: [PATCH] refactor: explicitly convert difference_t to size_t --- toml/serializer.hpp | 35 ++++++++++++++++++++++++----------- 1 file changed, 24 insertions(+), 11 deletions(-) diff --git a/toml/serializer.hpp b/toml/serializer.hpp index cba2923..6a508a6 100644 --- a/toml/serializer.hpp +++ b/toml/serializer.hpp @@ -29,7 +29,7 @@ struct serializer using array_type = typename value_type::array_type ; using table_type = typename value_type::table_type ; - serializer(const std::size_t w = 80, + serializer(const std::size_t w = 80u, const int float_prec = std::numeric_limits::max_digits10, const bool can_be_inlined = false, std::vector ks = {}) @@ -50,7 +50,8 @@ struct serializer { const auto fmt = "%.*g"; const auto bsz = std::snprintf(nullptr, 0, fmt, this->float_prec_, f); - std::vector buf(bsz + 1, '\0'); // +1 for null character(\0) + // +1 for null character(\0) + std::vector buf(static_cast(bsz + 1), '\0'); std::snprintf(buf.data(), buf.size(), fmt, this->float_prec_, f); std::string token(buf.begin(), std::prev(buf.end())); @@ -58,16 +59,27 @@ struct serializer { token += '0'; } - const auto e = std::find_if(token.cbegin(), token.cend(), - [](const char c) -> bool { - return c == 'E' || c == 'e'; + + const auto e = std::find_if( + token.cbegin(), token.cend(), [](const char c) noexcept -> bool { + return c == 'e' || c == 'E'; }); - if(e == token.cend()) + const auto has_exponent = (token.cend() != e); + const auto has_fraction = (token.cend() != std::find( + token.cbegin(), token.cend(), '.')); + + if(!has_exponent && !has_fraction) + { + // the resulting value does not have any float specific part! + token += ".0"; + return token; + } + if(!has_exponent) { return token; // there is no exponent part. just return it. } - // zero-prefix in an exponent is not allowed in TOML. + // zero-prefix in an exponent is NOT allowed in TOML. // remove it if it exists. bool sign_exists = false; std::size_t zero_prefix = 0; @@ -81,7 +93,8 @@ struct serializer { const auto offset = std::distance(token.cbegin(), e) + (sign_exists ? 2 : 1); - token.erase(offset, zero_prefix); + token.erase(static_cast(offset), + zero_prefix); } return token; } @@ -609,7 +622,7 @@ struct serializer template class M, template class V> std::string -format(const basic_value& v, std::size_t w = 80, +format(const basic_value& v, std::size_t w = 80u, int fprec = std::numeric_limits::max_digits10, bool force_inline = false) { @@ -638,8 +651,8 @@ std::basic_ostream& operator<<(std::basic_ostream& os, const basic_value& v) { // get status of std::setw(). - const std::size_t w = os.width(); - const int fprec = os.precision(); + const auto w = static_cast(os.width()); + const int fprec = os.precision(); os.width(0); if(!v.comments().empty())