IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
string.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/stringid.hxx>
8#include <fmt/format.h>
9
10namespace ice
11{
12
13 template<typename CharT> requires ice::concepts::SupportedCharType<CharT>
15 {
16 using CharType = CharT;
17 using ValueType = CharType const;
19 using ConstReverseIterator = std::reverse_iterator<ValueType*>;
24
25 ValueType* _data = nullptr;
27
28 constexpr BasicString() noexcept = default;
29
30 constexpr BasicString(ValueType* ptr_array_nt) noexcept
31 : _data{ ptr_array_nt }
33 { }
34
35 constexpr BasicString(ValueType* ptr_array, ice::ncount count) noexcept
36 : _data{ ptr_array }
37 , _count{ count.native() }
38 { }
39
40 constexpr BasicString(ValueType* ptr_array_begin, ValueType* ptr_array_end) noexcept
41 : _data{ ptr_array_begin }
42 , _count{ static_cast<ice::u64>(ptr_array_end - ptr_array_begin) }
43 { }
44
45 template<ice::u64 Size>
46 constexpr BasicString(ValueType(&char_array)[Size]) noexcept
47 : _data{ char_array }
48 , _count{ Size }
49 { }
50
51 constexpr BasicString(std::basic_string_view<CharType> string_view) noexcept
52 : _data{ string_view.data() }
53 , _count{ string_view.size() }
54 { }
55
56 constexpr auto data() const noexcept -> ValueType* { return _data; }
57 constexpr auto size() const noexcept -> SizeType { return SizeType{ _count, sizeof(CharType) }; }
58
59 constexpr auto data_view() const noexcept -> ice::Data;
60
61 constexpr operator std::basic_string_view<CharType>() const noexcept { return { _data, _count }; }
62 };
63
65
66
67 constexpr auto operator""_str(char const* buffer, size_t size) noexcept -> ice::BasicString<char>
68 {
69 return ice::BasicString<char>{ buffer, size };
70 }
71
72 template<typename CharT> requires ice::concepts::SupportedCharType<CharT>
73 constexpr auto BasicString<CharT>::data_view() const noexcept -> ice::Data
74 {
75 return Data{
76 .location = _data,
77 .size = size().bytes(),
78 .alignment = ice::align_of<CharType>
79 };
80 }
81
84
85 template<typename CharType, typename T> requires ice::concepts::RODataObject<T>
86 constexpr auto string_from_data(T ro_data, ice::nindex offset, ice::ncount size) noexcept -> ice::BasicString<CharType>
87 {
88 return ice::String{
89 reinterpret_cast<CharType const*>(ro_data.location) + offset,
90 ice::min<ice::ncount::base_type>(ro_data.size.value, size)
91 };
92 }
93
94 template<typename CharType, typename T> requires ice::concepts::RODataObject<T>
95 constexpr auto string_from_data(T ro_data) noexcept -> ice::BasicString<CharType>
96 {
97 return ice::string_from_data<CharType>(ro_data, 0, ro_data.size.value);
98 }
99
100 constexpr auto hash(ice::String value) noexcept -> ice::u64
101 {
102 return ice::hash(std::string_view{ value });
103 }
104
105 constexpr auto hash32(ice::String value) noexcept -> ice::u32
106 {
107 return ice::hash32(std::string_view{ value });
108 }
109
110 constexpr auto stringid(ice::String value) noexcept -> ice::StringID
111 {
112 return ice::stringid(value.data(), value.size().u64());
113 }
114
115} // namespace ice
116
117template<typename CharType>
118struct fmt::formatter<ice::BasicString<CharType>> : public fmt::formatter<std::basic_string_view<CharType>>
119{
120 template<typename FormatContext>
121 constexpr auto format(ice::BasicString<CharType> value, FormatContext& ctx) const noexcept
122 {
123 return fmt::formatter<std::basic_string_view<CharType>>::format(value, ctx);
124 }
125};
Definition mem_types.hxx:40
Definition string_concepts.hxx:20
Definition string_concepts.hxx:12
Definition mem_buffer.hxx:20
constexpr auto min(arr_t< Size, T > left, arr_t< Size, U > right) noexcept -> arr_t< Size, T >
Definition array_operations.hxx:60
constexpr auto strptr_size(CharType const *str) noexcept -> ice::ncount::base_type
Definition string_concepts.hxx:72
SPDX-License-Identifier: MIT.
Definition array.hxx:12
ice::BasicString< char > String
Definition string.hxx:82
constexpr auto string_from_data(T ro_data, ice::nindex offset, ice::ncount size) noexcept -> ice::BasicString< CharType >
Definition string.hxx:86
std::uint64_t u64
Definition types.hxx:27
ice::BasicString< wchar_t > WString
Definition string.hxx:83
constexpr ice::ualign align_of
Definition mem_info.hxx:15
constexpr auto hash32(ice::String value) noexcept -> ice::u32
Definition string.hxx:105
constexpr auto count(T const (&)[Size]) noexcept -> ice::u32
Definition base.hxx:43
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
constexpr auto hash(ice::HeapString<> const &value) noexcept -> ice::u64
Definition heap_string.hxx:251
constexpr auto format(ice::BasicString< CharType > value, FormatContext &ctx) const noexcept
Definition string.hxx:121
Definition string.hxx:15
constexpr auto size() const noexcept -> SizeType
Definition string.hxx:57
constexpr BasicString(ValueType(&char_array)[Size]) noexcept
Definition string.hxx:46
constexpr BasicString(ValueType *ptr_array_begin, ValueType *ptr_array_end) noexcept
Definition string.hxx:40
constexpr BasicString(ValueType *ptr_array, ice::ncount count) noexcept
Definition string.hxx:35
SizeType::base_type _count
Definition string.hxx:26
ice::ncount SizeType
Definition string.hxx:22
constexpr BasicString(std::basic_string_view< CharType > string_view) noexcept
Definition string.hxx:51
constexpr auto data() const noexcept -> ValueType *
Definition string.hxx:56
std::reverse_iterator< ValueType * > ConstReverseIterator
Definition string.hxx:19
CharType const ValueType
Definition string.hxx:17
CharType CharType
Definition string.hxx:16
constexpr auto data_view() const noexcept -> ice::Data
Definition string.hxx:73
constexpr BasicString() noexcept=default
ConstIterator Iterator
Definition string.hxx:20
ValueType * ConstIterator
Definition string.hxx:18
ConstReverseIterator ReverseIterator
Definition string.hxx:21
ice::BasicString< CharType > StringType
Definition string.hxx:23
ValueType * _data
Definition string.hxx:25
Definition mem_data.hxx:17
Definition ncount.hxx:14
ice::detail::nvalue_base_utype base_type
Definition nvalue.hxx:75
Definition nindex.hxx:13
Definition readonly_operations.hxx:14