From 5f603ac41f33313b5655fc2c207f583d021aac32 Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Fri, 28 Aug 2020 19:58:26 -0700 Subject: [PATCH 1/3] P1391R4 "Range Constructor for string_view" --- stl/inc/xstring | 12 +++++++++++ tests/std/tests/P0220R1_string_view/test.cpp | 21 ++++++++++++++++++++ 2 files changed, 33 insertions(+) diff --git a/stl/inc/xstring b/stl/inc/xstring index 5cd221c2f72..8bb7e66e6f4 100644 --- a/stl/inc/xstring +++ b/stl/inc/xstring @@ -1240,6 +1240,15 @@ public: #endif // _CONTAINER_DEBUG_LEVEL > 0 } +#ifdef __cpp_lib_concepts + // clang-format off + template _Se> + requires (is_same_v, _Elem> && !is_convertible_v<_Se, size_type>) + constexpr basic_string_view(_It _First, _Se _Last) noexcept // strengthened + : _Mydata(_STD to_address(_First)), _Mysize(static_cast(_Last - _First)) {} + // clang-format on +#endif // __cpp_lib_concepts + _NODISCARD constexpr const_iterator begin() const noexcept { #if _ITERATOR_DEBUG_LEVEL >= 1 return const_iterator(_Mydata, _Mysize, 0); @@ -1628,6 +1637,9 @@ private: }; #ifdef __cpp_lib_concepts +template _Se> +basic_string_view(_It, _Se) -> basic_string_view>; + namespace ranges { template inline constexpr bool enable_view> = true; diff --git a/tests/std/tests/P0220R1_string_view/test.cpp b/tests/std/tests/P0220R1_string_view/test.cpp index 1d0fd5abba2..0a43301cb7a 100644 --- a/tests/std/tests/P0220R1_string_view/test.cpp +++ b/tests/std/tests/P0220R1_string_view/test.cpp @@ -7,6 +7,7 @@ #include #include #include +#include #include @@ -303,6 +304,25 @@ constexpr bool test_case_buffer_constructor() { return true; } +constexpr bool test_case_contiguous_constructor() { +#ifdef __cpp_lib_ranges + const vector expectedData{'n', 'o', ' ', 'n', 'u', 'l', 'l'}; + // Also tests the corresponding deduction guide: + basic_string_view sv(expectedData.begin(), expectedData.end()); + static_assert(is_same_v); + assert(sv.data() == expectedData.data()); + assert(sv.size() == 7); + assert(sv.length() == 7); + assert(!sv.empty()); + assert(sv[1] == 'o'); + assert(sv.at(1) == 'o'); + assert(sv.front() == 'n'); + assert(sv.back() == 'l'); +#endif // __cpp_lib_ranges + + return true; +} + template constexpr bool test_case_iterators() { using iterator = typename basic_string_view::iterator; @@ -1072,6 +1092,7 @@ int main() { test_case_default_constructor(); test_case_ntcts_constructor(); test_case_buffer_constructor(); + test_case_contiguous_constructor(); test_case_iterators>(); test_case_iterators>(); test_case_prefix>(); From 449ec64c0d9b49969f50db6181962f05c2ba99ab Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Mon, 31 Aug 2020 08:54:57 -0700 Subject: [PATCH 2/3] Casey's review comment --- tests/std/tests/P0220R1_string_view/test.cpp | 3 +-- 1 file changed, 1 insertion(+), 2 deletions(-) diff --git a/tests/std/tests/P0220R1_string_view/test.cpp b/tests/std/tests/P0220R1_string_view/test.cpp index 0a43301cb7a..1f155e1fd46 100644 --- a/tests/std/tests/P0220R1_string_view/test.cpp +++ b/tests/std/tests/P0220R1_string_view/test.cpp @@ -308,8 +308,7 @@ constexpr bool test_case_contiguous_constructor() { #ifdef __cpp_lib_ranges const vector expectedData{'n', 'o', ' ', 'n', 'u', 'l', 'l'}; // Also tests the corresponding deduction guide: - basic_string_view sv(expectedData.begin(), expectedData.end()); - static_assert(is_same_v); + same_as auto sv = basic_string_view(expectedData.begin(), expectedData.end()); assert(sv.data() == expectedData.data()); assert(sv.size() == 7); assert(sv.length() == 7); From 7f9bc5c8a5c631bdceca1ac4f5986b7480b3bfff Mon Sep 17 00:00:00 2001 From: Casey Carter Date: Tue, 1 Sep 2020 09:16:10 -0700 Subject: [PATCH 3/3] STL's review comment; fix guards --- tests/std/tests/P0220R1_string_view/test.cpp | 9 +++++---- 1 file changed, 5 insertions(+), 4 deletions(-) diff --git a/tests/std/tests/P0220R1_string_view/test.cpp b/tests/std/tests/P0220R1_string_view/test.cpp index 1f155e1fd46..3e611eef4d1 100644 --- a/tests/std/tests/P0220R1_string_view/test.cpp +++ b/tests/std/tests/P0220R1_string_view/test.cpp @@ -1,13 +1,13 @@ // Copyright (c) Microsoft Corporation. // SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception +#include #include #include #include #include #include #include -#include #include @@ -305,8 +305,8 @@ constexpr bool test_case_buffer_constructor() { } constexpr bool test_case_contiguous_constructor() { -#ifdef __cpp_lib_ranges - const vector expectedData{'n', 'o', ' ', 'n', 'u', 'l', 'l'}; +#ifdef __cpp_lib_concepts + const array expectedData{'n', 'o', ' ', 'n', 'u', 'l', 'l'}; // Also tests the corresponding deduction guide: same_as auto sv = basic_string_view(expectedData.begin(), expectedData.end()); assert(sv.data() == expectedData.data()); @@ -317,7 +317,7 @@ constexpr bool test_case_contiguous_constructor() { assert(sv.at(1) == 'o'); assert(sv.front() == 'n'); assert(sv.back() == 'l'); -#endif // __cpp_lib_ranges +#endif // __cpp_lib_concepts return true; } @@ -1047,6 +1047,7 @@ static_assert(c_string_view{"abcd"} == "abcd"); static_assert(test_case_default_constructor()); static_assert(test_case_ntcts_constructor()); static_assert(test_case_buffer_constructor()); +static_assert(test_case_contiguous_constructor()); #if defined(__clang__) || defined(__EDG__) // TRANSITION, VSO-284079 "C1XX's C++14 constexpr emits bogus warnings C4146, // C4308, C4307 for basic_string_view::iterator" static_assert(test_case_iterators());