From cb56e1ed266594ca7b9a10fedfae691f2f6e3b6d 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 --- xo-object2/include/xo/object2/StringOps.hpp | 11 +++++++++++ xo-object2/utest/StringOps.test.cpp | 20 ++++++++++++++++++-- 2 files changed, 29 insertions(+), 2 deletions(-) diff --git a/xo-object2/include/xo/object2/StringOps.hpp b/xo-object2/include/xo/object2/StringOps.hpp index 3cc71988..a8e231e9 100644 --- a/xo-object2/include/xo/object2/StringOps.hpp +++ b/xo-object2/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/xo-object2/utest/StringOps.test.cpp b/xo-object2/utest/StringOps.test.cpp index 60069b53..bfd3fbd3 100644 --- a/xo-object2/utest/StringOps.test.cpp +++ b/xo-object2/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*/