xo-object2: + DString.sprintf

This commit is contained in:
Roland Conybeare 2026-01-14 16:42:11 -05:00
commit 7e0b182da3
2 changed files with 52 additions and 0 deletions

View file

@ -11,6 +11,7 @@
#include <xo/indentlog/print/ppindentinfo.hpp>
#include <string_view>
#include <cstdint>
#include <cstdio>
namespace xo {
namespace scm {
@ -119,6 +120,25 @@ namespace xo {
/** replace contents with @p other, or prefix of up to @p capacity - 1 chars **/
DString & assign(const DString & other);
///@}
/** @defgroup dstring-general general methods **/
///@{
/** format string into this DString using printf-style formatting.
* Truncates if result exceeds capacity.
* @return number of characters written (excluding null terminator)
**/
template <typename... Args>
size_type sprintf(const char * fmt, Args&&... args) {
int n = std::snprintf(chars_, capacity_, fmt, std::forward<Args>(args)...);
if (n < 0) {
size_ = 0;
chars_[0] = '\0';
} else {
size_ = (n < static_cast<int>(capacity_)) ? n : capacity_ - 1;
}
return size_;
}
// TODO - behave like std::string, to the extent feasible
// insert

View file

@ -260,6 +260,38 @@ namespace xo {
REQUIRE(copy->capacity() == src->capacity());
REQUIRE(std::strcmp(copy->chars(), src->chars()) == 0);
}
TEST_CASE("DString-sprintf", "[object2][DString]")
{
ArenaConfig cfg { .name_ = "testarena",
.size_ = 4*1024 };
DArena arena = DArena::map(cfg);
auto alloc = with_facet<AAllocator>::mkobj(&arena);
DString * s = DString::empty(alloc, 32);
auto n = s->sprintf("hello %s %d", "world", 42);
REQUIRE(n == 14);
REQUIRE(s->size() == 14);
REQUIRE(std::strcmp(s->chars(), "hello world 42") == 0);
}
TEST_CASE("DString-sprintf-truncate", "[object2][DString]")
{
ArenaConfig cfg { .name_ = "testarena",
.size_ = 4*1024 };
DArena arena = DArena::map(cfg);
auto alloc = with_facet<AAllocator>::mkobj(&arena);
DString * s = DString::empty(alloc, 8);
auto n = s->sprintf("hello world");
REQUIRE(n == 7);
REQUIRE(s->size() == 7);
REQUIRE(std::strcmp(s->chars(), "hello w") == 0);
}
} /*namespace ut*/
} /*namespace xo*/