Skip to content

Commit 5bf2f2d

Browse files
committed
added spaceship string
1 parent e68b14a commit 5bf2f2d

File tree

2 files changed

+64
-15
lines changed

2 files changed

+64
-15
lines changed

stl/inc/xstring

Lines changed: 34 additions & 15 deletions
Original file line numberDiff line numberDiff line change
@@ -34,11 +34,12 @@ _STD_BEGIN
3434
// STRUCT TEMPLATE _Char_traits (FROM <string>)
3535
template <class _Elem, class _Int_type>
3636
struct _Char_traits { // properties of a string or stream element
37-
using char_type = _Elem;
38-
using int_type = _Int_type;
39-
using pos_type = streampos;
40-
using off_type = streamoff;
41-
using state_type = _Mbstatet;
37+
using char_type = _Elem;
38+
using int_type = _Int_type;
39+
using pos_type = streampos;
40+
using off_type = streamoff;
41+
using state_type = _Mbstatet;
42+
using comparison_category = strong_ordering;
4243

4344
// For copy/move, we can uniformly call memcpy/memmove (or their builtin versions) for all element types.
4445

@@ -212,11 +213,12 @@ private:
212213
using _Primary_char_traits = _Char_traits<_Elem, unsigned short>;
213214

214215
public:
215-
using char_type = _Elem;
216-
using int_type = unsigned short;
217-
using pos_type = streampos;
218-
using off_type = streamoff;
219-
using state_type = mbstate_t;
216+
using char_type = _Elem;
217+
using int_type = unsigned short;
218+
using pos_type = streampos;
219+
using off_type = streamoff;
220+
using state_type = mbstate_t;
221+
using comparison_category = strong_ordering;
220222

221223
using _Primary_char_traits::_Copy_s;
222224
using _Primary_char_traits::copy;
@@ -350,11 +352,12 @@ private:
350352
using _Primary_char_traits = _Char_traits<_Elem, _Int_type>;
351353

352354
public:
353-
using char_type = _Elem;
354-
using int_type = _Int_type;
355-
using pos_type = streampos;
356-
using off_type = streamoff;
357-
using state_type = mbstate_t;
355+
using char_type = _Elem;
356+
using int_type = _Int_type;
357+
using pos_type = streampos;
358+
using off_type = streamoff;
359+
using state_type = mbstate_t;
360+
using comparison_category = strong_ordering;
358361

359362
using _Primary_char_traits::_Copy_s;
360363
using _Primary_char_traits::copy;
@@ -4511,16 +4514,19 @@ _NODISCARD bool operator==(
45114514
return _Left._Equal(_Right);
45124515
}
45134516

4517+
#if !_HAS_CXX20
45144518
template <class _Elem, class _Traits, class _Alloc>
45154519
_NODISCARD bool operator==(_In_z_ const _Elem* const _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) {
45164520
return _Right._Equal(_Left);
45174521
}
4522+
#endif // !_HAS_CXX20
45184523

45194524
template <class _Elem, class _Traits, class _Alloc>
45204525
_NODISCARD bool operator==(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) {
45214526
return _Left._Equal(_Right);
45224527
}
45234528

4529+
#if !_HAS_CXX20
45244530
template <class _Elem, class _Traits, class _Alloc>
45254531
_NODISCARD bool operator!=(
45264532
const basic_string<_Elem, _Traits, _Alloc>& _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) noexcept {
@@ -4600,6 +4606,19 @@ template <class _Elem, class _Traits, class _Alloc>
46004606
_NODISCARD bool operator>=(const basic_string<_Elem, _Traits, _Alloc>& _Left, _In_z_ const _Elem* const _Right) {
46014607
return !(_Left < _Right);
46024608
}
4609+
#endif // !_HAS_CXX20
4610+
4611+
#if _HAS_CXX20
4612+
template <class _Elem, class _Traits, class _Alloc>
4613+
_NODISCARD int operator<=>(
4614+
const basic_string<_Elem, _Traits, _Alloc>& _Left, const basic_string<_Elem, _Traits, _Alloc>& _Right) noexcept {
4615+
return _Left.compare(_Right);
4616+
}
4617+
template <class _Elem, class _Traits, class _Alloc>
4618+
_NODISCARD int operator<=>(const basic_string<_Elem, _Traits, _Alloc>& _Left, const _Elem* _Right) {
4619+
return _Left.compare(_Right);
4620+
}
4621+
#endif // _HAS_CXX20
46034622

46044623
using string = basic_string<char, char_traits<char>, allocator<char>>;
46054624
using wstring = basic_string<wchar_t, char_traits<wchar_t>, allocator<wchar_t>>;

tests/std/tests/P1614R2_spaceship/test.cpp

Lines changed: 30 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -454,6 +454,36 @@ void ordering_test_cases() {
454454
static_assert(std::is_same_v<SpaceshipType<WeaklyOrderdByOmissionMatch>, std::weak_ordering>);
455455
static_assert(std::is_same_v<SpaceshipType<PartiallyOrderedMatch>, std::partial_ordering>);
456456
}
457+
{ // Strings library
458+
std::string a1 = "abcdef";
459+
std::string a2 = "abcdef";
460+
std::string a3 = "abcdefg";
461+
std::string a4 = "abcde";
462+
std::string a5 = "abddef";
463+
std::string a6 = "abbdef";
464+
465+
assert((a1 <=> a2) == 0);
466+
assert((a1 <=> a3) == -1);
467+
assert((a1 <=> a4) == 1);
468+
assert((a1 <=> a5) == -1);
469+
assert((a1 <=> a6) == 1);
470+
471+
assert(a1 == a2);
472+
assert(a1 >= a2);
473+
assert(a1 <= a2);
474+
assert(a1 < a3);
475+
assert(a1 <= a3);
476+
assert(a1 != a3);
477+
assert(a1 > a4);
478+
assert(a1 >= a4);
479+
assert(a1 != a4);
480+
assert(a1 < a5);
481+
assert(a1 <= a5);
482+
assert(a1 != a5);
483+
assert(a1 > a6);
484+
assert(a1 >= a6);
485+
assert(a1 != a6);
486+
}
457487
{ // Diagnostics Library
458488
diagnostics_test<std::error_code>();
459489
diagnostics_test<std::error_condition>();

0 commit comments

Comments
 (0)