IceShard 1
A personal game engine project, with development focused on 2D/2.5D games.
Loading...
Searching...
No Matches
i18n_detail.hxx
Go to the documentation of this file.
1
3
4#pragma once
5#include <ice/string.hxx>
6#include <fmt/format.h>
7
8namespace ice::i18n
9{
10
11 struct I18NFlags
12 {
13 constexpr explicit I18NFlags(ice::String data) noexcept
14 : _data{ data }
15 {
16 if (_data.not_empty() && _data.back() == '|')
17 {
18 _data = _data.substr(0, _data.size() - 1);
19 }
20 }
21
22 constexpr bool operator==(ice::String rhs) const noexcept { return _data == rhs; }
23
24 constexpr auto language(fmt::format_args const& args) const noexcept -> ice::String;
25
27 };
28
29 namespace detail
30 {
31
32 template<typename ArgType>
34 {
35 ArgType& _reference;
36
37 constexpr bool operator()(ArgType arg) const noexcept
38 {
39 _reference = arg;
40 return true;
41 }
42
43 constexpr bool operator()(auto const& unused) const noexcept
44 {
45 return false;
46 }
47 };
48
49 } // namespace detail
50
51 constexpr auto I18NFlags::language(fmt::format_args const& args) const noexcept -> ice::String
52 {
53 ice::nindex const lang_start = _data.find_first_of('@');
54 ice::nindex const lang_end = _data.find_first_of(", |", lang_start);
55 if (lang_start == ice::nindex_none)
56 {
57 return { };
58 }
59
60 ice::String const lang = _data.substr(lang_start + 1, lang_end - lang_start - 1);
61 if (lang[0] == '{')
62 {
63 ice::nindex const lang_arg_end = lang.find_first_of('}');
64 if (lang_arg_end != std::string_view::npos && lang_arg_end > 1)
65 {
66 ice::i32 const id = atol(lang.data());
67 // if (ice::from_chars(lang, id) == S_Ok)
68 {
69 ice::String result{};
70 {
71 // Find the format arg result
73 args.get(id).visit(lang_arg_visitor);
74 }
76 return result;
77 }
78 }
79 }
80
81 return lang;
82 }
83
84 constexpr void parse(
85 ice::String in_str,
86 ice::u32& out_hash,
87 ice::u8& out_path,
88 ice::u8& out_key,
89 ice::u8& out_flags,
90 ice::u8& out_fallback
91 ) noexcept
92 {
93 ice::nindex const path_sep = in_str.find_first_of('/');
95 out_path = (path_sep + 1).u8();
96
97 ice::String str = in_str.substr(path_sep + 1);
98 ice::nindex const key_sep = str.find_first_of(" |");
99 ice::String const key = str.substr(0, key_sep);
100 out_key = key.size().u8();
101
102 // Create a hash from the reference value. `a.path/a.key`
103 out_hash = ice::hash32(in_str.substr(key_sep + 1));
104
105 // Find additional params or the fallback string
106 if (key_sep.is_valid())
107 {
108 str = str.substr(key_sep);
109 if (str[0] == ' ')
110 {
111 ice::nindex const flags_sep = str.find_first_of('|');
112 ICE_ASSERT_CORE(flags_sep.value_or(2) >= 2); // We need at least one more character for flags to be valid!
113
114 if (flags_sep.is_valid())
115 {
116 out_flags = flags_sep.u8() - 1u;
117 out_fallback = (str.size() - flags_sep).u8() - 1u;
118 }
119 else
120 {
121 out_flags = str.size().u8() - 1u;
122 }
123 }
124 else
125 {
126 out_flags = 0;
127 out_fallback = str.size().u8() - 1u;
128 }
129 }
130 }
131
132} // namespace ice::i18n
#define ICE_ASSERT_CORE(expression)
Definition assert_core.hxx:43
Definition span.hxx:129
Definition i18n_detail.hxx:30
Definition i18n_detail.hxx:9
constexpr void parse(ice::String in_str, ice::u32 &out_hash, ice::u8 &out_path, ice::u8 &out_key, ice::u8 &out_flags, ice::u8 &out_fallback) noexcept
Definition i18n_detail.hxx:84
constexpr auto strptr_size(CharType const *str) noexcept -> ice::ncount::base_type
Definition string_concepts.hxx:72
ice::BasicString< char > String
Definition string.hxx:82
std::int32_t i32
Definition types.hxx:21
constexpr auto hash32(ice::String value) noexcept -> ice::u32
Definition string.hxx:105
std::uint32_t u32
Definition types.hxx:26
std::uint8_t u8
Definition types.hxx:24
static constexpr ice::nindex_invalid_t nindex_none
Definition nindex.hxx:56
constexpr auto size() const noexcept -> SizeType
Definition string.hxx:57
SizeType::base_type _count
Definition string.hxx:26
constexpr auto data() const noexcept -> ValueType *
Definition string.hxx:56
ValueType * _data
Definition string.hxx:25
ice::String _data
Definition i18n_detail.hxx:26
constexpr I18NFlags(ice::String data) noexcept
Definition i18n_detail.hxx:13
constexpr auto language(fmt::format_args const &args) const noexcept -> ice::String
Definition i18n_detail.hxx:51
constexpr bool operator==(ice::String rhs) const noexcept
Definition i18n_detail.hxx:22
Definition i18n_detail.hxx:34
constexpr bool operator()(auto const &unused) const noexcept
Definition i18n_detail.hxx:43
constexpr bool operator()(ArgType arg) const noexcept
Definition i18n_detail.hxx:37
ArgType & _reference
Definition i18n_detail.hxx:35
Definition nindex.hxx:13
constexpr auto u8() const noexcept
Definition nvalue.hxx:111
constexpr bool is_valid(this Self self) noexcept
Definition nvalue.hxx:86
constexpr auto value_or(this Self self, ice::concepts::NValueCompatibleType auto fallback) noexcept
Definition nvalue.hxx:89
constexpr bool not_empty(this Self const &self) noexcept
Definition readonly_operations.hxx:22
constexpr auto find_first_of(this Self const &self, typename Self::CharType character_value, ice::nindex start=0) noexcept -> ice::nindex
Definition readonly_operations.hxx:75
constexpr auto back(this Self const &self) noexcept -> typename Self::CharType
Definition readonly_operations.hxx:34
constexpr auto substr(this Self const &self, ice::nindex pos, ice::ncount len={}) noexcept -> typename Self::StringType
Definition readonly_operations.hxx:40