File tree 3 files changed +30
-3
lines changed
3 files changed +30
-3
lines changed Original file line number Diff line number Diff line change @@ -188,4 +188,17 @@ TEST(utf8_string, comparison_operators)
188
188
ASSERT_EQ (result_4, false );
189
189
ASSERT_EQ (result_5, false );
190
190
ASSERT_EQ (result_6, false );
191
+ }
192
+
193
+ // ---------------------------------------------------------------------------------------------------------------------
194
+ TEST (utf8_string, index_operator)
195
+ {
196
+ utf8::ustring::uchar expected{ " п" };
197
+ utf8::ustring ustr{ " привет" };
198
+
199
+ ASSERT_EQ (expected, ustr[0 ]);
200
+ ASSERT_NE (expected, ustr[1 ]);
201
+
202
+ ustr.replace_char (" s" , 0 );
203
+ ASSERT_EQ (utf8::ustring::uchar{ " s" }, ustr[0 ]);
191
204
}
Original file line number Diff line number Diff line change @@ -159,6 +159,17 @@ namespace utf8
159
159
return (String != other);
160
160
}
161
161
162
+ // -----------------------------------------------------------------------------------------------------------------
163
+ const ustring::uchar ustring::operator [](std::size_t index) const
164
+ {
165
+ const SSymbol_Info& symbol_info{ Symbols_Info[index ] };
166
+
167
+ ustring::uchar tmp{ };
168
+ std::memcpy (tmp.data (), String.c_str () + symbol_info.Symbol_Offset , symbol_info.Symbol_Size );
169
+
170
+ return tmp;
171
+ }
172
+
162
173
// -----------------------------------------------------------------------------------------------------------------
163
174
void ustring::replace_char (std::basic_string_view<char > new_symbol, std::size_t idx)
164
175
{
@@ -173,6 +184,7 @@ namespace utf8
173
184
std::basic_string<char > new_string{ };
174
185
new_string.resize (old_string_size + new_symbol_size - old_symbol_info.Symbol_Size ); // calculate & resize new string size
175
186
187
+ // TODO: refactor
176
188
for (std::size_t i{}, counter{}; i < old_string_size;)
177
189
{
178
190
if (i == replace_position)
Original file line number Diff line number Diff line change 27
27
#include < cstring>
28
28
#include < cstdint>
29
29
#include < cstddef>
30
+ #include < array>
30
31
#include < stdexcept>
31
32
32
33
#define ONE_BYTE 0b0000'0000 // first bit 0
@@ -38,6 +39,9 @@ namespace utf8
38
39
{
39
40
class ustring
40
41
{
42
+ public:
43
+ typedef std::array<char , 5 > uchar;
44
+
41
45
public:
42
46
friend std::ostream& operator <<(std::ostream& os, const ustring& ustr);
43
47
friend std::istream& operator >>(std::istream& is, ustring& ustr);
@@ -68,9 +72,7 @@ namespace utf8
68
72
bool operator !=(const std::basic_string<char >& other) const ;
69
73
bool operator !=(const std::basic_string_view<char >& other) const ;
70
74
71
- // TODO: ...............................................
72
- char & operator [](std::size_t index) = delete ;
73
- const char & operator [](std::size_t index) const = delete ;
75
+ const ustring::uchar operator [](std::size_t index) const ;
74
76
75
77
void replace_char (std::basic_string_view<char > new_symbol, std::size_t idx);
76
78
You can’t perform that action at this time.
0 commit comments