Skip to content

Commit e87fb26

Browse files
authored
Merge pull request #2 from b1tflyyyy/dev
added new ctors & operators & refactored code & added additional tests for utf8::ustring
2 parents b002e4f + ca9ae12 commit e87fb26

File tree

4 files changed

+200
-17
lines changed

4 files changed

+200
-17
lines changed

source/main.cpp

+1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
#include <iostream>
22
#include <chrono>
3+
34
#include <utf8_string.hpp>
45

56
int main()

tests/utf8_string_test.cpp

+82
Original file line numberDiff line numberDiff line change
@@ -61,4 +61,86 @@ TEST(utf8_string, replace_char_case_3)
6161
ustr.replace_char("مرحبا", 2);
6262

6363
ASSERT_EQ(ustr.get_str(), "прمرحباпgт");
64+
}
65+
66+
// ---------------------------------------------------------------------------------------------------------------------
67+
TEST(utf8_string, ctors)
68+
{
69+
// ----------------------------- const char* PART -----------------------------
70+
const char* c_str{ "привет" };
71+
72+
utf8::ustring ustr_from_c_str{ c_str };
73+
ASSERT_EQ(c_str, ustr_from_c_str.get_c_str());
74+
75+
ustr_from_c_str.replace_char("g", 0);
76+
ASSERT_NE(c_str, ustr_from_c_str.get_c_str());
77+
78+
79+
// ----------------------------- std::string PART -----------------------------
80+
std::string cpp_str{ "пока" };
81+
82+
utf8::ustring ustr_from_cpp_str{ cpp_str };
83+
ASSERT_EQ(cpp_str, ustr_from_cpp_str.get_c_str());
84+
85+
ustr_from_cpp_str.replace_char("h", 0);
86+
ASSERT_NE(cpp_str, ustr_from_cpp_str.get_c_str());
87+
88+
89+
// ----------------------------- std::string_view PART -----------------------------
90+
std::string_view cpp_str_view{ "что" };
91+
92+
utf8::ustring ustr_from_cpp_str_view{ cpp_str_view };
93+
ASSERT_EQ(cpp_str_view, ustr_from_cpp_str_view.get_c_str());
94+
95+
ustr_from_cpp_str_view.replace_char("L", 0);
96+
ASSERT_NE(cpp_str_view, ustr_from_cpp_str_view.get_c_str());
97+
98+
99+
// ----------------------------- move ctor PART -----------------------------
100+
const char* move_data{ "привет мир" };
101+
102+
utf8::ustring ustr_move{ move_data };
103+
auto other_ustr_move{ std::move(ustr_move) };
104+
105+
ASSERT_EQ(other_ustr_move.get_c_str(), move_data);
106+
ASSERT_NE(ustr_move.get_c_str(), move_data);
107+
108+
109+
// ----------------------------- copy ctor PART -----------------------------
110+
const char* copy_data{ "хеллоу ворлд" };
111+
112+
utf8::ustring copy_data_first{ copy_data };
113+
utf8::ustring copy_data_second{ copy_data_first };
114+
115+
ASSERT_EQ(copy_data, copy_data_first.get_c_str());
116+
ASSERT_EQ(copy_data, copy_data_second.get_c_str());
117+
ASSERT_EQ(copy_data_first, copy_data_second);
118+
119+
// TODO: write for other ctors ....
120+
}
121+
122+
// ---------------------------------------------------------------------------------------------------------------------
123+
TEST(utf8_string, operators)
124+
{
125+
// ----------------------------- move operator PART -----------------------------
126+
const char* move_data{ "hello world" };
127+
128+
utf8::ustring ustr_move{ move_data };
129+
utf8::ustring other_ustr_move;
130+
other_ustr_move = std::move(ustr_move);
131+
132+
ASSERT_EQ(other_ustr_move.get_c_str(), move_data);
133+
ASSERT_NE(ustr_move.get_c_str(), move_data);
134+
135+
136+
// ----------------------------- copy operator PART -----------------------------
137+
const char* copy_data{ "хеллоу ворлд" };
138+
139+
utf8::ustring copy_data_first{ copy_data };
140+
utf8::ustring copy_data_second;
141+
copy_data_second = copy_data_first;
142+
143+
ASSERT_EQ(copy_data, copy_data_first.get_c_str());
144+
ASSERT_EQ(copy_data, copy_data_second.get_c_str());
145+
ASSERT_EQ(copy_data_first, copy_data_second);
64146
}

