Skip to content

Commit

Permalink
implicit convert fixed_string to a std::string
Browse files Browse the repository at this point in the history
  • Loading branch information
MichaelMiller- committed Jan 31, 2024
1 parent c8c13c6 commit a4c9c5d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 5 deletions.
7 changes: 2 additions & 5 deletions include/sec21/fixed_string.h
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,13 @@ namespace sec21
template <auto N>
struct fixed_string
{
static_assert(N > 0);
char data[N + 1]{};

constexpr fixed_string(const char (&str)[N + 1]) { std::copy_n(str, N + 1, data); }

constexpr std::string_view sv() const { return {data}; }
constexpr auto operator<=>(fixed_string const&) const = default;

auto operator<=>(fixed_string const&) const = default;

constexpr operator char const*() const { return data; }
constexpr operator std::string_view() const { return data; }
};

template <auto N>
Expand Down
1 change: 1 addition & 0 deletions tests/CMakeLists.txt
Original file line number Diff line number Diff line change
Expand Up @@ -36,6 +36,7 @@ test("concat")
test("database.column_type")
test("database.constraints")
test("database.query")
test("fixed_string")
test("for_each_adjacent")
test("for_each_chunk")
test("for_each_indexed")
Expand Down
15 changes: 15 additions & 0 deletions tests/fixed_string.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
#include <catch.hpp>

#include <sec21/fixed_string.h>

TEST_CASE("fixed_string class", "[sec21]")
{
using namespace sec21;
using namespace std::literals;

SECTION("implicit conversion to std::string_view")
{
STATIC_REQUIRE(""sv == fixed_string(""));
STATIC_REQUIRE("name"sv == fixed_string("name"));
}
}

0 comments on commit a4c9c5d

Please sign in to comment.