Used library comparison functions to compare pointers

This commit is contained in:
Krystian Stasiowski
2020-02-01 15:22:50 -05:00
parent c3ea8a976d
commit ad4a2f191b
3 changed files with 41 additions and 146 deletions

View File

@ -574,7 +574,8 @@ find_first_of(
return last; return last;
} }
// Check if a pointer lies within a range without unspecified behavior, allowing it to be used in a constant evaluation // Check if a pointer lies within a range (inclusive) without unspecified behavior,
// allowing it to be used in a constant evaluation
template<typename T> template<typename T>
BOOST_STATIC_STRING_CPP14_CONSTEXPR BOOST_STATIC_STRING_CPP14_CONSTEXPR
inline inline
@ -584,10 +585,7 @@ is_inside(
const T* src_last, const T* src_last,
const T* ptr) const T* ptr)
{ {
for (; src_first != src_last; ++src_first) return std::greater_equal<void>()(ptr, src_first) && std::less_equal<void>()(ptr, src_last);
if (src_first == ptr)
return true;
return false;
} }
} // detail } // detail

View File

@ -363,15 +363,7 @@ insert(
const auto s = &*first; const auto s = &*first;
BOOST_STATIC_STRING_THROW_IF( BOOST_STATIC_STRING_THROW_IF(
count > max_size() - curr_size, std::length_error{"count > max_size() - size()"}); count > max_size() - curr_size, std::length_error{"count > max_size() - size()"});
// Makes this more constexpr friendly if we can use is_constant_evaluated const bool inside = detail::is_inside(curr_data, curr_data + curr_size, s);
const bool inside =
#ifdef BOOST_STATIC_STRING_USE_IS_CONST_EVAL
std::is_constant_evaluated() ?
detail::is_inside(curr_data, curr_data + curr_size, s) :
s <= &curr_data[curr_size] && s >= curr_data;
#else
s <= &curr_data[curr_size] && s >= curr_data;
#endif
if (!inside || (inside && ((s - curr_data) + count <= index))) if (!inside || (inside && ((s - curr_data) + count <= index)))
{ {
Traits::move(&curr_data[index + count], &curr_data[index], curr_size - index + 1); Traits::move(&curr_data[index + count], &curr_data[index], curr_size - index + 1);
@ -669,15 +661,7 @@ replace(
curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2, curr_size - (std::min)(n1, curr_size - pos) >= max_size() - n2,
std::length_error{"replaced string exceeds max_size()"}); std::length_error{"replaced string exceeds max_size()"});
n1 = (std::min)(n1, curr_size - pos); n1 = (std::min)(n1, curr_size - pos);
// Makes this more constexpr friendly if we can use is_constant_evaluated const bool inside = detail::is_inside(curr_data, curr_data + curr_size, s);
const bool inside =
#ifdef BOOST_STATIC_STRING_USE_IS_CONST_EVAL
std::is_constant_evaluated() ?
detail::is_inside(curr_data, curr_data + curr_size, s) :
s <= &curr_data[curr_size] && s >= curr_data;
#else
s <= &curr_data[curr_size] && s >= curr_data;
#endif
if (inside && size_type(s - curr_data) == pos && n1 == n2) if (inside && size_type(s - curr_data) == pos && n1 == n2)
return *this; return *this;
if (!inside || (inside && ((s - curr_data) + n2 <= pos))) if (!inside || (inside && ((s - curr_data) + n2 <= pos)))

View File

@ -893,9 +893,9 @@ public:
Strong guarantee. Strong guarantee.
@note @note The insertion is done unchecked when
The insertion is done unchecked, as the source cannot be the capacity of `str` differs from that of the
within the destination. string the function is called on.
@note All references, pointers, or iterators @note All references, pointers, or iterators
referring to contained elements are invalidated. Any referring to contained elements are invalidated. Any
@ -921,27 +921,7 @@ public:
return insert_unchecked(index, str.data(), str.size()); return insert_unchecked(index, str.data(), str.size());
} }
/** Insert a string. #ifndef GENERATING_DOCUMENTATION
Inserts the string `str`
at the position `index`.
@par Exception Safety
Strong guarantee.
@note All references, pointers, or iterators
referring to contained elements are invalidated. Any
past-the-end iterators are also invalidated.
@return `*this`
@param index The index to insert at.
@param str The string to insert.
@throw std::length_error `size() + str.size() > max_size()`
@throw std::out_of_range `index > size()`
*/
BOOST_STATIC_STRING_CPP14_CONSTEXPR BOOST_STATIC_STRING_CPP14_CONSTEXPR
basic_static_string& basic_static_string&
insert( insert(
@ -950,6 +930,7 @@ public:
{ {
return insert(index, str.data(), str.size()); return insert(index, str.data(), str.size());
} }
#endif
/** Insert a string. /** Insert a string.
@ -960,9 +941,9 @@ public:
Strong guarantee. Strong guarantee.
@note @note The insertion is done unchecked when
The insertion is done unchecked, as the source cannot be the capacity of `str` differs from that of the
within the destination. string the function is called on.
@note All references, pointers, or iterators @note All references, pointers, or iterators
referring to contained elements are invalidated. Any referring to contained elements are invalidated. Any
@ -997,31 +978,7 @@ public:
return insert_unchecked(index, str.data() + index_str, (std::min)(count, str.size() - index_str)); return insert_unchecked(index, str.data() + index_str, (std::min)(count, str.size() - index_str));
} }
/** Insert a string. #ifndef GENERATING_DOCUMENTATION
Inserts a string, obtained by `str.substr(index_str, count)`
at the position `index`.
@par Exception Safety
Strong guarantee.
@note All references, pointers, or iterators
referring to contained elements are invalidated. Any
past-the-end iterators are also invalidated.
@return `*this`
@param index The index to insert at.
@param str The string from which to insert.
@param index_str The index in `str` to start inserting from.
@param count The number of characters to insert.
The default argument for this parameter is @ref npos.
@throw std::length_error `size() + str.substr(index_str, count).size() > max_size()`
@throw std::out_of_range `index > size()`
@throw std::out_of_range `index_str > str.size()`
*/
BOOST_STATIC_STRING_CPP14_CONSTEXPR BOOST_STATIC_STRING_CPP14_CONSTEXPR
basic_static_string& basic_static_string&
insert( insert(
@ -1035,6 +992,7 @@ public:
); );
return insert(index, str.data() + index_str, (std::min)(count, str.size() - index_str)); return insert(index, str.data() + index_str, (std::min)(count, str.size() - index_str));
} }
#endif
/** Insert a character. /** Insert a character.
@ -1942,8 +1900,7 @@ public:
BOOST_STATIC_STRING_CPP14_CONSTEXPR BOOST_STATIC_STRING_CPP14_CONSTEXPR
void void
swap( swap(
basic_static_string<M, CharT, Traits>& s) BOOST_STATIC_STRING_NO_EXCEPTIONS_NOEXCEPT; basic_static_string<M, CharT, Traits>& s) BOOST_STATIC_STRING_NO_EXCEPTIONS_NOEXCEPT;
/** Replace a substring with a string. /** Replace a substring with a string.
@ -1954,9 +1911,9 @@ public:
Strong guarantee. Strong guarantee.
@note @note The replacement is done unchecked when
The replacement is done unchecked, as the source cannot be the capacity of `str` differs from that of the
within the destination. string the function is called on.
All references, pointers, or iterators All references, pointers, or iterators
referring to contained elements are invalidated. Any referring to contained elements are invalidated. Any
@ -1984,28 +1941,7 @@ public:
return replace_unchecked(pos1, n1, str.data(), str.size()); return replace_unchecked(pos1, n1, str.data(), str.size());
} }
/** Replace a substring with a string. #ifndef GENERATING_DOCUMENTATION
Replaces `rcount` characters starting at index `pos1` with those
of `str`, where `rcount` is `std::min(n1, size() - pos1)`.
@par Exception Safety
Strong guarantee.
@note All references, pointers, or iterators
referring to contained elements are invalidated. Any
past-the-end iterators are also invalidated.
@return `*this`
@param pos1 The index to replace at.
@param n1 The number of characters to replace.
@param str The string to replace with.
@throw std::length_error `size() + (str.size() - rcount) > max_size()`
@throw std::out_of_range `pos1 > size()`
*/
BOOST_STATIC_STRING_CPP14_CONSTEXPR BOOST_STATIC_STRING_CPP14_CONSTEXPR
basic_static_string& basic_static_string&
replace( replace(
@ -2015,6 +1951,7 @@ public:
{ {
return replace(pos1, n1, str.data(), str.size()); return replace(pos1, n1, str.data(), str.size());
} }
#endif
/** Replace a substring with a substring. /** Replace a substring with a substring.
@ -2025,50 +1962,9 @@ public:
Strong guarantee. Strong guarantee.
@note All references, pointers, or iterators @note The replacement is done unchecked when
referring to contained elements are invalidated. Any the capacity of `str` differs from that of the
past-the-end iterators are also invalidated. string the function is called on.
@return `*this`
@param pos1 The index to replace at.
@param n1 The number of characters to replace.
@param str The string to replace with.
@param pos2 The index to begin the substring.
@param n2 The length of the substring.
The default argument for this parameter is @ref npos.
@throw std::length_error `size() + (std::min(str.size(), n2) - rcount) > max_size()`
@throw std::out_of_range `pos1 > size()`
@throw std::out_of_range `pos2 > str.size()`
*/
BOOST_STATIC_STRING_CPP14_CONSTEXPR
basic_static_string&
replace(
size_type pos1,
size_type n1,
const basic_static_string& str,
size_type pos2,
size_type n2 = npos) BOOST_STATIC_STRING_NO_EXCEPTIONS_NOEXCEPT
{
BOOST_STATIC_STRING_THROW_IF(
pos2 > str.size(), std::out_of_range{"pos2 > str.size()"}
);
return replace(pos1, n1, str.data() + pos2, (std::min)(n2, str.size() - pos2));
}
/** Replace a substring with a substring.
Replaces `rcount` characters starting at index `pos1` with those of
`str.subview(pos2, n2)`, where `rcount` is `std::min(n1, size() - pos1)`.
@par Exception Safety
Strong guarantee.
@note
The replacement is done unchecked, as the source cannot be
within the destination.
All references, pointers, or iterators All references, pointers, or iterators
referring to contained elements are invalidated. Any referring to contained elements are invalidated. Any
@ -2103,6 +1999,23 @@ public:
return replace_unchecked(pos1, n1, str.data() + pos2, (std::min)(n2, str.size() - pos2)); return replace_unchecked(pos1, n1, str.data() + pos2, (std::min)(n2, str.size() - pos2));
} }
#ifndef GENERATING_DOCUMENTATION
BOOST_STATIC_STRING_CPP14_CONSTEXPR
basic_static_string&
replace(
size_type pos1,
size_type n1,
const basic_static_string& str,
size_type pos2,
size_type n2 = npos) BOOST_STATIC_STRING_NO_EXCEPTIONS_NOEXCEPT
{
BOOST_STATIC_STRING_THROW_IF(
pos2 > str.size(), std::out_of_range{"pos2 > str.size()"}
);
return replace(pos1, n1, str.data() + pos2, (std::min)(n2, str.size() - pos2));
}
#endif
/** Replace a substring with an object convertible to `string_view_type`. /** Replace a substring with an object convertible to `string_view_type`.
Constructs a temporary `string_view_type` object `sv` from `t`, and Constructs a temporary `string_view_type` object `sv` from `t`, and