From b628dbd1874090b5fc29ef85068d2d221c6f61c2 Mon Sep 17 00:00:00 2001 From: Roland Conybeare Date: Thu, 15 Jan 2026 11:17:55 -0500 Subject: [PATCH] xo-object2: + StringOps::from_cstr + utest --- include/xo/object2/StringOps.hpp | 11 +++++++++++ utest/StringOps.test.cpp | 20 ++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/include/xo/object2/StringOps.hpp b/include/xo/object2/StringOps.hpp index 3cc7198..a8e231e 100644 --- a/include/xo/object2/StringOps.hpp +++ b/include/xo/object2/StringOps.hpp @@ -22,6 +22,10 @@ namespace xo { template static obj empty(obj mm, size_type cap); + + template + static obj from_cstr(obj mm, + const char * cstr); }; template @@ -30,6 +34,13 @@ namespace xo { { return obj(DString::empty(mm, cap)); } + + template + obj + StringOps::from_cstr(obj mm, const char * cstr) + { + return obj(DString::from_cstr(mm, cstr)); + } } } diff --git a/utest/StringOps.test.cpp b/utest/StringOps.test.cpp index 60069b5..bfd3fbd 100644 --- a/utest/StringOps.test.cpp +++ b/utest/StringOps.test.cpp @@ -26,7 +26,7 @@ namespace xo { DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); - obj s = StringOps::empty(alloc, 16); + auto s = StringOps::empty(alloc, 16); REQUIRE(s.data() != nullptr); REQUIRE(s.data()->capacity() == 16); @@ -41,13 +41,29 @@ namespace xo { DArena arena = DArena::map(cfg); auto alloc = with_facet::mkobj(&arena); - obj s = StringOps::empty(alloc, 32); + auto s = StringOps::empty(alloc, 32); s.data()->sprintf("hello %s %d", "world", 42); REQUIRE(s.data()->size() == 14); REQUIRE(std::strcmp(s.data()->chars(), "hello world 42") == 0); } + + TEST_CASE("StringOps-from_cstr", "[object2][StringOps]") + { + ArenaConfig cfg { .name_ = "testarena", + .size_ = 4*1024 }; + DArena arena = DArena::map(cfg); + auto alloc = with_facet::mkobj(&arena); + + const char * cstr = "hello world"; + auto s = StringOps::from_cstr(alloc, cstr); + + REQUIRE(s.data() != nullptr); + REQUIRE(s.data()->capacity() == 12); + REQUIRE(s.data()->size() == 11); + REQUIRE(std::strcmp(s.data()->chars(), cstr) == 0); + } } /*namespace ut*/ } /*namespace xo*/