utf8-string-lib/utf8_string.cpp

+92-15
Original file line numberDiff line numberDiff line change
@@ -14,34 +14,88 @@ namespace utf8
1414
std::istream& operator>>(std::istream& is, ustring& ustr)
1515
{
1616
// TODO: .............................................
17+
throw std::runtime_error("operator >> for utf8::ustring not implemented yet !");
1718
}
1819

1920
// -----------------------------------------------------------------------------------------------------------------
20-
constexpr ustring::ustring()
21+
ustring::ustring()
2122
{
2223
Symbols_Info.reserve(100);
2324
String.reserve(100);
2425
}
2526

2627
// -----------------------------------------------------------------------------------------------------------------
27-
ustring::ustring(const char* utf8_str)
28+
ustring::ustring(const std::size_t bytes_reserve)
2829
{
29-
Symbols_Info.reserve(100);
30-
String.reserve(100);
30+
Symbols_Info.reserve(bytes_reserve);
31+
String.reserve(bytes_reserve);
32+
}
3133

32-
// TODO: redo this part
33-
std::size_t next_symbol_position{}; // next symbol position in bytes
34-
for (std::size_t i{}; utf8_str[i] != '\0'; ++i)
35-
{
36-
String.push_back(utf8_str[i]);
37-
if (next_symbol_position == i) // if it`s beginning of a symbol
38-
{
39-
std::uint8_t symbol_size{ Get_Size_Of_Symbol(utf8_str[i]) };
40-
Symbols_Info.push_back(SSymbol_Info{ symbol_size, next_symbol_position });
34+
// -----------------------------------------------------------------------------------------------------------------
35+
ustring::ustring(const char* default_string) : ustring(100)
36+
{
37+
Copy_With_Metadata_To_Internal_String(default_string);
38+
}
4139

42-
next_symbol_position += symbol_size;
43-
}
40+
// -----------------------------------------------------------------------------------------------------------------
41+
ustring::ustring(const char* default_string, const std::size_t bytes_reserve) : ustring(bytes_reserve)
42+
{
43+
Copy_With_Metadata_To_Internal_String(default_string);
44+
}
45+
46+
// -----------------------------------------------------------------------------------------------------------------
47+
ustring::ustring(const std::basic_string<char>& default_string) : ustring(default_string.c_str(), default_string.size() * 2)
48+
{ }
49+
50+
// -----------------------------------------------------------------------------------------------------------------
51+
ustring::ustring(const std::basic_string_view<char>& default_string) : ustring(default_string.data(), default_string.size() * 2)
52+
{ }
53+
54+
// -----------------------------------------------------------------------------------------------------------------
55+
ustring::ustring(const ustring& other)
56+
{
57+
String = other.String;
58+
Symbols_Info = other.Symbols_Info;
59+
}
60+
61+
// -----------------------------------------------------------------------------------------------------------------
62+
ustring::ustring(ustring&& other) noexcept
63+
{
64+
String = std::move(other.String);
65+
Symbols_Info = std::move(other.Symbols_Info);
66+
}
67+
68+
// -----------------------------------------------------------------------------------------------------------------
69+
ustring& ustring::operator=(const ustring& other)
70+
{
71+
if (this != &other)
72+
{
73+
String = other.String;
74+
Symbols_Info = other.Symbols_Info;
4475
}
76+
77+
return *this;
78+
}
79+
80+
// -----------------------------------------------------------------------------------------------------------------
81+
ustring& ustring::operator=(ustring&& other) noexcept
82+
{
83+
String = std::move(other.String);
84+
Symbols_Info = std::move(other.Symbols_Info);
85+
86+
return *this;
87+
}
88+
89+
// -----------------------------------------------------------------------------------------------------------------
90+
bool ustring::operator==(const ustring& other) const
91+
{
92+
return (String == other.String);
93+
}
94+
95+
// -----------------------------------------------------------------------------------------------------------------
96+
bool ustring::operator!=(const ustring& other) const
97+
{
98+
return (String != other.String);
4599
}
46100

47101
// -----------------------------------------------------------------------------------------------------------------
@@ -80,6 +134,12 @@ namespace utf8
80134
return String;
81135
}
82136

