Skip to content

Commit

Permalink
Added sexp_simple_string_t methods and tests to maintain backwards co…
Browse files Browse the repository at this point in the history
…mpatibility
  • Loading branch information
maxirmx committed Jun 3, 2024
1 parent bb5f053 commit ea2dd6e
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
10 changes: 10 additions & 0 deletions include/sexpp/sexp.h
Original file line number Diff line number Diff line change
Expand Up @@ -112,6 +112,11 @@ class SEXP_PUBLIC_SYMBOL sexp_simple_string_t : public std::vector<octet_t>,
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
Expand Down Expand Up @@ -143,6 +148,11 @@ class SEXP_PUBLIC_SYMBOL sexp_simple_string_t : public std::vector<octet_t>,
}

uint32_t as_unsigned() const noexcept;

std::string as_string(void) const
{
return std::string(reinterpret_cast<const char *>(data()), size());
}
};

inline bool operator==(const sexp_simple_string_t *left, const std::string &right) noexcept
Expand Down
8 changes: 8 additions & 0 deletions tests/src/primitives-tests.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -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

0 comments on commit ea2dd6e

Please sign in to comment.