IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
static_string.hxx
Go to the documentation of this file.
1
3#pragma once
6#include <ice/string.hxx>
7
8namespace ice
9{
10
11 template<ice::u32 Capacity = 12, typename CharT = char> requires ice::concepts::SupportedCharType<CharT>
13 {
14 using CharType = CharT;
17 using ReverseIterator = std::reverse_iterator<CharType*>;
18 using ConstIterator = CharType const*;
19 using ConstReverseIterator = std::reverse_iterator<CharType const*>;
22
24 ValueType _data[Capacity];
25
26 constexpr StaticString() noexcept;
27 template<u32 Size>
28 constexpr StaticString(CharType const(&str_array)[Size]) noexcept;
29 constexpr StaticString(BasicString<CharType> string) noexcept;
30
31 constexpr auto operator=(StaticString const& other) noexcept -> StaticString&;
32 constexpr auto operator=(BasicString<CharType> str) noexcept -> StaticString&;
33
34 constexpr operator ice::BasicString<CharType>() const noexcept;
35
36 // ReadOnly+ Operations
37 template<typename Self>
38 constexpr auto data(this Self& self) noexcept { return self._data; }
39 constexpr auto size() const noexcept -> SizeType { return SizeType{ _size, sizeof(CharType) }; }
40
41 // Mutable operations
42 constexpr void resize(ice::ncount new_size) noexcept;
43 constexpr auto capacity() const noexcept -> SizeType { return SizeType{ Capacity, sizeof(CharType) }; }
44
45 // Data info
46 constexpr auto data_view() const noexcept -> ice::Data;
47 };
48
49 template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
50 constexpr StaticString<Capacity, CharT>::StaticString() noexcept
51 : _size{ 0 }
52 , _data{ }
53 {
54 }
55
56 template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
57 template<ice::u32 Size>
58 constexpr StaticString<Capacity, CharT>::StaticString(CharType const(&str_array)[Size]) noexcept
59 : StaticString{ ice::BasicString<CharType>{ str_array, Size } }
60 {
61 }
62
63 template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
65 : _size{ ice::min(Capacity - 1, str.size().u32()) }
66 , _data{ }
67 {
68 if (std::is_constant_evaluated())
69 {
70 for (ice::u32 idx = 0; idx < _size; ++idx)
71 {
72 _data[idx] = str[idx];
73 }
74 }
75 else if (str.not_empty())
76 {
77 ice::memcpy(this->memory_view(), str.data_view());
78 }
79 _data[_size] = ValueType{ 0 };
80 }
81
82 //template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
83 //template<ice::u32 OtherSize>
84 //constexpr StaticString<Capacity, CharType>::StaticString(StaticString<OtherSize, CharType> const& other) noexcept
85 // : _size{ ice::min(Capacity - 1, ice::string::size(other)) }
86 // , _data{ }
87 //{
88 // if (std::is_constant_evaluated())
89 // {
90 // for (ice::ucount idx = 0; idx < _size; ++idx)
91 // {
92 // _data[idx] = other[idx];
93 // }
94 // }
95 // else
96 // {
97 // ice::memcpy(ice::string::memory(*this), ice::string::data_view(other));
98 // }
99 // _data[_size] = ValueType{ 0 };
100 //}
101
102 //template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
103 //template<ice::ucount OtherCapacity>
104 //constexpr auto StaticString<Capacity, CharType>::operator=(StaticString<OtherCapacity, CharType> const& other) noexcept -> StaticString<Capacity, CharType>&
105 //{
106 // if (this != &other)
107 // {
108 // ice::string::clear(*this);
109
110 // if (other._size > 0)
111 // {
112 // ice::memcpy(
113 // ice::string::memory(*this),
114 // ice::string::data_view(other)
115 // );
116 // }
117
118 // _size = ice::min(Capacity - 1, other._size);
119 // _data[_size] = CharType{ };
120 // }
121 // return *this;
122 //}
123
124 template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
126 {
127 CharType const* const other_str_begin = other.begin();
128 bool const part_of_this = other_str_begin >= this->begin()
129 && other_str_begin < this->end();
130
131 if (!part_of_this)
132 {
133 this->clear();
134 ice::memcpy(this->memory_view(), other.data_view());
135 this->resize(ice::min<ncount>(Capacity - 1, other.size()));
136 }
137 return *this;
138 }
139
140 template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
142 {
144 }
145
146 template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
147 constexpr void StaticString<Capacity, CharT>::resize(ice::ncount new_size) noexcept
148 {
149 _size = ice::min(Capacity - 1, new_size.u32());
150 _data[_size] = CharType{ 0 };
151 }
152
153 //template<ice::u32 Capacity, typename CharType>
154 //constexpr bool operator==(ice::StaticString<Capacity, CharType> const& left, CharType const* right) noexcept
155 //{
156 // return ice::BasicString<CharType>{ left } == ice::BasicString<CharType>{ right };
157 //}
158
159 //template<ice::u32 Capacity, typename CharType>
160 //constexpr bool operator==(ice::StaticString<Capacity, CharType> const& left, ice::BasicString<CharType> right) noexcept
161 //{
162 // return ice::BasicString<CharType>{ left } == right;
163 //}
164
165 //template<ice::u32 Capacity, typename CharType>
166 //constexpr bool operator==(ice::BasicString<CharType> left, ice::StaticString<Capacity, CharType> const& right) noexcept
167 //{
168 // return left == ice::BasicString<CharType>{ right };
169 //}
170
171 template<ice::u32 Capacity, typename CharT> requires ice::concepts::SupportedCharType<CharT>
172 constexpr auto StaticString<Capacity, CharT>::data_view() const noexcept -> ice::Data
173 {
174 return Data{
175 .location = _data,
176 .size = size().bytes(),
177 .alignment = ice::align_of<CharType>
178 };
179 }
180
181 template<ice::u32 Capacity = 12>
183 {
184 return ice::stringid(value._data, value._size);
185 }
186
187} // namespace ice
Definition string_concepts.hxx:12
Definition container_concepts.hxx:12
constexpr auto min(arr_t< Size, T > left, arr_t< Size, U > right) noexcept -> arr_t< Size, T >
Definition array_operations.hxx:60
SPDX-License-Identifier: MIT.
Definition array.hxx:12
constexpr ice::ualign align_of
Definition mem_info.hxx:15
BaseStringID< ice::build::Constant_StringID_DebugInfoEnabled > StringID
\copy ice::BaseStringID.
Definition stringid.hxx:15
std::uint32_t u32
Definition types.hxx:26
constexpr auto stringid(ice::StaticString< Capacity, char > value) noexcept -> ice::StringID
Definition static_string.hxx:182
auto memcpy(void *dest, void const *source, ice::usize size) noexcept -> void *
Definition string.hxx:15
Definition mem_data.hxx:17
Definition static_string.hxx:13
std::reverse_iterator< CharType * > ReverseIterator
Definition static_string.hxx:17
CharType ValueType
Definition static_string.hxx:15
ice::BasicString< CharType > StringType
Definition static_string.hxx:21
constexpr auto data_view() const noexcept -> ice::Data
Definition static_string.hxx:172
constexpr auto size() const noexcept -> SizeType
Definition static_string.hxx:39
constexpr auto operator=(StaticString const &other) noexcept -> StaticString &
ice::ncount SizeType
Definition static_string.hxx:20
SizeType::base_type _size
Definition static_string.hxx:23
constexpr StaticString() noexcept
Definition static_string.hxx:50
constexpr auto data(this Self &self) noexcept
Definition static_string.hxx:38
CharType * Iterator
Definition static_string.hxx:16
std::reverse_iterator< CharType const * > ConstReverseIterator
Definition static_string.hxx:19
ValueType _data[Capacity]
Definition static_string.hxx:24
constexpr void resize(ice::ncount new_size) noexcept
Definition static_string.hxx:147
CharType const * ConstIterator
Definition static_string.hxx:18
constexpr auto capacity() const noexcept -> SizeType
Definition static_string.hxx:43
CharT CharType
Definition static_string.hxx:14
Definition ncount.hxx:14
ice::detail::nvalue_base_utype base_type
Definition nvalue.hxx:75
Definition editable_operations.hxx:14
constexpr auto memory_view(this Self &self) noexcept -> ice::Memory
Definition editable_operations.hxx:168
void clear(this Self &self) noexcept
Definition editable_operations.hxx:16
constexpr auto begin(this Self &self) noexcept -> typename Self::Iterator
Definition editable_operations.hxx:132
constexpr auto end(this Self &self) noexcept -> typename Self::Iterator
Definition editable_operations.hxx:138