mirror of
https://github.com/boostorg/utility.git
synced 2025-05-09 15:04:00 +00:00
Use assert in string_view::remove_prefix/suffix to enforce preconditions.
This is in line with std::string_view::remove_prefix/suffix definition, where calling the method with n > size() is UB. We're keeping the check to clamp n to size() for now for backward compatibility so that it can be eventually removed. Closes https://github.com/boostorg/utility/issues/92.
This commit is contained in:
parent
06548cf5fa
commit
581654e408
@ -23,6 +23,7 @@
|
|||||||
#include <boost/io/ostream_put.hpp>
|
#include <boost/io/ostream_put.hpp>
|
||||||
#include <boost/utility/string_view_fwd.hpp>
|
#include <boost/utility/string_view_fwd.hpp>
|
||||||
#include <boost/throw_exception.hpp>
|
#include <boost/throw_exception.hpp>
|
||||||
|
#include <boost/assert.hpp>
|
||||||
|
|
||||||
#include <cstddef>
|
#include <cstddef>
|
||||||
#include <stdexcept>
|
#include <stdexcept>
|
||||||
@ -140,6 +141,8 @@ namespace boost {
|
|||||||
void clear() BOOST_NOEXCEPT { len_ = 0; } // Boost extension
|
void clear() BOOST_NOEXCEPT { len_ = 0; } // Boost extension
|
||||||
|
|
||||||
BOOST_CXX14_CONSTEXPR void remove_prefix(size_type n) {
|
BOOST_CXX14_CONSTEXPR void remove_prefix(size_type n) {
|
||||||
|
BOOST_ASSERT(n <= size());
|
||||||
|
// This check is deprecated and is left for backward compatibility. It will be removed in the future.
|
||||||
if ( n > len_ )
|
if ( n > len_ )
|
||||||
n = len_;
|
n = len_;
|
||||||
ptr_ += n;
|
ptr_ += n;
|
||||||
@ -147,6 +150,8 @@ namespace boost {
|
|||||||
}
|
}
|
||||||
|
|
||||||
BOOST_CXX14_CONSTEXPR void remove_suffix(size_type n) {
|
BOOST_CXX14_CONSTEXPR void remove_suffix(size_type n) {
|
||||||
|
BOOST_ASSERT(n <= size());
|
||||||
|
// This check is deprecated and is left for backward compatibility. It will be removed in the future.
|
||||||
if ( n > len_ )
|
if ( n > len_ )
|
||||||
n = len_;
|
n = len_;
|
||||||
len_ -= n;
|
len_ -= n;
|
||||||
|
Loading…
x
Reference in New Issue
Block a user