forked from ClearFoundry/ClearScript
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathStdString.h
More file actions
247 lines (192 loc) · 7.6 KB
/
StdString.h
File metadata and controls
247 lines (192 loc) · 7.6 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
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
//-----------------------------------------------------------------------------
// StdChar
//-----------------------------------------------------------------------------
using StdChar = char16_t;
static_assert(sizeof(StdChar) == sizeof(uint16_t));
//-----------------------------------------------------------------------------
// StdString
//-----------------------------------------------------------------------------
class StdString final
{
//-------------------------------------------------------------------------
// types
//-------------------------------------------------------------------------
using Value = std::basic_string<StdChar>;
using UTF8Converter = std::wstring_convert<std::codecvt_utf8_utf16<StdChar>, StdChar>;
public:
//-------------------------------------------------------------------------
// constructors
//-------------------------------------------------------------------------
StdString()
{
}
StdString(const StdString& that):
m_Value(that.m_Value)
{
}
StdString(StdString&& that) noexcept:
m_Value(std::move(that.m_Value))
{
}
explicit StdString(const Value& value):
m_Value(value)
{
}
explicit StdString(Value&& value):
m_Value(std::move(value))
{
}
explicit StdString(const StdChar* pValue):
m_Value(EnsureNonNull(pValue))
{
}
StdString(const StdChar* pValue, int32_t length):
StdString(Value(EnsureNonNull(pValue), length))
{
}
explicit StdString(const std::string& value):
m_Value(UTF8Converter().from_bytes(value))
{
}
//-------------------------------------------------------------------------
// assignment
//-------------------------------------------------------------------------
const StdString& operator=(const StdString& that)
{
m_Value = that.m_Value;
return *this;
}
const StdString& operator=(StdString&& that) noexcept
{
m_Value = std::move(that.m_Value);
return *this;
}
const StdString& operator=(const Value& value)
{
m_Value = value;
return *this;
}
const StdString& operator=(Value&& value)
{
m_Value = std::move(value);
return *this;
}
const StdString& operator=(const StdChar* pValue)
{
m_Value = EnsureNonNull(pValue);
return *this;
}
//-------------------------------------------------------------------------
// concatenation
//-------------------------------------------------------------------------
const StdString& operator+=(const StdString& that)
{
m_Value += that.m_Value;
return *this;
}
const StdString& operator+=(const Value& value)
{
m_Value += value;
return *this;
}
const StdString& operator+=(const StdChar* pValue)
{
m_Value += EnsureNonNull(pValue);
return *this;
}
const StdString& operator+=(StdChar value)
{
m_Value += value;
return *this;
}
//-------------------------------------------------------------------------
// comparison
//-------------------------------------------------------------------------
int Compare(const StdString& that) const { return m_Value.compare(that.m_Value); }
int Compare(const Value& value) const { return m_Value.compare(value); }
int Compare(const StdChar* pValue) const { return m_Value.compare(pValue); }
bool operator==(const StdString& that) const { return m_Value == that.m_Value; }
bool operator==(const Value& value) const { return m_Value == value; }
bool operator==(const StdChar* pValue) const { return m_Value == pValue; }
bool operator!=(const StdString& that) const { return m_Value != that.m_Value; }
bool operator!=(const Value& value) const { return m_Value != value; }
bool operator!=(const StdChar* pValue) const { return m_Value != pValue; }
bool operator<(const StdString& that) const { return m_Value < that.m_Value; }
bool operator<(const Value& value) const { return m_Value < value; }
bool operator<(const StdChar* pValue) const { return m_Value < pValue; }
bool operator<=(const StdString& that) const { return m_Value <= that.m_Value; }
bool operator<=(const Value& value) const { return m_Value <= value; }
bool operator<=(const StdChar* pValue) const { return m_Value <= pValue; }
bool operator>(const StdString& that) const { return m_Value > that.m_Value; }
bool operator>(const Value& value) const { return m_Value > value; }
bool operator>(const StdChar* pValue) const { return m_Value > pValue; }
bool operator>=(const StdString& that) const { return m_Value >= that.m_Value; }
bool operator>=(const Value& value) const { return m_Value >= value; }
bool operator>=(const StdChar* pValue) const { return m_Value >= pValue; }
//-------------------------------------------------------------------------
// miscellaneous
//-------------------------------------------------------------------------
int GetLength() const
{
return static_cast<int>(m_Value.length());
}
size_t GetDigest() const;
const StdChar* ToCString() const
{
return m_Value.c_str();
}
std::string ToUTF8() const
{
return UTF8Converter().to_bytes(m_Value);
}
//-------------------------------------------------------------------------
// V8 extensions
//-------------------------------------------------------------------------
StdString(v8::Isolate* pIsolate, v8::Local<v8::Value> hValue):
m_Value(GetValue(pIsolate, hValue))
{
}
explicit StdString(const v8_inspector::StringView& stringView):
m_Value(GetValue(stringView))
{
}
v8::MaybeLocal<v8::String> ToV8String(v8::Isolate* pIsolate, v8::NewStringType type) const
{
return v8::String::NewFromTwoByte(pIsolate, reinterpret_cast<const uint16_t*>(ToCString()), type, GetLength());
}
v8_inspector::StringView GetStringView(size_t index = 0, size_t length = SIZE_MAX) const
{
auto valueLength = m_Value.length();
index = std::min(index, valueLength);
length = std::min(length, valueLength - index);
return v8_inspector::StringView(reinterpret_cast<const uint16_t*>(ToCString() + index), length);
}
private:
//-------------------------------------------------------------------------
// internals
//-------------------------------------------------------------------------
uint32_t GetDigestAsUInt32() const;
uint64_t GetDigestAsUInt64() const;
static Value GetValue(v8::Isolate* pIsolate, v8::Local<v8::Value> hValue)
{
v8::String::Value value(pIsolate, hValue);
return Value(EnsureNonNull(reinterpret_cast<const StdChar*>(*value)), value.length());
}
static Value GetValue(const v8_inspector::StringView& stringView);
static const StdChar* EnsureNonNull(const StdChar* pValue);
Value m_Value;
};
//-----------------------------------------------------------------------------
// string and character literal support
//-----------------------------------------------------------------------------
#define SL(LITERAL) (u ## LITERAL)
//-----------------------------------------------------------------------------
// StdString implementation
//-----------------------------------------------------------------------------
inline const StdChar* StdString::EnsureNonNull(const StdChar* pValue)
{
return (pValue != nullptr) ? pValue : SL("");
}