/
SString.h
205 lines (175 loc) · 6.85 KB
/
SString.h
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
/*****************************************************************************
*
* PROJECT: Multi Theft Auto v1.0
* LICENSE: See LICENSE in the top level directory
* FILE: SString.h
* PURPOSE:
* DEVELOPERS: ccw <chris@codewave.co.uk>
* Alberto Alonso <rydencillo@gmail.com>
*
* Multi Theft Auto is available from http://www.multitheftauto.com/
*
*****************************************************************************/
#pragma once
//
// class SString
//
// Adds some functionality to the std::string class
//
#include <stdarg.h>
#ifdef WIN32
#ifndef va_copy
#define va_copy(dest, orig) (dest) = (orig)
#endif
#endif
class SString : public std::string
{
public:
// Constructors
SString ( )
: std::string ()
{ }
SString ( const char* szText )
: std::string ( szText ? szText : "" )
{ }
explicit SString ( const char* szFormat, ... )
: std::string ()
{
if ( szFormat )
{
va_list vl;
va_start ( vl, szFormat );
vFormat ( szFormat, vl );
va_end ( vl );
}
}
SString ( const std::string& strText )
: std::string ( strText )
{ }
SString& Format ( const char* szFormat, ... )
{
va_list vl;
va_start ( vl, szFormat );
SString& str = vFormat ( szFormat, vl );
va_end ( vl );
return str;
}
SString& vFormat ( const char* szFormat, va_list vl );
void OnFormatException ( const char* szFormat );
void OnInvalidParameter ( const char* szFormat );
// Access
char& operator[]( int iOffset )
{
return std::string::operator[]( iOffset );
}
// Operators
SString operator+( const char* other ) const
{
return std::string ( *this ) + other;
}
SString operator+( const std::string& other ) const
{
return std::string ( *this ) + other;
}
SString operator+( const SString& other ) const
{
return std::string ( *this ) + other;
}
// Assignment
operator const char*() const { return c_str (); } // Auto assign to const char* without using c_str()
const char* operator*( void ) const
{
return c_str ();
}
// Functions
void Split ( const SString& strDelim, std::vector < SString >& outResult, unsigned int uiMaxAmount = 0, unsigned int uiMinAmount = 0 ) const;
bool Split ( const SString& strDelim, SString* pstrLeft, SString* pstrRight, int iIndex = 1 ) const;
SString SplitLeft ( const SString& strDelim, SString* pstrRight = NULL, int iIndex = 1 ) const;
SString SplitRight ( const SString& strDelim, SString* pstrLeft = NULL, int iIndex = 1 ) const;
SString Replace ( const char* szOld, const char* szNew, bool bSearchJustReplaced = false ) const;
SString ReplaceI ( const char* szOld, const char* szNew, bool bSearchJustReplaced = false ) const;
SString TrimStart ( const char* szOld ) const;
SString TrimEnd ( const char* szOld ) const;
SString ToLower ( void ) const;
SString ToUpper ( void ) const;
SString ConformLineEndings ( void ) const;
bool Contains ( const SString& strOther ) const;
bool ContainsI ( const SString& strOther ) const;
bool CompareI ( const SString& strOther ) const;
bool EqualsI ( const SString& strOther ) const { return CompareI( strOther ); }
SString SubStr ( int iPos, int iCount = 0x3fffffff ) const;
SString Left ( int iCount ) const;
SString Right ( int iCount ) const;
bool EndsWith ( const SString& strOther ) const;
bool EndsWithI ( const SString& strOther ) const;
bool BeginsWith ( const SString& strOther ) const;
bool BeginsWithI ( const SString& strOther ) const;
static SString Join ( const SString& strDelim, const std::vector < SString >& parts, int iFirst = 0, int iCount = 0x3fffffff );
void AssignLeft ( const char* szOther, uint uiMaxLength );
};
class SStringX : public SString
{
public:
SStringX ( const char* szText )
: SString ( std::string ( szText ? szText : "" ) )
{ }
SStringX ( const char* szText, uint uiLength )
: SString ( std::string ( szText ? szText : "", uiLength ) )
{ }
};
//
// SCharStringRef
//
// String reference - Used for direct access to Lua strings
//
struct SCharStringRef
{
SCharStringRef ( void ) : pData ( NULL ), uiSize ( 0 ) {}
char* pData;
size_t uiSize;
};
//
// Faster type of SString::Split
// Uses pointers to a big buffer rather than an array of strings
//
template < class STRING_TYPE, class CHAR_TYPE >
class TSplitString : public std::vector < const CHAR_TYPE* >
{
public:
TSplitString ( void ) {}
TSplitString ( const STRING_TYPE& strInput, const STRING_TYPE& strDelim, unsigned int uiMaxAmount = 0, unsigned int uiMinAmount = 0 )
{
Split ( strInput, strDelim, uiMaxAmount, uiMinAmount );
}
void Split ( const STRING_TYPE& strInput, const STRING_TYPE& strDelim, unsigned int uiMaxAmount = 0, unsigned int uiMinAmount = 0 )
{
// Copy string to buffer
uint iInputLength = strInput.length ();
buffer.resize ( iInputLength + 1 );
memcpy ( &buffer[0], &strInput[0], ( iInputLength + 1 ) * sizeof ( CHAR_TYPE ) );
// Prime result list
this->clear ();
this->reserve ( 16U < uiMaxAmount ? 16U : uiMaxAmount );
// Split into pointers
size_t ulCurrentPoint = 0;
while ( true )
{
size_t ulPos = strInput.find ( strDelim, ulCurrentPoint );
if ( ulPos == STRING_TYPE::npos || ( uiMaxAmount > 0 && uiMaxAmount <= this->size () + 1 ) )
{
if ( ulCurrentPoint <= strInput.length () )
push_back ( &buffer[ ulCurrentPoint ] );
break;
}
push_back ( &buffer[ ulCurrentPoint ] );
buffer[ ulPos ] = 0;
ulCurrentPoint = ulPos + strDelim.length ();
}
while ( this->size () < uiMinAmount )
push_back ( &buffer[ iInputLength ] );
}
protected:
std::vector < CHAR_TYPE > buffer;
};
typedef TSplitString < std::string, char > CSplitString;
typedef TSplitString < std::wstring, wchar_t > CSplitStringW;