Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
1 change: 1 addition & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
6 changes: 6 additions & 0 deletions include/toml++/impl/formatter.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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;

Expand Down
3 changes: 3 additions & 0 deletions include/toml++/impl/forward_declarations.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
9 changes: 5 additions & 4 deletions include/toml++/impl/toml_formatter.inl
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down
88 changes: 88 additions & 0 deletions tests/manipulating_tables.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
}

Expand Down
16 changes: 12 additions & 4 deletions toml.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -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);

Expand Down Expand Up @@ -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;

Expand Down Expand Up @@ -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);

Expand Down