forked from faggotman69/Prometheus
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathUtlString.hpp
More file actions
204 lines (156 loc) · 5.6 KB
/
UtlString.hpp
File metadata and controls
204 lines (156 loc) · 5.6 KB
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
#pragma once
#include <cstdint>
#include <cstring>
#include "UtlMemory.hpp"
class CUtlBinaryBlock
{
public:
CUtlBinaryBlock(int growSize = 0, int initSize = 0);
// NOTE: nInitialLength indicates how much of the buffer starts full
CUtlBinaryBlock(void* pMemory, int nSizeInBytes, int nInitialLength);
CUtlBinaryBlock(const void* pMemory, int nSizeInBytes);
CUtlBinaryBlock(const CUtlBinaryBlock& src);
void Get(void *pValue, int nMaxLen) const;
void Set(const void *pValue, int nLen);
const void *Get() const;
void *Get();
unsigned char& operator[](int i);
const unsigned char& operator[](int i) const;
int Length() const;
void SetLength(int nLength); // Undefined memory will result
bool IsEmpty() const;
void Clear();
void Purge();
bool IsReadOnly() const;
CUtlBinaryBlock &operator=(const CUtlBinaryBlock &src);
// Test for equality
bool operator==(const CUtlBinaryBlock &src) const;
private:
CUtlMemory<unsigned char> m_Memory;
int m_nActualLength;
};
//-----------------------------------------------------------------------------
// class inlines
//-----------------------------------------------------------------------------
inline const void *CUtlBinaryBlock::Get() const
{
return m_Memory.Base();
}
inline void *CUtlBinaryBlock::Get()
{
return m_Memory.Base();
}
inline int CUtlBinaryBlock::Length() const
{
return m_nActualLength;
}
inline unsigned char& CUtlBinaryBlock::operator[](int i)
{
return m_Memory[i];
}
inline const unsigned char& CUtlBinaryBlock::operator[](int i) const
{
return m_Memory[i];
}
inline bool CUtlBinaryBlock::IsReadOnly() const
{
return m_Memory.IsReadOnly();
}
inline bool CUtlBinaryBlock::IsEmpty() const
{
return Length() == 0;
}
inline void CUtlBinaryBlock::Clear()
{
SetLength(0);
}
inline void CUtlBinaryBlock::Purge()
{
SetLength(0);
m_Memory.Purge();
}
//-----------------------------------------------------------------------------
// Simple string class.
// NOTE: This is *not* optimal! Use in tools, but not runtime code
//-----------------------------------------------------------------------------
class CUtlString
{
public:
CUtlString();
CUtlString(const char *pString);
CUtlString(const CUtlString& string);
// Attaches the string to external memory. Useful for avoiding a copy
CUtlString(void* pMemory, int nSizeInBytes, int nInitialLength);
CUtlString(const void* pMemory, int nSizeInBytes);
const char *Get() const;
void Set(const char *pValue);
// Set directly and don't look for a null terminator in pValue.
void SetDirect(const char *pValue, int nChars);
// Converts to c-strings
operator const char*() const;
// for compatibility switching items from UtlSymbol
const char *String() const { return Get(); }
// Returns strlen
int Length() const;
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 *Get();
void Clear();
void Purge();
// Strips the trailing slash
void StripTrailingSlash();
CUtlString &operator=(const CUtlString &src);
CUtlString &operator=(const char *src);
// Test for equality
bool operator==(const CUtlString &src) const;
bool operator==(const char *src) const;
bool operator!=(const CUtlString &src) const { return !operator==(src); }
bool operator!=(const char *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);
CUtlString operator+(int rhs);
int Format(const char *pFormat, ...);
// Take a piece out 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_t nStart = 0, int32_t nEnd = INT32_MAX);
// Grab a substring starting from the left or the right side.
CUtlString Left(int32_t nChars);
CUtlString Right(int32_t nChars);
// Replace all instances of one character with another.
CUtlString Replace(char cFrom, char cTo);
// Calls right through to V_MakeAbsolutePath.
CUtlString AbsPath(const char *pStartingDir = NULL);
// Gets the filename (everything except the path.. c:\a\b\c\somefile.txt -> somefile.txt).
CUtlString UnqualifiedFilename();
// Strips off one directory. Uses V_StripLastDir but strips the last slash also!
CUtlString DirName();
// 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);
private:
CUtlBinaryBlock m_Storage;
};
//-----------------------------------------------------------------------------
// Inline methods
//-----------------------------------------------------------------------------
inline bool CUtlString::IsEmpty() const
{
return Length() == 0;
}
inline int __cdecl CUtlString::SortCaseInsensitive(const CUtlString *pString1, const CUtlString *pString2)
{
return _stricmp(pString1->String(), pString2->String());
}
inline int __cdecl CUtlString::SortCaseSensitive(const CUtlString *pString1, const CUtlString *pString2)
{
return strcmp(pString1->String(), pString2->String());
}