HikoGUI
A low latency retained GUI
Loading...
Searching...
No Matches
fixed_string.hpp
1// Copyright Take Vos 2021.
2// Distributed under the Boost Software License, Version 1.0.
3// (See accompanying file LICENSE_1_0.txt or copy at https://www.boost.org/LICENSE_1_0.txt)
4
5
6
7#pragma once
8
9namespace tt {
10
28template<typename CharT, int N>
30 using value_type = CharT;
31
32 value_type _str[N + 1];
33
34 constexpr basic_fixed_string(basic_fixed_string const &) noexcept = default;
35 constexpr basic_fixed_string(basic_fixed_string &&) noexcept = default;
36 constexpr basic_fixed_string &operator=(basic_fixed_string const &) noexcept = default;
37 constexpr basic_fixed_string &operator=(basic_fixed_string &&) noexcept = default;
38
39 [[nodiscard]] constexpr basic_fixed_string() noexcept : _str()
40 {
41 std::memset(&_str[0], 0, sizeof(CharT) * N);
42 }
43
44 [[nodiscard]] constexpr basic_fixed_string(value_type const (&str)[N + 1]) noexcept : _str()
45 {
46 std::copy_n(str, N + 1, _str);
47 }
48
49 [[nodiscard]] operator std::basic_string<value_type>() const noexcept
50 {
51 return std::basic_string<value_type>{data(), size()};
52 }
53
54 [[nodiscard]] operator std::basic_string_view<value_type>() const noexcept
55 {
56 return std::basic_string_view<value_type>{data(), size()};
57 }
58
59 [[nodiscard]] operator value_type const *() const noexcept
60 {
61 return data();
62 }
63
64 [[nodiscard]] constexpr auto begin() const noexcept
65 {
66 return &_str[0];
67 }
68
69 [[nodiscard]] constexpr auto end() const noexcept
70 {
71 return &_str[N];
72 }
73
74 [[nodiscard]] constexpr size_t size() const noexcept
75 {
76 return N;
77 }
78
79 [[nodiscard]] constexpr value_type const *data() const noexcept
80 {
81 return &_str[0];
82 }
83
84 template<int M>
85 [[nodiscard]] constexpr auto operator+(basic_fixed_string<value_type, M> const &rhs) noexcept
86 {
88 std::copy_n(data(), N, &r._str[0]);
89 std::copy_n(rhs.data(), M + 1, &r._str[N]);
90 return r;
91 }
92
93 [[nodiscard]] friend bool operator==(basic_fixed_string const &lhs, basic_fixed_string const &rhs) noexcept = default;
94};
95
96template<typename CharT, size_t N>
97basic_fixed_string(CharT const (&)[N]) -> basic_fixed_string<CharT, N - 1>;
98
99
100template<size_t N>
102
103template<size_t N>
105
106template<size_t N>
108
109template<size_t N>
111
112template<size_t N>
114
115
116}
117
example: ``` template<tt::basic_fixed_string Foo> class A { auto bar() { return std::string{Foo}; } }...
Definition fixed_string.hpp:29
T copy_n(T... args)
T memset(T... args)