From 34f1fc04cf631623c23a261a9b59e26715063e2e Mon Sep 17 00:00:00 2001 From: Tomasz Sowa Date: Tue, 29 Jun 2021 23:25:31 +0200 Subject: [PATCH] added Space::remove(size_t table_index) for removing a table item fixed: pretty printing for Space format --- src/space/space.cpp | 11 ++++++++ src/space/space.h | 69 ++++++++++++++++++++++----------------------- 2 files changed, 45 insertions(+), 35 deletions(-) diff --git a/src/space/space.cpp b/src/space/space.cpp index f50402c..ca816f3 100644 --- a/src/space/space.cpp +++ b/src/space/space.cpp @@ -2001,6 +2001,17 @@ void Space::remove(const std::wstring & field) } +void Space::remove(size_t table_index) +{ + if( type == type_table && table_index < value.value_table.size() ) + { + delete value.value_table[table_index]; + value.value_table[table_index] = nullptr; + value.value_table.erase(value.value_table.begin() + table_index); + } +} + + bool Space::is_equal(const wchar_t * field, const char * val) const { const Space * space = get_space(field); diff --git a/src/space/space.h b/src/space/space.h index d5514ee..02241d4 100644 --- a/src/space/space.h +++ b/src/space/space.h @@ -515,6 +515,8 @@ public: void remove(const wchar_t * field); void remove(const std::wstring & field); + // remove a table item + void remove(size_t table_index); template void serialize_to_string(StreamType & stream) const @@ -547,7 +549,7 @@ public: { if( is_object() ) { - serialize_to_space_stream(str, pretty_print, 0, true); + serialize_to_space_stream(str, pretty_print, -1, true); } } @@ -1029,19 +1031,26 @@ protected: if( !is_main_object ) { str << '{'; - print_if(pretty_print && !value.value_object.empty(), str, '\n'); } bool is_first = true; for(auto & map_item : value.value_object) { - if( !is_first ) - print_if(pretty_print, str, '\n', ','); + if( is_first ) + { + print_if(pretty_print, str, '\n'); + print_level(pretty_print, level, str); + } + else + { + str << ','; + print_if(pretty_print, str, '\n'); + print_level(pretty_print, level, str); + } bool quote_field = should_field_be_quoted(map_item.first); - print_level(pretty_print, level, str); print_if(quote_field, str, '"'); serialize_string_buffer(map_item.first.c_str(), str, Escape::escape_space); print_if(quote_field, str, '"'); @@ -1050,17 +1059,15 @@ protected: str << '='; print_if(pretty_print, str, ' '); - map_item.second->serialize_to_space_stream(str, pretty_print, level + 1, false); + map_item.second->serialize_to_space_stream(str, pretty_print, level, false); is_first = false; } - print_if(!is_first && pretty_print, str, '\n'); - if( !is_main_object ) { - print_level(pretty_print, level - 1, str); + print_if(pretty_print && !is_first, str, '\n'); + print_level(pretty_print && !is_first, level - 1, str); str << '}'; - print_if(pretty_print, str, '\n'); } } @@ -1069,29 +1076,29 @@ protected: template void serialize_space_table(StreamType & str, bool pretty_print, int level) const { - bool multivalue_table = false; bool is_first = true; - - if( value.value_table.size() > 1 ) - { - multivalue_table = true; - } - str << '('; - print_if(pretty_print && multivalue_table, str, '\n'); for(Space * space : value.value_table) { - if( !is_first ) - print_if(pretty_print, str, '\n', ','); + if( is_first ) + { + print_if(pretty_print, str, '\n'); + print_level(pretty_print, level, str); + } + else + { + str << ','; + print_if(pretty_print, str, '\n'); + print_level(pretty_print, level, str); + } - print_level(pretty_print && multivalue_table, level, str); - space->serialize_to_space_stream(str, pretty_print, level + 1, false); + space->serialize_to_space_stream(str, pretty_print, level, false); is_first = false; } - print_if(pretty_print && multivalue_table, str, '\n'); - print_level(pretty_print && multivalue_table, level - 1, str); + print_if(pretty_print && !is_first, str, '\n'); + print_level(pretty_print && !is_first, level - 1, str); str << ')'; } @@ -1212,6 +1219,7 @@ protected: serialize_string_buffer(map_item.first.c_str(), str, Escape::escape_json); str << '"'; str << ':'; + print_if(pretty_print, str, ' '); map_item.second->serialize_to_json_stream(str, pretty_print, level); is_first = false; } @@ -1291,11 +1299,11 @@ protected: break; case type_object: - serialize_space_object(str, pretty_print, level, is_main_object); + serialize_space_object(str, pretty_print, level + 1, is_main_object); break; case type_table: - serialize_space_table(str, pretty_print, level); + serialize_space_table(str, pretty_print, level + 1); break; } } @@ -1341,15 +1349,6 @@ protected: str << c; } - template - void print_if(bool condition, StreamType & str, wchar_t c1, wchar_t c2) const - { - if( condition ) - str << c1; - else - str << c2; - } - void copy_from(const Space & space);