Skip to content

Commit

Permalink
feat: replaced assert with really_assert to align BoundedString ove…
Browse files Browse the repository at this point in the history
…rflow behaviour with other `Bounded` classes (#780)

* feat: replaced assert with really_assert to align BoundedString overflow behaviour with other Bounded classes

* chore: add missing ReallyAssert include
  • Loading branch information
daantimmer authored Dec 9, 2024
1 parent a7cc6ff commit d3b1218
Showing 1 changed file with 24 additions and 23 deletions.
47 changes: 24 additions & 23 deletions infra/util/BoundedString.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@
// BoundedString is similar to std::string, except that it can contain a maximum number of characters

#include "infra/util/MemoryRange.hpp"
#include "infra/util/ReallyAssert.hpp"
#include "infra/util/WithStorage.hpp"
#include <algorithm>
#include <cctype>
Expand Down Expand Up @@ -715,7 +716,7 @@ namespace infra
template<class T>
BoundedStringBase<T>& BoundedStringBase<T>::insert(size_type index, size_type count, char ch)
{
assert(length + count <= max_size());
really_assert(length + count <= max_size());
MoveUp(index, count);
for (; count != 0; --count)
range[index++] = ch;
Expand All @@ -732,7 +733,7 @@ namespace infra
template<class T>
BoundedStringBase<T>& BoundedStringBase<T>::insert(size_type index, const char* s, size_type count)
{
assert(length + count <= max_size());
really_assert(length + count <= max_size());
MoveUp(index, count);
for (; count != 0; --count)
range[index++] = *s++;
Expand All @@ -743,7 +744,7 @@ namespace infra
template<class T>
BoundedStringBase<T>& BoundedStringBase<T>::insert(size_type index, const BoundedStringBase<T>& other)
{
assert(length + other.size() <= max_size());
really_assert(length + other.size() <= max_size());
MoveUp(index, other.size());
for (const_iterator i = other.begin(); i != other.end(); ++i)
range[index++] = *i;
Expand All @@ -754,7 +755,7 @@ namespace infra
template<class T>
BoundedStringBase<T>& BoundedStringBase<T>::insert(size_type index, const BoundedStringBase<T>& other, size_type index_str, size_type count)
{
assert(length + count <= max_size());
really_assert(length + count <= max_size());
MoveUp(index, count);
const_iterator i = other.begin() + index_str;
for (; count != 0; --count)
Expand All @@ -778,7 +779,7 @@ namespace infra
template<class T>
typename BoundedStringBase<T>::iterator BoundedStringBase<T>::insert(const_iterator pos, char ch)
{
assert(length != max_size());
really_assert(length != max_size());
size_type index = pos - begin();
MoveUp(index, 1);
range[index++] = ch;
Expand All @@ -789,7 +790,7 @@ namespace infra
template<class T>
typename BoundedStringBase<T>::iterator BoundedStringBase<T>::insert(const_iterator pos, size_type count, char ch)
{
assert(length + count <= max_size());
really_assert(length + count <= max_size());
size_type index = pos - begin();
MoveUp(index, count);
for (; count != 0; --count)
Expand Down Expand Up @@ -823,22 +824,22 @@ namespace infra
template<class T>
void BoundedStringBase<T>::push_back(char ch)
{
assert(length != max_size());
really_assert(length != max_size());
range[length] = ch;
++length;
}

template<class T>
void BoundedStringBase<T>::pop_back()
{
assert(length > 0);
really_assert(length > 0);
--length;
}

template<class T>
BoundedStringBase<T>& BoundedStringBase<T>::append(size_type count, char ch)
{
assert(length + count <= max_size());
really_assert(length + count <= max_size());
for (; count > 0; --count, ++length)
range[length] = ch;

Expand All @@ -849,7 +850,7 @@ namespace infra
template<class U>
BoundedStringBase<T>& BoundedStringBase<T>::append(const BoundedStringBase<U>& other)
{
assert(length + other.size() <= max_size());
really_assert(length + other.size() <= max_size());
for (const_iterator i = other.begin(); i != other.end(); ++i, ++length)
range[length] = *i;

Expand All @@ -860,7 +861,7 @@ namespace infra
template<class U>
BoundedStringBase<T>& BoundedStringBase<T>::append(const BoundedStringBase<U>& other, size_type pos, size_type count)
{
assert(length + count <= max_size());
really_assert(length + count <= max_size());
for (const_iterator i = other.begin() + pos; i != other.begin() + pos + count; ++i, ++length)
range[length] = *i;

Expand All @@ -870,7 +871,7 @@ namespace infra
template<class T>
BoundedStringBase<T>& BoundedStringBase<T>::append(const char* s, size_type count)
{
assert(length + count <= max_size());
really_assert(length + count <= max_size());
for (const char* i = s; i != s + count; ++i, ++length)
range[length] = *i;

Expand Down Expand Up @@ -1065,23 +1066,23 @@ namespace infra
template<class T>
BoundedStringBase<T> BoundedStringBase<T>::substr(size_type pos, size_type count)
{
assert(pos <= length);
really_assert(pos <= length);
count = std::min(count, length - pos);
return BoundedStringBase<T>(begin() + pos, count);
}

template<class T>
BoundedStringBase<const T> BoundedStringBase<T>::substr(size_type pos, size_type count) const
{
assert(pos <= length);
really_assert(pos <= length);
count = std::min(count, length - pos);
return BoundedStringBase<const T>(begin() + pos, count);
}

template<class T>
typename BoundedStringBase<T>::size_type BoundedStringBase<T>::copy(char* dest, size_type count, size_type pos)
{
assert(pos <= length);
really_assert(pos <= length);
count = std::min(count, length - pos);
std::copy(begin() + pos, begin() + pos + count, dest);
return count;
Expand All @@ -1096,7 +1097,7 @@ namespace infra
template<class T>
void BoundedStringBase<T>::resize(size_type count, char ch)
{
assert(count <= max_size());
really_assert(count <= max_size());

if (count > length)
std::fill(end(), begin() + count, ch);
Expand Down Expand Up @@ -1138,7 +1139,7 @@ namespace infra
template<class T>
typename BoundedStringBase<T>::size_type BoundedStringBase<T>::find(const char* s, size_type pos, size_type count) const
{
assert(pos <= length);
really_assert(pos <= length);
for (const_iterator i = begin() + pos; i + count <= end(); ++i)
if (CompareImpl(i, i + count, s, s + count) == 0)
return i - begin();
Expand Down Expand Up @@ -1364,7 +1365,7 @@ namespace infra
template<class T>
void BoundedStringBase<T>::AssignToRange(infra::MemoryRange<NonConstT> range, size_type& length, size_type count, char ch)
{
assert(count <= range.size());
really_assert(count <= range.size());
length = count;
std::fill(range.begin(), range.begin() + length, ch);
}
Expand All @@ -1373,7 +1374,7 @@ namespace infra
template<class U>
void BoundedStringBase<T>::AssignToRange(infra::MemoryRange<NonConstT> range, size_type& length, const BoundedStringBase<U>& other)
{
assert(other.size() <= range.size());
really_assert(other.size() <= range.size());
length = other.length;
std::copy(other.begin(), other.begin() + length, range.begin());
}
Expand All @@ -1383,15 +1384,15 @@ namespace infra
void BoundedStringBase<T>::AssignToRange(infra::MemoryRange<NonConstT> range, size_type& length, const BoundedStringBase<U>& other, size_type pos, size_type count)
{
count = std::min(count, other.size());
assert(count <= range.size());
really_assert(count <= range.size());
length = count;
std::copy(other.range.begin() + pos, other.range.begin() + pos + length, range.begin());
}

template<class T>
void BoundedStringBase<T>::AssignToRange(infra::MemoryRange<NonConstT> range, size_type& length, const char* s, size_type count)
{
assert(count <= range.size());
really_assert(count <= range.size());
length = count;
std::copy(s, s + length, range.begin());
}
Expand Down Expand Up @@ -1447,7 +1448,7 @@ namespace infra
template<class T>
void BoundedStringBase<T>::ReplaceImpl(char* begin1, size_type count1, const char* begin2, size_type count2)
{
assert(length - count1 + count2 <= max_size());
really_assert(length - count1 + count2 <= max_size());

std::memmove(begin1 + count2, begin1 + count1, length - count1 - (begin() - begin1));
std::memmove(begin1, begin2, count2);
Expand All @@ -1457,7 +1458,7 @@ namespace infra
template<class T>
void BoundedStringBase<T>::ReplaceImpl(char* begin1, size_type count1, char ch, size_type count2)
{
assert(length - count1 + count2 <= max_size());
really_assert(length - count1 + count2 <= max_size());

std::memmove(begin1 + count2, begin1 + count1, length - count1 - (begin() - begin1));
std::fill(begin1, begin1 + count2, ch);
Expand Down

0 comments on commit d3b1218

Please sign in to comment.