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
12 changes: 12 additions & 0 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -1240,6 +1240,15 @@ public:
#endif // _CONTAINER_DEBUG_LEVEL > 0
}

#ifdef __cpp_lib_concepts
// clang-format off
template <contiguous_iterator _It, sized_sentinel_for<_It> _Se>
requires (is_same_v<iter_value_t<_It>, _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<size_type>(_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);
Expand Down Expand Up @@ -1628,6 +1637,9 @@ private:
};

#ifdef __cpp_lib_concepts
template <contiguous_iterator _It, sized_sentinel_for<_It> _Se>
basic_string_view(_It, _Se) -> basic_string_view<iter_value_t<_It>>;

namespace ranges {
template <class _Elem, class _Traits>
inline constexpr bool enable_view<basic_string_view<_Elem, _Traits>> = true;
Expand Down
21 changes: 21 additions & 0 deletions tests/std/tests/P0220R1_string_view/test.cpp
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
// Copyright (c) Microsoft Corporation.
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception

#include <array>
#include <assert.h>
#include <sstream>
#include <stdexcept>
Expand Down Expand Up @@ -303,6 +304,24 @@ constexpr bool test_case_buffer_constructor() {
return true;
}

constexpr bool test_case_contiguous_constructor() {
#ifdef __cpp_lib_concepts
const array expectedData{'n', 'o', ' ', 'n', 'u', 'l', 'l'};
// Also tests the corresponding deduction guide:
same_as<string_view> auto sv = basic_string_view(expectedData.begin(), expectedData.end());
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_concepts

return true;
}

template <class CharT, class Traits>
constexpr bool test_case_iterators() {
using iterator = typename basic_string_view<CharT, Traits>::iterator;
Expand Down Expand Up @@ -1028,6 +1047,7 @@ static_assert(c_string_view{"abcd"} == "abcd");
static_assert(test_case_default_constructor());
static_assert(test_case_ntcts_constructor<constexpr_char_traits>());
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<char, constexpr_char_traits>());
Expand Down Expand Up @@ -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<char, char_traits<char>>();
test_case_iterators<wchar_t, char_traits<wchar_t>>();
test_case_prefix<char, char_traits<char>>();
Expand Down