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
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
|
#include "slang-string.h"
#include "text-io.h"
namespace Slang
{
// OSString
OSString::OSString()
: beginData(0)
, endData(0)
{}
OSString::OSString(wchar_t* begin, wchar_t* end)
: beginData(begin)
, endData(end)
{}
OSString::~OSString()
{
if (beginData)
{
delete[] beginData;
}
}
static const wchar_t kEmptyOSString[] = { 0 };
wchar_t const* OSString::begin() const
{
return beginData ? beginData : kEmptyOSString;
}
wchar_t const* OSString::end() const
{
return endData ? endData : kEmptyOSString;
}
// StringSlice
StringSlice::StringSlice()
: representation(0)
, beginIndex(0)
, endIndex(0)
{}
StringSlice::StringSlice(String const& str, UInt beginIndex, UInt endIndex)
: representation(str.buffer)
, beginIndex(beginIndex)
, endIndex(endIndex)
{}
//
_EndLine EndLine;
String operator+(const char * op1, const String & op2)
{
String result(op1);
result.append(op2);
return result;
}
String operator+(const String & op1, const char * op2)
{
String result(op1);
result.append(op2);
return result;
}
String operator+(const String & op1, const String & op2)
{
String result(op1);
result.append(op2);
return result;
}
int StringToInt(const String & str, int radix)
{
if (str.StartsWith("0x"))
return (int)strtoll(str.Buffer(), NULL, 16);
else
return (int)strtoll(str.Buffer(), NULL, radix);
}
unsigned int StringToUInt(const String & str, int radix)
{
if (str.StartsWith("0x"))
return (unsigned int)strtoull(str.Buffer(), NULL, 16);
else
return (unsigned int)strtoull(str.Buffer(), NULL, radix);
}
double StringToDouble(const String & str)
{
return (double)strtod(str.Buffer(), NULL);
}
float StringToFloat(const String & str)
{
return strtof(str.Buffer(), NULL);
}
#if 0
String String::ReplaceAll(String src, String dst) const
{
String rs = *this;
int index = 0;
int srcLen = src.length;
int len = rs.length;
while ((index = rs.IndexOf(src, index)) != -1)
{
rs = rs.SubString(0, index) + dst + rs.SubString(index + srcLen, len - index - srcLen);
len = rs.length;
}
return rs;
}
#endif
String String::FromWString(const wchar_t * wstr)
{
#ifdef _WIN32
return Slang::Encoding::UTF16->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
#else
return Slang::Encoding::UTF32->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
#endif
}
String String::FromWString(const wchar_t * wstr, const wchar_t * wend)
{
#ifdef _WIN32
return Slang::Encoding::UTF16->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
#else
return Slang::Encoding::UTF32->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
#endif
}
String String::FromWChar(const wchar_t ch)
{
#ifdef _WIN32
return Slang::Encoding::UTF16->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
#else
return Slang::Encoding::UTF32->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
#endif
}
String String::FromUnicodePoint(unsigned int codePoint)
{
char buf[6];
int len = Slang::EncodeUnicodePointToUTF8(buf, (int)codePoint);
buf[len] = 0;
return String(buf);
}
OSString String::ToWString(UInt* outLength) const
{
if (!buffer)
{
return OSString();
}
else
{
List<char> buf;
Slang::Encoding::UTF16->GetBytes(buf, *this);
auto length = buf.Count() / sizeof(wchar_t);
if (outLength)
*outLength = length;
buf.Add(0);
buf.Add(0);
wchar_t* beginData = (wchar_t*)buf.Buffer();
wchar_t* endData = beginData + length;
buf.ReleaseBuffer();
return OSString(beginData, endData);
}
}
//
void String::ensureUniqueStorageWithCapacity(UInt requiredCapacity)
{
if (buffer && buffer->isUniquelyReferenced() && buffer->capacity >= requiredCapacity)
return;
UInt newCapacity = buffer ? 2*buffer->capacity : 16;
if (newCapacity < requiredCapacity)
{
newCapacity = requiredCapacity;
}
UInt length = getLength();
StringRepresentation* newRepresentation = StringRepresentation::createWithCapacityAndLength(newCapacity, length);
if (buffer)
{
memcpy(newRepresentation->getData(), buffer->getData(), length + 1);
}
buffer = newRepresentation;
}
char* String::prepareForAppend(UInt count)
{
auto oldLength = getLength();
auto newLength = oldLength + count;
ensureUniqueStorageWithCapacity(newLength);
return getData() + oldLength;
}
void String::append(const char* textBegin, char const* textEnd)
{
auto oldLength = getLength();
auto textLength = textEnd - textBegin;
auto newLength = oldLength + textLength;
ensureUniqueStorageWithCapacity(newLength);
memcpy(getData() + oldLength, textBegin, textLength);
getData()[newLength] = 0;
buffer->length = newLength;
}
void String::append(char const* str)
{
if (str)
{
append(str, str + strlen(str));
}
}
void String::append(char chr)
{
append(&chr, &chr + 1);
}
void String::append(String const& str)
{
if (!buffer)
{
buffer = str.buffer;
return;
}
append(str.begin(), str.end());
}
void String::append(StringSlice const& slice)
{
append(slice.begin(), slice.end());
}
void String::append(int value, int radix)
{
enum { kCount = 33 };
char* data = prepareForAppend(kCount);
auto count = IntToAscii(data, value, radix);
ReverseInternalAscii(data, count);
buffer->length += count;
}
void String::append(unsigned int value, int radix)
{
enum { kCount = 33 };
char* data = prepareForAppend(kCount);
auto count = IntToAscii(data, value, radix);
ReverseInternalAscii(data, count);
buffer->length += count;
}
void String::append(long long value, int radix)
{
enum { kCount = 65 };
char* data = prepareForAppend(kCount);
auto count = IntToAscii(data, value, radix);
ReverseInternalAscii(data, count);
buffer->length += count;
}
void String::append(float val, const char * format)
{
enum { kCount = 128 };
char* data = prepareForAppend(kCount);
sprintf_s(data, kCount, format, val);
buffer->length += strnlen_s(data, kCount);
}
void String::append(double val, const char * format)
{
enum { kCount = 128 };
char* data = prepareForAppend(kCount);
sprintf_s(data, kCount, format, val);
buffer->length += strnlen_s(data, kCount);
}
}
|