From fd50b11523e247efa21d90eafe70eccaec7121b9 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Sun, 18 Oct 2020 18:30:04 +0900 Subject: [PATCH] refactor: add write_comments() --- toml/serializer.hpp | 56 ++++++++++++++++----------------------------- 1 file changed, 20 insertions(+), 36 deletions(-) diff --git a/toml/serializer.hpp b/toml/serializer.hpp index 385e79b..79a6b92 100644 --- a/toml/serializer.hpp +++ b/toml/serializer.hpp @@ -523,15 +523,8 @@ struct serializer continue; } - if(!kv.second.comments().empty() && !no_comment_) - { - for(const auto& c : kv.second.comments()) - { - token += '#'; - token += c; - token += '\n'; - } - } + token += write_comments(kv.second); + const auto key_and_sep = format_key(kv.first) + " = "; const auto residual_width = (this->width_ > key_and_sep.size()) ? this->width_ - key_and_sep.size() : 0; @@ -577,15 +570,7 @@ struct serializer tmp += '\n'; } - if(!kv.second.comments().empty() && !no_comment_) - { - for(const auto& c : kv.second.comments()) - { - token += '#'; - token += c; - token += '\n'; - } - } + token += write_comments(kv.second); token += tmp; } return token; @@ -621,15 +606,8 @@ struct serializer failed = true; break; } - if(!no_comment_) - { - for(const auto& c : item.comments()) - { - token += '#'; - token += c; - token += '\n'; - } - } + // write comments for the table itself + token += write_comments(item); const auto t = this->make_inline_table(item.as_table()); @@ -653,15 +631,7 @@ struct serializer std::string token; for(const auto& item : v) { - if(!no_comment_) - { - for(const auto& c : item.comments()) - { - token += '#'; - token += c; - token += '\n'; - } - } + token += write_comments(item); token += "[["; token += format_keys(keys_); token += "]]\n"; @@ -670,6 +640,20 @@ struct serializer return token; } + std::string write_comments(const value_type& v) const + { + std::string retval; + if(this->no_comment_) {return retval;} + + for(const auto& c : v.comments()) + { + retval += '#'; + retval += c; + retval += '\n'; + } + return retval; + } + bool is_array_of_tables(const value_type& v) const { if(!v.is_array()) {return false;}