-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathString.cpp
More file actions
62 lines (55 loc) · 1.46 KB
/
Copy pathString.cpp
File metadata and controls
62 lines (55 loc) · 1.46 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
#include "String.hpp"
_STR::String(const char *ptr) {
size = std::strlen(ptr);
char bits = 1; // Because number of bits is less than 255, I will use char bcoz 1 byte as it also.
if (size != 0) {
bits = __builtin_clz(size);
if (bits != sizeof(int) * 8) {
bits++;
}
}
data = new char[1 << bits];
if (data == nullptr) {
std::cerr << _k_mem_err;
throw EC::BAD_ALLOC;
}
std::memcpy(data, ptr, size);
}
_STR::String(const String &str): size(str.size) {
data = new char[size];
if (data == nullptr) {
std::cerr << _k_mem_err;
throw EC::BAD_ALLOC;
}
std::memcpy(data, str.data, size);
}
_STR::String(String &&str) noexcept: size(str.size), data(str.data) {
str.data = nullptr;
str.size = 0;
}
String& _STR::operator=(const String &str) {
if (this == &str) return *this;
if (data != nullptr) delete[] data;
size = str.size;
data = new char[size];
if (data == nullptr) {
std::cerr << _k_mem_err;
throw EC::BAD_ALLOC;
}
std::memcpy(data, str.data, size);
return *this;
}
String& _STR::operator=(String &&str) noexcept {
if (this == &str) return *this;
if (data != nullptr) delete[] data;
size = str.size;
data = str.data;
str.size = 0;
str.data = nullptr;
}
inline const char& _STR::operator[](const unsigned int index) const noexcept {
return *(data + index);
}
_STR::~String() {
delete[] data;
}