feat: add no_comment option to serializer

This commit is contained in:
ToruNiina
2019-06-29 14:59:18 +09:00
parent 937a3b4a2e
commit d5299fef04

View File

@@ -71,9 +71,10 @@ struct serializer
serializer(const std::size_t w = 80u, serializer(const std::size_t w = 80u,
const int float_prec = std::numeric_limits<toml::floating>::max_digits10, const int float_prec = std::numeric_limits<toml::floating>::max_digits10,
const bool can_be_inlined = false, const bool can_be_inlined = false,
const bool no_comment = false,
std::vector<toml::key> ks = {}) std::vector<toml::key> ks = {})
: can_be_inlined_(can_be_inlined), float_prec_(float_prec), width_(w), : can_be_inlined_(can_be_inlined), no_comment_(no_comment),
keys_(std::move(ks)) float_prec_(float_prec), width_(w), keys_(std::move(ks))
{} {}
~serializer() = default; ~serializer() = default;
@@ -256,11 +257,14 @@ struct serializer
failed = true; failed = true;
break; break;
} }
for(const auto& c : item.comments()) if(!no_comment_)
{ {
token += '#'; for(const auto& c : item.comments())
token += c; {
token += '\n'; token += '#';
token += c;
token += '\n';
}
} }
const auto t = this->make_inline_table(item.as_table()); const auto t = this->make_inline_table(item.as_table());
@@ -285,11 +289,14 @@ struct serializer
std::string token; std::string token;
for(const auto& item : v) for(const auto& item : v)
{ {
for(const auto& c : item.comments()) if(!no_comment_)
{ {
token += '#'; for(const auto& c : item.comments())
token += c; {
token += '\n'; token += '#';
token += c;
token += '\n';
}
} }
token += "[["; token += "[[";
token += this->serialize_dotted_key(keys_); token += this->serialize_dotted_key(keys_);
@@ -326,7 +333,7 @@ struct serializer
token += "[\n"; token += "[\n";
for(const auto& item : v) for(const auto& item : v)
{ {
if(!item.comments().empty()) if(!item.comments().empty() && !no_comment_)
{ {
// if comment exists, the element must be the only element in the line. // if comment exists, the element must be the only element in the line.
// e.g. the following is not allowed. // e.g. the following is not allowed.
@@ -502,6 +509,9 @@ struct serializer
// if an element of a table or an array has a comment, it cannot be inlined. // if an element of a table or an array has a comment, it cannot be inlined.
bool has_comment_inside(const array_type& a) const noexcept bool has_comment_inside(const array_type& a) const noexcept
{ {
// if no_comment is set, comments would not be written.
if(this->no_comment_) {return false;}
for(const auto& v : a) for(const auto& v : a)
{ {
if(!v.comments().empty()) {return true;} if(!v.comments().empty()) {return true;}
@@ -510,6 +520,9 @@ struct serializer
} }
bool has_comment_inside(const table_type& t) const noexcept bool has_comment_inside(const table_type& t) const noexcept
{ {
// if no_comment is set, comments would not be written.
if(this->no_comment_) {return false;}
for(const auto& kv : t) for(const auto& kv : t)
{ {
if(!kv.second.comments().empty()) {return true;} if(!kv.second.comments().empty()) {return true;}
@@ -566,7 +579,7 @@ struct serializer
continue; continue;
} }
if(!kv.second.comments().empty()) if(!kv.second.comments().empty() && !no_comment_)
{ {
for(const auto& c : kv.second.comments()) for(const auto& c : kv.second.comments())
{ {
@@ -605,8 +618,8 @@ struct serializer
std::vector<toml::key> ks(this->keys_); std::vector<toml::key> ks(this->keys_);
ks.push_back(kv.first); ks.push_back(kv.first);
auto tmp = visit(serializer( auto tmp = visit(serializer(this->width_, this->float_prec_,
this->width_, this->float_prec_, !multiline_table_printed, ks), !multiline_table_printed, this->no_comment_, ks),
kv.second); kv.second);
if((!multiline_table_printed) && if((!multiline_table_printed) &&
@@ -620,7 +633,7 @@ struct serializer
tmp += '\n'; tmp += '\n';
} }
if(!kv.second.comments().empty()) if(!kv.second.comments().empty() && !no_comment_)
{ {
for(const auto& c : kv.second.comments()) for(const auto& c : kv.second.comments())
{ {
@@ -644,6 +657,7 @@ struct serializer
private: private:
bool can_be_inlined_; bool can_be_inlined_;
bool no_comment_;
int float_prec_; int float_prec_;
std::size_t width_; std::size_t width_;
std::vector<toml::key> keys_; std::vector<toml::key> keys_;
@@ -654,7 +668,7 @@ template<typename C,
std::string std::string
format(const basic_value<C, M, V>& v, std::size_t w = 80u, format(const basic_value<C, M, V>& v, std::size_t w = 80u,
int fprec = std::numeric_limits<toml::floating>::max_digits10, int fprec = std::numeric_limits<toml::floating>::max_digits10,
bool force_inline = false) bool no_comment = false, bool force_inline = false)
{ {
// if value is a table, it is considered to be a root object. // if value is a table, it is considered to be a root object.
// the root object can't be an inline table. // the root object can't be an inline table.
@@ -666,7 +680,7 @@ format(const basic_value<C, M, V>& v, std::size_t w = 80u,
oss << v.comments(); oss << v.comments();
oss << '\n'; // to split the file comment from the first element oss << '\n'; // to split the file comment from the first element
} }
oss << visit(serializer<C, M, V>(w, fprec, false), v); oss << visit(serializer<C, M, V>(w, fprec, no_comment, false), v);
return oss.str(); return oss.str();
} }
return visit(serializer<C, M, V>(w, fprec, force_inline), v); return visit(serializer<C, M, V>(w, fprec, force_inline), v);
@@ -712,15 +726,15 @@ operator<<(std::basic_ostream<charT, traits>& os, const basic_value<C, M, V>& v)
// by defualt, iword is initialized byl 0. And by default, toml11 outputs // by defualt, iword is initialized byl 0. And by default, toml11 outputs
// comments. So `0` means showcomment. 1 means nocommnet. // comments. So `0` means showcomment. 1 means nocommnet.
const bool show_comment = (0 == os.iword(detail::comment_index(os))); const bool no_comment = (1 == os.iword(detail::comment_index(os)));
if(show_comment && v.is_table() && !v.comments().empty()) if(!no_comment && v.is_table() && !v.comments().empty())
{ {
os << v.comments(); os << v.comments();
os << '\n'; // to split the file comment from the first element os << '\n'; // to split the file comment from the first element
} }
// the root object can't be an inline table. so pass `false`. // the root object can't be an inline table. so pass `false`.
os << visit(serializer<C, M, V>(w, fprec, false), v); os << visit(serializer<C, M, V>(w, fprec, false, no_comment), v);
// if v is a non-table value, and has only one comment, then // if v is a non-table value, and has only one comment, then
// put a comment just after a value. in the following way. // put a comment just after a value. in the following way.
@@ -739,7 +753,7 @@ operator<<(std::basic_ostream<charT, traits>& os, const basic_value<C, M, V>& v)
// //
// In this case, it is impossible to put comments before key-value pair. // In this case, it is impossible to put comments before key-value pair.
// The only way to preserve comments is to put all of them after a value. // The only way to preserve comments is to put all of them after a value.
if(show_comment && !v.is_table() && !v.comments().empty()) if(!no_comment && !v.is_table() && !v.comments().empty())
{ {
os << " #"; os << " #";
for(const auto& c : v.comments()) {os << c;} for(const auto& c : v.comments()) {os << c;}