Refactor min checks

This commit is contained in:
Krystian Stasiowski
2020-01-03 18:43:59 -05:00
parent 18e6af9cee
commit e1e992380a
2 changed files with 20 additions and 8 deletions

View File

@ -569,6 +569,22 @@ 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
template<typename T>
BOOST_STATIC_STRING_CPP14_CONSTEXPR
inline
bool
is_inside(
const T* src_first,
const T* src_last,
const T* ptr)
{
for (; src_first != src_last; ++src_first)
if (src_first == ptr)
return true;
return false;
}
} // detail } // detail
} // static_string } // static_string
} // boost } // boost

View File

@ -631,8 +631,7 @@ replace(
const auto curr_data = data(); const auto curr_data = data();
BOOST_STATIC_STRING_THROW_IF( BOOST_STATIC_STRING_THROW_IF(
pos > curr_size, std::out_of_range{"pos > size()"}); pos > curr_size, std::out_of_range{"pos > size()"});
if (pos + n1 >= curr_size) n1 = (std::min)(n1, curr_size - pos);
n1 = curr_size - pos;
return replace(curr_data + pos, curr_data + pos + n1, s, s + n2); return replace(curr_data + pos, curr_data + pos + n1, s, s + n2);
} }
@ -653,8 +652,7 @@ replace(
BOOST_STATIC_STRING_THROW_IF( BOOST_STATIC_STRING_THROW_IF(
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()"});
if (pos + n1 >= curr_size) n1 = (std::min)(n1, curr_size - pos);
n1 = curr_size - pos;
Traits::move(&curr_data[pos + n2], &curr_data[pos + n1], curr_size - pos - n1 + 1); Traits::move(&curr_data[pos + n2], &curr_data[pos + n1], curr_size - pos - n1 + 1);
Traits::assign(&curr_data[pos], n2, c); Traits::assign(&curr_data[pos], n2, c);
this->set_size(curr_size + (n2 - n1)); this->set_size(curr_size + (n2 - n1));
@ -684,8 +682,7 @@ replace(
BOOST_STATIC_STRING_THROW_IF( BOOST_STATIC_STRING_THROW_IF(
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()"});
if (pos + n1 >= curr_size) n1 = (std::min)(n1, curr_size - pos);
n1 = curr_size - pos;
const bool inside = s <= &curr_data[curr_size] && s >= curr_data; const bool inside = s <= &curr_data[curr_size] && s >= curr_data;
if (inside && size_type(s - curr_data) == pos && n1 == n2) if (inside && size_type(s - curr_data) == pos && n1 == n2)
return *this; return *this;
@ -753,8 +750,7 @@ replace(
// Rotate to the correct order. [i2, end] will now start with the replaced string, continue to the existing string not being replaced, and end with a null terminator // Rotate to the correct order. [i2, end] will now start with the replaced string, continue to the existing string not being replaced, and end with a null terminator
std::rotate(&curr_data[pos], &curr_data[curr_size + 1], &curr_data[curr_size + n2 + 1]); std::rotate(&curr_data[pos], &curr_data[curr_size + 1], &curr_data[curr_size + n2 + 1]);
// Cap the size // Cap the size
if (pos + n1 >= curr_size) n1 = (std::min)(n1, curr_size - pos);
n1 = curr_size - pos;
// Move everything from the end of the splice point to the end of the rotated string to the begining of the splice point // Move everything from the end of the splice point to the end of the rotated string to the begining of the splice point
Traits::move(&curr_data[pos + n2], &curr_data[pos + n2 + n1], (curr_size + (n2 - n1)) - pos); Traits::move(&curr_data[pos + n2], &curr_data[pos + n2 + n1], (curr_size + (n2 - n1)) - pos);
this->set_size(curr_size + (n2 - n1)); this->set_size(curr_size + (n2 - n1));