-
Notifications
You must be signed in to change notification settings - Fork 1
/
E16_String.h
129 lines (110 loc) · 2.94 KB
/
E16_String.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
124
125
126
127
128
129
#ifndef E16_String_H
#define E16_String_H
#include <memory>
using std::allocator;
#include <utility>
using std::pair;
#include <algorithm>
using std::for_each;
#include <iostream>
using std::ostream;
class String{
friend ostream& operator<<(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():first(nullptr), last(nullptr) {}
String(const char*);
String(const String&);
String& operator=(const String&);
~String();
char* begin() const { return first; }
char* end() const { return last; }
size_t size() const { return last - first; }
void push_back(const char s);
const char* c_str() const { return first; }
private:
static allocator<char> alloc;
char* first;
char* last;
private:
// void chk_n_allocate();
void free();
// void reallocate();
pair<char*, char*> alloc_n_copy(const char* b, const char* e);
void alloc_n_move(const char* b, const char* e);
};
allocator<char> String::alloc = allocator<char>();
//ostream& operator<<(ostream& os, const String& s);
bool operator==(const String& lhs, const String& rhs);
bool operator!=(const String& lhs, const String& rhs);
void String::alloc_n_move(const char* b, const char* e){
auto newdata = alloc.allocate(e-b);
auto dest = newdata;
// for_each(first, last, [dest](char s){ alloc.construct(dest++, s) });
for( ; first != last; ++first){
alloc.construct(dest++, *first);
}
free();
}
pair<char*, char*> String::alloc_n_copy(const char* b, const char* e){
auto newdata = alloc.allocate(e-b);
auto dest = std::uninitialized_copy(b, e, newdata);
return {newdata, dest};
}
void String::free(){
if(first){
for_each(first, last, [this](char& s) { alloc.destroy(&s); });
alloc.deallocate(first, size());
}
}
String::String(const char* s){
auto start = s;
while(*s != '\0'){
++s;
}
auto ret = alloc_n_copy(start, s);
first = ret.first;
last = ret.second;
}
String& String::operator=(const String& s){
auto ret = alloc_n_copy(s.first, s.last);
free();
first = ret.first;
last = ret.second;
return *this;
}
String::~String(){
free();
}
void String::push_back(const char c){
*last++ = c;
}
ostream& operator<<(ostream& os, const String& s){
char* c = const_cast<char*>(s.c_str());
while(*c){
os << *c++;
}
return os;
}
//bool operator==(const String& lhs, const String& rhs){
// char* lc = const_cast<char*>(lhs.c_str());
// char* rc = const_cast<char*>(rhs.c_str());
// if(lhs.size() != rhs.size()) return false;
// while(*lc){
// if(*lc == *rc){
// ++lc;
// ++rc;
// } else {
// return false;
// }
// }
// return true;
//}
bool operator==(const String& lhs, const String& rhs){
return (lhs.size() == rhs.size()) && std::equal(lhs.begin(), lhs.end(), rhs.begin());
}
bool operator!=(const String& lhs, const String& rhs){
return !(lhs == rhs);
}
#endif