From 161be80fd6418bde2e62cdaace229817824a7000 Mon Sep 17 00:00:00 2001 From: Riyyi Date: Mon, 18 Jul 2022 12:50:09 +0200 Subject: [PATCH] Util: Make second to last element detection more compatible std::prev doesn't work on std::unordered_map, this patch makes it work. --- src/util/json/serializer.cpp | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) diff --git a/src/util/json/serializer.cpp b/src/util/json/serializer.cpp index 4b0956b..2bf681a 100644 --- a/src/util/json/serializer.cpp +++ b/src/util/json/serializer.cpp @@ -5,7 +5,7 @@ */ #include // uint32_t -#include // prev +#include // next #include // ostringstream #include @@ -74,12 +74,13 @@ void Serializer::dumpArray(const Value& value, const uint32_t indentLevel) } auto values = value.m_value.array->elements(); - for (auto it = values.begin(); it != values.end(); ++it) { + auto last = values.end(); + for (auto it = values.begin(); it != last; ++it) { m_output += std::string(m_indent * (indentLevel + 1), m_indentCharacter); dumpHelper(*it, indentLevel + 1); // Add comma, except after the last element - if (it != std::prev(values.end(), 1)) { + if (std::next(it) != last) { m_output += ","; } if (m_indent > 0) { @@ -103,7 +104,8 @@ void Serializer::dumpObject(const Value& value, const uint32_t indentLevel) } auto members = value.m_value.object->members(); - for (auto it = members.begin(); it != members.end(); ++it) { + auto last = members.end(); + for (auto it = members.begin(); it != last; ++it) { m_output += std::string(m_indent * (indentLevel + 1), m_indentCharacter); m_output += "\"" + it->first + "\":"; if (m_indent > 0) { @@ -112,7 +114,7 @@ void Serializer::dumpObject(const Value& value, const uint32_t indentLevel) dumpHelper(it->second, indentLevel + 1); // Add comma, except after the last element - if (it != std::prev(members.end(), 1)) { + if (std::next(it) != last) { m_output += ","; } if (m_indent > 0) {