My favorites | Sign in
Project Home Downloads Source
Checkout   Browse   Changes    
 
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
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
template <class B>
force_inline
void AString<B>::Insert(int pos, const char *s)
{
Insert(pos, s, strlen__(s));
}

template <class B>
void AString<B>::Cat(int c, int count)
{
tchar *s = B::Insert(GetLength(), count, NULL);
while(count--)
*s++ = c;
}

template <class B>
force_inline
void AString<B>::Cat(const tchar *s)
{
Cat(s, strlen__(s));
}

template <class B>
int AString<B>::Compare(const tchar *b) const
{
const tchar *a = B::Begin();
const tchar *ae = End();
for(;;) {
if(a >= ae)
return *b == 0 ? 0 : -1;
if(*b == 0)
return 1;
int q = cmpval__(*a++) - cmpval__(*b++);
if(q)
return q;
}
}

template <class B>
typename AString<B>::String AString<B>::Mid(int from, int count) const
{
int l = GetLength();
if(from > l) from = l;
if(from < 0) from = 0;
if(count < 0)
count = 0;
if(from + count > l)
count = l - from;
return String(B::Begin() + from, count);
}

template <class B>
int AString<B>::Find(int chr, int from) const
{
ASSERT(from >= 0 && from <= GetLength());
const tchar *e = End();
const tchar *ptr = B::Begin();
for(const tchar *s = ptr + from; s < e; s++)
if(*s == chr)
return (int)(s - ptr);
return -1;
}

template <class B>
int AString<B>::ReverseFind(int chr, int from) const
{
ASSERT(from >= 0 && from <= GetLength());
if(from < GetLength()) {
const tchar *ptr = B::Begin();
for(const tchar *s = ptr + from; s >= ptr; s--)
if(*s == chr)
return (int)(s - ptr);
}
return -1;
}

template <class B>
int AString<B>::ReverseFind(int len, const tchar *s, int from) const
{
ASSERT(from >= 0 && from <= GetLength());
if(from < GetLength()) {
const tchar *ptr = B::Begin();
const tchar *p = ptr + from - len + 1;
len *= sizeof(tchar);
while(p >= ptr) {
if(memcmp(s, p, len) == 0)
return (int)(p - ptr);
p--;
}
}
return -1;
}

template <class B>
void AString<B>::Replace(const tchar *find, int findlen, const tchar *replace, int replacelen)
{
String r;
int i = 0;
const tchar *p = B::Begin();
for(;;) {
int j = Find(findlen, find, i);
if(j < 0)
break;
r.Cat(p + i, j - i);
r.Cat(replace, replacelen);
i = j + findlen;
}
r.Cat(p + i, B::GetCount() - i);
*this = r;
}

template <class B>
int AString<B>::ReverseFind(const tchar *s, int from) const
{
return ReverseFind(strlen__(s), s, from);
}

template <class B>
int AString<B>::ReverseFind(int chr) const
{
return B::GetCount() ? ReverseFind(chr, B::GetCount() - 1) : -1;
}

template <class B>
int AString<B>::Find(int len, const tchar *s, int from) const
{
ASSERT(from >= 0 && from <= GetLength());
const tchar *ptr = B::Begin();
const tchar *p = ptr + from;
int l = GetLength() - len - from;
if(l < 0)
return -1;
const tchar *e = p + l;
len *= sizeof(tchar);
while(p <= e) {
if(memcmp(s, p, len) == 0)
return (int)(p - ptr);
p++;
}
return -1;
}

template <class B>
void AString<B>::Replace(const String& find, const String& replace)
{
Replace(~find, find.GetCount(), ~replace, replace.GetCount());
}

template <class B>
force_inline
void AString<B>::Replace(const tchar *find, const tchar *replace)
{
Replace(find, (int)strlen__(find), replace, (int)strlen__(replace));
}

template <class B>
force_inline
void AString<B>::Replace(const String& find, const tchar *replace)
{
Replace(~find, find.GetCount(), replace, (int)strlen__(replace));
}

template <class B>
force_inline
void AString<B>::Replace(const tchar *find, const String& replace)
{
Replace(find, (int)strlen__(find), ~replace, replace.GetCount());
}

template <class B>
bool AString<B>::StartsWith(const tchar *s, int len) const
{
if(len > GetLength()) return false;
return memcmp(s, B::Begin(), len * sizeof(tchar)) == 0;
}

template <class B>
force_inline
bool AString<B>::StartsWith(const tchar *s) const
{
return StartsWith(s, strlen__(s));
}

