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
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
|
//========= Copyright Valve Corporation, All rights reserved. ============//
//
// Purpose:
//
//=============================================================================
#ifndef UTLSTRING_H
#define UTLSTRING_H
#ifdef _WIN32
#pragma once
#endif
#include "tier1/utlmemory.h"
#include "tier1/strtools.h"
#include "limits.h"
#if defined( OSX )
inline wchar_t *wcsdup(const wchar_t *pString)
{
wchar_t *pMemory;
if (!pString)
return NULL;
size_t len = (wcslen(pString) + 1);
if ((pMemory = (wchar_t *)malloc(len * sizeof(wchar_t))) != NULL)
{
return wcscpy( pMemory, pString );
}
return NULL;
}
inline size_t strnlen(const char *s, size_t n)
{
const char *p = (const char *)memchr(s, 0, n);
return (p ? p - s : n);
}
#endif
//-----------------------------------------------------------------------------
// Simple string class.
// NOTE: This is *not* optimal! Use in tools, but not runtime code
//-----------------------------------------------------------------------------
class CUtlString
{
public:
typedef enum
{
PATTERN_NONE = 0x00000000,
PATTERN_DIRECTORY = 0x00000001
} TUtlStringPattern;
public:
CUtlString();
CUtlString( const char *pString );
CUtlString( const char *pString, int length );
CUtlString( const CUtlString& string );
#ifdef MOVE_CONSTRUCTOR_SUPPORT
// Support moving of CUtlString objects. Long live C++11
// This move constructor will get called when appropriate, such as when
// returning objects from functions, or otherwise copying from temporaries
// which are about to be destroyed. It can also be explicitly invoked with
// std::move().
// Move constructor:
CUtlString( CUtlString&& rhs )
{
// Move the string pointer from the source to this -- be sure to
// zero out the source to avoid double frees.
m_pString = rhs.m_pString;
rhs.m_pString = 0;
}
// Move assignment operator:
CUtlString& operator=( CUtlString&& rhs )
{
// Move the string pointer from the source to this -- be sure to
// zero out the source to avoid double frees.
m_pString = rhs.m_pString;
rhs.m_pString = 0;
return *this;
}
#endif
~CUtlString();
const char *Get( ) const;
void Set( const char *pValue );
operator const char*() const;
// Set directly and don't look for a null terminator in pValue.
// nChars does not include the nul and this will only copy
// at most nChars (even if pValue is longer). If nChars
// is >strlen(pValue) it will copy past the end, don't do it
// Does nothing if pValue == String()
void SetDirect( const char *pValue, int nChars );
// for compatibility switching items from UtlSymbol
const char *String() const { return Get(); }
// Returns strlen
int Length() const;
// IsEmpty() is more efficient than Length() == 0
bool IsEmpty() const;
// Sets the length (used to serialize into the buffer )
// Note: If nLen != 0, then this adds an extra byte for a null-terminator.
void SetLength( int nLen );
char *GetForModify();
void Clear();
void Purge();
// Case Change
void ToLower();
void ToUpper();
void Append( const char *pAddition, int nChars );
void Append( const char *pchAddition );
void Append( const char chAddition ) { char temp[2] = { chAddition, 0 }; Append( temp ); }
// Strips the trailing slash
void StripTrailingSlash();
void FixSlashes( char cSeparator = CORRECT_PATH_SEPARATOR );
// Trim whitespace
void TrimLeft( char cTarget );
void TrimLeft( const char *szTargets = "\t\r\n " );
void TrimRight( char cTarget );
void TrimRight( const char *szTargets = "\t\r\n " );
void Trim( char cTarget );
void Trim( const char *szTargets = "\t\r\n " );
bool IsEqual_CaseSensitive( const char *src ) const;
bool IsEqual_CaseInsensitive( const char *src ) const;
CUtlString &operator=( const CUtlString &src );
CUtlString &operator=( const char *src );
// Test for equality
bool operator==( const CUtlString &src ) const;
bool operator!=( const CUtlString &src ) const { return !operator==( src ); }
CUtlString &operator+=( const CUtlString &rhs );
CUtlString &operator+=( const char *rhs );
CUtlString &operator+=( char c );
CUtlString &operator+=( int rhs );
CUtlString &operator+=( double rhs );
CUtlString operator+( const char *pOther ) const;
CUtlString operator+( const CUtlString &other ) const;
CUtlString operator+( int rhs ) const;
bool MatchesPattern( const CUtlString &Pattern, int nFlags = 0 ) const; // case SENSITIVE, use * for wildcard in pattern string
char operator[]( int i ) const;
#if ! defined(SWIG)
// Don't let SWIG see the PRINTF_FORMAT_STRING attribute or it will complain.
int Format( PRINTF_FORMAT_STRING const char *pFormat, ... ) FMTFUNCTION( 2, 3 );
int FormatV( PRINTF_FORMAT_STRING const char *pFormat, va_list marker );
#else
int Format( const char *pFormat, ... );
int FormatV( const char *pFormat, va_list marker );
#endif
// Defining AltArgumentType_t hints that associative container classes should
// also implement Find/Insert/Remove functions that take const char* params.
typedef const char *AltArgumentType_t;
// Get a copy of part of the string.
// If you only specify nStart, it'll go from nStart to the end.
// You can use negative numbers and it'll wrap around to the start.
CUtlString Slice( int32 nStart=0, int32 nEnd=INT_MAX ) const;
// Get a substring starting from the left or the right side.
CUtlString Left( int32 nChars ) const;
CUtlString Right( int32 nChars ) const;
// Get a string with all instances of one character replaced with another.
CUtlString Replace( char cFrom, char cTo ) const;
// Replace all instances of specified string with another.
CUtlString Replace( const char *pszFrom, const char *pszTo ) const;
// Get this string as an absolute path (calls right through to V_MakeAbsolutePath).
CUtlString AbsPath( const char *pStartingDir=NULL ) const;
// Gets the filename (everything except the path.. c:\a\b\c\somefile.txt -> somefile.txt).
CUtlString UnqualifiedFilename() const;
// Gets a string with one directory removed. Uses V_StripLastDir but strips the last slash also!
CUtlString DirName() const;
// Get a string with the extension removed (with V_StripExtension).
CUtlString StripExtension() const;
// Get a string with the filename removed (uses V_UnqualifiedFileName and also strips the last slash)
CUtlString StripFilename() const;
// Get a string with the base filename (with V_FileBase).
CUtlString GetBaseFilename() const;
// Get a string with the file extension (with V_FileBase).
CUtlString GetExtension() const;
// Works like V_ComposeFileName.
static CUtlString PathJoin( const char *pStr1, const char *pStr2 );
// These can be used for utlvector sorts.
static int __cdecl SortCaseInsensitive( const CUtlString *pString1, const CUtlString *pString2 );
static int __cdecl SortCaseSensitive( const CUtlString *pString1, const CUtlString *pString2 );
// Empty string for those times when you need to return an empty string and
// either don't want to pay the construction cost, or are returning a
// const CUtlString& and cannot just return "".
static const CUtlString &GetEmptyString();
private:
// INTERNALS
// AllocMemory allocates enough space for length characters plus a terminating zero.
// Previous characters are preserved, the buffer is null-terminated, but new characters
// are not touched.
void *AllocMemory( uint32 length );
// If m_pString is not NULL, it points to the start of the string, and the memory allocation.
char *m_pString;
};
// // If these are not defined, CUtlConstString as rhs will auto-convert
// // to const char* and do logical operations on the raw pointers. Ugh.
// inline friend bool operator<( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) > 0; }
// inline friend bool operator==( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) == 0; }
// inline friend bool operator!=( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) != 0; }
inline bool operator==( const char *pString, const CUtlString &utlString )
{
return utlString.IsEqual_CaseSensitive( pString );
}
inline bool operator!=( const char *pString, const CUtlString &utlString )
{
return !utlString.IsEqual_CaseSensitive( pString );
}
inline bool operator==( const CUtlString &utlString, const char *pString )
{
return utlString.IsEqual_CaseSensitive( pString );
}
inline bool operator!=( const CUtlString &utlString, const char *pString )
{
return !utlString.IsEqual_CaseSensitive( pString );
}
//-----------------------------------------------------------------------------
// Inline methods
//-----------------------------------------------------------------------------
inline CUtlString::CUtlString()
: m_pString( NULL )
{
}
inline CUtlString::CUtlString( const char *pString )
: m_pString( NULL )
{
Set( pString );
}
inline CUtlString::CUtlString( const char *pString, int length )
: m_pString( NULL )
{
SetDirect( pString, length );
}
inline CUtlString::CUtlString( const CUtlString& string )
: m_pString( NULL )
{
Set( string.Get() );
}
inline CUtlString::~CUtlString()
{
Purge();
}
inline int CUtlString::Length() const
{
if (m_pString)
{
return V_strlen( m_pString );
}
return 0;
}
inline bool CUtlString::IsEmpty() const
{
return !m_pString || m_pString[0] == 0;
}
inline int __cdecl CUtlString::SortCaseInsensitive( const CUtlString *pString1, const CUtlString *pString2 )
{
return V_stricmp( pString1->String(), pString2->String() );
}
inline int __cdecl CUtlString::SortCaseSensitive( const CUtlString *pString1, const CUtlString *pString2 )
{
return V_strcmp( pString1->String(), pString2->String() );
}
// Converts to c-strings
inline CUtlString::operator const char*() const
{
return Get();
}
//-----------------------------------------------------------------------------
// Purpose: Implementation of low-level string functionality for character types.
//-----------------------------------------------------------------------------
template < typename T >
class StringFuncs
{
public:
static T *Duplicate( const T *pValue );
// Note that this function takes a character count, and does not guarantee null-termination.
static void Copy( T *out_pOut, const T *pIn, int iLengthInChars );
static int Compare( const T *pLhs, const T *pRhs );
static int CaselessCompare( const T *pLhs, const T *pRhs );
static int Length( const T *pValue );
static const T *FindChar( const T *pStr, const T cSearch );
static const T *EmptyString();
static const T *NullDebugString();
};
template < >
class StringFuncs<char>
{
public:
static char *Duplicate( const char *pValue ) { return strdup( pValue ); }
// Note that this function takes a character count, and does not guarantee null-termination.
static void Copy( OUT_CAP(iLengthInChars) char *out_pOut, const char *pIn, int iLengthInChars ) { strncpy( out_pOut, pIn, iLengthInChars ); }
static int Compare( const char *pLhs, const char *pRhs ) { return strcmp( pLhs, pRhs ); }
static int CaselessCompare( const char *pLhs, const char *pRhs ) { return Q_strcasecmp( pLhs, pRhs ); }
static int Length( const char *pValue ) { return (int)strlen( pValue ); }
static const char *FindChar( const char *pStr, const char cSearch ) { return strchr( pStr, cSearch ); }
static const char *EmptyString() { return ""; }
static const char *NullDebugString() { return "(null)"; }
};
template < >
class StringFuncs<wchar_t>
{
public:
static wchar_t *Duplicate( const wchar_t *pValue ) { return wcsdup( pValue ); }
// Note that this function takes a character count, and does not guarantee null-termination.
static void Copy( OUT_CAP(iLengthInChars) wchar_t *out_pOut, const wchar_t *pIn, int iLengthInChars ) { wcsncpy( out_pOut, pIn, iLengthInChars ); }
static int Compare( const wchar_t *pLhs, const wchar_t *pRhs ) { return wcscmp( pLhs, pRhs ); }
static int CaselessCompare( const wchar_t *pLhs, const wchar_t *pRhs ); // no implementation?
static int Length( const wchar_t *pValue ) { return (int)wcslen( pValue ); }
static const wchar_t *FindChar( const wchar_t *pStr, const wchar_t cSearch ) { return wcschr( pStr, cSearch ); }
static const wchar_t *EmptyString() { return L""; }
static const wchar_t *NullDebugString() { return L"(null)"; }
};
//-----------------------------------------------------------------------------
// Dirt-basic auto-release string class. Not intended for manipulation,
// can be stored in a container or forwarded as a functor parameter.
// Note the benefit over CUtlString: sizeof(CUtlConstString) == sizeof(char*).
// Also note: null char* pointers are treated identically to empty strings.
//-----------------------------------------------------------------------------
template < typename T = char >
class CUtlConstStringBase
{
public:
CUtlConstStringBase() : m_pString( NULL ) {}
explicit CUtlConstStringBase( const T *pString ) : m_pString( NULL ) { Set( pString ); }
CUtlConstStringBase( const CUtlConstStringBase& src ) : m_pString( NULL ) { Set( src.m_pString ); }
~CUtlConstStringBase() { Set( NULL ); }
void Set( const T *pValue );
void Clear() { Set( NULL ); }
const T *Get() const { return m_pString ? m_pString : StringFuncs<T>::EmptyString(); }
operator const T*() const { return m_pString ? m_pString : StringFuncs<T>::EmptyString(); }
bool IsEmpty() const { return m_pString == NULL; } // Note: empty strings are never stored by Set
int Compare( const T *rhs ) const;
// Logical ops
bool operator<( const T *rhs ) const { return Compare( rhs ) < 0; }
bool operator==( const T *rhs ) const { return Compare( rhs ) == 0; }
bool operator!=( const T *rhs ) const { return Compare( rhs ) != 0; }
bool operator<( const CUtlConstStringBase &rhs ) const { return Compare( rhs.m_pString ) < 0; }
bool operator==( const CUtlConstStringBase &rhs ) const { return Compare( rhs.m_pString ) == 0; }
bool operator!=( const CUtlConstStringBase &rhs ) const { return Compare( rhs.m_pString ) != 0; }
// If these are not defined, CUtlConstString as rhs will auto-convert
// to const char* and do logical operations on the raw pointers. Ugh.
inline friend bool operator<( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) > 0; }
inline friend bool operator==( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) == 0; }
inline friend bool operator!=( const T *lhs, const CUtlConstStringBase &rhs ) { return rhs.Compare( lhs ) != 0; }
CUtlConstStringBase &operator=( const T *src ) { Set( src ); return *this; }
CUtlConstStringBase &operator=( const CUtlConstStringBase &src ) { Set( src.m_pString ); return *this; }
// Defining AltArgumentType_t is a hint to containers that they should
// implement Find/Insert/Remove functions that take const char* params.
typedef const T *AltArgumentType_t;
protected:
const T *m_pString;
};
template < typename T >
void CUtlConstStringBase<T>::Set( const T *pValue )
{
if ( pValue != m_pString )
{
free( ( void* ) m_pString );
m_pString = pValue && pValue[0] ? StringFuncs<T>::Duplicate( pValue ) : NULL;
}
}
template < typename T >
int CUtlConstStringBase<T>::Compare( const T *rhs ) const
{
// Empty or null RHS?
if ( !rhs || !rhs[0] )
return m_pString ? 1 : 0;
// Empty *this, non-empty RHS?
if ( !m_pString )
return -1;
// Neither empty
return StringFuncs<T>::Compare( m_pString, rhs );
}
typedef CUtlConstStringBase<char> CUtlConstString;
typedef CUtlConstStringBase<wchar_t> CUtlConstWideString;
//-----------------------------------------------------------------------------
// Helper functor objects.
//-----------------------------------------------------------------------------
template < typename T > struct UTLConstStringCaselessStringLessFunctor { bool operator()( const CUtlConstStringBase<T>& a, const char *b ) const { return StringFuncs<T>::CaselessCompare( a.Get(), b ) < 0; } };
template < typename T > struct UTLConstStringCaselessStringEqualFunctor { bool operator()( const CUtlConstStringBase<T>& a, const char *b ) const { return StringFuncs<T>::CaselessCompare( a.Get(), b ) == 0; } };
// Helper function for CUtlMaps with a CUtlString key
inline bool UtlStringLessFunc( const CUtlString &lhs, const CUtlString &rhs ) { return V_strcmp( lhs.Get(), rhs.Get() ) < 0; }
inline bool UtlStringCaseInsensitiveLessFunc( const CUtlString &lhs, const CUtlString &rhs ) { return V_stricmp( lhs.Get(), rhs.Get() ) < 0; }
#endif // UTLSTRING_H
|