OpenBarnyard
 
Loading...
Searching...
No Matches
T2String8.cpp
Go to the documentation of this file.
1#include "ToshiPCH.h"
2#include "T2String8.h"
3#include "T2CharTraits.h"
4
5//-----------------------------------------------------------------------------
6// Enables memory debugging.
7// Note: Should be the last include!
8//-----------------------------------------------------------------------------
10
12
14
15TINT T2String8::FormatV( TCHAR* a_pcString, const TCHAR* a_pcFormat, va_list args )
16{
17 TINT iResult = vsprintf( a_pcString, a_pcFormat, args );
18 TASSERT( iResult != -1, "PS2/GC/X360 do not correctly support _vsnprintf, this code will cause memory to be clobbered on those platforms! Increase the size of the destination string to avoid this problem" );
19 return iResult;
20}
21
22TINT T2String8::FormatV( TCHAR* a_pcString, TINT size, const TCHAR* a_pcFormat, va_list args )
23{
24 TINT iResult = _vsnprintf( a_pcString, size, a_pcFormat, args );
25 TASSERT( iResult != -1, "PS2/GC/X360 do not correctly support _vsnprintf, this code will cause memory to be clobbered on those platforms! Increase the size of the destination string to avoid this problem" );
26 a_pcString[ size - 1 ] = '\0';
27 return iResult;
28}
29
30TINT T2String8::Format( TCHAR* a_pcString, const TCHAR* a_pcFormat, ... )
31{
32 va_list args;
33
34 va_start( args, a_pcFormat );
35 TINT iResult = FormatV( a_pcString, a_pcFormat, args );
36 va_end( args );
37
38 return iResult;
39}
40
41TINT T2String8::Format( TCHAR* a_pcString, TINT size, const TCHAR* a_pcFormat, ... )
42{
43 va_list args;
44
45 va_start( args, a_pcFormat );
46 TINT iResult = FormatV( a_pcString, size, a_pcFormat, args );
47 va_end( args );
48
49 return iResult;
50}
51
52TINT T2String8::Compare( const TCHAR* str1, const TCHAR* str2, TSIZE size )
53{
54 if ( size != -1 )
55 return strncmp( str1, str2, size );
56
57 return strcmp( str1, str2 );
58}
59
60TINT T2String8::CompareNoCase( const TCHAR* str1, const TCHAR* str2, TSIZE size /*= -1*/ )
61{
62 if ( size != -1 )
63 return _strnicmp( str1, str2, size );
64
65 return _stricmp( str1, str2 );
66}
67
68TCHAR* T2String8::Copy( TCHAR* dst, const TCHAR* src, TSIZE size /*= -1*/ )
69{
70 if ( size != -1 )
71 return strncpy( dst, src, size );
72
73 return strcpy( dst, src );
74}
75
76TCHAR* T2String8::Concat( TCHAR* dst, const TCHAR* src, TSIZE size /*= -1*/ )
77{
78 if ( size != -1 )
79 return strncat( dst, src, size );
80
81 return strcat( dst, src );
82}
83
84TCHAR* T2String8::CopySafe( TCHAR* dst, const TCHAR* src, TSIZE size )
85{
86 TSIZE srcLen = Length( src );
87 size = TMath::Min( size - 1, srcLen );
88 strncpy( dst, src, size );
89 dst[ size ] = '\0';
90 return dst;
91}
92
93const TCHAR* T2String8::FindChar( const TCHAR* str, TCHAR character )
94{
95 while ( TTRUE )
96 {
97 if ( *str == '\0' ) return TNULL;
98 if ( *str == character ) return str;
99 str++;
100 }
101
102 return TNULL;
103}
104
106{
107 while ( TTRUE )
108 {
109 if ( *str == '\0' ) return TNULL;
110 if ( *str == character ) return str;
111 str++;
112 }
113
114 return TNULL;
115}
116
117const TCHAR* T2String8::FindString( const TCHAR* str, const TCHAR* substr )
118{
119 return strstr( str, substr );
120}
121
122TCHAR* T2String8::FindString( TCHAR* str, const TCHAR* substr )
123{
124 return strstr( str, substr );
125}
126
128{
129 if ( str != TNULL ) return strlen( str );
130 return -1;
131}
132
134{
135 while ( *str != '\0' && T2Char8::IsLowerCase( *str ) )
136 str++;
137
138 return *str == '\0';
139}
140
142{
143 while ( *str != '\0' && T2Char8::IsUpperCase( *str ) )
144 str++;
145
146 return *str == '\0';
147}
148
150{
151 while ( *str )
152 {
153 *str = T2Char8::ToLowerCase( *str );
154 str++;
155 }
156}
157
159{
160 while ( *str )
161 {
162 *str = T2Char8::ToUpperCase( *str );
163 str++;
164 }
165}
166
167void T2String8::IntToString( TINT value, TCHAR* dst, TINT radix )
168{
169 _itoa( value, dst, radix );
170}
171
172void T2String8::IntToString( TINT value, TCHAR* dst, TINT unused, TINT radix )
173{
174 _itoa( value, dst, radix );
175}
176
178{
179 return atoi( src );
180}
181
183{
184 return TFLOAT( atof( src ) );
185}
186
188{
189 TCHAR* pszCursor = str;
190 while ( T2Char8::IsSpace( *pszCursor ) ) pszCursor++;
191
192 return pszCursor;
193}
194
196{
197 const TCHAR* pszCursor = str;
198 while ( T2Char8::IsSpace( *pszCursor ) ) pszCursor++;
199
200 return pszCursor;
201}
202
203TCHAR* T2String8::CreateCopy( const TCHAR* a_pcString, TSIZE a_uiSize /*= -1 */ )
204{
205 TSIZE uiSize = ( a_uiSize == -1 ) ? T2String8::Length( a_pcString ) : a_uiSize;
206
207 TCHAR* pcBuffer = new TCHAR[ uiSize + 1 ];
208 TUtil::MemCopy( pcBuffer, a_pcString, uiSize * sizeof( TCHAR ) );
209 pcBuffer[ uiSize ] = '\0';
210
211 return pcBuffer;
212}
213
#define TASSERT(X,...)
Definition Defines.h:138
#define TOSHI_NAMESPACE_START
Definition Defines.h:47
#define TOSHI_NAMESPACE_END
Definition Defines.h:50
size_t TSIZE
Definition Typedefs.h:9
char TCHAR
Definition Typedefs.h:20
float TFLOAT
Definition Typedefs.h:4
#define TNULL
Definition Typedefs.h:23
int TINT
Definition Typedefs.h:7
#define TTRUE
Definition Typedefs.h:25
bool TBOOL
Definition Typedefs.h:6
TFORCEINLINE const T & Min(const T &a, const T &b)
static CharType ToLowerCase(TINT a_cChar)
static CharType ToUpperCase(TINT a_cChar)
static TBOOL IsUpperCase(TINT a_cChar)
static TBOOL IsSpace(TINT a_cChar)
static TBOOL IsLowerCase(TINT a_cChar)
static TCHAR * FindChar(TCHAR *str, TCHAR character)
static TCHAR * CreateCopy(const TCHAR *a_pcString, TSIZE a_uiSize=-1)
static TSIZE Length(const TCHAR *str)
static TCHAR * SkipSpaces(TCHAR *str)
static void ToUpperCase(TCHAR *str)
static TCHAR ms_aScratchMem[SCRATCH_MEM_SIZE]
Definition T2String8.h:61
static TBOOL IsLowerCase(const TCHAR *str)
static TINT FormatV(TCHAR *a_pcString, TINT size, const TCHAR *a_pcFormat, va_list args)
Definition T2String8.cpp:22
static TINT Format(TCHAR *a_pcString, TINT size, const TCHAR *a_pcFormat,...)
Definition T2String8.cpp:41
static void IntToString(TINT value, TCHAR *dst, TINT unused, TINT radix)
static TFLOAT StringToFloat(const TCHAR *src)
static void ToLowerCase(TCHAR *str)
static TBOOL IsUpperCase(const TCHAR *str)
static TCHAR * Concat(TCHAR *dst, const TCHAR *src, TSIZE size=-1)
Definition T2String8.cpp:76
static TCHAR * CopySafe(TCHAR *dst, const TCHAR *src, TSIZE size)
Definition T2String8.cpp:84
static TINT Compare(const TCHAR *str1, const TCHAR *str2, TSIZE size=-1)
Definition T2String8.cpp:52
static TINT CompareNoCase(const TCHAR *str1, const TCHAR *str2, TSIZE size=-1)
Definition T2String8.cpp:60
static TCHAR * Copy(TCHAR *dst, const TCHAR *src, TSIZE size=-1)
Definition T2String8.cpp:68
static constexpr TSIZE SCRATCH_MEM_SIZE
Definition T2String8.h:12
static TCHAR * FindString(TCHAR *str, const TCHAR *substr)
static TINT StringToInt(const TCHAR *src)
static void * MemCopy(void *dst, const void *src, TSIZE size)
Definition TUtil.h:90