🔨 only calculate array index string when needed #1098

This commit is contained in:
Niels Lohmann 2018-05-27 12:04:22 +02:00
parent 90eb0a91e0
commit 481ace65c4
No known key found for this signature in database
GPG key ID: 7F3CEA63AE251B69
2 changed files with 22 additions and 16 deletions

View file

@ -21,8 +21,10 @@ template<typename IteratorType> class iteration_proxy
IteratorType anchor; IteratorType anchor;
/// an index for arrays (used to create key names) /// an index for arrays (used to create key names)
std::size_t array_index = 0; std::size_t array_index = 0;
/// last stringified array index
mutable std::size_t array_index_last = 0;
/// a string representation of the array index /// a string representation of the array index
std::string array_index_str = "0"; mutable std::string array_index_str = "0";
/// an empty string (to return a reference for primitive values) /// an empty string (to return a reference for primitive values)
const std::string empty_str = ""; const std::string empty_str = "";
@ -39,13 +41,7 @@ template<typename IteratorType> class iteration_proxy
iteration_proxy_internal& operator++() iteration_proxy_internal& operator++()
{ {
++anchor; ++anchor;
++array_index;
assert(anchor.m_object != nullptr);
if (anchor.m_object->is_array())
{
// update array index and string representation
array_index_str = std::to_string(++array_index);
}
return *this; return *this;
} }
@ -65,7 +61,14 @@ template<typename IteratorType> class iteration_proxy
{ {
// use integer array index as key // use integer array index as key
case value_t::array: case value_t::array:
{
if (array_index != array_index_last)
{
array_index_str = std::to_string(array_index);
array_index_last = array_index;
}
return array_index_str; return array_index_str;
}
// use key from the object // use key from the object
case value_t::object: case value_t::object:

View file

@ -4691,8 +4691,10 @@ template<typename IteratorType> class iteration_proxy
IteratorType anchor; IteratorType anchor;
/// an index for arrays (used to create key names) /// an index for arrays (used to create key names)
std::size_t array_index = 0; std::size_t array_index = 0;
/// last stringified array index
mutable std::size_t array_index_last = 0;
/// a string representation of the array index /// a string representation of the array index
std::string array_index_str = "0"; mutable std::string array_index_str = "0";
/// an empty string (to return a reference for primitive values) /// an empty string (to return a reference for primitive values)
const std::string empty_str = ""; const std::string empty_str = "";
@ -4709,13 +4711,7 @@ template<typename IteratorType> class iteration_proxy
iteration_proxy_internal& operator++() iteration_proxy_internal& operator++()
{ {
++anchor; ++anchor;
++array_index;
assert(anchor.m_object != nullptr);
if (anchor.m_object->is_array())
{
// update array index and string representation
array_index_str = std::to_string(++array_index);
}
return *this; return *this;
} }
@ -4735,7 +4731,14 @@ template<typename IteratorType> class iteration_proxy
{ {
// use integer array index as key // use integer array index as key
case value_t::array: case value_t::array:
{
if (array_index != array_index_last)
{
array_index_str = std::to_string(array_index);
array_index_last = array_index;
}
return array_index_str; return array_index_str;
}
// use key from the object // use key from the object
case value_t::object: case value_t::object: