GigaPasjans
Loading...
Searching...
No Matches
WideString.h
1//Wykonane przez Piotra ChudziƄskiego w dniu 10.04.2025
2
3#pragma once
4
5#include "../DataTypes.h"
6#include <fmt/format.h>
7#include <cstring>
8#include <string>
9
14{
15private:
16 wchar_t *m_Data;
17 int32 m_Length;
18
19public:
23 WideString() : m_Data(nullptr),
24 m_Length(0)
25 {
26 };
27
32 WideString(const std::wstring &data) : m_Data(nullptr),
33 m_Length(0)
34 {
35 Set(const_cast<wchar_t *>(data.data()), static_cast<int32>(data.size()));
36 }
37
42 WideString(const wchar_t *data) : m_Data(nullptr),
43 m_Length(0)
44 {
45 Set(const_cast<wchar_t *>(data), static_cast<int32>(wcslen(data)));
46 }
47
51 WideString(const wchar_t *data, int32 length) : m_Data(nullptr),
52 m_Length(0)
53 {
54 Set(const_cast<wchar_t *>(data), length);
55 }
56
60 WideString(const wchar_t *start, const wchar_t *end) : m_Data(nullptr),
61 m_Length(0)
62 {
63 Set(const_cast<wchar_t *>(start), static_cast<int32>(end - start));
64 }
65
69 explicit WideString(wchar_t c) : m_Data(nullptr),
70 m_Length(0)
71 {
72 Set(&c, 1);
73 }
74
76 {
77 if (m_Data)
78 {
79 free(m_Data);
80 m_Data = nullptr;
81 }
82 }
83
87 WideString(const WideString &str) : m_Data(nullptr),
88 m_Length(0)
89 {
90 if (str.Get())
91 {
92 Set(str.Get(), str.m_Length);
93 }
94 };
95
96 WideString(WideString &&str) noexcept : m_Data(str.m_Data),
97 m_Length(str.m_Length)
98 {
99 str.m_Data = nullptr;
100 str.m_Length = 0;
101 };
102
103 WideString &operator=(const WideString &str)
104 {
105 if (this == &str)
106 return *this;
107 Set(str.Get(), str.m_Length);
108
109 return *this;
110 }
111
112 WideString &operator=(WideString &&str)
113 noexcept
114 {
115 if (this == &str)
116 return *this;
117 Set(str.Get(), str.m_Length);
118
119 free(str.m_Data);
120 str.m_Length = 0;
121 str.m_Data = nullptr;
122
123 return *this;
124 }
125
126 bool operator==(const WideString &str) const
127 {
128 return wcscmp(this->Get(), str.Get()) == 0;
129 };
130
131 WideString operator+(const wchar_t *str) const
132 {
133 int32 newLength = this->GetLength() + static_cast<int32>(wcslen(str)) + 1;
134 wchar_t *data = new wchar_t[newLength];
135
136 memcpy(data, this->Get(), this->GetLength() * sizeof(wchar_t));
137 wcscat(data, str);
138
139 WideString concated(data, newLength);
140
141 delete[] data;
142 return concated;
143 };
144
145 WideString operator+(const WideString &str) const
146 {
147 int32 newLength = this->GetLength() + static_cast<int32>(wcslen(str.Get())) + 1;
148 wchar_t *data = new wchar_t[newLength];
149
150 memcpy(data, this->Get(), this->GetLength() * sizeof(wchar_t));
151 wcscat(data, str.Get());
152
153 WideString concated(data, newLength);
154
155 delete[] data;
156 return concated;
157 };
158
164 void Set(wchar_t *data, int32 length);
165
171 void Allocate(int32 length);
172
177 [[nodiscard]] inline wchar_t *Get() const
178 {
179 return m_Data;
180 }
181
182 [[nodiscard]] inline int32 GetLength() const
183 {
184 return m_Length;
185 }
186
187 [[nodiscard]] bool StartsWith(const WideString &other) const;
188
189 [[nodiscard]] int32 HashCode() const;
190};
Represents a mutable sequence of utf-16 characters, providing various member functions for string man...
Definition WideString.h:14
WideString()
Initializes a new instance of the WideString class.
Definition WideString.h:23
void Set(wchar_t *data, int32 length)
Sets an array of characters to the string.
Definition Strings.cpp:84
wchar_t * Get() const
Gets a raw char sequence.
Definition WideString.h:177
WideString(wchar_t c)
Initializes a new instance of the WideString class.
Definition WideString.h:69
WideString(const WideString &str)
Initializes a new instance of the WideString class.
Definition WideString.h:87
WideString(const wchar_t *data, int32 length)
Initializes a new instance of the WideString class.
Definition WideString.h:51
WideString(const wchar_t *start, const wchar_t *end)
Initializes a new instance of the WideString class.
Definition WideString.h:60
WideString(const std::wstring &data)
Initializes a new instance of the WideString class.
Definition WideString.h:32
WideString(const wchar_t *data)
Initializes a new instance of the WideString class.
Definition WideString.h:42
void Allocate(int32 length)
Use only when string is empty.
Definition Strings.cpp:97