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
260 lines (199 loc) · 7.17 KB
/
Copy pathStdString.h
File metadata and controls
260 lines (199 loc) · 7.17 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
248
249
250
251
252
253
254
255
256
257
258
259
260
// Copyright (c) Microsoft Corporation. All rights reserved.
// Licensed under the MIT license.
#pragma once
#ifdef _M_CEE
//-----------------------------------------------------------------------------
// StringToUniPtr
//-----------------------------------------------------------------------------
class StringToUniPtr
{
public:
explicit StringToUniPtr(String^ gcValue):
m_pValue(V8ProxyHelpers::AllocString(gcValue))
{
}
operator const wchar_t*() const
{
return m_pValue;
}
~StringToUniPtr()
{
V8ProxyHelpers::FreeString(m_pValue);
}
private:
wchar_t* m_pValue;
};
#endif // _M_CEE
//-----------------------------------------------------------------------------
// StdString
//-----------------------------------------------------------------------------
class StdString
{
public:
//-------------------------------------------------------------------------
// constructors
//-------------------------------------------------------------------------
StdString()
{
}
StdString(const StdString& that):
m_Value(that.m_Value)
{
}
StdString(StdString&& that):
m_Value(std::move(that.m_Value))
{
}
explicit StdString(const std::wstring& value):
m_Value(value)
{
}
explicit StdString(std::wstring&& value):
m_Value(std::move(value))
{
}
explicit StdString(const wchar_t* pValue):
m_Value(EnsureNonNull(pValue))
{
}
//-------------------------------------------------------------------------
// assignment
//-------------------------------------------------------------------------
const StdString& operator=(const StdString& that)
{
m_Value = that.m_Value;
return *this;
}
const StdString& operator=(StdString&& that)
{
m_Value = std::move(that.m_Value);
return *this;
}
const StdString& operator=(const std::wstring& value)
{
m_Value = value;
return *this;
}
const StdString& operator=(std::wstring&& value)
{
m_Value = std::move(value);
return *this;
}
const StdString& operator=(const wchar_t* 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 std::wstring& value)
{
m_Value += value;
return *this;
}
const StdString& operator+=(const wchar_t* pValue)
{
m_Value += EnsureNonNull(pValue);
return *this;
}
const StdString& operator+=(wchar_t value)
{
m_Value += value;
return *this;
}
//-------------------------------------------------------------------------
// comparison
//-------------------------------------------------------------------------
int Compare(const StdString& that) const { return m_Value.compare(that.m_Value); }
int Compare(const std::wstring& value) const { return m_Value.compare(value); }
int Compare(const wchar_t* pValue) const { return m_Value.compare(pValue); }
bool operator==(const StdString& that) const { return m_Value == that.m_Value; }
bool operator==(const std::wstring& value) const { return m_Value == value; }
bool operator==(const wchar_t* pValue) const { return m_Value == pValue; }
bool operator!=(const StdString& that) const { return m_Value != that.m_Value; }
bool operator!=(const std::wstring& value) const { return m_Value != value; }
bool operator!=(const wchar_t* pValue) const { return m_Value != pValue; }
bool operator<(const StdString& that) const { return m_Value < that.m_Value; }
bool operator<(const std::wstring& value) const { return m_Value < value; }
bool operator<(const wchar_t* pValue) const { return m_Value < pValue; }
bool operator<=(const StdString& that) const { return m_Value <= that.m_Value; }
bool operator<=(const std::wstring& value) const { return m_Value <= value; }
bool operator<=(const wchar_t* pValue) const { return m_Value <= pValue; }
bool operator>(const StdString& that) const { return m_Value > that.m_Value; }
bool operator>(const std::wstring& value) const { return m_Value > value; }
bool operator>(const wchar_t* pValue) const { return m_Value > pValue; }
bool operator>=(const StdString& that) const { return m_Value >= that.m_Value; }
bool operator>=(const std::wstring& value) const { return m_Value >= value; }
bool operator>=(const wchar_t* pValue) const { return m_Value >= pValue; }
//-------------------------------------------------------------------------
// miscellaneous
//-------------------------------------------------------------------------
int GetLength() const
{
return static_cast<int>(m_Value.length());
}
const wchar_t* ToCString() const
{
return m_Value.c_str();
}
#ifdef _M_CEE
//-------------------------------------------------------------------------
// managed extensions
//-------------------------------------------------------------------------
public:
explicit StdString(String^ gcValue):
m_Value(StringToUniPtr(gcValue), gcValue->Length)
{
}
String^ ToManagedString() const
{
return gcnew String(ToCString(), 0, GetLength());
}
#else // !_M_CEE
//-------------------------------------------------------------------------
// V8 extensions
//-------------------------------------------------------------------------
public:
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) const
{
return v8::String::NewFromTwoByte(pIsolate, reinterpret_cast<const uint16_t*>(ToCString()), v8::NewStringType::kNormal, 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(ToCString() + index, length);
}
private:
static std::wstring GetValue(v8::Isolate* pIsolate, v8::Local<v8::Value> hValue)
{
v8::String::Value value(pIsolate, hValue);
return std::wstring(EnsureNonNull(*value), value.length());
}
static std::wstring GetValue(const v8_inspector::StringView& stringView);
#endif // !_M_CEE
private:
//-------------------------------------------------------------------------
// internals
//-------------------------------------------------------------------------
static const wchar_t* EnsureNonNull(const wchar_t* pValue)
{
return (pValue != nullptr) ? pValue : L"";
}
std::wstring m_Value;
};