str/buf: Support std::back_inserter and data()
Change-Id: I07cc8067da62fbe4607d4dc2cf54dc25f559679a
Signed-off-by: William A. Kennington III <wak@google.com>
diff --git a/include/stdplus/str/buf.hpp b/include/stdplus/str/buf.hpp
index 2725e21..1870619 100644
--- a/include/stdplus/str/buf.hpp
+++ b/include/stdplus/str/buf.hpp
@@ -328,6 +328,11 @@
return as.store.dyn.ptr + oldlen;
}
+ constexpr void push_back(CharT c) noexcept
+ {
+ *append(1) = c;
+ }
+
constexpr void shrink(std::size_t amt) noexcept
{
if (as.isDyn())
@@ -340,7 +345,7 @@
}
}
- constexpr void reset() noexcept
+ constexpr void clear() noexcept
{
if (as.isDyn())
{
@@ -352,16 +357,26 @@
}
}
- constexpr CharT* begin() noexcept
+ constexpr CharT* data() noexcept
{
return as.isDyn() ? as.store.dyn.ptr : as.store.inl.ptr;
}
- constexpr const CharT* begin() const noexcept
+ constexpr const CharT* data() const noexcept
{
return as.isDyn() ? as.store.dyn.ptr : as.store.inl.ptr;
}
+ constexpr CharT* begin() noexcept
+ {
+ return data();
+ }
+
+ constexpr const CharT* begin() const noexcept
+ {
+ return data();
+ }
+
constexpr CharT* end() noexcept
{
return begin() + size();
diff --git a/include/stdplus/str/conv.hpp b/include/stdplus/str/conv.hpp
index e0cf810..070efa3 100644
--- a/include/stdplus/str/conv.hpp
+++ b/include/stdplus/str/conv.hpp
@@ -85,7 +85,7 @@
public:
constexpr std::basic_string_view<CharT> operator()(const T::type& v)
{
- buf.reset();
+ buf.clear();
T{}(buf, v);
return buf;
}
diff --git a/test/str/buf.cpp b/test/str/buf.cpp
index 904a59d..cd9580a 100644
--- a/test/str/buf.cpp
+++ b/test/str/buf.cpp
@@ -26,7 +26,7 @@
auto old_ptr = buf.begin();
// Ensure that we inline small amounts of data only
- std::copy(data.begin(), data.begin() + 4, buf.append(4));
+ std::copy(data.begin(), data.begin() + 4, std::back_inserter(buf));
EXPECT_EQ(buf.begin(), old_ptr);
EXPECT_EQ(buf.size(), 4);
EXPECT_EQ(std::string_view(data).substr(0, 4), buf);
@@ -152,7 +152,7 @@
{
StrBuf buf1;
buf1.append(data.size());
- buf1.reset();
+ buf1.clear();
buf1.append(1)[0] = 'a';
StrBuf buf2 = buf1;
StrBuf buf3;
diff --git a/test/str/conv.cpp b/test/str/conv.cpp
index 3a2f41c..969258d 100644
--- a/test/str/conv.cpp
+++ b/test/str/conv.cpp
@@ -103,7 +103,7 @@
StrBuf buf;
ToStrAdap<ToStr<TestValS>>{}(buf, TestValS{});
EXPECT_EQ("test", buf);
- buf.reset();
+ buf.clear();
auto ptr = buf.append(4);
EXPECT_EQ(4, ToStrAdap<ToStr<TestValS>>{}(ptr, TestValS{}) - ptr);
EXPECT_EQ("test", std::string_view(ptr, 4));