Represent buffer vector as string_view #7424

Open
biodranik wants to merge 1 commit from ab-buffer-vector-string-view into master
2 changed files with 43 additions and 1 deletions

View file

@ -21,7 +21,7 @@ namespace
UNIT_TEST(BufferVector_PushBackAndRealloc)
{
using ElementT = std::vector<int>;
ElementT element({1, 2, 3});
ElementT const element({1, 2, 3});
size_t constexpr kFixedSize = 2;
{
@ -414,3 +414,36 @@ UNIT_TEST(BufferVector_Erase)
TEST_EQUAL(v1[i], v2[i], ());
}
}
UNIT_TEST(BufferVector_StringView)
{
{
buffer_vector<std::string_view::value_type, 4> bv = {'a', 'b', 'c', 'd'};
std::string_view constexpr svStack = "abcd";
TEST_EQUAL(svStack, std::string_view{bv}, ());
bv.push_back('e');
std::string_view constexpr svHeap = "abcde";
TEST_EQUAL(svHeap, std::string_view{bv}, ());
}
{
buffer_vector<std::u16string_view::value_type, 4> bv = {u'à', u'b', u'ç', u'ð'};
std::u16string_view constexpr svStack = u"àbçð";
TEST_EQUAL(svStack, std::u16string_view{bv}, ());
bv.push_back(u'ë');
std::u16string_view constexpr svHeap = u"àbçðë";
TEST_EQUAL(svHeap, std::u16string_view{bv}, ());
}
{
buffer_vector<std::u32string_view::value_type, 4> bv = {U'à', U'b', U'ç', U'ð'};
std::u32string_view constexpr svStack = U"àbçð";
TEST_EQUAL(svStack, std::u32string_view{bv}, ());
bv.push_back(U'ë');
std::u32string_view constexpr svHeap = U"àbçðë";
TEST_EQUAL(svHeap, std::u32string_view{bv}, ());
}
}

View file

@ -213,6 +213,11 @@ public:
SetStaticSize(0);
}
explicit operator std::basic_string_view<T> const()
{
return std::basic_string_view<T>{data(), size()};
}
vng commented 2024-02-26 16:48:33 +00:00 (Migrated from github.com)
Review

Put into UniString class?

Put into UniString class?
/// @todo Here is some inconsistencies:
/// - "data" method should return 0 if vector is empty;\n
/// - potential memory overrun if m_dynamic is empty;\n
@ -288,6 +293,10 @@ public:
}
/// By value to be consistent with m_vec.push_back(m_vec[0]).
/// The bug comes after the following sequence:
/// 1. operator[] returns a reference for zero element.
/// 2. push_back causes switching from the stack to heap allocation and moves all elements (including zero one).
/// 3. An outdated reference to the already moved zero element is pushed back in the push_back.
void push_back(T t)
{
if (!IsDynamic())