Skip to content

Commit 81b7089

Browse files
committed
Containers: make ArrayView -> StringView construction constexpr.
So it's possible to semi-easily create constexpr StringView instances without having to supply explicit sizes. I.e., like this: constexpr StringView a = arrayView("hello").exceptSuffix(1);
1 parent b57097e commit 81b7089

File tree

4 files changed

+23
-7
lines changed

4 files changed

+23
-7
lines changed

doc/corrade-changelog.dox

+2-1
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,8 @@ namespace Corrade {
8686
- New @ref Containers::String class as a lightweight but more flexible
8787
alternative to @ref std::string
8888
- New @ref Containers::BasicStringView "Containers::StringView" class as a
89-
lightweight but more flexible alternative to C++17 @ref std::string_view
89+
lightweight but more flexible alternative to C++17 @ref std::string_view.
90+
See also [mosra/corrade#123](https://github.com/mosra/corrade/issues/123).
9091
- New @ref Containers::MoveReference and @ref Containers::AnyReference
9192
counterparts to @ref Containers::Reference for exclusively r-value
9293
references and both l-value and r-value references

src/Corrade/Containers/StringView.cpp

-2
Original file line numberDiff line numberDiff line change
@@ -63,8 +63,6 @@ template<class T> BasicStringView<T>::BasicStringView(String& string) noexcept:
6363
needs an explicit export otherwise the symbol doesn't get exported. */
6464
template<> template<> CORRADE_UTILITY_EXPORT BasicStringView<const char>::BasicStringView(const String& string) noexcept: BasicStringView{string.data(), string.size(), StringViewFlag::NullTerminated} {}
6565

66-
template<class T> BasicStringView<T>::BasicStringView(const StringViewFlags flags, const ArrayView<T> other) noexcept: BasicStringView{other.data(), other.size(), flags} {}
67-
6866
template<class T> Array<BasicStringView<T>> BasicStringView<T>::split(const char delimiter) const {
6967
Array<BasicStringView<T>> parts;
7068
T* const end = this->end();

src/Corrade/Containers/StringView.h

+3-3
Original file line numberDiff line numberDiff line change
@@ -1164,9 +1164,9 @@ BasicStringView {
11641164

11651165
/* Called from BasicStringView(U&&, StringViewFlags), see its comment
11661166
for details; arguments in a flipped order to avoid accidental
1167-
ambiguity. Not constexpr/inline to avoid header dependency on
1168-
ArrayView.h. */
1169-
explicit BasicStringView(StringViewFlags flags, ArrayView<T> data) noexcept;
1167+
ambiguity. The ArrayView type is a template to avoid having to
1168+
include ArrayView.h. */
1169+
template<class U, class = typename std::enable_if<std::is_same<T, U>::value>::type> constexpr explicit BasicStringView(StringViewFlags flags, ArrayView<U> data) noexcept: BasicStringView{data.data(), data.size(), flags} {}
11701170

11711171
/* Used by the char* constructor, delinlined because it calls into
11721172
std::strlen() */

src/Corrade/Containers/Test/StringViewTest.cpp

+18-1
Original file line numberDiff line numberDiff line change
@@ -24,12 +24,15 @@
2424
DEALINGS IN THE SOFTWARE.
2525
*/
2626

27+
/* Deliberately including first to make sure it works without ArrayView being
28+
included first */
29+
#include "Corrade/Containers/StringView.h"
30+
2731
#include <sstream>
2832

2933
#include "Corrade/Cpu.h"
3034
#include "Corrade/Containers/Array.h"
3135
#include "Corrade/Containers/StaticArray.h"
32-
#include "Corrade/Containers/StringView.h"
3336
#include "Corrade/TestSuite/Tester.h"
3437
#include "Corrade/TestSuite/Compare/Container.h"
3538
#include "Corrade/TestSuite/Compare/Numeric.h"
@@ -122,6 +125,7 @@ struct StringViewTest: TestSuite::Tester {
122125
void constructNullptrNullTerminated();
123126

124127
template<class T> void convertArrayView();
128+
void convertArrayViewConstexpr();
125129
template<class T> void convertVoidArrayView();
126130
void convertConstFromArrayView();
127131
void convertToConstArrayView();
@@ -263,6 +267,7 @@ StringViewTest::StringViewTest() {
263267

264268
&StringViewTest::convertArrayView<const char>,
265269
&StringViewTest::convertArrayView<char>,
270+
&StringViewTest::convertArrayViewConstexpr,
266271
&StringViewTest::convertConstFromArrayView,
267272
&StringViewTest::convertToConstArrayView,
268273
&StringViewTest::convertConstFromArray,
@@ -664,6 +669,18 @@ template<class T> void StringViewTest::convertArrayView() {
664669
CORRADE_COMPARE(static_cast<const void*>(array2.data()), &data[0]);
665670
}
666671

672+
constexpr const char String[] = "hell\0!!"; /* 7 chars + \0 at the end */
673+
674+
void StringViewTest::convertArrayViewConstexpr() {
675+
constexpr StringView view = {arrayView(String).exceptSuffix(1), StringViewFlag::Global|StringViewFlag::NullTerminated};
676+
constexpr std::size_t size = view.size();
677+
constexpr StringViewFlags flags = view.flags();
678+
constexpr const void* data = view.data();
679+
CORRADE_COMPARE(size, 7);
680+
CORRADE_COMPARE(flags, StringViewFlag::Global|StringViewFlag::NullTerminated);
681+
CORRADE_COMPARE(data, String);
682+
}
683+
667684
void StringViewTest::convertConstFromArrayView() {
668685
char data[] = "hello!";
669686
ArrayView<char> array = data;

0 commit comments

Comments
 (0)