Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

C++20 constexpr _Copy_s and simplify char8_t compare #861

Merged
merged 1 commit into from
May 30, 2020
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
17 changes: 6 additions & 11 deletions stl/inc/xstring
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,8 @@ struct _Char_traits { // properties of a string or stream element
return _First1;
}

_Pre_satisfies_(_Dest_size >= _Count) static _Elem* _Copy_s(_Out_writes_all_(_Dest_size) _Elem* const _First1,
_Pre_satisfies_(_Dest_size >= _Count) static _CONSTEXPR20 _Elem* _Copy_s(_Out_writes_all_(_Dest_size)
_Elem* const _First1,
const size_t _Dest_size, _In_reads_(_Count) const _Elem* const _First2,
const size_t _Count) noexcept { // copy [_First2, _First2 + _Count) to [_First1, _First1 + _Dest_size)
_STL_VERIFY(_Count <= _Dest_size, "invalid argument");
Expand Down Expand Up @@ -363,14 +364,7 @@ public:
_In_reads_(_Count) const _Elem* const _First2, const size_t _Count) noexcept /* strengthened */ {
// compare [_First1, _First1 + _Count) with [_First2, ...)
#if _HAS_CXX17
#if _HAS_U8_INTRINSICS
if constexpr (is_same_v<_Elem, char8_t>) {
return __builtin_u8memcmp(_First1, _First2, _Count);
} else
#endif // _HAS_U8_INTRINSICS
{
return __builtin_memcmp(_First1, _First2, _Count);
}
return __builtin_memcmp(_First1, _First2, _Count);
#else // _HAS_CXX17
StephanTLavavej marked this conversation as resolved.
Show resolved Hide resolved
return _CSTD memcmp(_First1, _First2, _Count);
#endif // _HAS_CXX17
Expand Down Expand Up @@ -1372,8 +1366,9 @@ public:
return _Count;
}

_Pre_satisfies_(_Dest_size >= _Count) constexpr size_type _Copy_s(_Out_writes_all_(_Dest_size) _Elem* const _Dest,
const size_type _Dest_size, size_type _Count, const size_type _Off = 0) const {
_Pre_satisfies_(_Dest_size >= _Count) _CONSTEXPR20 size_type
_Copy_s(_Out_writes_all_(_Dest_size) _Elem* const _Dest, const size_type _Dest_size, size_type _Count,
const size_type _Off = 0) const {
// copy [_Off, _Off + _Count) to [_Dest, _Dest + _Count)
_Check_offset(_Off);
_Count = _Clamp_suffix_size(_Off, _Count);
Expand Down
2 changes: 1 addition & 1 deletion tests/std/tests/P0220R1_string_view/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -1034,11 +1034,11 @@ static_assert(test_case_iterators<char, constexpr_char_traits>());
static_assert(test_case_prefix<char, constexpr_char_traits>());
static_assert(test_case_suffix<char, constexpr_char_traits>());
static_assert(test_case_swap<char, constexpr_char_traits>());
static_assert(test_case_Copy_s<constexpr_char_traits>());
static_assert(test_case_substr<constexpr_char_traits>());
static_assert(test_case_compare<char, constexpr_char_traits>());
#if _HAS_CXX20
static_assert(test_case_copy<constexpr_char_traits>());
static_assert(test_case_Copy_s<constexpr_char_traits>());
static_assert(test_case_starts_with_ends_with<char, constexpr_char_traits, false>());
#endif // _HAS_CXX20
static_assert(test_case_operators<char, constexpr_char_traits>());
Expand Down
80 changes: 79 additions & 1 deletion tests/std/tests/P1032R1_miscellaneous_constexpr/test.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -272,7 +272,7 @@ constexpr bool run_tests() {
assert(last - first == static_cast<ptrdiff_t>(needle.size()));
}

// test char_traits move/copy/assign and basic_string_view::copy
// test char_traits assign/copy/_Copy_s/move and basic_string_view::copy/_Copy_s
{
using Elem = char;
using Traits = char_traits<Elem>;
Expand Down Expand Up @@ -314,6 +314,15 @@ constexpr bool run_tests() {

assert(Traits::move(buf + 5, buf + 3, 11) == buf + 5); // overlapping, dest after src
assert(buf == "....a.ababcbabc.c.."sv);

assert(Traits::_Copy_s(buf, 20, src, 18) == buf);
assert(buf == "cute fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 4, 10) == 4);
assert(buf == "abc. fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 11) == 11);
assert(buf == "..........a KITTEN."sv);
}

{
Expand Down Expand Up @@ -357,6 +366,15 @@ constexpr bool run_tests() {

assert(Traits::move(buf + 5, buf + 3, 11) == buf + 5); // overlapping, dest after src
assert(buf == u"....a.ababcbabc.c.."sv);

assert(Traits::_Copy_s(buf, 20, src, 18) == buf);
assert(buf == u"cute fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 4, 10) == 4);
assert(buf == u"abc. fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 11) == 11);
assert(buf == u"..........a KITTEN."sv);
}

{
Expand Down Expand Up @@ -400,6 +418,15 @@ constexpr bool run_tests() {

assert(Traits::move(buf + 5, buf + 3, 11) == buf + 5); // overlapping, dest after src
assert(buf == U"....a.ababcbabc.c.."sv);

assert(Traits::_Copy_s(buf, 20, src, 18) == buf);
assert(buf == U"cute fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 4, 10) == 4);
assert(buf == U"abc. fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 11) == 11);
assert(buf == U"..........a KITTEN."sv);
}

{
Expand Down Expand Up @@ -443,6 +470,15 @@ constexpr bool run_tests() {

assert(Traits::move(buf + 5, buf + 3, 11) == buf + 5); // overlapping, dest after src
assert(buf == L"....a.ababcbabc.c.."sv);

assert(Traits::_Copy_s(buf, 20, src, 18) == buf);
assert(buf == L"cute fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 4, 10) == 4);
assert(buf == L"abc. fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 11) == 11);
assert(buf == L"..........a KITTEN."sv);
}

#ifdef __cpp_lib_char8_t
Expand Down Expand Up @@ -487,9 +523,51 @@ constexpr bool run_tests() {

assert(Traits::move(buf + 5, buf + 3, 11) == buf + 5); // overlapping, dest after src
assert(buf == u8"....a.ababcbabc.c.."sv);

assert(Traits::_Copy_s(buf, 20, src, 18) == buf);
assert(buf == u8"cute fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 4, 10) == 4);
assert(buf == u8"abc. fluffy KITTEN."sv);

assert(sv._Copy_s(buf, 20, 11) == 11);
assert(buf == u8"..........a KITTEN."sv);
}
#endif // __cpp_lib_char8_t

// test char_traits::compare refactoring through basic_string_view (not part of P1032R1)
{
assert("cat"sv == "cat"sv);
assert("cat"sv < "category"sv);
assert("catastrophe"sv > "cat"sv);
assert("catch"sv < "category"sv);
assert("catenary"sv > "catapult"sv);

assert(u"cat"sv == u"cat"sv);
assert(u"cat"sv < u"category"sv);
assert(u"catastrophe"sv > u"cat"sv);
assert(u"catch"sv < u"category"sv);
assert(u"catenary"sv > u"catapult"sv);

assert(U"cat"sv == U"cat"sv);
assert(U"cat"sv < U"category"sv);
assert(U"catastrophe"sv > U"cat"sv);
assert(U"catch"sv < U"category"sv);
assert(U"catenary"sv > U"catapult"sv);

assert(L"cat"sv == L"cat"sv);
assert(L"cat"sv < L"category"sv);
assert(L"catastrophe"sv > L"cat"sv);
assert(L"catch"sv < L"category"sv);
assert(L"catenary"sv > L"catapult"sv);

assert(u8"cat"sv == u8"cat"sv);
assert(u8"cat"sv < u8"category"sv);
assert(u8"catastrophe"sv > u8"cat"sv);
assert(u8"catch"sv < u8"category"sv);
assert(u8"catenary"sv > u8"catapult"sv);
}

return true;
}

Expand Down