Corrade::Containers::StringIterable class new in Git master

Wrapper for any sequential container of strings or string views.

Useful where it's desirable to have a single interface accepting an ArrayView of String, StringView or const char* and anything convertible to these.

This class adds extra indirection to allow iterating over various input containers with a single code path. Assuming the API itself isn't bottlenecked on iteration performance, it should be an acceptable tradeoff compared to having to implement multiple code paths or have extra overloads that unify the process by copying the data to a temporary container first.

Usage

A StringIterable can be implicitly created from an ArrayView<StringView> or StridedArrayView1D<StringView>, (strided) array view of (const) String, const char*; std::initializer_list; and any other type convertible to these. Such as plain C arrays, Array or STL types convertible to an ArrayView.

Example usage — passing main() arguments to StringView::join() to print them:

int main(int argc, char** argv);
int main(int argc, char** argv) {
    Utility::Debug{} << " "_s.join(Containers::arrayView(argv, argc)); 
}

On the API implementation side, the usual container interface is exposed — in particular isEmpty(), size(), operator[](), front(), back() as well as range-for access:

void foo(const Containers::StringIterable& strings) {
    for(Containers::StringView string: strings) {
        
    }
}

Constructors, destructors, conversion operators

StringIterable(std::nullptr_t = nullptr) constexpr noexcept
Default constructor.
template<class U, class = decltype(StringIterable{std::declval<U && >(), Implementation::IterableOverloadPriority<1>{}})>
StringIterable(U&& data)
Construct from any sequential iterable container.
StringIterable(std::initializer_list<StringView> view) noexcept
Construct from an initializer list.

Public functions

auto data() const -> const void*
Data pointer.
auto size() const -> std::size_t
Number of items in the container.
auto stride() const -> std::ptrdiff_t
Stride between items in the container.
auto isEmpty() const -> bool
Whether the container is empty.
auto operator[](std::size_t i) const -> StringView
Element access.
auto begin() const -> StringIterableIterator
Iterator to first element.
auto cbegin() const -> StringIterableIterator
auto end() const -> StringIterableIterator
Iterator to (one item after) last element.
auto cend() const -> StringIterableIterator
auto front() const -> StringView
First element.
auto back() const -> StringView
Last element.

Function documentation

Corrade::Containers::StringIterable::StringIterable(std::nullptr_t = nullptr) constexpr noexcept

Default constructor.

Creates an instance with nullptr data and size and stride set to 0.

template<class U, class = decltype(StringIterable{std::declval<U && >(), Implementation::IterableOverloadPriority<1>{}})>
Corrade::Containers::StringIterable::StringIterable(U&& data)

Construct from any sequential iterable container.

U can be an ArrayView<T>, StridedArrayView1D<T> and any type convertible to these — see ArrayView and StridedArrayView docs for more information.

The T can then be (const) String, StringView, MutableStringView or const char*.

const void* Corrade::Containers::StringIterable::data() const

Data pointer.

Not meant to be used directly, as the returned value may point to an arbitrary string (view) type, with no possibility to distinguish it.

std::size_t Corrade::Containers::StringIterable::size() const

Number of items in the container.

std::ptrdiff_t Corrade::Containers::StringIterable::stride() const

Stride between items in the container.

Depends on size of the actual string (view) type as well as whether the original container is contiguous.

bool Corrade::Containers::StringIterable::isEmpty() const

Whether the container is empty.

StringView Corrade::Containers::StringIterable::operator[](std::size_t i) const

Element access.

Expects that i is less than size(). The returned view has StringViewFlag::Global or StringViewFlag::NullTerminated set depending on what the original string type was — for example, if was a String or a const char*, all items will have StringViewFlag::NullTerminated set.

StringIterableIterator Corrade::Containers::StringIterable::begin() const

Iterator to first element.

StringIterableIterator Corrade::Containers::StringIterable::cbegin() const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

StringIterableIterator Corrade::Containers::StringIterable::end() const

Iterator to (one item after) last element.

StringIterableIterator Corrade::Containers::StringIterable::cend() const

This is an overloaded member function, provided for convenience. It differs from the above function only in what argument(s) it accepts.

StringView Corrade::Containers::StringIterable::front() const

First element.

Expects there is at least one element. See operator[]() for information about returned StringViewFlags.

StringView Corrade::Containers::StringIterable::back() const

Last element.

Expects there is at least one element. See operator[]() for information about returned StringViewFlags.