-
Notifications
You must be signed in to change notification settings - Fork 5
/
Copy pathString_ex16.h
123 lines (107 loc) · 2.71 KB
/
String_ex16.h
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
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
#ifndef STRING_H_
#define STRING_H_
#include <memory>
#include <algorithm>
#include <cstring>
#include <iostream>
class String
{
friend std::ostream &operator<<(std::ostream &os, const String &s);
friend bool operator==(const String lhs, const String rhs);
friend bool operator!=(const String lhs, const String rhs);
public:
String();
String(const char*);
String(const String&);
String(String&&) noexcept;
String& operator=(const String&);
String& operator=(String&&) noexcept;
char *begin() const { return elements; }
char *end() const { return first_free; }
~String();
private:
std::pair<char*, char*> alloc_n_copy(const char*, const char*);
void free();
std::allocator<char> alloc;
char *elements;
char *first_free;
};
std::pair<char*, char*> String::alloc_n_copy(const char *begin, const char *end)
{
char *p = alloc.allocate(end - begin);
return{p, std::uninitialized_copy(begin, end, p)};
}
String::String(const char* cp)
{
size_t n = strlen(cp);
auto newstr = alloc_n_copy(cp, cp + n);
elements = newstr.first;
first_free = newstr.second;
}
String::String()
{
String("");
}
String::String(const String &rhs)
{
auto newstr = alloc_n_copy(rhs.begin(), rhs.end());
elements = newstr.first;
first_free = newstr.second;
std::cout << "String(const String &rhs)" << std::endl;
}
String::String(String &&s) noexcept : alloc(std::move(s.alloc)), elements(std::move(s.elements)), first_free(std::move(s.first_free))
{
std::cout << "String::String(String &&s) noexcept" << std::endl;
s.elements = s.first_free = nullptr;
}
void String::free()
{
if(elements)
{
std::for_each(elements, first_free, [this](char cp){ alloc.destroy(&cp); });
alloc.deallocate(elements, first_free - elements);
}
}
String& String::operator=(const String& rhs)
{
auto newstr = alloc_n_copy(rhs.begin(), rhs.end());
free();
elements = newstr.first;
first_free = newstr.second;
std::cout << "String& operator=(const String& rhs)" << std::endl;
return *this;
}
String& String::operator=(String &&rhs) noexcept
{
std::cout << "String& String::operator=(String &&rhs) noexcept" << std::endl;
if(&rhs != this)
{
free();
alloc = std::move(rhs.alloc);
elements = std::move(rhs.elements);
first_free = std::move(rhs.first_free);
rhs.elements = rhs.first_free = nullptr;
}
return *this;
}
String::~String()
{
free();
}
std::ostream &operator<<(std::ostream &os, const String &s)
{
for(auto iter = s.elements; iter != s.first_free; ++iter)
{
os << *iter ;
}
return os;
}
bool operator==(const String lhs, const String rhs)
{
return (lhs.first_free - lhs.elements) == (rhs.first_free - rhs.elements) && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
bool operator!=(const String lhs, const String rhs)
{
return !(lhs == rhs);
}
#endif