mirror of
https://github.com/ToruNiina/toml11.git
synced 2025-09-17 09:08:08 +08:00
feat: improve array serialization
- make multiline array more clean - short-circuit for empty array
This commit is contained in:
@@ -203,6 +203,10 @@ struct serializer
|
|||||||
}
|
}
|
||||||
return token;
|
return token;
|
||||||
}
|
}
|
||||||
|
if(v.empty())
|
||||||
|
{
|
||||||
|
return std::string("[]");
|
||||||
|
}
|
||||||
|
|
||||||
// not an array of tables. normal array. first, try to make it inline.
|
// not an array of tables. normal array. first, try to make it inline.
|
||||||
{
|
{
|
||||||
@@ -216,11 +220,28 @@ struct serializer
|
|||||||
|
|
||||||
// if the length exceeds this->width_, print multiline array
|
// if the length exceeds this->width_, print multiline array
|
||||||
std::string token;
|
std::string token;
|
||||||
|
std::string current_line;
|
||||||
token += "[\n";
|
token += "[\n";
|
||||||
for(const auto& item : v)
|
for(const auto& item : v)
|
||||||
{
|
{
|
||||||
token += toml::visit(*this, item);
|
const auto next_elem = toml::visit(*this, item);
|
||||||
token += ",\n";
|
if(current_line.size() + next_elem.size() + 1 < this->width_)
|
||||||
|
{
|
||||||
|
current_line += next_elem;
|
||||||
|
current_line += ',';
|
||||||
|
}
|
||||||
|
else if(current_line.empty())
|
||||||
|
{
|
||||||
|
// the next elem cannot be within the width.
|
||||||
|
token += next_elem;
|
||||||
|
token += ",\n";
|
||||||
|
}
|
||||||
|
else
|
||||||
|
{
|
||||||
|
token += current_line;
|
||||||
|
token += ",\n";
|
||||||
|
current_line = next_elem;
|
||||||
|
}
|
||||||
}
|
}
|
||||||
token += "]\n";
|
token += "]\n";
|
||||||
return token;
|
return token;
|
||||||
|
Reference in New Issue
Block a user