From a00a906482e5451c6b91d35aa1bb4e89cc5a26a1 Mon Sep 17 00:00:00 2001 From: ToruNiina Date: Thu, 14 Feb 2019 16:17:32 +0900 Subject: [PATCH] fix: add comma at correct position --- toml/serializer.hpp | 17 ++++++++++++++--- 1 file changed, 14 insertions(+), 3 deletions(-) diff --git a/toml/serializer.hpp b/toml/serializer.hpp index 9328bdd..1944ead 100644 --- a/toml/serializer.hpp +++ b/toml/serializer.hpp @@ -224,7 +224,10 @@ struct serializer token += "[\n"; for(const auto& item : v) { - const auto next_elem = toml::visit(*this, item); + auto next_elem = toml::visit(*this, item); + // newline between array-value and comma is not allowed + if(next_elem.back() == '\n'){next_elem.pop_back();} + if(current_line.size() + next_elem.size() + 1 < this->width_) { current_line += next_elem; @@ -235,14 +238,22 @@ struct serializer // the next elem cannot be within the width. token += next_elem; token += ",\n"; + // keep current line empty } - else + else // current_line has some tokens and it exceeds width { + assert(current_line.back() == ','); token += current_line; - token += ",\n"; + token += '\n'; current_line = next_elem; + current_line += ','; } } + if(!current_line.empty()) + { + if(current_line.back() != '\n') {current_line += '\n';} + token += current_line; + } token += "]\n"; return token; }