Alternate e697dbe9c5997e35395fe158628dd8c5209481da
for Visual Studio 2022 and Windows 11.
読み取り中…
検索中…
一致する文字列を見つけられません
TString.cpp
[詳解]
1// ----------------------------------------------------------------------------
6
7#include "pch.h"
8#include "TString.h"
9
10using namespace alt;
11
13{
14 _lptszString = NULL;
15}
16
17TString::TString (LPCTSTR lpctszString)
18 :TString ()
19{
20 this->Copy (lpctszString);
21}
22
24 : TString ()
25{
26 this->Copy (string.Ptr ());
27}
28
29TString::TString (INT length)
30{
31 _lptszString = CreateMemory (length);
32}
33
35{
36 Clear ();
37}
38
39INT TString::Len () const
40{
41 return Length (_lptszString);
42}
43
44INT TString::Byte () const
45{
46 return Len () * sizeof (TCHAR);
47}
48
49BOOL TString::operator == (LPCTSTR lpctszString) const
50{
51 if (_lptszString == NULL && lpctszString == NULL) return TRUE;
52 if (_lptszString == NULL || lpctszString == NULL) return FALSE;
53
54 int size = max (Length (_lptszString), Length (lpctszString));
55 int result = Compare (lpctszString, _lptszString, size);
56
57 return result == 0 ? TRUE : FALSE;
58}
59
60BOOL TString::operator == (const TString& string) const
61{
62 int size = max (Length (_lptszString), string.Len ());
63 int result = Compare (string.Ctr (), _lptszString, size);
64
65 return result == 0 ? TRUE : FALSE;
66}
67
68BOOL TString::operator != (LPCTSTR lpctszString) const
69{
70 return !this->operator==(lpctszString);
71}
72
73BOOL TString::operator != (const TString& string) const
74{
75 return !this->operator==(string);
76}
77
78VOID TString::operator = (LPCTSTR lpctszString)
79{
80 this->Copy (lpctszString);
81}
82
83VOID TString::operator = (const TString& string)
84{
85 this->Copy (string.Ptr ());
86}
87
88TString& TString::operator << (LPCTSTR lpctszString)
89{
90 this->Add (lpctszString);
91
92 return *this;
93}
94
96{
97 this->Add (string.Ptr ());
98
99 return *this;
100}
101
103{
104 // UINT64_MAX 18446744073709551615
105 TCHAR tszBuf[22];
106 wsprintf (tszBuf, _T ("%d"), iValue);
107 this->Add (tszBuf);
108
109 return *this;
110}
111
112TString TString::operator + (LPCTSTR lpctszString)
113{
114 TString string (_lptszString);
115 string += lpctszString;
116
117 return string;
118}
119
121{
122 TString baseString (_lptszString);
123 baseString += string;
124
125 return baseString;
126}
127
128TString& TString::operator += (LPCTSTR lpctszString)
129{
130 this->Add (lpctszString);
131
132 return *this;
133}
134
136{
137 this->Add (string.Ptr ());
138
139 return *this;
140}
141
142bool TString::operator < (const TString& string) const
143{
144 int size = max (Length (_lptszString), string.Len ());
145 int response = Compare (_lptszString, string.Ctr (), size);
146 return response < 0 ? true : false;
147};
148
149TCHAR TString::operator [] (int index) const
150{
151 TCHAR ret;
152
153 if (index <= this->Len ())
154 {
155 ret = _lptszString[index];
156 }
157 else
158 {
159 ret = '\0';
160 }
161
162 return ret;
163}
164
166{
167 int iStartPos = 0;
168 int iLen = this->Len ();
169
170 for (int i = 0; i < iLen; i++)
171 {
172 if (_lptszString[i] != ' ')
173 {
174 iStartPos = i;
175 break;
176 }
177 }
178
179 for (int i = 0; i < (iLen - iStartPos); i++)
180 {
181 _lptszString[i] = _lptszString[i + iStartPos];
182 }
183
184 _lptszString[(iLen - iStartPos)] = 0;
185
186 return *this;
187}
188
190{
191 int iStartPos = 0;
192 int iLen = this->Len ();
193
194 for (int i = (iLen - 1); i >= 0; i--)
195 {
196 if (_lptszString[i] != ' ')
197 {
198 iStartPos = i;
199 break;
200 }
201 }
202
203 for (int i = (iLen - 1); i > iStartPos; i--)
204 {
205 _lptszString[i] = 0;
206 }
207
208 return *this;
209}
210
212{
213 this->TrimLeft ();
214 this->TrimRight ();
215
216 return *this;
217}
218
219TString TString::Substring (int startPos, int length)
220{
221 TString response1;
222 int len = this->Len ();
223 int targetLen = startPos + length;
224 if (targetLen > len || targetLen == 0) return response1;
225
226 int cutSize = length > 0 ? length + 1 : len - startPos + 1;
227 TString response2 (cutSize + sizeof (TCHAR));
228 LPTSTR p = lstrcpyn (response2.Ptr (), &_lptszString[startPos], cutSize);
229
230 return response2;
231}
232
233TString TString::Replace (TString& charsBefore, TString& charsAfter)
234{
235 int findPos = this->Find (charsBefore);
236 if (findPos == -1) return *this;
237
238 TString returnString = this->Substring (0, findPos);
239 returnString += charsAfter;
240 TString wordsAfter = this->Substring (findPos + charsBefore.Len ());
241 returnString += wordsAfter;
242
243 return returnString;
244}
245
246TString TString::ReplaceAll (TString& charsBefore, TString& charsAfter)
247{
248 TString returnString;
249 TString tempString = this->Ctr ();
250
251 for (;;)
252 {
253 int findPos = tempString.Find (charsBefore);
254 if (findPos == -1) return tempString;
255
256 returnString = tempString.Substring (0, findPos);
257 returnString += charsAfter;
258 TString wordsAfter =
259 tempString.Substring (findPos + charsBefore.Len ());
260 returnString += wordsAfter;
261 tempString = returnString;
262 }
263
264 return returnString;
265}
266
268{
269 int length = this->Len ();
270 int beginPos = 0;
271 TCHAR check;
272 TCHAR delimiter;
274
275 for (int i = 0; i <= length; i++) // 最後のnullまで確認する
276 {
277 check = _lptszString[i];
278 // delimitersも最後にnullを持っている
279 for (int j = 0; j < delimiters.Len () + 1; j++)
280 {
281 delimiter = delimiters[j];
282 if (check == delimiter)
283 {
284 TString item (i - beginPos);
285 LPTSTR p = lstrcpyn (
286 item.Ptr (), &_lptszString[beginPos], i - beginPos + 1);
287 response.Add (item);
288 beginPos = i + 1;
289 }
290 }
291 }
292
293 return response;
294}
295
296int TString::Find (TString keyword, int position)
297{
298 int response;
299 int pos = 0;
300
301 LPCTSTR pctszS1 = &_lptszString[position];
302 LPCTSTR pctszS2 = keyword.Ptr ();
303
304 int len = keyword.Len ();
305
306 if (pctszS1 == NULL && pctszS2 == NULL)
307 {
308 return 0;
309 }
310#pragma warning(push)
311#pragma warning(disable:28182)
312 do
313 {
314 if (*pctszS1 == *pctszS2)
315 {
316 len--;
317 pctszS2++;
318 }
319 else
320 {
321 pos++;
322 }
323 pctszS1++;
324
325 } while (*pctszS1 != '\0' && *pctszS2 != '\0' && len != 0);
326#pragma warning (pop)
327
328 response = len == 0 ? pos : -1;
329
330 return response == -1 ? -1 : response + position;
331}
332
333TString& TString::Format (LPCTSTR format, ...)
334{
335 va_list args;
336 int len;
337
338 va_start (args, format);
339
340 Clear ();
341 len = (_vsctprintf (format, args) + 1) * sizeof (TCHAR);
343 _vstprintf_s (_lptszString, len, format, args);
344
345 va_end (args);
346
347 return *this;
348}
349
350BOOL TString::FromMultiByte (LPCSTR lpcszSJIS, UINT codePage)
351{
352 // 必要サイズを取得
353 DWORD dwWideSize = ::MultiByteToWideChar (
354 codePage, MB_PRECOMPOSED, lpcszSJIS, -1, nullptr, 0);
355 TString response (dwWideSize);
356
357 DWORD dwResponse = ::MultiByteToWideChar (
358 codePage, MB_PRECOMPOSED, lpcszSJIS, -1, response.Ptr (), dwWideSize);
359 *this = response;
360
361 return dwResponse > 0 ? TRUE : FALSE;
362}
363
364int TString::ToSJIS (LPSTR lpszSJIS, DWORD dwLen)
365{
366 return ::WideCharToMultiByte (
367 CP_ACP, 0, _lptszString, -1, lpszSJIS, dwLen, NULL, NULL);
368}
369
371{
372 int value = 0;
373 int n = 0;
374
375 auto power10 = [](int up)
376 {
377 if (up == 0) return 1;
378
379 int response = 10;
380
381 for (int i = 1; i < up; i++)
382 {
383 response *= 10;
384 }
385
386 return response;
387 };
388
389 for (int i = (Len () - 1); i >= 0; i--)
390 {
391 TCHAR t = _lptszString[i];
392 if (t == '-')
393 {
394 value = -value;
395 break;
396 }
397 value += (t - '0') * power10 (n++);
398 }
399
400 return value;
401}
402
403// private functions --------------------------------------------------
404
406{
407 if (_lptszString) delete[] _lptszString;
408 _lptszString = NULL;
409}
410
411LPTSTR TString::CreateMemory (ULONGLONG size)
412{
413 LPTSTR lptszMemory = new TCHAR[size + sizeof (TCHAR)];
414 ZeroMemory (lptszMemory, (size + sizeof (TCHAR)));
415 return lptszMemory;
416}
417
418VOID TString::Copy (LPCTSTR lpctszString)
419{
420 if (lpctszString)
421 {
422 Clear ();
423 DWORD dwLen = Length (lpctszString);
424 _lptszString = CreateMemory (dwLen);
425 LPTSTR lptszDst = _lptszString;
426 LPCTSTR lpctszSrc = (LPTSTR)lpctszString;
427 while ((*lpctszSrc) != '\0')
428 {
429 *lptszDst++ = *lpctszSrc++;
430 }
431 *lptszDst = NULL;
432 }
433}
434
435VOID TString::Add (LPCTSTR lpctszString)
436{
437 if (lpctszString)
438 {
439 ULONGLONG length =
440 static_cast<ULONGLONG>(this->Len ()) +
441 Length (lpctszString) + sizeof (TCHAR);
442 LPTSTR lptszNewBuffer = CreateMemory (length);
443 if (_lptszString) lstrcat (lptszNewBuffer, _lptszString);
444 if (lpctszString) lstrcat (lptszNewBuffer, lpctszString);
445 Clear ();
446 _lptszString = lptszNewBuffer;
447 }
448}
449
451 LPCTSTR lpctszString1, LPCTSTR lpctszString2, int size) const
452{
453 int response = 0;
454
455 LPCTSTR lpctszS1 = lpctszString1;
456 LPCTSTR lpctszS2 = lpctszString2;
457
458 while (size-- > 0)
459 {
460 if ((response = *lpctszS1 - *lpctszS2++) != 0 || !*lpctszS1++)
461 {
462 break;
463 }
464 }
465
466 return response;
467}
468
469INT TString::Length (LPCTSTR lpctszString) const
470{
471 INT i = 0;
472
473 if (lpctszString != NULL)
474 {
475 while (lpctszString[i] != NULL)
476 {
477 i++;
478 }
479 }
480
481 return i;
482}
文字列に関するWindowsAPIを集約したクラス
#define size
プリコンパイル済みヘッダー ファイルです。
文字列に関するWindowsAPIを集約したクラス
Definition: TString.h:17
VOID APIENTRY Copy(LPCTSTR lpctszString)
Definition: TString.cpp:418
skeleton::Array< TString > APIENTRY Split(const TString &delimiters)
文字列を指定した文字列で分割します。
Definition: TString.cpp:267
TCHAR APIENTRY operator[](int index) const
文字列の一部を取り出します。
Definition: TString.cpp:149
LPTSTR APIENTRY CreateMemory(ULONGLONG size)
Definition: TString.cpp:411
TString &APIENTRY Format(LPCTSTR format,...)
フォーマットに従ってパラメータを文字列化します。
Definition: TString.cpp:333
INT APIENTRY Length(LPCTSTR lpctszString) const
Definition: TString.cpp:469
INT APIENTRY Byte() const
内部で確保している文字列(バイト数)を取得します。
Definition: TString.cpp:44
APIENTRY TString()
コンストラクタ
Definition: TString.cpp:12
int APIENTRY Compare(LPCTSTR lpctszString1, LPCTSTR lpctszString2, int size) const
Definition: TString.cpp:450
APIENTRY ~TString()
デストラクタ
Definition: TString.cpp:34
VOID APIENTRY operator=(LPCTSTR lpctszString)
文字列を代入します。
Definition: TString.cpp:78
VOID APIENTRY Clear()
Definition: TString.cpp:405
TString &APIENTRY Trim()
左右の余白を切り取ります。
Definition: TString.cpp:211
INT APIENTRY Len() const
内部で確保している文字列数を取得します。
Definition: TString.cpp:39
TString APIENTRY Replace(TString &charsBefore, TString &charsAfter)
文字列を置換します。
Definition: TString.cpp:233
LPCTSTR APIENTRY Ctr() const
内部で確保している文字列ポインタを取得します。
Definition: TString.h:46
BOOL APIENTRY operator!=(LPCTSTR lpctszString) const
文字列と比較します。
Definition: TString.cpp:68
TString &APIENTRY TrimRight()
右側の余白を切り取ります。
Definition: TString.cpp:189
int APIENTRY ToSJIS(LPSTR lpszSJIS, DWORD dwLen)
SJIS文字列を出力します。
Definition: TString.cpp:364
TString APIENTRY ReplaceAll(TString &charsBefore, TString &charsAfter)
文字列を置換します。
Definition: TString.cpp:246
VOID APIENTRY Add(LPCTSTR lpctszString)
Definition: TString.cpp:435
TString &APIENTRY operator<<(LPCTSTR lpctszString)
文字列を代入します。
Definition: TString.cpp:88
BOOL APIENTRY operator==(LPCTSTR lpctszString) const
文字列と比較します。
Definition: TString.cpp:49
TString APIENTRY operator+(LPCTSTR lpctszString)
文字列を追加します。
Definition: TString.cpp:112
TString APIENTRY Substring(int startPos, int length=0)
文字列を切り出します。
Definition: TString.cpp:219
int APIENTRY Find(TString keyword, int position=0)
文字列を検索します。
Definition: TString.cpp:296
TString &APIENTRY TrimLeft()
左側の余白を切り取ります。
Definition: TString.cpp:165
TString &APIENTRY operator+=(LPCTSTR lpctszString)
文字列を追加します。
Definition: TString.cpp:128
LPTSTR APIENTRY Ptr() const
内部で確保している文字列ポインタを取得します。
Definition: TString.h:42
int APIENTRY ParseInt() const
文字列の数値化
Definition: TString.cpp:370
LPTSTR _lptszString
Definition: TString.h:215
BOOL APIENTRY FromMultiByte(LPCSTR lpcszSJIS, UINT codePage=CP_ACP)
マルチバイト文字列を取り込みます。
Definition: TString.cpp:350
bool APIENTRY operator<(const TString &string) const
文字列の大小を比較します。
Definition: TString.cpp:142
サイズ可変の配列を具現したクラス
Definition: Array.hpp:20
VOID APIENTRY Add(T &item)
Array<T>へ値を追加
Definition: Array.hpp:83
Definition: DBLibrary.h:12