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#pragma once
6
7#include <string>
8#include <format>
9
10namespace tt {
11
29template<typename CharT, int N>
31 using value_type = CharT;
32
33 CharT _str[N];
34
35 constexpr basic_fixed_string(basic_fixed_string const &) noexcept = default;
36 constexpr basic_fixed_string(basic_fixed_string &&) noexcept = default;
37 constexpr basic_fixed_string &operator=(basic_fixed_string const &) noexcept = default;
38 constexpr basic_fixed_string &operator=(basic_fixed_string &&) noexcept = default;
39
40 [[nodiscard]] constexpr basic_fixed_string() noexcept : _str()
41 {
42 for (size_t i = 0; i != N; ++i) {
43 _str[i] = CharT{};
44 }
45 }
46
47 [[nodiscard]] constexpr basic_fixed_string(CharT const (&str)[N]) noexcept : _str()
48 {
49 for (size_t i = 0; i != N; ++i) {
50 _str[i] = str[i];
51 }
52 }
53
54 [[nodiscard]] operator std::basic_string<CharT>() const noexcept
55 {
56 return std::basic_string<CharT>{data(), size()};
57 }
58
59 [[nodiscard]] operator std::basic_string_view<CharT>() const noexcept
60 {
61 return std::basic_string_view<CharT>{data(), size()};
62 }
63
64 [[nodiscard]] operator CharT const *() const noexcept
65 {
66 return data();
67 }
68
69 [[nodiscard]] constexpr auto begin() const noexcept
70 {
71 return &_str[0];
72 }
73
74 [[nodiscard]] constexpr auto end() const noexcept
75 {
76 return &_str[N];
77 }
78
79 [[nodiscard]] constexpr size_t size() const noexcept
80 {
81 return N - 1;
82 }
83
84 [[nodiscard]] constexpr CharT const *data() const noexcept
85 {
86 return &_str[0];
87 }
88
89 [[nodiscard]] constexpr CharT const *c_str() const noexcept
90 {
91 return &_str[0];
92 }
93
94 [[nodiscard]] friend bool operator==(basic_fixed_string const &lhs, basic_fixed_string const &rhs) noexcept = default;
95};
96
97template<typename CharT>
98[[nodiscard]] constexpr size_t basic_fixed_string_length_(CharT const *str) noexcept
99{
100 size_t i = 0;
101 while (str[i++] != CharT{}) {
102 }
103 return i;
104}
105
106} // namespace tt
107
108namespace std {
109
110template<typename T, size_t N, typename CharT>
111struct std::formatter<tt::basic_fixed_string<T, N>, CharT> : std::formatter<T const *, CharT> {
112 auto format(tt::basic_fixed_string<T, N> const &t, auto &fc)
113 {
114 return std::formatter<T const *, CharT>::format(t.data(), fc);
115 }
116};
117
118} // namespace std
STL namespace.
example: ``` template<tt::basic_fixed_string Foo> class A { auto bar() { return std::string{Foo}; } }...
Definition fixed_string.hpp:30