/
stringex.cpp
404 lines (337 loc) · 13.5 KB
/
stringex.cpp
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
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
//////////////////////////////////////////////////////////////////////
// StringEx.cpp
//
#include "stdafx.h"
#include "StringEx.h"
// Insert - Inserts a sub string into the string
// Returns - Reference to the same string object
// pos - Position to insert at. Extends the string with spaces if needed
// s - Sub string to insert
CStringEx& CStringEx::Insert(int pos, LPCTSTR s)
{
int len = lstrlen(s);
if ( len == 0 )
return *this;
int oldlen = GetLength();
int newlen = oldlen + len;
LPTSTR str;
if ( pos >= oldlen )
{
// insert after end of string
newlen += pos - oldlen ;
str = GetBuffer( newlen );
_tcsnset( str+oldlen, _T(' '), pos-oldlen );
_tcsncpy( str+pos, s, len );
}
else
{
// normal insert
str = GetBuffer( newlen );
memmove( str+pos+len, str+pos, sizeof(_T(' ')) *(oldlen-pos) );
_tcsncpy( str+pos, s, len );
}
ReleaseBuffer( newlen );
return *this;
}
// Insert - Inserts a character into the string
// Returns - Reference to the same string object
// pos - Position to insert at. Extends the string with spaces if needed
// c - Char to insert
CStringEx& CStringEx::Insert(int pos, TCHAR c)
{
TCHAR buf[2];
buf[0] = c;
buf[1] = _T('\0');
return Insert( pos, buf );
}
// Delete - Deletes a segment of the string and resizes it
// Returns - Reference to the same string object
// pos - Position of the string segment to remove
// len - Number of characters to remove
CStringEx& CStringEx::Delete(int pos, int len)
{
int strLen = GetLength();
if( pos >= strLen)
return *this;
if(len < 0 ||len > strLen - pos)
len = strLen - pos;
LPTSTR str = GetBuffer( strLen );
memmove(str+pos, str+pos+len, sizeof(_T(' ')) *(strLen-pos));
ReleaseBuffer( strLen - len );
return *this;
}
// Replace - Replaces a substring with another
// Returns - Reference to the same string object
// pos - Position of the substring
// len - Length of substring to be replaced
// s - New substring
CStringEx& CStringEx::Replace(int pos, int len, LPCTSTR s)
{
Delete(pos, len);
Insert(pos, s);
return *this;
}
// Find - Finds the position of a character in a string
// Returns - A zero-based index
// ch - Character to look for
// startpos - Position to start looking from
int CStringEx::Find( TCHAR ch, int startpos /*= 0*/ ) const
{
// find first single character
LPTSTR lpsz = _tcschr(m_pchData + startpos, (_TUCHAR)ch);
// return -1 if not found and index otherwise
return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
}
// Find - Finds the position of a substring in a string
// Returns - A zero-based index
// lpszSub - Substring to look for
// startpos - Position to start looking from
int CStringEx::Find( LPCTSTR lpszSub, int startpos /*= 0*/ ) const
{
ASSERT(AfxIsValidString(lpszSub, FALSE));
// find first matching substring
LPTSTR lpsz = _tcsstr(m_pchData+startpos, lpszSub);
// return -1 for not found, distance from beginning otherwise
return (lpsz == NULL) ? -1 : (int)(lpsz - m_pchData);
}
// FindNoCase - Case insensitive find
// Returns - A zero-based index
// ch - Char to search for
// startpos - Position to start looking from
int CStringEx::FindNoCase( TCHAR ch, int startpos /*= 0*/ ) const
{
unsigned int locase = Find( tolower( ch ), startpos );
unsigned int upcase = Find( toupper( ch ), startpos );
return locase < upcase ? locase : upcase;
}
// FindNoCase - Case insensitive find
// Returns - A zero-based index
// lpszSub - Substring to search for
// startpos - Position to start looking from
int CStringEx::FindNoCase( LPCTSTR lpszSub, int startpos /*= 0*/ ) const
{
CStringEx sLowerThis = *this;
sLowerThis.MakeLower();
CStringEx sLowerSub = lpszSub;
sLowerSub.MakeLower();
return sLowerThis.Find( sLowerSub, startpos );
}
// FindReplace - Find a substring and replace with another
// Returns - Number of instances replaced
// lpszSub - Substring to look for
// lpszReplaceWith - Substring to replace with
// bGlobal - Flag to indicate whether all occurances
// should be replaced
int CStringEx::FindReplace( LPCTSTR lpszSub, LPCTSTR lpszReplaceWith,
BOOL bGlobal /*= TRUE*/ )
{
int iReplaced = 0;
// find first matching substring
LPTSTR lpsz;
int pos = 0;
int lenSub = lstrlen( lpszSub );
int lenReplaceWith = lstrlen( lpszReplaceWith );
while( (lpsz = _tcsstr(m_pchData + pos, lpszSub)) != NULL )
{
pos = (int)(lpsz - m_pchData);
Replace( pos, lenSub, lpszReplaceWith );
pos += lenReplaceWith;
iReplaced++;
if( !bGlobal ) break;
}
return iReplaced;
}
// FindReplaceNoCase - Find a substring and replace with another
// Does case insensitive search
// Returns - Number of instances replaced
// lpszSub - Substring to look for
// lpszReplaceWith - Substring to replace with
// bGlobal - Flag to indicate whether all occurances
// should be replaced
int CStringEx::FindReplaceNoCase( LPCTSTR lpszSub, LPCTSTR lpszReplaceWith,
BOOL bGlobal /*= TRUE*/ )
{
CStringEx sLowerThis = *this;
sLowerThis.MakeLower();
CStringEx sLowerSub = lpszSub;
sLowerSub.MakeLower();
int iReplaced = 0;
// find first matching substring
LPTSTR lpsz;
int pos = 0, offset = 0;
int lenSub = lstrlen( lpszSub );
int lenReplaceWith = lstrlen( lpszReplaceWith );
while( (lpsz = _tcsstr(sLowerThis.m_pchData + pos, sLowerSub.m_pchData)) != NULL )
{
pos = (int)(lpsz - sLowerThis.m_pchData);
Replace( pos+offset, lenSub, lpszReplaceWith );
offset += lenReplaceWith - lenSub;
pos += lenSub;
iReplaced++;
if( !bGlobal ) break;
}
return iReplaced;
}
// ReverseFind - Searches for the last match of a substring
// Returns - A zero-based index
// lpszSub - Substring to search for
// startpos - Position to start looking from, in reverse dir
int CStringEx::ReverseFind( LPCTSTR lpszSub, int startpos /*= -1*/ ) const
{
int lenSub = lstrlen( lpszSub );
int len = lstrlen( m_pchData );
if(0 < lenSub && 0 < len)
{
if( startpos == -1 || startpos >= len ) startpos = len - 1;
for ( LPTSTR lpszReverse = m_pchData + startpos ;
lpszReverse != m_pchData ; --lpszReverse)
if (_tcsncmp(lpszSub, lpszReverse, lenSub ) == 0)
return (lpszReverse - m_pchData);
}
return -1;
}
// ReverseFindNoCase - Searches for the last match of a substring
// Search is case insensitive
// Returns - A zero-based index
// lpszSub - Character to search for
// startpos - Position to start looking from, in reverse dir
int CStringEx::ReverseFindNoCase(TCHAR ch, int startpos /*= -1*/ ) const
{
TCHAR a[2];
a[0] = ch;
a[1] = 0;
return ReverseFindNoCase( a, startpos );
}
// ReverseFindNoCase - Searches for the last match of a substring
// Search is case insensitive
// Returns - A zero-based index
// lpszSub - Substring to search for
// startpos - Position to start looking from, in reverse dir
int CStringEx::ReverseFindNoCase( LPCTSTR lpszSub, int startpos /*= -1*/ ) const
{
//LPTSTR lpszEnd = m_pchData + lstrlen
int lenSub = lstrlen( lpszSub );
int len = lstrlen( m_pchData );
if(0 < lenSub && 0 < len)
{
if( startpos == -1 || startpos >= len ) startpos = len - 1;
for ( LPTSTR lpszReverse = m_pchData + startpos ;
lpszReverse >= m_pchData ; --lpszReverse)
if (_tcsnicmp(lpszSub, lpszReverse, lenSub ) == 0)
return (lpszReverse - m_pchData);
}
return -1;
}
// GetField - Gets a delimited field
// Returns - A CStringEx object that contains a copy of
// the specified field
// delim - The delimiter string
// fieldnum - The field index - zero is the first
// NOTE - Returns the whole string for field zero
// if delim not found
// Returns empty string if # of delim found
// is less than fieldnum
CStringEx CStringEx::GetField( LPCTSTR delim, int fieldnum)
{
LPTSTR lpsz, lpszRemainder = m_pchData, lpszret;
int retlen, lenDelim = lstrlen( delim );
while( fieldnum-- >= 0 )
{
lpszret = lpszRemainder;
lpsz = _tcsstr(lpszRemainder, delim);
if( lpsz )
{
// We did find the delim
retlen = lpsz - lpszRemainder;
lpszRemainder = lpsz + lenDelim;
}
else
{
retlen = lstrlen( lpszRemainder );
lpszRemainder += retlen; // change to empty string
}
}
return Mid( lpszret - m_pchData, retlen );
}
// GetField - Gets a delimited field
// Returns - A CStringEx object that contains a copy of
// the specified field
// delim - The delimiter char
// fieldnum - The field index - zero is the first
// NOTE - Returns the whole string for field zero
// if delim not found
// Returns empty string if # of delim found
// is less than fieldnum
CStringEx CStringEx::GetField( TCHAR delim, int fieldnum)
{
LPTSTR lpsz, lpszRemainder = m_pchData, lpszret;
int retlen;
while( fieldnum-- >= 0 )
{
lpszret = lpszRemainder;
lpsz = _tcschr(lpszRemainder, (_TUCHAR)delim);
if( lpsz )
{
// We did find the delim
retlen = lpsz - lpszRemainder;
lpszRemainder = lpsz + 1;
}
else
{
retlen = lstrlen( lpszRemainder );
lpszRemainder += retlen; // change to empty string
}
}
return Mid( lpszret - m_pchData, retlen );
}
// GetFieldCount - Get number of fields in a string
// Returns - The number of fields
// Is always greater than zero
// delim - The delimiter character
int CStringEx::GetFieldCount( TCHAR delim )
{
TCHAR a[2];
a[0] = delim;
a[1] = 0;
return GetFieldCount( a );
}
// GetFieldCount - Get number of fields in a string
// Returns - The number of fields
// Is always greater than zero
// delim - The delimiter string
int CStringEx::GetFieldCount( LPCTSTR delim )
{
LPTSTR lpsz, lpszRemainder = m_pchData;
int lenDelim = lstrlen( delim );
int iCount = 1;
while( (lpsz = _tcsstr(lpszRemainder, delim)) != NULL )
{
lpszRemainder = lpsz + lenDelim;
iCount++;
}
return iCount;
}
// GetDelimitedField - Finds a field delimited on both ends
// Returns - A CStringEx object that contains a copy of
// the specified field
// delimStart - Delimiter at the start of the field
// delimEnd - Delimiter at the end of the field
CStringEx CStringEx::GetDelimitedField( LPCTSTR delimStart, LPCTSTR delimEnd, int fieldnum /*= 0*/)
{
LPTSTR lpsz, lpszEnd, lpszRet, lpszRemainder = m_pchData ;
int lenDelimStart = lstrlen( delimStart ), lenDelimEnd = lstrlen( delimEnd );
while( fieldnum-- >= 0 )
{
lpsz = _tcsstr(lpszRemainder, delimStart);
if( lpsz )
{
// We did find the Start delim
lpszRet = lpszRemainder = lpsz + lenDelimStart;
lpszEnd = _tcsstr(lpszRemainder, delimEnd);
if( lpszEnd == NULL ) return"";
lpszRemainder = lpsz + lenDelimEnd;
}
else return "";
}
return Mid( lpszRet - m_pchData, lpszEnd - lpszRet );
}