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
|
#include "slang-string.h"
#include "text-io.h"
namespace CoreLib
{
namespace Basic
{
_EndLine EndLine;
String StringConcat(const char * lhs, int leftLen, const char * rhs, int rightLen)
{
String res;
res.length = leftLen + rightLen;
res.buffer = new char[res.length + 1];
strcpy_s(res.buffer.Ptr(), res.length + 1, lhs);
strcpy_s(res.buffer + leftLen, res.length + 1 - leftLen, rhs);
return res;
}
String operator+(const char * op1, const String & op2)
{
if(!op2.buffer)
return String(op1);
return StringConcat(op1, (int)strlen(op1), op2.buffer.Ptr(), op2.length);
}
String operator+(const String & op1, const char * op2)
{
if(!op1.buffer)
return String(op2);
return StringConcat(op1.buffer.Ptr(), op1.length, op2, (int)strlen(op2));
}
String operator+(const String & op1, const String & op2)
{
if(!op1.buffer && !op2.buffer)
return String();
else if(!op1.buffer)
return String(op2);
else if(!op2.buffer)
return String(op1);
return StringConcat(op1.buffer.Ptr(), op1.length, op2.buffer.Ptr(), op2.length);
}
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);
}
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;
}
String String::FromWString(const wchar_t * wstr)
{
#ifdef _WIN32
return CoreLib::IO::Encoding::UTF16->ToString((const char*)wstr, (int)(wcslen(wstr) * sizeof(wchar_t)));
#else
return CoreLib::IO::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 CoreLib::IO::Encoding::UTF16->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
#else
return CoreLib::IO::Encoding::UTF32->ToString((const char*)wstr, (int)((wend - wstr) * sizeof(wchar_t)));
#endif
}
String String::FromWChar(const wchar_t ch)
{
#ifdef _WIN32
return CoreLib::IO::Encoding::UTF16->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
#else
return CoreLib::IO::Encoding::UTF32->ToString((const char*)&ch, (int)(sizeof(wchar_t)));
#endif
}
String String::FromUnicodePoint(unsigned int codePoint)
{
char buf[6];
int len = CoreLib::IO::EncodeUnicodePointToUTF8(buf, (int)codePoint);
buf[len] = 0;
return String(buf);
}
const wchar_t * String::ToWString(int * len) const
{
if (!buffer)
{
if (len)
*len = 0;
return L"";
}
else
{
if (wcharBuffer)
{
if (len)
*len = (int)wcslen(wcharBuffer);
return wcharBuffer;
}
List<char> buf;
CoreLib::IO::Encoding::UTF16->GetBytes(buf, *this);
if (len)
*len = buf.Count() / sizeof(wchar_t);
buf.Add(0);
buf.Add(0);
const_cast<String*>(this)->wcharBuffer = (wchar_t*)buf.Buffer();
buf.ReleaseBuffer();
return wcharBuffer;
}
}
String String::PadLeft(char ch, int pLen)
{
StringBuilder sb;
for (int i = 0; i < pLen - this->length; i++)
sb << ch;
for (int i = 0; i < this->length; i++)
sb << buffer[i];
return sb.ProduceString();
}
String String::PadRight(char ch, int pLen)
{
StringBuilder sb;
for (int i = 0; i < this->length; i++)
sb << buffer[i];
for (int i = 0; i < pLen - this->length; i++)
sb << ch;
return sb.ProduceString();
}
}
}
|