137+
// -----------------------------------------------------------------------------------------------------------------
138+
const std::basic_string<char>& ustring::get_c_str() const
139+
{
140+
return String;
141+
}
142+
83143
// -----------------------------------------------------------------------------------------------------------------
84144
std::uint8_t ustring::Get_Size_Of_Symbol(std::uint8_t symbol)
85145
{
@@ -111,4 +171,21 @@ namespace utf8
111171
Symbols_Info[i].Symbol_Offset += new_offset;
112172
}
113173
}
174+
175+
// -----------------------------------------------------------------------------------------------------------------
176+
void ustring::Copy_With_Metadata_To_Internal_String(const char* default_string)
177+
{
178+
std::size_t next_symbol_position{}; // next symbol position in bytes
179+
for (std::size_t i{}; default_string[i] != '\0'; ++i)
180+
{
181+
String.push_back(default_string[i]);
182+
if (next_symbol_position == i) // if it`s beginning of a symbol
183+
{
184+
std::uint8_t symbol_size{ Get_Size_Of_Symbol(default_string[i]) };
185+
Symbols_Info.push_back(SSymbol_Info{ symbol_size, next_symbol_position });
186+
187+
next_symbol_position += symbol_size;
188+
}
189+
}
190+
}
114191
}

utf8-string-lib/utf8_string.hpp

+25-2
Original file line numberDiff line numberDiff line change
@@ -5,6 +5,7 @@
55
#include <cstring>
66
#include <cstdint>
77
#include <cstddef>
8+
#include <stdexcept>
89

910
#define ONE_BYTE 0b0000'0000 // first bit 0
1011
#define TWO_BYTE 0b1100'0000 // first three bits 110
@@ -20,15 +21,37 @@ namespace utf8
2021
friend std::istream& operator>>(std::istream& is, ustring& ustr);
2122

2223
public:
23-
constexpr ustring();
24-
explicit ustring(const char* utf8_str);
24+
ustring();
25+
explicit ustring(const std::size_t bytes_reserve);
26+
explicit ustring(const char* default_string);
27+
explicit ustring(const char* default_string, const std::size_t bytes_reserve);
28+
explicit ustring(const std::basic_string<char>& default_string);
29+
explicit ustring(const std::basic_string_view<char>& default_string);
30+
31+
constexpr ~ustring() = default;
32+
33+
explicit ustring(const ustring& other);
34+
explicit ustring(ustring&& other) noexcept;
35+
36+
ustring& operator=(const ustring& other);
37+
ustring& operator=(ustring&& other) noexcept;
38+
39+
bool operator==(const ustring& other) const;
40+
bool operator!=(const ustring& other) const;
41+
42+
// TODO: ...............................................
43+
char& operator[](std::size_t index) = delete;
44+
const char& operator[](std::size_t index) const = delete;
2545

2646
void replace_char(std::basic_string_view<char> new_symbol, std::size_t idx);
47+
2748
std::basic_string<char>& get_str();
49+
[[nodiscard]] const std::basic_string<char>& get_c_str() const;
2850

2951
private:
3052
std::uint8_t Get_Size_Of_Symbol(std::uint8_t symbol);
3153
void Recalculate_Symbols_Offset(std::size_t start_pos, std::ptrdiff_t new_offset);
54+
void Copy_With_Metadata_To_Internal_String(const char* default_string);
3255

3356
private:
3457
struct SSymbol_Info

0 commit comments

Comments
 (0)