| Line | Branch | Exec | Source |
|---|---|---|---|
| 1 | // SPDX-FileCopyrightText: 2026 Dennis Gläser <dennis.a.glaeser@gmail.com> | ||
| 2 | // SPDX-License-Identifier: MIT | ||
| 3 | /*! | ||
| 4 | * \file | ||
| 5 | * \ingroup Common | ||
| 6 | * \copydoc GridFormat::MultiString | ||
| 7 | */ | ||
| 8 | #ifndef GRIDFORMAT_COMMON_MULTI_STRING_HPP_ | ||
| 9 | #define GRIDFORMAT_COMMON_MULTI_STRING_HPP_ | ||
| 10 | |||
| 11 | #include <string> | ||
| 12 | #include <string_view> | ||
| 13 | #include <ranges> | ||
| 14 | #include <vector> | ||
| 15 | #include <cstddef> | ||
| 16 | |||
| 17 | namespace GridFormat { | ||
| 18 | |||
| 19 | /*! | ||
| 20 | * \ingroup Common | ||
| 21 | * \brief A sequence of strings, stored contiguously with each string terminated by '\0'. | ||
| 22 | * \details This is the form in which fields hold multiple strings, e.g. string meta data. It is a | ||
| 23 | * range of characters, so it can be passed as meta data and fields can be exported into it. | ||
| 24 | * A trailing string without terminator counts as a string, and an empty sequence of characters | ||
| 25 | * represents a single empty string, just like an empty std::string. | ||
| 26 | */ | ||
| 27 | class MultiString { | ||
| 28 | public: | ||
| 29 | using value_type = char; | ||
| 30 | |||
| 31 | 23430 | MultiString() = default; | |
| 32 | |||
| 33 | 7607 | explicit MultiString(const std::vector<std::string>& strings) { | |
| 34 |
2/2✓ Branch 5 taken 22043 times.
✓ Branch 6 taken 7607 times.
|
29650 | for (const auto& s : strings) { |
| 35 |
1/2✓ Branch 1 taken 22043 times.
✗ Branch 2 not taken.
|
22043 | _chars.append(s); |
| 36 |
1/2✓ Branch 1 taken 22043 times.
✗ Branch 2 not taken.
|
22043 | _chars.push_back('\0'); |
| 37 | } | ||
| 38 | 7607 | } | |
| 39 | |||
| 40 | //! Return views on the individual strings (not available on temporaries, the views would dangle) | ||
| 41 | std::vector<std::string_view> slices() const && = delete; | ||
| 42 | 22105 | std::vector<std::string_view> slices() const & { | |
| 43 |
4/4✓ Branch 1 taken 22103 times.
✓ Branch 2 taken 2 times.
✓ Branch 4 taken 22101 times.
✓ Branch 5 taken 2 times.
|
22105 | const bool has_trailing_nul = !_chars.empty() && _chars.back() == '\0'; |
| 44 |
2/2✓ Branch 0 taken 22101 times.
✓ Branch 1 taken 4 times.
|
22105 | auto data = has_trailing_nul ? std::string_view{_chars.data(), _chars.size() - 1} : std::string_view{_chars}; |
| 45 |
2/4✓ Branch 1 taken 22105 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 22105 times.
✗ Branch 5 not taken.
|
22105 | auto slices = std::views::split(data, '\0') | std::views::transform([] (auto&& s) { |
| 46 |
2/4✓ Branch 1 taken 36833 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 36833 times.
✗ Branch 5 not taken.
|
36833 | return std::string_view(std::ranges::begin(s), std::ranges::end(s)); |
| 47 |
1/2✓ Branch 1 taken 22105 times.
✗ Branch 2 not taken.
|
22105 | }); |
| 48 |
3/4✓ Branch 1 taken 22105 times.
✗ Branch 2 not taken.
✓ Branch 3 taken 3 times.
✓ Branch 4 taken 22102 times.
|
22105 | if (std::ranges::empty(slices)) |
| 49 |
1/2✓ Branch 2 taken 3 times.
✗ Branch 3 not taken.
|
9 | return {{""}}; |
| 50 |
3/6✓ Branch 1 taken 22102 times.
✗ Branch 2 not taken.
✓ Branch 4 taken 22102 times.
✗ Branch 5 not taken.
✓ Branch 7 taken 22102 times.
✗ Branch 8 not taken.
|
66306 | return std::vector<std::string_view>{std::ranges::begin(slices), std::ranges::end(slices)}; |
| 51 | } | ||
| 52 | |||
| 53 | 69297 | std::size_t size() const { return _chars.size(); } | |
| 54 | const char* data() const { return _chars.data(); } | ||
| 55 | 23429 | void resize(std::size_t n, char c = '\0') { _chars.resize(n, c); } | |
| 56 | |||
| 57 | 23429 | auto begin() { return _chars.begin(); } | |
| 58 | 15286 | auto begin() const { return _chars.begin(); } | |
| 59 | auto end() { return _chars.end(); } | ||
| 60 | 15286 | auto end() const { return _chars.end(); } | |
| 61 | |||
| 62 |
1/2✗ Branch 0 not taken.
✓ Branch 1 taken 492 times.
|
492 | friend bool operator==(const MultiString&, const MultiString&) = default; |
| 63 | |||
| 64 | private: | ||
| 65 | 492 | std::string _chars; | |
| 66 | }; | ||
| 67 | |||
| 68 | } // namespace GridFormat | ||
| 69 | |||
| 70 | #endif // GRIDFORMAT_COMMON_MULTI_STRING_HPP_ | ||
| 71 |