IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
varstring.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/string.hxx>
8
9namespace ice
10{
11
12 using VarStringTag = struct _tagVarString;
13
14 template<typename CharT = char>
16 {
17 static_assert(sizeof(CharT) == 1, "Wider characters are not supported yet!");
18
19 using CharType = CharT;
20 using ValueType = CharType const;
22 using ConstReverseIterator = std::reverse_iterator<ValueType*>;
28
30
31 inline VarStringBase() noexcept;
32 inline ~VarStringBase() noexcept = default;
33
34 inline explicit VarStringBase(CharType const* data) noexcept;
35
36 constexpr auto data() const noexcept -> ValueType*;
37 constexpr auto size() const noexcept -> SizeType;
38
39 inline operator ice::BasicString<CharType>() const noexcept;
40 };
41
42 using VarString = VarStringBase<char>;
43 //using VarWString = VarStringBase<ice::wchar>;
44
45 namespace varstring
46 {
47
49 {
50 ice::ncount::base_type temp = size.native();
51 ice::usize bytes = 0_B;
52 while (size > 0x7f)
53 {
54 temp >>= 7;
55 bytes += 1_B;
56 }
57 return size.bytes() + bytes + 1_B;
58 }
59
60 inline auto read_size(char const* data, ice::usize& out_bytes) noexcept -> ice::ncount
61 {
62 ice::ncount::base_type result = 0;
63 if (data != nullptr)
64 {
65 ice::u8 const* var_byte = reinterpret_cast<ice::u8 const*>(data);
66 out_bytes = 1_B;
67 while (*var_byte & 0x80)
68 {
69 result += *var_byte;
70 result <<= 7;
71 var_byte += 1;
72 out_bytes += 1_B;
73 }
74 result += *var_byte;
75 }
76 return ice::ncount{ result, sizeof(char) };
77 }
78
79 inline auto read_size(char const* data) noexcept -> ice::ncount
80 {
81 ice::usize bytes;
82 return read_size(data, bytes);
83 }
84
85 template<typename CharType>
86 inline auto read_data(CharType* data) noexcept -> CharType*
87 {
88 ice::usize bytes = 0_B;
89 if (data == nullptr || read_size(data, bytes) == 0)
90 {
91 return nullptr;
92 }
93 return data + bytes.value;
94 }
95
96 inline auto write_size(void* data, ice::ncount size) noexcept -> ice::ncount
97 {
99 ice::nindex byte = 0;
100 ice::u8* const var_byte = reinterpret_cast<ice::u8*>(data);
101 while (size > 0x7f)
102 {
103 var_byte[byte] = (size & 0x7f) | 0x80;
104 temp >>= 7;
105 byte += 1;
106 }
107 var_byte[byte] = size & 0x7f;
108 return byte + 1;
109 }
110
111 } // namespace varstring
112
113 template<typename CharT>
115 : _data{ nullptr }
116 {
117 }
118
119 template<typename CharT>
120 inline VarStringBase<CharT>::VarStringBase(CharT const* str_ptr) noexcept
121 : _data{ str_ptr }
122 {
123 }
124
125 template<typename CharT>
126 inline constexpr auto VarStringBase<CharT>::data() const noexcept -> ValueType*
127 {
129 }
130
131 template<typename CharT>
132 inline constexpr auto VarStringBase<CharT>::size() const noexcept -> SizeType
133 {
135 }
136
137 template<typename CharT>
139 {
140 ice::usize bytes = 0_B;
142 return { _data + bytes.value, size };
143 }
144
145} // namespace ice
Definition span.hxx:129
Definition heap_varstring.hxx:50
auto read_data(CharType *data) noexcept -> CharType *
Definition varstring.hxx:86
auto calc_required_size(ice::ncount size) noexcept -> ice::usize
Definition varstring.hxx:48
auto write_size(void *data, ice::ncount size) noexcept -> ice::ncount
Definition varstring.hxx:96
auto read_size(char const *data, ice::usize &out_bytes) noexcept -> ice::ncount
Definition varstring.hxx:60
SPDX-License-Identifier: MIT.
Definition array.hxx:12
struct _tagVarString VarStringTag
Definition varstring.hxx:12
std::uint8_t u8
Definition types.hxx:24
VarStringBase< char > VarString
Definition varstring.hxx:42
Definition string.hxx:15
Definition varstring.hxx:16
std::reverse_iterator< ValueType * > ConstReverseIterator
Definition varstring.hxx:22
ValueType * _data
Definition varstring.hxx:29
ice::ncount SizeType
Definition varstring.hxx:25
ConstReverseIterator ReverseIterator
Definition varstring.hxx:24
ice::BasicString< CharType > StringType
Definition varstring.hxx:26
CharT CharType
Definition varstring.hxx:19
VarStringBase() noexcept
Definition varstring.hxx:114
CharType const ValueType
Definition varstring.hxx:20
VarStringTag TypeTag
Definition varstring.hxx:27
ValueType * ConstIterator
Definition varstring.hxx:21
constexpr auto size() const noexcept -> SizeType
constexpr auto data() const noexcept -> ValueType *
ConstIterator Iterator
Definition varstring.hxx:23
Definition ncount.hxx:14
ice::detail::nvalue_base_utype base_type
Definition nvalue.hxx:75
Definition nindex.hxx:13
Definition readonly_operations.hxx:14
Represents a unsigned size value on the given platform.
Definition mem_size_types.hxx:26
base_type value
Definition mem_size_types.hxx:35