-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathStringUtil.cpp
278 lines (206 loc) · 4.97 KB
/
StringUtil.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
#include "stdafx.h"
#include "StringUtil.h"
#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[]=__FILE__;
#endif
namespace Str // String Util
{
CStringW ToStringW( int Value )
{
CStringW ret;
ret.Format( L"%d" , Value );
return ret;
}
CStringW ToStringW( float Value )
{
CStringW ret;
ret.Format( L"%f" , Value );
return ret;
}
CStringW ToStringW( float Value , int Point )
{
WCHAR pre[] = { L'%' , L'.' , TCHAR( Point + L'0' ) , L'f' , L'\0' };
CStringW ret;
ret.Format( pre , Value );
return ret;
}
CStringW ToStringW( double Value )
{
CStringW ret;
ret.Format( L"%f" , Value );
return ret;
}
CStringW ToStringW( double Value , int Point )
{
WCHAR pre[] = { L'%' , L'.' , TCHAR( Point + L'0' ) , L'f' , L'\0' };
CStringW ret;
ret.Format( pre , Value );
return ret;
}
CStringA ToStringA( int Value )
{
CStringA ret;
ret.Format( "%d" , Value );
return ret;
}
CStringA ToStringA( float Value )
{
CStringA ret;
ret.Format("%f" , Value );
return ret;
}
CStringA ToStringA( float Value , int Point )
{
char pre[] = { '%' , '.' , char( Point + '0' ) , 'f' , '\0' };
CStringA ret;
ret.Format( pre , Value );
return ret;
}
CStringA ToStringA( double Value )
{
CStringA ret;
ret.Format( "%f" , Value );
return ret;
}
CStringA ToStringA( double Value , int Point )
{
char pre[] = { '%' , '.' , char( Point + '0' ) , 'f' , '\0' };
CStringA ret;
ret.Format( pre , Value );
return ret;
}
int ToInt( LPCTSTR Str )
{
return _ttoi( Str );
}
double ToDouble( LPCTSTR Str )
{
return _ttof( Str );
}
HighOrderVector<CStringA> TokenizerA( const CStringA& Str , CHAR Step )
{
return TokenizerW( (CStringW)Str , (WCHAR)Step )
.map<CStringA>( []( CStringW& arg ) { return (CStringA)arg; });
}
HighOrderVector<CStringW> TokenizerW( const CStringW& Str , WCHAR Step )
{
CString Value;
int Index = 0;
HighOrderVector<CString> Ret;
while ( AfxExtractSubString( Value , Str , Index++ , Step ) )
{
if ( Value.IsEmpty() ) continue;
Ret.push_back( std::move( Value ) );
}
return Ret;
}
HighOrderVector<CStringA> TokenizerA( const CStringA& Str , const CStringA& Step )
{
return TokenizerW( (CStringW)Str , (CStringW)Step )
.map<CStringA>( []( CStringW& arg ) { return (CStringA)arg; });
}
HighOrderVector<CStringW> TokenizerW( const CStringW& Str , const CStringW& Step )
{
int Pos = 0;
HighOrderVector<CStringW> Ret;
CStringW Value;
do
{
Value = Str.Tokenize( Step , Pos );
if ( Value.IsEmpty() ) break;
Ret.push_back( Value );
} while( true ) ;
return Ret;
}
CStringW _W( LPCTSTR Format , ... )
{
va_list ap;
va_start( ap, Format );
WCHAR Result[4096] = { 0 , };
vswprintf_s( Result , Format , ap );
return CStringW( Result );
}
CStringA _A( LPCSTR Format , ... )
{
va_list ap;
va_start( ap, Format );
CHAR Result[4096] = { 0 , };
vsprintf_s( Result , Format , ap );
return CStringA( Result );
}
int HexToIntW( WCHAR ch )
{
if ( L'0' <= ch && ch <= L'9' ) return ch - L'0';
if ( L'a' <= ch && ch <= L'f' ) return ch - L'a' + 10;
if ( L'A' <= ch && ch <= L'F' ) return ch - L'A' + 10;
return 0;
}
int HexToIntA( CHAR Hex )
{
return HexToIntW( (WCHAR)Hex );
}
int HexToIntW( CStringW Hex )
{
// 16진수 Prefix "0x" 부분 삭제(만약 존재한다면)
if ( Hex.GetLength() > 2 )
{
TCHAR Prefix1 = Hex.GetAt(0);
TCHAR Prefix2 = Hex.GetAt(1);
if ( Prefix1 == L'0' && (Prefix2 == L'x' || Prefix2 == L'X') )
{
Hex.Delete(0);
Hex.Delete(0);
}
}
int Ret = 0;
int Len = Hex.GetLength();
for ( int i = 0 ; i < Len ; ++i )
{
Ret <<= 4;
Ret |= HexToIntW( Hex.GetAt(i) );
}
return Ret;
}
int HexToIntA( CStringA Hex )
{
return HexToIntW( (CStringW)Hex );
}
BOOL HasPrefixA( const CStringA& String , const CStringA& Prefix )
{
if ( String.GetLength() < Prefix.GetLength() ) return FALSE;
return String.Left( Prefix.GetLength() ) == Prefix;
}
BOOL HasSuffixA( const CStringA& String , const CStringA& Suffix )
{
if ( String.GetLength() < Suffix.GetLength() ) return FALSE;
return String.Right( Suffix.GetLength() ) == Suffix;
}
BOOL HasPrefixW( const CStringW& String , const CStringW& Prefix )
{
if ( String.GetLength() < Prefix.GetLength() ) return FALSE;
return String.Left( Prefix.GetLength() ) == Prefix;
}
BOOL HasSuffixW( const CStringW& String , const CStringW& Suffix )
{
if ( String.GetLength() < Suffix.GetLength() ) return FALSE;
return String.Right( Suffix.GetLength() ) == Suffix;
}
CString IntToHex( int Value , int MinimumLen )
{
CString Ret = STR( _T("%X") , Value );
if ( MinimumLen <= 0 ) return Ret;
int RetLen = Ret.GetLength();
for ( int i = MinimumLen - RetLen ; i > 0 ; --i )
Ret.Insert( 0 , _T('0') );
return Ret;
}
CString ByteToHex( const BYTE* pSrc , int Size )
{
CString Ret;
for ( int i = 0 ; i < Size ; ++i )
Ret.Append( IntToHex( pSrc[i] , 0 ) );
return Ret;
}
};