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