Skip to content

Commit

Permalink
feat: changed BoundedVector::copy to copy up to length-pos characters
Browse files Browse the repository at this point in the history
aligned with standard library behaviour
  • Loading branch information
daantimmer committed Nov 27, 2024
1 parent 9f0f928 commit 1b9e60d
Show file tree
Hide file tree
Showing 2 changed files with 6 additions and 1 deletion.
4 changes: 3 additions & 1 deletion 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 @@ -1063,7 +1064,8 @@ namespace infra
template<class T>
typename BoundedStringBase<T>::size_type BoundedStringBase<T>::copy(char* dest, size_type count, size_type pos)
{
assert(pos + count <= length);
assert(pos <= length);
count = std::min(count, length - pos);
std::copy(begin() + pos, begin() + pos + count, dest);
return count;
}
Expand Down
3 changes: 3 additions & 0 deletions infra/util/test/TestBoundedString.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -585,6 +585,9 @@ TEST(BoundedStringTest, TestCopy)
infra::BoundedString::WithStorage<5> string("abcde");
EXPECT_EQ(3, string.copy(buffer, 3, 1));
EXPECT_EQ('b', buffer[0]);

EXPECT_EQ(2, string.copy(buffer, 5, 3));
EXPECT_EQ('d', buffer[0]);
}

TEST(BoundedStringTest, TestResize)
Expand Down

0 comments on commit 1b9e60d

Please sign in to comment.