diff options
| -rw-r--r-- | README.md | 1 | ||||
| -rw-r--r-- | include/toml++/impl/formatter.hpp | 6 | ||||
| -rw-r--r-- | include/toml++/impl/forward_declarations.hpp | 3 | ||||
| -rw-r--r-- | include/toml++/impl/toml_formatter.inl | 9 | ||||
| -rw-r--r-- | tests/manipulating_tables.cpp | 88 | ||||
| -rw-r--r-- | toml.hpp | 16 |
6 files changed, 115 insertions, 8 deletions
@@ -312,6 +312,7 @@ UTF-8 decoding is performed using a state machine based on Bjoern Hoehrmann's '[ - **[@tyler92](https://github.com/tyler92)** - Fixed stack overflow that occurred during fuzzing tests - **[@whiterabbit963](https://github.com/whiterabbit963)** - Fixed a bug with value_or conversions - **[@ximion](https://github.com/ximion)** - Added support for installation with meson +- **[@rafal-c](https://github.com/rafal-c)** - Added a formatting flag <br> ## Contact diff --git a/include/toml++/impl/formatter.hpp b/include/toml++/impl/formatter.hpp index 0c97833f29..65005b124e 100644 --- a/include/toml++/impl/formatter.hpp +++ b/include/toml++/impl/formatter.hpp @@ -130,6 +130,12 @@ TOML_IMPL_NAMESPACE_START return !!(config_.flags & format_flags::terse_key_value_pairs); } + TOML_PURE_INLINE_GETTER + bool force_multiline_arrays() const noexcept + { + return !!(config_.flags & format_flags::force_multiline_arrays); + } + TOML_EXPORTED_MEMBER_FUNCTION void attach(std::ostream& stream) noexcept; diff --git a/include/toml++/impl/forward_declarations.hpp b/include/toml++/impl/forward_declarations.hpp index 386a9e0688..f4dfe4ffd2 100644 --- a/include/toml++/impl/forward_declarations.hpp +++ b/include/toml++/impl/forward_declarations.hpp @@ -343,6 +343,9 @@ TOML_NAMESPACE_START // abi namespace /// \brief Avoids the use of whitespace around key-value pairs. terse_key_value_pairs = (1ull << 12), + + /// \brief Always print multiline arrays (one element per line). + force_multiline_arrays = (1ull << 13), }; TOML_MAKE_FLAGS(format_flags); diff --git a/include/toml++/impl/toml_formatter.inl b/include/toml++/impl/toml_formatter.inl index 8b4f633dca..bc8303248b 100644 --- a/include/toml++/impl/toml_formatter.inl +++ b/include/toml++/impl/toml_formatter.inl @@ -179,10 +179,11 @@ TOML_NAMESPACE_START } const auto original_indent = indent(); - const auto multiline = TOML_ANON_NAMESPACE::toml_formatter_forces_multiline( - arr, - 120u, - indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent)); + const auto multiline = force_multiline_arrays() + || TOML_ANON_NAMESPACE::toml_formatter_forces_multiline( + arr, + 120u, + indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent)); print_unformatted("["sv); diff --git a/tests/manipulating_tables.cpp b/tests/manipulating_tables.cpp index afed60450e..56f7b3bf3c 100644 --- a/tests/manipulating_tables.cpp +++ b/tests/manipulating_tables.cpp @@ -614,6 +614,94 @@ key8 = [ ])"sv; CHECK(to_string(input, toml_formatter::default_flags, format_flags::indentation) == expected_without_indentation); + + // forcing multiline arrays: even short arrays become one-per-line (with array elements indented) + constexpr auto expected_forced_multiline = R"(key1 = 'val1' +key2 = [ + 1, + 2, + 3, + 4, + '5' +] +key3 = [ + 'this is a really long array', + 'and should be split over multiple lines', + 'by the formatter', + 'unless i dun goofed', + 'i guess thats what tests are for' +] + +[sub1] +key4 = 'val' + +[sub2] +key5 = 'val' + + [sub2.sub3] + key6 = 'val' + key7 = [ + 1, + 2, + 3, + 4, + '5' + ] + key8 = [ + 'this is a really long array', + 'and should be split over multiple lines', + 'by the formatter', + 'unless i dun goofed', + 'i guess thats what tests are for' + ])"sv; + + CHECK(to_string(input, toml_formatter::default_flags | format_flags::force_multiline_arrays) + == expected_forced_multiline); + + // forcing multiline arrays without indenting array elements + constexpr auto expected_forced_without_indented_arrays = R"(key1 = 'val1' +key2 = [ +1, +2, +3, +4, +'5' +] +key3 = [ +'this is a really long array', +'and should be split over multiple lines', +'by the formatter', +'unless i dun goofed', +'i guess thats what tests are for' +] + +[sub1] +key4 = 'val' + +[sub2] +key5 = 'val' + + [sub2.sub3] + key6 = 'val' + key7 = [ + 1, + 2, + 3, + 4, + '5' + ] + key8 = [ + 'this is a really long array', + 'and should be split over multiple lines', + 'by the formatter', + 'unless i dun goofed', + 'i guess thats what tests are for' + ])"sv; + + CHECK(to_string(input, + toml_formatter::default_flags | format_flags::force_multiline_arrays, + format_flags::indent_array_elements) + == expected_forced_without_indented_arrays); } } @@ -1674,6 +1674,7 @@ TOML_NAMESPACE_START // abi namespace indentation = indent_sub_tables | indent_array_elements, relaxed_float_precision = (1ull << 11), terse_key_value_pairs = (1ull << 12), + force_multiline_arrays = (1ull << 13), }; TOML_MAKE_FLAGS(format_flags); @@ -9890,6 +9891,12 @@ TOML_IMPL_NAMESPACE_START return !!(config_.flags & format_flags::terse_key_value_pairs); } + TOML_PURE_INLINE_GETTER + bool force_multiline_arrays() const noexcept + { + return !!(config_.flags & format_flags::force_multiline_arrays); + } + TOML_EXPORTED_MEMBER_FUNCTION void attach(std::ostream& stream) noexcept; @@ -17195,10 +17202,11 @@ TOML_NAMESPACE_START } const auto original_indent = indent(); - const auto multiline = TOML_ANON_NAMESPACE::toml_formatter_forces_multiline( - arr, - 120u, - indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent)); + const auto multiline = force_multiline_arrays() + || TOML_ANON_NAMESPACE::toml_formatter_forces_multiline( + arr, + 120u, + indent_columns() * static_cast<size_t>(original_indent < 0 ? 0 : original_indent)); print_unformatted("["sv); |
