Commit 02e2a7d8 by Jonathan Wakely Committed by Jonathan Wakely

Refactor std::basic_string_view members and add noexcept

	* include/bits/string_view.tcc (find_first_of, find_last_of)
	(find_first_not_of, find_last_not_of): Add noexcept.
	* include/std/string_view (basic_string_view(const _CharT*))
	(basic_string_view(const _CharT*, size_type))
	(front, back, remove_prefix, remove_suffix, find, rfind)
	(find_first_of, find_first_not_of): Add noexcept.
	(at(size_type), _S_compare(size_type, size_type)): Replace conditional
	expressions with if statements.
	(copy(_CharT*, size_type, size_type), substr(size_type, size_type)):
	Use _M_check for length checks.
	(compare(basic_string_view)): Reformat.
	(_M_check(size_type, const char)): Add noexcept(false).
	(_M_limit(size_type, size_type)): Use noexcept not _GLIBCXX_NOEXCEPT.

From-SVN: r254044
parent c58a9f35
2017-10-24 Jonathan Wakely <jwakely@redhat.com> 2017-10-24 Jonathan Wakely <jwakely@redhat.com>
* include/bits/string_view.tcc (find_first_of, find_last_of)
(find_first_not_of, find_last_not_of): Add noexcept.
* include/std/string_view (basic_string_view(const _CharT*))
(basic_string_view(const _CharT*, size_type))
(front, back, remove_prefix, remove_suffix, find, rfind)
(find_first_of, find_first_not_of): Add noexcept.
(at(size_type), _S_compare(size_type, size_type)): Replace conditional
expressions with if statements.
(copy(_CharT*, size_type, size_type), substr(size_type, size_type)):
Use _M_check for length checks.
(compare(basic_string_view)): Reformat.
(_M_check(size_type, const char)): Add noexcept(false).
(_M_limit(size_type, size_type)): Use noexcept not _GLIBCXX_NOEXCEPT.
PR libstdc++/82685 PR libstdc++/82685
* include/experimental/string_view (operator""sv): Add noexcept. * include/experimental/string_view (operator""sv): Add noexcept.
* include/std/string_view (operator""sv): Likewise. * include/std/string_view (operator""sv): Likewise.
......
...@@ -119,7 +119,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -119,7 +119,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _CharT, typename _Traits> template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type constexpr typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>:: basic_string_view<_CharT, _Traits>::
find_first_of(const _CharT* __str, size_type __pos, size_type __n) const find_first_of(const _CharT* __str, size_type __pos,
size_type __n) const noexcept
{ {
__glibcxx_requires_string_len(__str, __n); __glibcxx_requires_string_len(__str, __n);
for (; __n && __pos < this->_M_len; ++__pos) for (; __n && __pos < this->_M_len; ++__pos)
...@@ -135,7 +136,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -135,7 +136,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _CharT, typename _Traits> template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type constexpr typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>:: basic_string_view<_CharT, _Traits>::
find_last_of(const _CharT* __str, size_type __pos, size_type __n) const find_last_of(const _CharT* __str, size_type __pos,
size_type __n) const noexcept
{ {
__glibcxx_requires_string_len(__str, __n); __glibcxx_requires_string_len(__str, __n);
size_type __size = this->size(); size_type __size = this->size();
...@@ -156,7 +158,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -156,7 +158,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _CharT, typename _Traits> template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type constexpr typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>:: basic_string_view<_CharT, _Traits>::
find_first_not_of(const _CharT* __str, size_type __pos, size_type __n) const find_first_not_of(const _CharT* __str, size_type __pos,
size_type __n) const noexcept
{ {
__glibcxx_requires_string_len(__str, __n); __glibcxx_requires_string_len(__str, __n);
for (; __pos < this->_M_len; ++__pos) for (; __pos < this->_M_len; ++__pos)
...@@ -179,7 +182,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -179,7 +182,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
template<typename _CharT, typename _Traits> template<typename _CharT, typename _Traits>
constexpr typename basic_string_view<_CharT, _Traits>::size_type constexpr typename basic_string_view<_CharT, _Traits>::size_type
basic_string_view<_CharT, _Traits>:: basic_string_view<_CharT, _Traits>::
find_last_not_of(const _CharT* __str, size_type __pos, size_type __n) const find_last_not_of(const _CharT* __str, size_type __pos,
size_type __n) const noexcept
{ {
__glibcxx_requires_string_len(__str, __n); __glibcxx_requires_string_len(__str, __n);
size_type __size = this->_M_len; size_type __size = this->_M_len;
......
...@@ -96,14 +96,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -96,14 +96,14 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr basic_string_view(const basic_string_view&) noexcept = default; constexpr basic_string_view(const basic_string_view&) noexcept = default;
constexpr basic_string_view(const _CharT* __str) constexpr basic_string_view(const _CharT* __str) noexcept
: _M_len{__str == nullptr ? 0 : traits_type::length(__str)}, : _M_len{__str == nullptr ? 0 : traits_type::length(__str)},
_M_str{__str} _M_str{__str}
{ } { }
constexpr basic_string_view(const _CharT* __str, size_type __len) constexpr
: _M_len{__len}, basic_string_view(const _CharT* __str, size_type __len) noexcept
_M_str{__str} : _M_len{__len}, _M_str{__str}
{ } { }
constexpr basic_string_view& constexpr basic_string_view&
...@@ -177,17 +177,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -177,17 +177,15 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr const _CharT& constexpr const _CharT&
at(size_type __pos) const at(size_type __pos) const
{ {
return __pos < this->_M_len if (__pos >= _M_len)
? *(this->_M_str + __pos) __throw_out_of_range_fmt(__N("basic_string_view::at: __pos "
: (__throw_out_of_range_fmt(__N("basic_string_view::at: __pos " "(which is %zu) >= this->size() "
"(which is %zu) >= this->size() " "(which is %zu)"), __pos, this->size());
"(which is %zu)"), return *(this->_M_str + __pos);
__pos, this->size()),
*this->_M_str);
} }
constexpr const _CharT& constexpr const _CharT&
front() const front() const noexcept
{ {
// TODO: Assert to restore in a way compatible with the constexpr. // TODO: Assert to restore in a way compatible with the constexpr.
// __glibcxx_assert(this->_M_len > 0); // __glibcxx_assert(this->_M_len > 0);
...@@ -195,7 +193,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -195,7 +193,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
} }
constexpr const _CharT& constexpr const _CharT&
back() const back() const noexcept
{ {
// TODO: Assert to restore in a way compatible with the constexpr. // TODO: Assert to restore in a way compatible with the constexpr.
// __glibcxx_assert(this->_M_len > 0); // __glibcxx_assert(this->_M_len > 0);
...@@ -209,7 +207,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -209,7 +207,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// [string.view.modifiers], modifiers: // [string.view.modifiers], modifiers:
constexpr void constexpr void
remove_prefix(size_type __n) remove_prefix(size_type __n) noexcept
{ {
__glibcxx_assert(this->_M_len >= __n); __glibcxx_assert(this->_M_len >= __n);
this->_M_str += __n; this->_M_str += __n;
...@@ -217,7 +215,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -217,7 +215,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
} }
constexpr void constexpr void
remove_suffix(size_type __n) remove_suffix(size_type __n) noexcept
{ this->_M_len -= __n; } { this->_M_len -= __n; }
constexpr void constexpr void
...@@ -235,38 +233,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -235,38 +233,27 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
copy(_CharT* __str, size_type __n, size_type __pos = 0) const copy(_CharT* __str, size_type __n, size_type __pos = 0) const
{ {
__glibcxx_requires_string_len(__str, __n); __glibcxx_requires_string_len(__str, __n);
if (__pos > this->_M_len) __pos = _M_check(__pos, "basic_string_view::copy");
__throw_out_of_range_fmt(__N("basic_string_view::copy: __pos " const size_type __rlen = std::min(__n, _M_len - __pos);
"(which is %zu) > this->size() "
"(which is %zu)"),
__pos, this->size());
size_type __rlen{std::min(__n, size_type{this->_M_len - __pos})};
for (auto __begin = this->_M_str + __pos, for (auto __begin = this->_M_str + __pos,
__end = __begin + __rlen; __begin != __end;) __end = __begin + __rlen; __begin != __end;)
*__str++ = *__begin++; *__str++ = *__begin++;
return __rlen; return __rlen;
} }
// [string.view.ops], string operations:
constexpr basic_string_view constexpr basic_string_view
substr(size_type __pos, size_type __n=npos) const substr(size_type __pos, size_type __n = npos) const noexcept(false)
{ {
return __pos <= this->_M_len __pos = _M_check(__pos, "basic_string_view::substr");
? basic_string_view{this->_M_str + __pos, const size_type __rlen = std::min(__n, _M_len - __pos);
std::min(__n, size_type{this->_M_len - __pos})} return basic_string_view{_M_str + __pos, __rlen};
: (__throw_out_of_range_fmt(__N("basic_string_view::substr: __pos "
"(which is %zu) > this->size() "
"(which is %zu)"),
__pos, this->size()), basic_string_view{});
} }
constexpr int constexpr int
compare(basic_string_view __str) const noexcept compare(basic_string_view __str) const noexcept
{ {
int __ret = traits_type::compare(this->_M_str, __str._M_str, const size_type __rlen = std::min(this->_M_len, __str._M_len);
std::min(this->_M_len, __str._M_len)); int __ret = traits_type::compare(this->_M_str, __str._M_str, __rlen);
if (__ret == 0) if (__ret == 0)
__ret = _S_compare(this->_M_len, __str._M_len); __ret = _S_compare(this->_M_len, __str._M_len);
return __ret; return __ret;
...@@ -279,7 +266,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -279,7 +266,9 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr int constexpr int
compare(size_type __pos1, size_type __n1, compare(size_type __pos1, size_type __n1,
basic_string_view __str, size_type __pos2, size_type __n2) const basic_string_view __str, size_type __pos2, size_type __n2) const
{ return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2)); } {
return this->substr(__pos1, __n1).compare(__str.substr(__pos2, __n2));
}
constexpr int constexpr int
compare(const _CharT* __str) const noexcept compare(const _CharT* __str) const noexcept
...@@ -291,7 +280,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -291,7 +280,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr int constexpr int
compare(size_type __pos1, size_type __n1, compare(size_type __pos1, size_type __n1,
const _CharT* __str, size_type __n2) const const _CharT* __str, size_type __n2) const noexcept(false)
{ {
return this->substr(__pos1, __n1) return this->substr(__pos1, __n1)
.compare(basic_string_view(__str, __n2)); .compare(basic_string_view(__str, __n2));
...@@ -302,13 +291,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -302,13 +291,13 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ return this->find(__str._M_str, __pos, __str._M_len); } { return this->find(__str._M_str, __pos, __str._M_len); }
constexpr size_type constexpr size_type
find(_CharT __c, size_type __pos=0) const noexcept; find(_CharT __c, size_type __pos = 0) const noexcept;
constexpr size_type constexpr size_type
find(const _CharT* __str, size_type __pos, size_type __n) const noexcept; find(const _CharT* __str, size_type __pos, size_type __n) const noexcept;
constexpr size_type constexpr size_type
find(const _CharT* __str, size_type __pos=0) const noexcept find(const _CharT* __str, size_type __pos = 0) const noexcept
{ return this->find(__str, __pos, traits_type::length(__str)); } { return this->find(__str, __pos, traits_type::length(__str)); }
constexpr size_type constexpr size_type
...@@ -350,7 +339,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -350,7 +339,8 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
{ return this->rfind(__c, __pos); } { return this->rfind(__c, __pos); }
constexpr size_type constexpr size_type
find_last_of(const _CharT* __str, size_type __pos, size_type __n) const; find_last_of(const _CharT* __str, size_type __pos,
size_type __n) const noexcept;
constexpr size_type constexpr size_type
find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept find_last_of(const _CharT* __str, size_type __pos = npos) const noexcept
...@@ -366,7 +356,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -366,7 +356,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr size_type constexpr size_type
find_first_not_of(const _CharT* __str, find_first_not_of(const _CharT* __str,
size_type __pos, size_type __n) const; size_type __pos, size_type __n) const noexcept;
constexpr size_type constexpr size_type
find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept find_first_not_of(const _CharT* __str, size_type __pos = 0) const noexcept
...@@ -385,7 +375,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -385,7 +375,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
constexpr size_type constexpr size_type
find_last_not_of(const _CharT* __str, find_last_not_of(const _CharT* __str,
size_type __pos, size_type __n) const; size_type __pos, size_type __n) const noexcept;
constexpr size_type constexpr size_type
find_last_not_of(const _CharT* __str, find_last_not_of(const _CharT* __str,
...@@ -396,7 +386,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -396,7 +386,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
} }
constexpr size_type constexpr size_type
_M_check(size_type __pos, const char* __s) const _M_check(size_type __pos, const char* __s) const noexcept(false)
{ {
if (__pos > this->size()) if (__pos > this->size())
__throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > " __throw_out_of_range_fmt(__N("%s: __pos (which is %zu) > "
...@@ -407,7 +397,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -407,7 +397,7 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
// NB: _M_limit doesn't check for a bad __pos value. // NB: _M_limit doesn't check for a bad __pos value.
constexpr size_type constexpr size_type
_M_limit(size_type __pos, size_type __off) const _GLIBCXX_NOEXCEPT _M_limit(size_type __pos, size_type __off) const noexcept
{ {
const bool __testoff = __off < this->size() - __pos; const bool __testoff = __off < this->size() - __pos;
return __testoff ? __off : this->size() - __pos; return __testoff ? __off : this->size() - __pos;
...@@ -418,11 +408,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION ...@@ -418,11 +408,12 @@ _GLIBCXX_BEGIN_NAMESPACE_VERSION
static constexpr int static constexpr int
_S_compare(size_type __n1, size_type __n2) noexcept _S_compare(size_type __n1, size_type __n2) noexcept
{ {
return difference_type{__n1 - __n2} > std::numeric_limits<int>::max() const difference_type __diff{__n1 - __n2};
? std::numeric_limits<int>::max() if (__diff > std::numeric_limits<int>::max())
: difference_type{__n1 - __n2} < std::numeric_limits<int>::min() return std::numeric_limits<int>::max();
? std::numeric_limits<int>::min() if (__diff < std::numeric_limits<int>::min())
: static_cast<int>(difference_type{__n1 - __n2}); return std::numeric_limits<int>::min();
return static_cast<int>(__diff);
} }
size_t _M_len; size_t _M_len;
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment