Skip to content

Commit e8f0060

Browse files
authored
Merge pull request #21 from iris-cpp/fixed_string
Add `fixed_string`
2 parents c47c766 + fd1f6b2 commit e8f0060

3 files changed

Lines changed: 56 additions & 2 deletions

File tree

include/iris/fixed_string.hpp

Lines changed: 45 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,45 @@
1+
#ifndef IRIS_FIXED_STRING_HPP
2+
#define IRIS_FIXED_STRING_HPP
3+
4+
// SPDX-License-Identifier: MIT
5+
6+
#include <algorithm>
7+
#include <array>
8+
#include <string>
9+
#include <string_view>
10+
11+
namespace iris {
12+
13+
template<class CharT, std::size_t N, class Traits = std::char_traits<CharT>>
14+
struct basic_fixed_string
15+
{
16+
using storage_type = std::array<CharT, N + 1>;
17+
18+
storage_type data{};
19+
20+
using traits_type = Traits;
21+
using value_type = CharT;
22+
using iterator = storage_type::iterator;
23+
using const_iterator = storage_type::const_iterator;
24+
using string_view_type = std::basic_string_view<CharT, Traits>;
25+
26+
constexpr basic_fixed_string() noexcept = default;
27+
constexpr basic_fixed_string(CharT const (&str)[N + 1]) { std::ranges::copy(str, data.begin()); }
28+
29+
[[nodiscard]] constexpr iterator begin() noexcept { return data.begin(); }
30+
[[nodiscard]] constexpr const_iterator begin() const noexcept { return data.begin(); }
31+
[[nodiscard]] constexpr iterator end() noexcept { return data.end() - 1; }
32+
[[nodiscard]] constexpr const_iterator end() const noexcept { return data.end() - 1; }
33+
34+
[[nodiscard]] constexpr operator string_view_type() const { return {this->begin(), this->end()}; }
35+
};
36+
37+
template<class CharT, std::size_t N>
38+
basic_fixed_string(CharT const (&)[N]) -> basic_fixed_string<CharT, N - 1>;
39+
40+
template<std::size_t N>
41+
using fixed_string = basic_fixed_string<char, N>;
42+
43+
} // iris
44+
45+
#endif

test/core.cpp

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -5,10 +5,12 @@
55
#include <iris/type_traits.hpp>
66
#include <iris/requirements.hpp>
77
#include <iris/compare.hpp>
8+
#include <iris/fixed_string.hpp>
89

910
#include <concepts>
1011
#include <utility>
1112
#include <type_traits>
13+
#include <ranges>
1214

1315
namespace unit_test {
1416

@@ -431,4 +433,11 @@ TEST_CASE("synth_three_way")
431433
STATIC_CHECK(iris::cmp::synth_three_way{}(NoThreeWay{0}, NoThreeWay{1}) == std::weak_ordering::less);
432434
}
433435

436+
TEST_CASE("fixed_string")
437+
{
438+
using namespace std::string_view_literals;
439+
constexpr iris::fixed_string str = "foobar";
440+
CHECK((std::ranges::equal(str, "foobar"sv)));
441+
}
442+
434443
} // unit_test

test/enum.cpp

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ enum class SpellType : std::uint8_t
3131
ATTR_THUNDER = 1 << 4,
3232
};
3333

34-
template <>
34+
template<>
3535
struct iris::bitops_enabled<MyFlags> : std::true_type
3636
{
3737
static MyFlags parse(std::string_view sv) noexcept
@@ -44,7 +44,7 @@ struct iris::bitops_enabled<MyFlags> : std::true_type
4444
}
4545
};
4646

47-
template <>
47+
template<>
4848
struct iris::bitops_enabled<SpellType> : std::true_type
4949
{
5050
static constexpr int min_bit = 2;

0 commit comments

Comments
 (0)