IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
editable_operations.hxx
Go to the documentation of this file.
1
3
4#pragma once
6#include <fmt/format.h>
7
8namespace ice::string
9{
10
12
14 {
15 template<MutableStringType Self>
16 inline void clear(this Self& self) noexcept
17 {
18 self.resize(0);
19 }
20
21 template<MutableStringType Self>
22 inline void push_back(this Self& self, typename Self::CharType character) noexcept
23 {
24 ice::ncount new_size = self.size() + 1;
25 ice::ncount const capacity = self.capacity();
26
27 // Handle resizing if supported
29 {
30 if (new_size >= capacity)
31 {
32 self.grow(new_size);
33 }
34 }
35 else
36 {
37 ICE_ASSERT_CORE(new_size < capacity);
38 new_size = ice::min(new_size, capacity - 1);
39 }
40
41
42 self.resize(new_size);
43 self.data()[new_size - 1] = character;
44 }
45
46 template<MutableStringType Self>
47 inline void push_back(this Self& self, typename Self::CharType const* cstr) noexcept
48 {
49 return self.push_back(ice::string::String<Self>{ cstr });
50 }
51
52 template<MutableStringType Self>
53 inline void push_back(this Self& self, ice::StringType auto const& other) noexcept
54 {
55 if (other.not_empty())
56 {
57 ice::ncount new_size = self.size() + other.size();
58 ice::ncount const capacity = self.capacity();
59
60 // Handle resizing if supported
62 {
63 if (new_size + 1 >= capacity)
64 {
65 self.grow(new_size + 1);
66 }
67 }
68 else
69 {
70 ICE_ASSERT_CORE(new_size < capacity);
71 new_size = ice::min(new_size, capacity - 1);
72 }
73
75 self.end(),
76 other.cbegin(),
77 other.size().bytes()
78 );
79
80 self.resize(new_size);
81 }
82 }
83
84 template<MutableStringType Self, typename... Args>
85 inline constexpr void push_format(
86 this Self& self,
87 fmt::format_string<Args...> format,
88 Args&&... args
89 ) noexcept
90 {
91 ice::ncount const pushed_size = ::fmt::formatted_size(format, std::forward<Args>(args)...);
92 ice::ncount const capacity = self.capacity() - 1;
93 ice::ncount final_size = self.size() + pushed_size;
94
95 // Handle resizing if supported
97 {
98 if (final_size >= capacity)
99 {
100 self.grow(final_size + 1);
101 }
102
103 ::fmt::format_to_n(self.end(), pushed_size, format, std::forward<Args>(args)...);
104 }
105 else
106 {
107 //ICE_ASSERT_CORE(final_size < capacity);
108 final_size = ice::min(final_size, capacity);
109
110 ice::ncount const allowed_growth = capacity - self.size();
111 if (allowed_growth > 0)
112 {
113 ::fmt::format_to_n(self.end(), allowed_growth, format, std::forward<Args>(args)...);
114 }
115 }
116 self.resize(final_size);
117 }
118
119 template<MutableStringType Self>
120 inline void pop_back(this Self& self, ice::ncount count = 1) noexcept
121 {
122 if (self.data() != nullptr)
123 {
124 ice::ncount const current_size = self.size();
125 self.resize(current_size - ice::min(current_size, count));
126 }
127 }
128
129 // Iterators
130
131 template<MutableStringType Self>
132 constexpr auto begin(this Self& self) noexcept -> typename Self::Iterator
133 {
134 return self.data();
135 }
136
137 template<MutableStringType Self>
138 constexpr auto end(this Self& self) noexcept -> typename Self::Iterator
139 {
140 return self.data() + self.size();
141 }
142
143 template<MutableStringType Self>
144 constexpr auto rbegin(this Self& self) noexcept -> typename Self::ReverseIterator
145 {
146 return typename Self::ReverseIterator{ self.data() + self.size() };
147 }
148
149 template<MutableStringType Self>
150 constexpr auto rend(this Self& self) noexcept -> typename Self::ReverseIterator
151 {
152 return typename Self::ReverseIterator{ self.data() };
153 }
154
155 // Operators
156
157 using ReadOnlyOperations::operator[];
158
159 template<MutableStringType Self>
160 constexpr auto operator[](this Self& self, ice::nindex index) noexcept -> typename Self::ValueType&
161 {
162 return self.data()[index.native()];
163 }
164
165 // Data Helpers
166
167 template<MutableStringType Self>
168 constexpr auto memory_view(this Self& self) noexcept -> ice::Memory
169 {
170 return Memory{ .location = self.data(), .size = self.capacity(), .alignment = ice::align_of<typename Self::CharType> };
171 }
172 };
173
174} // namespace ice::string
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition string_concepts.hxx:33
Definition string_concepts.hxx:42
constexpr auto min(arr_t< Size, T > left, arr_t< Size, U > right) noexcept -> arr_t< Size, T >
Definition array_operations.hxx:60
Definition editable_operations.hxx:9
typename StringT::StringType String
Definition string_concepts.hxx:64
constexpr ice::ualign align_of
Definition mem_info.hxx:15
constexpr auto count(T const (&)[Size]) noexcept -> ice::u32
Definition base.hxx:43
auto memcpy(void *dest, void const *source, ice::usize size) noexcept -> void *
Definition mem_memory.hxx:13
Definition ncount.hxx:14
Definition nindex.hxx:13
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 rbegin(this Self &self) noexcept -> typename Self::ReverseIterator
Definition editable_operations.hxx:144
void push_back(this Self &self, typename Self::CharType const *cstr) noexcept
Definition editable_operations.hxx:47
void pop_back(this Self &self, ice::ncount count=1) noexcept
Definition editable_operations.hxx:120
void push_back(this Self &self, ice::StringType auto const &other) noexcept
Definition editable_operations.hxx:53
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
constexpr auto rend(this Self &self) noexcept -> typename Self::ReverseIterator
Definition editable_operations.hxx:150
void push_back(this Self &self, typename Self::CharType character) noexcept
Definition editable_operations.hxx:22
constexpr void push_format(this Self &self, fmt::format_string< Args... > format, Args &&... args) noexcept
Definition editable_operations.hxx:85
constexpr auto operator[](this Self &self, ice::nindex index) noexcept -> typename Self::ValueType &
Definition editable_operations.hxx:160
Definition readonly_operations.hxx:14