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 "required.hpp"
8#include "strings.hpp"
9#include <string>
10#include <string_view>
11#include <format>
12#include <array>
13
14namespace hi::inline v1 {
15
33template<typename CharT, int N>
35 using value_type = CharT;
36
38
39 constexpr basic_fixed_string() noexcept : _str{}
40 {
41 }
42
43 constexpr basic_fixed_string(basic_fixed_string const &) noexcept = default;
44 constexpr basic_fixed_string(basic_fixed_string &&) noexcept = default;
45 constexpr basic_fixed_string &operator=(basic_fixed_string const &) noexcept = default;
46 constexpr basic_fixed_string &operator=(basic_fixed_string &&) noexcept = default;
47
48 template<std::size_t O>
49 constexpr basic_fixed_string(basic_fixed_string<value_type, O> const &other) noexcept requires(O < N) : _str{}
50 {
51 for (auto i = 0_uz; i != O; ++i) {
52 _str[i] = other._str[i];
53 }
54 }
55
56 template<std::size_t O>
57 constexpr basic_fixed_string &operator=(basic_fixed_string<value_type, O> const &other) noexcept requires(O < N)
58 {
59 auto i = 0_uz;
60 for (; i != O; ++i) {
61 _str[i] = other._str[i];
62 }
63 for (; i != N; ++i) {
64 _str[i] = value_type{};
65 }
66 return *this;
67 }
68
69 template<std::size_t O>
70 constexpr basic_fixed_string(value_type const (&str)[O]) noexcept : _str{}
71 {
72 static_assert((O - 1) <= N);
73
74 for (auto i = 0_uz; i != (O - 1); ++i) {
75 _str[i] = str[i];
76 }
77 }
78
79 template<std::size_t O>
80 constexpr basic_fixed_string &operator=(value_type const (&str)[O]) noexcept
81 {
82 static_assert((O - 1) <= N);
83
84 auto i = 0_uz;
85 for (; i != (O - 1); ++i) {
86 _str[i] = str[i];
87 }
88 for (; i != N; ++i) {
89 _str[i] = value_type{};
90 }
91 return *this;
92 }
93
94 constexpr explicit basic_fixed_string(std::basic_string_view<value_type> str) noexcept : _str{}
95 {
96 hi_axiom(str.size() <= N);
97
98 for (auto i = 0_uz; i != str.size(); ++i) {
99 _str[i] = str[i];
100 }
101 }
102
103 constexpr explicit basic_fixed_string(std::basic_string<value_type> const &str) noexcept : _str{}
104 {
105 hi_axiom(str.size() <= N);
106
107 for (auto i = 0_uz; i != str.size(); ++i) {
108 _str[i] = str[i];
109 }
110 }
111
114 constexpr explicit basic_fixed_string(value_type const *str) noexcept : _str{}
115 {
116 auto i = 0_uz;
117 for (; i != N and str[i] != value_type{}; ++i) {
118 _str[i] = str[i];
119 }
120
121 hi_axiom(str[i] == value_type{});
122 }
123
124 operator std::basic_string_view<value_type>() const noexcept
125 {
126 return std::basic_string_view<value_type>{_str.data(), size()};
127 }
128
129 [[nodiscard]] constexpr std::size_t size() const noexcept
130 {
131 for (auto i = 0_uz; i != N; ++i) {
132 if (_str[i] == value_type{}) {
133 return i;
134 }
135 }
136 return N;
137 }
138
139 [[nodiscard]] constexpr auto begin() noexcept
140 {
141 return _str.begin();
142 }
143
144 [[nodiscard]] constexpr auto end() noexcept
145 {
146 return _str.begin() + size();
147 }
148
154 [[nodiscard]] friend constexpr basic_fixed_string to_title(basic_fixed_string const &rhs) noexcept
155 {
156 auto r = rhs;
157
158 bool first = true;
159 for (auto &c: r) {
160 if (first) {
161 c = to_upper(c);
162 first = false;
163 } else if (c == ' ') {
164 first = true;
165 } else {
166 c = to_lower(c);
167 }
168 }
169
170 return r;
171 }
172
173 [[nodiscard]] friend constexpr bool
174 operator==(basic_fixed_string const &lhs, basic_fixed_string const &rhs) noexcept = default;
175 [[nodiscard]] friend constexpr auto
176 operator<=>(basic_fixed_string const &lhs, basic_fixed_string const &rhs) noexcept = default;
177
178 [[nodiscard]] friend constexpr bool
179 operator==(std::basic_string_view<value_type> const &lhs, basic_fixed_string const &rhs) noexcept
180 {
181 return lhs == static_cast<decltype(lhs)>(rhs);
182 }
183
184 [[nodiscard]] friend constexpr bool
185 operator==(basic_fixed_string const &lhs, std::basic_string_view<value_type> const &rhs) noexcept
186 {
187 return static_cast<decltype(rhs)>(lhs) == rhs;
188 }
189};
190
191template<std::size_t N>
192using fixed_string = basic_fixed_string<char, N>;
193
194//template<typename CharT>
195//[[nodiscard]] constexpr std::size_t basic_fixed_string_length_(CharT const *str) noexcept
196//{
197// std::size_t i = 0;
198// while (str[i++] != CharT{}) {}
199// return i;
200//}
201
202
203template<typename CharT, std::size_t N>
204basic_fixed_string(CharT const (&str)[N]) -> basic_fixed_string<CharT, N - 1>;
205
206} // namespace hi::inline v1
207
208template<typename T, std::size_t N, typename CharT>
209struct std::formatter<hi::basic_fixed_string<T, N>, CharT> : std::formatter<T const *, CharT> {
210 auto format(hi::basic_fixed_string<T, N> const &t, auto &fc)
211 {
212 return std::formatter<T const *, CharT>::format(t.data(), fc);
213 }
214};
This file includes required definitions.
example: ``` template<hi::basic_fixed_string Foo> class A { auto bar() { return std::string{Foo}; } }...
Definition fixed_string.hpp:34
friend constexpr basic_fixed_string to_title(basic_fixed_string const &rhs) noexcept
Convert the current string to using title case.
Definition fixed_string.hpp:154
constexpr basic_fixed_string(value_type const *str) noexcept
Initialize the string from a nul-terminated c-string.
Definition fixed_string.hpp:114
T begin(T... args)
T data(T... args)
T end(T... args)