xo-object2: + DString::assign

This commit is contained in:
Roland Conybeare 2026-01-13 17:07:03 -05:00
commit d3ba2900d6
3 changed files with 108 additions and 0 deletions

View file

@ -7,6 +7,7 @@
#include <xo/alloc2/Allocator.hpp> #include <xo/alloc2/Allocator.hpp>
#include <xo/facet/obj.hpp> #include <xo/facet/obj.hpp>
#include <string_view>
#include <cstdint> #include <cstdint>
namespace xo { namespace xo {
@ -27,6 +28,9 @@ namespace xo {
using size_type = std::uint32_t; using size_type = std::uint32_t;
using AAllocator = xo::mm::AAllocator; using AAllocator = xo::mm::AAllocator;
/** @defgroup dstring-ctors constructors **/
///@{
/** create empty string with space for @cap chars /** create empty string with space for @cap chars
* (including null terminator). * (including null terminator).
* Use memory from allocator @p mm * Use memory from allocator @p mm
@ -40,10 +44,69 @@ namespace xo {
static DString * from_cstr(obj<AAllocator> mm, static DString * from_cstr(obj<AAllocator> mm,
const char * cstr); const char * cstr);
///@}
/** @defgroup dstring-access access methods **/
///@{
size_type capacity() const noexcept { return capacity_; } size_type capacity() const noexcept { return capacity_; }
size_type size() const noexcept { return size_; } size_type size() const noexcept { return size_; }
const char * chars() const noexcept { return chars_; } const char * chars() const noexcept { return chars_; }
///@}
/** @defgroup dstring-iterators iterators **/
///@{
///@}
/** @defgroup dstring-assign assignment **/
///@{
/** put string into empty state **/
void clear() noexcept { size_ = 0; chars_[0] = '\0'; }
/** replace contents with @p other, or prefix of up to @p capacity - 1 chars **/
DString & assign(const DString & other);
// TODO - behave like std::string, to the extent feasible
// insert
// insert_range
// erase
// push_back
// append
// append_range
// operator+=
// replace
// replace_with_range
// copy
// find
// rfind
// find_first_of
// find_first_not_of
// find_last_of
// find_last_not_of
// compare
// starts_with
// end_with
// contains
// substr
///@}
/** @defgroup dstring-conversion-operators conversion operators **/
///@{
operator std::string_view() const noexcept { return std::string_view(chars_); }
/** @brief conversion oeprator to C-style string.
*
* Example
* @code
* DString s = ...;
* ::strcmp(s, "obey...");
* @endcode
**/
operator const char * () const noexcept { return &(chars_[0]); }
///@}
private: private:
/** extent of @ref chars_ array **/ /** extent of @ref chars_ array **/
size_type capacity_ = 0; size_type capacity_ = 0;

View file

@ -4,6 +4,7 @@
**/ **/
#include "DString.hpp" #include "DString.hpp"
#include <algorithm>
#include <cstring> #include <cstring>
namespace xo { namespace xo {
@ -50,6 +51,18 @@ namespace xo {
return result; return result;
} }
DString &
DString::assign(const DString & other)
{
size_type n = std::min(other.size_, capacity_ - 1);
std::memcpy(chars_, other.chars_, n);
chars_[n] = '\0';
size_ = n;
return *this;
}
} /*namespace scm*/ } /*namespace scm*/
} /*namespace xo*/ } /*namespace xo*/

View file

@ -47,6 +47,38 @@ namespace xo {
REQUIRE(s->size() == 11); REQUIRE(s->size() == 11);
REQUIRE(std::strcmp(s->chars(), cstr) == 0); REQUIRE(std::strcmp(s->chars(), cstr) == 0);
} }
TEST_CASE("DString-assign", "[object2][DString]")
{
ArenaConfig cfg { .name_ = "testarena",
.size_ = 4*1024 };
DArena arena = DArena::map(cfg);
auto alloc = with_facet<AAllocator>::mkobj(&arena);
DString * src = DString::from_cstr(alloc, "hello");
DString * dst = DString::empty(alloc, 16);
dst->assign(*src);
REQUIRE(dst->size() == 5);
REQUIRE(std::strcmp(dst->chars(), "hello") == 0);
}
TEST_CASE("DString-assign-truncate", "[object2][DString]")
{
ArenaConfig cfg { .name_ = "testarena",
.size_ = 4*1024 };
DArena arena = DArena::map(cfg);
auto alloc = with_facet<AAllocator>::mkobj(&arena);
DString * src = DString::from_cstr(alloc, "hello world");
DString * dst = DString::empty(alloc, 6);
dst->assign(*src);
REQUIRE(dst->size() == 5);
REQUIRE(std::strcmp(dst->chars(), "hello") == 0);
}
} /*namespace ut*/ } /*namespace ut*/
} /*namespace xo*/ } /*namespace xo*/