From d3b12180e6ff6082911d857f68454c505458c10a Mon Sep 17 00:00:00 2001 From: Daan Timmer <8293597+daantimmer@users.noreply.github.com> Date: Mon, 9 Dec 2024 11:10:53 +0100 Subject: [PATCH] feat: replaced assert with really_assert to align `BoundedString` overflow 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 --- infra/util/BoundedString.hpp | 47 ++++++++++++++++++------------------ 1 file changed, 24 insertions(+), 23 deletions(-) diff --git a/infra/util/BoundedString.hpp b/infra/util/BoundedString.hpp index 22d8f906f..d6ea5a1dc 100644 --- a/infra/util/BoundedString.hpp +++ b/infra/util/BoundedString.hpp @@ -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 #include @@ -715,7 +716,7 @@ namespace infra template BoundedStringBase& BoundedStringBase::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; @@ -732,7 +733,7 @@ namespace infra template BoundedStringBase& BoundedStringBase::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++; @@ -743,7 +744,7 @@ namespace infra template BoundedStringBase& BoundedStringBase::insert(size_type index, const BoundedStringBase& 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; @@ -754,7 +755,7 @@ namespace infra template BoundedStringBase& BoundedStringBase::insert(size_type index, const BoundedStringBase& 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) @@ -778,7 +779,7 @@ namespace infra template typename BoundedStringBase::iterator BoundedStringBase::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; @@ -789,7 +790,7 @@ namespace infra template typename BoundedStringBase::iterator BoundedStringBase::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) @@ -823,7 +824,7 @@ namespace infra template void BoundedStringBase::push_back(char ch) { - assert(length != max_size()); + really_assert(length != max_size()); range[length] = ch; ++length; } @@ -831,14 +832,14 @@ namespace infra template void BoundedStringBase::pop_back() { - assert(length > 0); + really_assert(length > 0); --length; } template BoundedStringBase& BoundedStringBase::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; @@ -849,7 +850,7 @@ namespace infra template BoundedStringBase& BoundedStringBase::append(const BoundedStringBase& 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; @@ -860,7 +861,7 @@ namespace infra template BoundedStringBase& BoundedStringBase::append(const BoundedStringBase& 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; @@ -870,7 +871,7 @@ namespace infra template BoundedStringBase& BoundedStringBase::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; @@ -1065,7 +1066,7 @@ namespace infra template BoundedStringBase BoundedStringBase::substr(size_type pos, size_type count) { - assert(pos <= length); + really_assert(pos <= length); count = std::min(count, length - pos); return BoundedStringBase(begin() + pos, count); } @@ -1073,7 +1074,7 @@ namespace infra template BoundedStringBase BoundedStringBase::substr(size_type pos, size_type count) const { - assert(pos <= length); + really_assert(pos <= length); count = std::min(count, length - pos); return BoundedStringBase(begin() + pos, count); } @@ -1081,7 +1082,7 @@ namespace infra template typename BoundedStringBase::size_type BoundedStringBase::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; @@ -1096,7 +1097,7 @@ namespace infra template void BoundedStringBase::resize(size_type count, char ch) { - assert(count <= max_size()); + really_assert(count <= max_size()); if (count > length) std::fill(end(), begin() + count, ch); @@ -1138,7 +1139,7 @@ namespace infra template typename BoundedStringBase::size_type BoundedStringBase::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(); @@ -1364,7 +1365,7 @@ namespace infra template void BoundedStringBase::AssignToRange(infra::MemoryRange 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); } @@ -1373,7 +1374,7 @@ namespace infra template void BoundedStringBase::AssignToRange(infra::MemoryRange range, size_type& length, const BoundedStringBase& other) { - assert(other.size() <= range.size()); + really_assert(other.size() <= range.size()); length = other.length; std::copy(other.begin(), other.begin() + length, range.begin()); } @@ -1383,7 +1384,7 @@ namespace infra void BoundedStringBase::AssignToRange(infra::MemoryRange range, size_type& length, const BoundedStringBase& 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()); } @@ -1391,7 +1392,7 @@ namespace infra template void BoundedStringBase::AssignToRange(infra::MemoryRange 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()); } @@ -1447,7 +1448,7 @@ namespace infra template void BoundedStringBase::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); @@ -1457,7 +1458,7 @@ namespace infra template void BoundedStringBase::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);