From ea2dd6e2ce71a7ef1cf6fa07e4477791cade8e18 Mon Sep 17 00:00:00 2001 From: "Maxim [maxirmx] Samsonov" Date: Sun, 2 Jun 2024 01:19:59 +0300 Subject: [PATCH] Added sexp_simple_string_t methods and tests to maintain backwards compatibility --- include/sexpp/sexp.h | 10 ++++++++++ tests/src/primitives-tests.cpp | 8 ++++++++ 2 files changed, 18 insertions(+) diff --git a/include/sexpp/sexp.h b/include/sexpp/sexp.h index de0d093..c7f70f7 100644 --- a/include/sexpp/sexp.h +++ b/include/sexpp/sexp.h @@ -112,6 +112,11 @@ class SEXP_PUBLIC_SYMBOL sexp_simple_string_t : public std::vector, push_back((octet_t)(c & 0xFF)); return *this; } + const sexp_simple_string_t &append(const octet_t *d, size_t ln) + { + insert(end(), d, d + ln); + return *this; + } // Returns length for printing simple string as a token size_t advanced_length_token(void) const { return size(); } // Returns length for printing simple string as a base64 string @@ -143,6 +148,11 @@ class SEXP_PUBLIC_SYMBOL sexp_simple_string_t : public std::vector, } uint32_t as_unsigned() const noexcept; + + std::string as_string(void) const + { + return std::string(reinterpret_cast(data()), size()); + } }; inline bool operator==(const sexp_simple_string_t *left, const std::string &right) noexcept diff --git a/tests/src/primitives-tests.cpp b/tests/src/primitives-tests.cpp index 74222bc..8d8f5f9 100644 --- a/tests/src/primitives-tests.cpp +++ b/tests/src/primitives-tests.cpp @@ -412,4 +412,12 @@ TEST_F(PrimitivesTests, SimpleStringConstructors) EXPECT_TRUE(sss2 == "test"); } +TEST_F(PrimitivesTests, Compat_pre_0_9) +{ + const octet_t tss[] = {'t', 'e', 's', 't', 'b', 'a'}; + + sexp_simple_string_t sss; + sss.append(tss, 4); + EXPECT_TRUE(strcmp(sss.as_string().c_str(), "test") == 0); +} } // namespace