Mark comparison operators for string_view as constexpr; add tests. Reference: https://svn.boost.org/trac/boost/ticket/12896

This commit is contained in:
Marshall Clow 2017-05-03 19:58:02 -07:00
parent 68b26cddbe
commit 330b49d602
3 changed files with 152 additions and 30 deletions

View File

@ -368,7 +368,7 @@ namespace boost {
// Comparison operators // Comparison operators
// Equality // Equality
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator==(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
if (x.size () != y.size ()) return false; if (x.size () != y.size ()) return false;
return x.compare(y) == 0; return x.compare(y) == 0;
@ -376,7 +376,7 @@ namespace boost {
// Inequality // Inequality
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator!=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
if ( x.size () != y.size ()) return true; if ( x.size () != y.size ()) return true;
return x.compare(y) != 0; return x.compare(y) != 0;
@ -384,173 +384,173 @@ namespace boost {
// Less than // Less than
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return x.compare(y) < 0; return x.compare(y) < 0;
} }
// Greater than // Greater than
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return x.compare(y) > 0; return x.compare(y) > 0;
} }
// Less than or equal to // Less than or equal to
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return x.compare(y) <= 0; return x.compare(y) <= 0;
} }
// Greater than or equal to // Greater than or equal to
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return x.compare(y) >= 0; return x.compare(y) >= 0;
} }
// "sufficient additional overloads of comparison functions" // "sufficient additional overloads of comparison functions"
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator==(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x == basic_string_view<charT, traits>(y); return x == basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator==(const std::basic_string<charT, traits, Allocator> & x, inline BOOST_CXX14_CONSTEXPR bool operator==(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) == y; return basic_string_view<charT, traits>(x) == y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator==(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator==(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x == basic_string_view<charT, traits>(y); return x == basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator==(const charT * x, inline BOOST_CXX14_CONSTEXPR bool operator==(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) == y; return basic_string_view<charT, traits>(x) == y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator!=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x != basic_string_view<charT, traits>(y); return x != basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator!=(const std::basic_string<charT, traits, Allocator> & x, inline BOOST_CXX14_CONSTEXPR bool operator!=(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) != y; return basic_string_view<charT, traits>(x) != y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator!=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator!=(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x != basic_string_view<charT, traits>(y); return x != basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator!=(const charT * x, inline BOOST_CXX14_CONSTEXPR bool operator!=(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) != y; return basic_string_view<charT, traits>(x) != y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator<(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x < basic_string_view<charT, traits>(y); return x < basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator<(const std::basic_string<charT, traits, Allocator> & x, inline BOOST_CXX14_CONSTEXPR bool operator<(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) < y; return basic_string_view<charT, traits>(x) < y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator<(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x < basic_string_view<charT, traits>(y); return x < basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<(const charT * x, inline BOOST_CXX14_CONSTEXPR bool operator<(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) < y; return basic_string_view<charT, traits>(x) < y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator>(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x > basic_string_view<charT, traits>(y); return x > basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator>(const std::basic_string<charT, traits, Allocator> & x, inline BOOST_CXX14_CONSTEXPR bool operator>(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) > y; return basic_string_view<charT, traits>(x) > y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator>(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x > basic_string_view<charT, traits>(y); return x > basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>(const charT * x, inline BOOST_CXX14_CONSTEXPR bool operator>(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) > y; return basic_string_view<charT, traits>(x) > y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator<=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x <= basic_string_view<charT, traits>(y); return x <= basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator<=(const std::basic_string<charT, traits, Allocator> & x, inline BOOST_CXX14_CONSTEXPR bool operator<=(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) <= y; return basic_string_view<charT, traits>(x) <= y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator<=(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x <= basic_string_view<charT, traits>(y); return x <= basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator<=(const charT * x, inline BOOST_CXX14_CONSTEXPR bool operator<=(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) <= y; return basic_string_view<charT, traits>(x) <= y;
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator>=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x,
const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT { const std::basic_string<charT, traits, Allocator> & y) BOOST_NOEXCEPT {
return x >= basic_string_view<charT, traits>(y); return x >= basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits, typename Allocator> template<typename charT, typename traits, typename Allocator>
inline bool operator>=(const std::basic_string<charT, traits, Allocator> & x, inline BOOST_CXX14_CONSTEXPR bool operator>=(const std::basic_string<charT, traits, Allocator> & x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) >= y; return basic_string_view<charT, traits>(x) >= y;
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>=(basic_string_view<charT, traits> x, inline BOOST_CXX14_CONSTEXPR bool operator>=(basic_string_view<charT, traits> x,
const charT * y) BOOST_NOEXCEPT { const charT * y) BOOST_NOEXCEPT {
return x >= basic_string_view<charT, traits>(y); return x >= basic_string_view<charT, traits>(y);
} }
template<typename charT, typename traits> template<typename charT, typename traits>
inline bool operator>=(const charT * x, inline BOOST_CXX14_CONSTEXPR bool operator>=(const charT * x,
basic_string_view<charT, traits> y) BOOST_NOEXCEPT { basic_string_view<charT, traits> y) BOOST_NOEXCEPT {
return basic_string_view<charT, traits>(x) >= y; return basic_string_view<charT, traits>(x) >= y;
} }

View File

@ -32,6 +32,7 @@ test-suite utility
[ run string_ref_test2.cpp unit_test_framework ] [ run string_ref_test2.cpp unit_test_framework ]
[ run string_ref_test_io.cpp unit_test_framework ] [ run string_ref_test_io.cpp unit_test_framework ]
# [ compile-fail string_view_from_rvalue.cpp ] # [ compile-fail string_view_from_rvalue.cpp ]
[ compile string_view_constexpr_test1.cpp ]
[ run string_view_test1.cpp unit_test_framework ] [ run string_view_test1.cpp unit_test_framework ]
[ run string_view_test2.cpp unit_test_framework ] [ run string_view_test2.cpp unit_test_framework ]
[ run string_view_test_io.cpp unit_test_framework ] [ run string_view_test_io.cpp unit_test_framework ]

View File

@ -0,0 +1,121 @@
/*
Copyright (c) Marshall Clow 2017-2017.
Distributed under the Boost Software License, Version 1.0. (See accompanying
file LICENSE_1_0.txt or copy at http://www.boost.org/LICENSE_1_0.txt)
For more information, see http://www.boost.org
*/
#include <new> // for placement new
#include <iostream>
#include <cstddef> // for NULL, std::size_t, std::ptrdiff_t
#include <cstring> // for std::strchr and std::strcmp
#include <cstdlib> // for std::malloc and std::free
#include <boost/config.hpp>
#include <boost/utility/string_view.hpp>
#define BOOST_TEST_MAIN
#include <boost/test/unit_test.hpp>
#if __cplusplus < 201402L
BOOST_AUTO_TEST_CASE( test_main ) {}
#else
struct constexpr_char_traits
{
typedef char char_type;
typedef int int_type;
typedef std::streamoff off_type;
typedef std::streampos pos_type;
typedef std::mbstate_t state_type;
static void assign(char_type& c1, const char_type& c2) noexcept { c1 = c2; }
static constexpr bool eq(char_type c1, char_type c2) noexcept { return c1 == c2; }
static constexpr bool lt(char_type c1, char_type c2) noexcept { return c1 < c2; }
static constexpr int compare(const char_type* s1, const char_type* s2, size_t n) noexcept;
static constexpr size_t length(const char_type* s) noexcept;
static constexpr const char_type* find(const char_type* s, size_t n, const char_type& a) noexcept;
static constexpr char_type* move(char_type* s1, const char_type* s2, size_t n) noexcept;
static constexpr char_type* copy(char_type* s1, const char_type* s2, size_t n) noexcept;
static constexpr char_type* assign(char_type* s, size_t n, char_type a) noexcept;
static constexpr int_type not_eof(int_type c) noexcept { return eq_int_type(c, eof()) ? ~eof() : c; }
static constexpr char_type to_char_type(int_type c) noexcept { return char_type(c); }
static constexpr int_type to_int_type(char_type c) noexcept { return int_type(c); }
static constexpr bool eq_int_type(int_type c1, int_type c2) noexcept { return c1 == c2; }
static constexpr int_type eof() noexcept { return EOF; }
};
// yields:
// 0 if for each i in [0,n), X::eq(s1[i],s2[i]) is true;
// else, a negative value if, for some j in [0,n), X::lt(s1[j],s2[j]) is true and
// for each i in [0,j) X::eq(s2[i],s2[i]) is true;
// else a positive value.
constexpr int constexpr_char_traits::compare(const char_type* s1, const char_type* s2, size_t n) noexcept
{
for (; n != 0; --n, ++s1, ++s2)
{
if (lt(*s1, *s2))
return -1;
if (lt(*s2, *s1))
return 1;
}
return 0;
}
// yields: the smallest i such that X::eq(s[i],charT()) is true.
constexpr size_t constexpr_char_traits::length(const char_type* s) noexcept
{
size_t len = 0;
for (; !eq(*s, char_type(0)); ++s)
++len;
return len;
}
typedef boost::basic_string_view<char, constexpr_char_traits> string_view;
BOOST_AUTO_TEST_CASE( test_main )
{
constexpr string_view sv1;
constexpr string_view sv2{"abc", 3}; // ptr, len
constexpr string_view sv3{"def"}; // ptr
constexpr const char *s1 = "";
constexpr const char *s2 = "abc";
static_assert( (sv1 == sv1), "" );
static_assert(!(sv1 == sv2), "" );
static_assert( (sv1 != sv2), "" );
static_assert( (sv1 < sv2), "" );
static_assert( (sv1 <= sv2), "" );
static_assert(!(sv1 > sv2), "" );
static_assert(!(sv1 >= sv2), "" );
static_assert(!(s1 == sv2), "" );
static_assert( (s1 != sv2), "" );
static_assert( (s1 < sv2), "" );
static_assert( (s1 <= sv2), "" );
static_assert(!(s1 > sv2), "" );
static_assert(!(s1 >= sv2), "" );
static_assert(!(sv1 == s2), "" );
static_assert( (sv1 != s2), "" );
static_assert( (sv1 < s2), "" );
static_assert( (sv1 <= s2), "" );
static_assert(!(sv1 > s2), "" );
static_assert(!(sv1 >= s2), "" );
static_assert( sv1.compare(sv2) < 0, "" );
static_assert( sv1.compare(sv1) == 0, "" );
static_assert( sv3.compare(sv1) > 0, "" );
static_assert( sv1.compare(s2) < 0, "" );
static_assert( sv1.compare(s1) == 0, "" );
static_assert( sv3.compare(s1) > 0, "" );
}
#endif