Minor cleanup for string_view; mostly from suggestions by Richard Smith. Also, make the tests pass under c++03
git-svn-id: https://llvm.org/svn/llvm-project/libcxx/trunk@212185 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
@@ -223,11 +223,11 @@ namespace std {
|
|||||||
basic_string_view(const basic_string<_CharT, _Traits, _Allocator>& __str) _NOEXCEPT
|
basic_string_view(const basic_string<_CharT, _Traits, _Allocator>& __str) _NOEXCEPT
|
||||||
: __data (__str.data()), __size(__str.size()) {}
|
: __data (__str.data()), __size(__str.size()) {}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
||||||
basic_string_view(const _CharT* __s, size_type __len)
|
basic_string_view(const _CharT* __s, size_type __len)
|
||||||
: __data(__s), __size(__len)
|
: __data(__s), __size(__len)
|
||||||
{
|
{
|
||||||
_LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): recieved nullptr");
|
// _LIBCPP_ASSERT(__len == 0 || __s != nullptr, "string_view::string_view(_CharT *, size_t): recieved nullptr");
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
||||||
@@ -284,18 +284,16 @@ namespace std {
|
|||||||
return __data[__pos];
|
return __data[__pos];
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
||||||
const_reference front() const
|
const_reference front() const
|
||||||
{
|
{
|
||||||
_LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty");
|
return _LIBCPP_ASSERT(!empty(), "string_view::front(): string is empty"), __data[0];
|
||||||
return __data[0];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
||||||
const_reference back() const
|
const_reference back() const
|
||||||
{
|
{
|
||||||
_LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty");
|
return _LIBCPP_ASSERT(!empty(), "string_view::back(): string is empty"), __data[__size-1];
|
||||||
return __data[__size-1];
|
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
||||||
@@ -360,12 +358,15 @@ namespace std {
|
|||||||
return __rlen;
|
return __rlen;
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR_AFTER_CXX11 basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
|
_LIBCPP_CONSTEXPR basic_string_view substr(size_type __pos = 0, size_type __n = npos) const
|
||||||
{
|
{
|
||||||
if (__pos > size())
|
// if (__pos > size())
|
||||||
throw out_of_range("string_view::substr");
|
// throw out_of_range("string_view::substr");
|
||||||
size_type __rlen = _VSTD::min( __n, size() - __pos );
|
// size_type __rlen = _VSTD::min( __n, size() - __pos );
|
||||||
return basic_string_view(data() + __pos, __rlen);
|
// return basic_string_view(data() + __pos, __rlen);
|
||||||
|
return __pos > size()
|
||||||
|
? throw out_of_range("string_view::substr")
|
||||||
|
: basic_string_view(data() + __pos, _VSTD::min(__n, size() - __pos));
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
|
_LIBCPP_CONSTEXPR_AFTER_CXX11 int compare(basic_string_view __sv) const _NOEXCEPT
|
||||||
@@ -377,32 +378,32 @@ namespace std {
|
|||||||
return __retval;
|
return __retval;
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
|
||||||
int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
|
int compare(size_type __pos1, size_type __n1, basic_string_view __sv) const
|
||||||
{
|
{
|
||||||
return substr(__pos1, __n1).compare(__sv);
|
return substr(__pos1, __n1).compare(__sv);
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
|
||||||
int compare( size_type __pos1, size_type __n1,
|
int compare( size_type __pos1, size_type __n1,
|
||||||
basic_string_view _sv, size_type __pos2, size_type __n2) const
|
basic_string_view _sv, size_type __pos2, size_type __n2) const
|
||||||
{
|
{
|
||||||
return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2));
|
return substr(__pos1, __n1).compare(_sv.substr(__pos2, __n2));
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
|
||||||
int compare(const _CharT* __s) const
|
int compare(const _CharT* __s) const
|
||||||
{
|
{
|
||||||
return compare(basic_string_view(__s));
|
return compare(basic_string_view(__s));
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
|
||||||
int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
|
int compare(size_type __pos1, size_type __n1, const _CharT* __s) const
|
||||||
{
|
{
|
||||||
return substr(__pos1, __n1).compare(basic_string_view(__s));
|
return substr(__pos1, __n1).compare(basic_string_view(__s));
|
||||||
}
|
}
|
||||||
|
|
||||||
_LIBCPP_CONSTEXPR _LIBCPP_INLINE_VISIBILITY
|
_LIBCPP_CONSTEXPR_AFTER_CXX11 _LIBCPP_INLINE_VISIBILITY
|
||||||
int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
|
int compare(size_type __pos1, size_type __n1, const _CharT* __s, size_type __n2) const
|
||||||
{
|
{
|
||||||
return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
|
return substr(__pos1, __n1).compare(basic_string_view(__s, __n2));
|
||||||
|
@@ -38,7 +38,7 @@ int main () {
|
|||||||
assert ( test ( U"a", 1 ));
|
assert ( test ( U"a", 1 ));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if _LIBCPP_STD_VER > 11
|
#if __cplusplus >= 201103L
|
||||||
{
|
{
|
||||||
constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
|
constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
|
||||||
static_assert ( sv.length() == 2, "" );
|
static_assert ( sv.length() == 2, "" );
|
||||||
|
@@ -38,7 +38,7 @@ int main () {
|
|||||||
assert ( test ( U"a", 1 ));
|
assert ( test ( U"a", 1 ));
|
||||||
#endif
|
#endif
|
||||||
|
|
||||||
#if _LIBCPP_STD_VER > 11
|
#if __cplusplus >= 201103L
|
||||||
{
|
{
|
||||||
constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
|
constexpr std::experimental::basic_string_view<char> sv ( "ABC", 2 );
|
||||||
static_assert ( sv.length() == 2, "" );
|
static_assert ( sv.length() == 2, "" );
|
||||||
|
@@ -53,10 +53,10 @@ void test2 ( const CharT *s, size_t len ) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
using string_view = std::experimental::string_view;
|
typedef std::experimental::string_view string_view;
|
||||||
using u16string_view = std::experimental::u16string_view;
|
typedef std::experimental::u16string_view u16string_view;
|
||||||
using u32string_view = std::experimental::u32string_view;
|
typedef std::experimental::u32string_view u32string_view;
|
||||||
using wstring_view = std::experimental::wstring_view;
|
typedef std::experimental::wstring_view wstring_view;
|
||||||
|
|
||||||
test1<string_view> ();
|
test1<string_view> ();
|
||||||
test1<u16string_view> ();
|
test1<u16string_view> ();
|
||||||
|
@@ -33,10 +33,10 @@ void test () {
|
|||||||
}
|
}
|
||||||
|
|
||||||
int main () {
|
int main () {
|
||||||
using string_view = std::experimental::string_view;
|
typedef std::experimental::string_view string_view;
|
||||||
using u16string_view = std::experimental::u16string_view;
|
typedef std::experimental::u16string_view u16string_view;
|
||||||
using u32string_view = std::experimental::u32string_view;
|
typedef std::experimental::u32string_view u32string_view;
|
||||||
using wstring_view = std::experimental::wstring_view;
|
typedef std::experimental::wstring_view wstring_view;
|
||||||
|
|
||||||
test<string_view> ();
|
test<string_view> ();
|
||||||
test<u16string_view> ();
|
test<u16string_view> ();
|
||||||
|
@@ -40,10 +40,10 @@ test(S s)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
using string_view = std::experimental::string_view;
|
typedef std::experimental::string_view string_view;
|
||||||
using u16string_view = std::experimental::u16string_view;
|
typedef std::experimental::u16string_view u16string_view;
|
||||||
using u32string_view = std::experimental::u32string_view;
|
typedef std::experimental::u32string_view u32string_view;
|
||||||
using wstring_view = std::experimental::wstring_view;
|
typedef std::experimental::wstring_view wstring_view;
|
||||||
|
|
||||||
test(string_view ());
|
test(string_view ());
|
||||||
test(u16string_view());
|
test(u16string_view());
|
||||||
|
@@ -48,10 +48,10 @@ test(S s)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
using string_view = std::experimental::string_view;
|
typedef std::experimental::string_view string_view;
|
||||||
using u16string_view = std::experimental::u16string_view;
|
typedef std::experimental::u16string_view u16string_view;
|
||||||
using u32string_view = std::experimental::u32string_view;
|
typedef std::experimental::u32string_view u32string_view;
|
||||||
using wstring_view = std::experimental::wstring_view;
|
typedef std::experimental::wstring_view wstring_view;
|
||||||
|
|
||||||
test(string_view ());
|
test(string_view ());
|
||||||
test(u16string_view());
|
test(u16string_view());
|
||||||
|
@@ -41,10 +41,10 @@ test(S s)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
using string_view = std::experimental::string_view;
|
typedef std::experimental::string_view string_view;
|
||||||
using u16string_view = std::experimental::u16string_view;
|
typedef std::experimental::u16string_view u16string_view;
|
||||||
using u32string_view = std::experimental::u32string_view;
|
typedef std::experimental::u32string_view u32string_view;
|
||||||
using wstring_view = std::experimental::wstring_view;
|
typedef std::experimental::wstring_view wstring_view;
|
||||||
|
|
||||||
test(string_view ());
|
test(string_view ());
|
||||||
test(u16string_view());
|
test(u16string_view());
|
||||||
|
@@ -48,10 +48,10 @@ test(S s)
|
|||||||
|
|
||||||
int main()
|
int main()
|
||||||
{
|
{
|
||||||
using string_view = std::experimental::string_view;
|
typedef std::experimental::string_view string_view;
|
||||||
using u16string_view = std::experimental::u16string_view;
|
typedef std::experimental::u16string_view u16string_view;
|
||||||
using u32string_view = std::experimental::u32string_view;
|
typedef std::experimental::u32string_view u32string_view;
|
||||||
using wstring_view = std::experimental::wstring_view;
|
typedef std::experimental::wstring_view wstring_view;
|
||||||
|
|
||||||
test(string_view ());
|
test(string_view ());
|
||||||
test(u16string_view());
|
test(u16string_view());
|
||||||
|
Reference in New Issue
Block a user