template <class B>
bool AString<B>::EndsWith(const tchar *s, int len) const
{
int l = GetLength();
if(len > l) return false;
return memcmp(s, B::Begin() + l - len, len * sizeof(tchar)) == 0;
}

template <class B>
force_inline
bool AString<B>::EndsWith(const tchar *s) const
{
return EndsWith(s, strlen__(s));
}

template <class B>
int AString<B>::Find(const tchar *s, int from) const
{
return Find(strlen__(s), s, from);
}

template <class B>
int AString<B>::FindFirstOf(int len, const tchar *s, int from) const
{
ASSERT(from >= 0 && from <= GetLength());
const tchar *ptr = B::Begin();
const tchar *e = B::End();
const tchar *se = s + len;
if(len == 1) {
tchar c1 = s[0];
for(const tchar *bs = ptr + from; bs < e; bs++) {
if(*bs == c1)
return (int)(bs - ptr);
}
return -1;
}
if(len == 2) {
tchar c1 = s[0];
tchar c2 = s[1];
for(const tchar *bs = ptr + from; bs < e; bs++) {
tchar ch = *bs;
if(ch == c1 || ch == c2)
return (int)(bs - ptr);
}
return -1;
}
if(len == 3) {
tchar c1 = s[0];
tchar c2 = s[1];
tchar c3 = s[2];
for(const tchar *bs = ptr + from; bs < e; bs++) {
tchar ch = *bs;
if(ch == c1 || ch == c2 || ch == c3)
return (int)(bs - ptr);
}
return -1;
}
if(len == 4) {
tchar c1 = s[0];
tchar c2 = s[1];
tchar c3 = s[2];
tchar c4 = s[3];
for(const tchar *bs = ptr + from; bs < e; bs++) {
tchar ch = *bs;
if(ch == c1 || ch == c2 || ch == c3 || ch == c4)
return (int)(bs - ptr);
}
return -1;
}
for(const tchar *bs = ptr + from; bs < e; bs++)
for(const tchar *ss = s; ss < se; ss++)
if(*bs == *ss)
return (int)(bs - ptr);
return -1;
}

template <class B>
int AString<B>::FindFirstOf(const tchar *s, int from) const
{
return FindFirstOf(strlen__(s), s, from);
}

inline int String0::Compare(const String0& s) const
{
#ifdef FAST_STRING_COMPARE
if((chr[KIND] | s.chr[KIND]) == 0) {
#ifdef CPU_64
uint64 a64 = SwapEndian64(q[0]);
uint64 b64 = SwapEndian64(s.q[0]);
if(a64 != b64)
return a64 < b64 ? -1 : 1;
uint32 a32 = SwapEndian32(w[2]);
uint32 b32 = SwapEndian32(s.w[2]);
if(a32 != b32)
return a32 < b32 ? -1 : 1;
#else
uint32 a32 = SwapEndian32(w[0]);
uint32 b32 = SwapEndian32(s.w[0]);
if(a32 != b32)
return a32 < b32 ? -1 : 1;
a32 = SwapEndian32(w[1]);
b32 = SwapEndian32(s.w[1]);
if(a32 != b32)
return a32 < b32 ? -1 : 1;
a32 = SwapEndian32(w[2]);
b32 = SwapEndian32(s.w[2]);
if(a32 != b32)
return a32 < b32 ? -1 : 1;
#endif
uint16 a16 = SwapEndian16(v[6]);
uint16 b16 = SwapEndian16(s.v[6]);
if(a16 != b16)
return a16 < b16 ? -1 : 1;
return 0;
}
#endif
return LCompare(s);
}

force_inline
String& String::operator=(const char *s)
{
AssignLen(s, strlen__(s));
return *this;
}

force_inline
String::String(const char *s)
{
String0::Set(s, strlen__(s));
}

force_inline
void StringBuffer::Strlen()
{
SetLength((int)strlen__(begin));
}

force_inline
void StringBuffer::Cat(const char *s)
{
Cat(s, (int)strlen__(s));
}

Change log

r4569 by cxl on Feb 10, 2012   Diff
Core: String::Replace now supports all 4
signature variants (RM #250)
Go to: 
Project members, sign in to write a code review

Older revisions

r4225 by cxl on Dec 2, 2011   Diff
Core: force_inline, String now has
optimized strlen(literal) situations
r2768 by cxl on Oct 11, 2010   Diff
*Core: String Replace fixed to compile
with GCC
r2765 by cxl on Oct 10, 2010   Diff
Core: String::Replace
All revisions of this file

File info

Size: 6807 bytes, 326 lines

File properties

svn:eol-style
native
Powered by Google Project Hosting