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 <string>
9#include <string_view>
10#include <format>
11#include <array>
12
13namespace hi::inline v1 {
14
32template<typename CharT, int N>
34 using value_type = CharT;
35
37
38 constexpr basic_fixed_string() noexcept : _str{} {}
39
40 constexpr basic_fixed_string(basic_fixed_string const&) noexcept = default;
41 constexpr basic_fixed_string(basic_fixed_string&&) noexcept = default;
42 constexpr basic_fixed_string& operator=(basic_fixed_string const&) noexcept = default;
43 constexpr basic_fixed_string& operator=(basic_fixed_string&&) noexcept = default;
44
45 template<std::size_t O>
46 constexpr basic_fixed_string(value_type const (&str)[O]) noexcept requires((O - 1) == N) : _str{}
47 {
48 for (auto i = 0_uz; i != (O - 1); ++i) {
49 _str[i] = str[i];
50 }
51 }
52
53 template<std::size_t O>
54 constexpr basic_fixed_string& operator=(value_type const (&str)[O]) noexcept requires((O - 1) == N)
55 {
56 auto i = 0_uz;
57 for (; i != (O - 1); ++i) {
58 _str[i] = str[i];
59 }
60 for (; i != N; ++i) {
61 _str[i] = value_type{};
62 }
63 return *this;
64 }
65
66 constexpr operator std::basic_string_view<value_type>() const noexcept
67 {
68 return std::basic_string_view<value_type>{_str.data(), size()};
69 }
70
71 [[nodiscard]] constexpr std::size_t size() const noexcept
72 {
73 return N;
74 }
75
76 [[nodiscard]] constexpr auto begin() noexcept
77 {
78 return _str.begin();
79 }
80
81 [[nodiscard]] constexpr auto end() noexcept
82 {
83 return _str.begin() + size();
84 }
85
86
87
88 [[nodiscard]] constexpr bool operator==(basic_fixed_string const& rhs) const noexcept = default;
89 [[nodiscard]] constexpr auto operator<=>(basic_fixed_string const& rhs) const noexcept = default;
90
91 template<size_t O>
92 [[nodiscard]] constexpr bool operator==(basic_fixed_string<CharT, O> const& rhs) const noexcept requires(O != N)
93 {
94 return false;
95 }
96
97 template<size_t O>
98 [[nodiscard]] constexpr auto operator<=>(basic_fixed_string<CharT, O> const& rhs) const noexcept requires(O != N)
99 {
100 return static_cast<std::basic_string_view<CharT>>(*this) <=> static_cast<std::basic_string_view<CharT>>(rhs);
101 }
102
103 [[nodiscard]] constexpr bool operator==(std::basic_string_view<CharT> rhs) const noexcept
104 {
105 return static_cast<std::basic_string_view<CharT>>(*this) == rhs;
106 }
107
108 [[nodiscard]] constexpr auto operator<=>(std::basic_string_view<CharT> rhs) const noexcept
109 {
110 return static_cast<std::basic_string_view<CharT>>(*this) <=> rhs;
111 }
112};
113
114template<std::size_t N>
116
117// template<typename CharT>
118//[[nodiscard]] constexpr std::size_t basic_fixed_string_length_(CharT const *str) noexcept
119//{
120// std::size_t i = 0;
121// while (str[i++] != CharT{}) {}
122// return i;
123// }
124
125template<typename CharT, std::size_t N>
126basic_fixed_string(CharT const (&str)[N]) -> basic_fixed_string<CharT, N - 1>;
127
128} // namespace hi::inline v1
129
130template<typename T, std::size_t N, typename CharT>
131struct std::formatter<hi::basic_fixed_string<T, N>, CharT> : std::formatter<T const *, CharT> {
132 auto format(hi::basic_fixed_string<T, N> const& t, auto& fc)
133 {
134 return std::formatter<T const *, CharT>::format(t.data(), fc);
135 }
136};
This file includes required definitions.
example: ``` template<hi::basic_fixed_string Foo> class A { auto bar() { return std::string{Foo}; } }...
Definition fixed_string.hpp:33
T begin(T... args)
T data(T... args)