-
Notifications
You must be signed in to change notification settings - Fork 12
Expand file tree
/
Copy pathdstring.h
More file actions
226 lines (198 loc) · 6.7 KB
/
dstring.h
File metadata and controls
226 lines (198 loc) · 6.7 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
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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
// Copyright (c) 2014 ipkn.
// Licensed under the MIT license.
#pragma once
#include "dptr.h"
#include <string>
#include <cstring>
#include <iostream>
namespace dumpable
{
template <typename T, typename Traits = std::char_traits<T>>
class dbasic_string : protected dptr<T>
{
protected:
void assign(const T* begin, dumpable::size_t size)
{
if (!size)
{
clear();
return;
}
if (dumpable::detail::dptr_alloc())
{
isPooled_ = true;
size_ = size;
void* buf = dptr<T>::alloc_internal((size+1) * sizeof(T));
Traits::copy((T*)buf, begin, size+1);
}
else
{
isPooled_ = false;
size_ = size;
dptr<T>::operator =(new T[size+1]);
Traits::copy((T*)*this, begin, size+1);
}
}
public:
explicit dbasic_string() : size_(0), isPooled_(false) {}
dbasic_string(const T* str)
{
dumpable::size_t length = Traits::length(str);
assign(str, length);
}
dbasic_string(const std::basic_string<T, Traits>& s)
{
assign(s.c_str(), s.size());
}
dbasic_string(const dbasic_string<T, Traits>& s)
{
assign(s.c_str(), s.size());
}
dbasic_string(dbasic_string<T, Traits>&& s) noexcept
: dptr<T>(std::move(s)), size_(s.size_), isPooled_(s.isPooled_)
{
s.size_ = 0;
s.isPooled_ = false;
}
~dbasic_string()
{
clear();
}
void clear()
{
T* begin = (T*)*this;
if (!isPooled_ && begin)
{
delete[] begin;
}
dptr<T>::operator =(nullptr);
size_ = 0;
isPooled_ = false;
}
T* begin() const noexcept { return (T*)*this; }
T* end() const noexcept { return begin() + size(); }
const T* c_str() const noexcept
{
return data();
}
const T* data() const noexcept
{
if (empty())
return (T*)"\x00\x00\x00\x00";
return (T*)*this;
}
T& operator[](int index) const noexcept { return *(begin() + index); }
dumpable::size_t size() const noexcept { return size_; }
bool empty() const noexcept { return !size_; }
T& front() const noexcept { return *begin(); }
T& back() const noexcept { return *(end()-1); }
public:
dbasic_string<T, Traits>& operator = (const T* str)
{
clear();
dumpable::size_t length = Traits::length(str);
assign(str, length);
return *this;
}
dbasic_string<T, Traits>& operator = (const std::basic_string<T, Traits>& s)
{
clear();
assign(s.c_str(), s.size());
return *this;
}
dbasic_string<T, Traits>& operator = (const dbasic_string<T, Traits>& s)
{
if (&s == this)
return *this;
clear();
assign(s.c_str(), s.size());
return *this;
}
dbasic_string<T, Traits>& operator = (dbasic_string<T, Traits>&& s)
{
if (&s == this)
return *this;
size_ = s.size_;
isPooled_ = s.isPooled_;
dptr<T>::operator =(std::move(s));
s.size_ = 0;
s.isPooled_ = false;
return *this;
}
private:
dumpable::size_t size_;
char isPooled_;
};
typedef dbasic_string<char> dstring;
typedef dbasic_string<wchar_t> dwstring;
template <typename T, typename Traits>
inline bool operator == (const dbasic_string<T, Traits>& a, const T* b)
{
dumpable::size_t length = Traits::length(b);
if (length != a.size())
return false;
return !Traits::compare(b, a.c_str(), a.size()+1);
}
template <typename T, typename Traits>
inline bool operator == (const T* a, const dbasic_string<T, Traits>& b)
{
dumpable::size_t length = Traits::length(a);
if (length != b.size())
return false;
return !Traits::compare(a, b.c_str(), b.size()+1);
}
template <typename T, typename Traits>
inline std::ostream& operator << (std::ostream& os, const dbasic_string<T, Traits>& str)
{
os << str.c_str();
return os;
}
template <typename T, typename Traits>
bool operator == (const dbasic_string<T, Traits>& a, const dbasic_string<T, Traits>& b)
{
if (a.size() != b.size())
return false;
if (a.c_str() == b.c_str())
return true;
return !Traits::compare(a.c_str(), b.c_str(), a.size()+1);
}
template <typename T, typename Traits>
bool operator == (const std::basic_string<T, Traits>& a, const dbasic_string<T, Traits>& b)
{
if (a.size() != b.size())
return false;
return !Traits::compare(a.c_str(), b.c_str(), a.size()+1);
}
template <typename T, typename Traits>
bool operator == (const dbasic_string<T, Traits>& a, const std::basic_string<T, Traits>& b)
{
if (a.size() != b.size())
return false;
return !Traits::compare(a.c_str(), b.c_str(), a.size()+1);
}
template <typename T, typename Traits>
inline bool operator != (const dbasic_string<T, Traits>& a, const dbasic_string<T, Traits>& b)
{
return !(a==b);
}
template <typename T, typename Traits>
inline bool operator != (const dbasic_string<T, Traits>& a, const std::basic_string<T, Traits>& b)
{
return !(a==b);
}
template <typename T, typename Traits>
inline bool operator != (const std::basic_string<T, Traits>& a, const dbasic_string<T, Traits>& b)
{
return !(a==b);
}
template <typename T, typename Traits>
inline bool operator != (const dbasic_string<T, Traits>& a, const T* b)
{
return !(a==b);
}
template <typename T, typename Traits>
inline bool operator != (const T* a, const dbasic_string<T, Traits>& b)
{
return !(a==b);
}
}