TNB Library
TnbStrLib.h
[詳解]
1#pragma once
14#include "TnbDef.h"
15
16
17
18//T-TestCaseコードカバレッジDisable
19#pragma comment(user,"T-Coverage Disable")
20
21
22
23#include <stdlib.h>
24#include <WinNls.h>
25
26
27
28//TNB Library
29namespace TNB {
30namespace STRLIB
31{
32
44 inline size_t GetLen(LPCSTR lpsz)
45 {
46 size_t r = 0;
47 LPCSTR P = lpsz;
48 if ( P != NULL )
49 {
50 while ( *P++ != 0 )
51 {
52 r++;
53 }
54 ASSERTLIB(r == strlen(lpsz));
55 }
56 return r;
57 }
58
66 inline size_t GetLen(LPCWSTR lpsz)
67 {
68 size_t r = 0;
69 LPCWSTR P = lpsz;
70 if ( P != NULL )
71 {
72 while ( *P++ != 0 )
73 {
74 r++;
75 }
76 ASSERTLIB(r == wcslen(lpsz));
77 }
78 return r;
79 }
80
89 inline void Copy(LPSTR _dst, LPCSTR src)
90 {
91 ASSERT(! ::IsBadWritePtr(_dst, GetLen(src)));
92 do
93 {
94 *_dst = *src++;
95 }
96 while ( *_dst++ != 0 );
97 }
98
107 inline void Copy(LPWSTR _dst, LPCWSTR src)
108 {
109 ASSERT(! ::IsBadWritePtr(_dst, GetLen(src) * sizeof(WCHAR)));
110 do
111 {
112 *_dst = *src++;
113 }
114 while ( *_dst++ != 0 );
115 }
116
135 inline int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len = -1, DWORD dwCmpFlags = 0)
136 {
137 if ( P2 == NULL ){ P2 = ""; }
138 int l = ToInt(len);
139 #ifndef _WIN32_WCE
140 return ::CompareStringA(LOCALE_USER_DEFAULT, dwCmpFlags, P1, l, P2, l) - CSTR_EQUAL;
141 #else
142 DWORD f = NORM_IGNOREKANATYPE | NORM_IGNORENONSPACE | NORM_IGNORESYMBOLS;
143 if ( (dwCmpFlags & f) != 0 )
144 {
145 ASSERT0(false, "STRLIB::Compare()", "一部のフラグは無視されます");
146 }
147 if ( (dwCmpFlags & NORM_IGNORECASE) != 0 )
148 {
149 return (l < 0) ? ::_stricmp(P1, P2) : ::_strnicmp(P1, P2, l);
150 }
151 return (l < 0) ? ::strcmp(P1, P2) : ::strncmp(P1, P2, l);
152 #endif
153 }
154
167 inline int Compare(LPCWSTR P1, LPCWSTR P2, INT_PTR len = -1, DWORD dwCmpFlags = 0)
168 {
169 if ( P2 == NULL ){ P2 = L""; }
170 int l = ToInt(len);
171 return ::CompareStringW(LOCALE_USER_DEFAULT, dwCmpFlags, P1, l, P2, l) - CSTR_EQUAL;
172 }
173
184 inline INT_PTR IndexOf(LPCSTR lpszText, char c, INDEX iFromIndex = 0)
185 {
186 LPCSTR P = ::strchr(&lpszText[iFromIndex], c);
187 return (P == NULL) ? (-1) : (P - lpszText);
188 }
189
200 inline INT_PTR IndexOf(LPCWSTR lpszText, WCHAR c, INDEX iFromIndex = 0)
201 {
202 LPCWSTR P = ::wcschr(&lpszText[iFromIndex], c);
203 return (P == NULL) ? (-1) : (P - lpszText);
204 }
205
216 inline INT_PTR IndexOneOf(LPCSTR lpszText, LPCSTR lpszMark, INDEX iFromIndex = 0)
217 {
218 size_t l = GetLen(&lpszText[iFromIndex]);
219 size_t r = ::strcspn(&lpszText[iFromIndex], lpszMark);
220 return (r >= l) ? -1 : r + iFromIndex;
221 }
222
233 inline INT_PTR IndexOneOf(LPCWSTR lpszText, LPCWSTR lpszMark, INDEX iFromIndex = 0)
234 {
235 size_t l = GetLen(&lpszText[iFromIndex]);
236 size_t r = ::wcscspn(&lpszText[iFromIndex], lpszMark);
237 return (r >= l) ? -1 : r + iFromIndex;
238 }
239
249 inline INT_PTR ReverseIndexOf(LPCSTR lpszText, char c)
250 {
251 LPCSTR P = ::strrchr(lpszText, c);
252 return (P == NULL) ? (-1) : (P - lpszText);
253 }
254
264 inline INT_PTR ReverseIndexOf(LPCWSTR lpszText, char c)
265 {
266 LPCWSTR P = ::wcsrchr(lpszText, c);
267 return (P == NULL) ? (-1) : (P - lpszText);
268 }
269
280 inline INT_PTR IndexOf(LPCSTR lpszText, LPCSTR lpszWord, INDEX iFromIndex = 0)
281 {
282 LPCSTR P = strstr(&lpszText[iFromIndex], lpszWord);
283 return (P == NULL) ? (-1) : (P - lpszText);
284 }
285
296 inline INT_PTR IndexOf(LPCWSTR lpszText, LPCWSTR lpszWord, INDEX iFromIndex = 0)
297 {
298 LPCWSTR P = wcsstr(&lpszText[iFromIndex], lpszWord);
299 return (P == NULL) ? (-1) : (P - lpszText);
300 }
301
310 inline INT_PTR PathIndexOf(LPCSTR lpszText)
311 {
312 INT_PTR p1 = ReverseIndexOf(lpszText, '\\');
313 INT_PTR p2 = ReverseIndexOf(lpszText, '/');
314 return (p1 < p2) ? p2 : p1;
315 }
316
325 inline INT_PTR PathIndexOf(LPCWSTR lpszText)
326 {
327 INT_PTR p1 = ReverseIndexOf(lpszText, '\\');
328 INT_PTR p2 = ReverseIndexOf(lpszText, '/');
329 return (p1 < p2) ? p2 : p1;
330 }
331
341 inline int GetCharSize(char c)
342 {
343 return ::IsDBCSLeadByte(c) ? 2 : 1;
344 }
345
353 inline int GetCharSize(WCHAR c)
354 {
355 return 1;
356 }
357
367 inline int ToInt(LPCSTR lpsz, int iBase = 10)
368 {
369 return strtol(lpsz, NULL, iBase);
370 }
371
381 inline int ToInt(LPCWSTR lpsz, int iBase = 10)
382 {
383 return wcstol(lpsz, NULL, iBase);
384 }
385
395 inline DWORD ToDword(LPCSTR lpsz, int iBase = 10)
396 {
397 return strtoul(lpsz, NULL, iBase);
398 }
399
409 inline DWORD ToDword(LPCWSTR lpsz, int iBase = 10)
410 {
411 return wcstoul(lpsz, NULL, iBase);
412 }
413
421 inline double ToDouble(LPCSTR lpsz)
422 {
423 return strtod(lpsz, NULL);
424 }
425
433 inline double ToDouble(LPCWSTR lpsz)
434 {
435 return wcstod(lpsz, NULL);
436 }
437
445 inline LONGLONG ToLonglong(LPCSTR lpsz)
446 {
447 return _atoi64(lpsz);
448 }
449
457 inline LONGLONG ToLonglong(LPCWSTR lpsz)
458 {
459 return _wtoi64(lpsz);
460 }
461
471 inline bool IsAsciiOnly(LPCSTR lpsz)
472 {
473 while ( *lpsz != 0 )
474 {
475 if ( GetCharSize(*lpsz++) != 1 )
476 {
477 return false;// ASCII以外発見
478 }
479 }
480 return true;
481 }
482
492 inline int HexCharToInt(int c)
493 {
494 int r = -1;
495 if ( c >= 'a' && c <= 'f' )
496 {
497 r = (c - 'a') + 10;
498 }
499 else if ( c >= 'A' && c <= 'F' )
500 {
501 r = (c - 'A') + 10;
502 }
503 else if ( c >= '0' && c <= '9' )
504 {
505 r = c - '0';
506 }
507 return r;
508 }
509
518 inline char IntToHexChar(int c)
519 {
520 return "0123456789ABCDEF"[c & 0x000F];
521 }
522
532 template<typename TYP>
533 inline int EscCharToInt(const TYP*& _lpsz)
534 {
535 if ( *_lpsz != '\\' ){ return -1; }
536 int r = -1;
537 const TYP* P = _lpsz;
538 P++;
539 TYP c = *P++;
540 switch ( c )
541 {
542 case 't': // タブ文字
543 r = '\t';
544 break;
545 case 'n': // 改行文字
546 r = '\n';
547 break;
548 case 'r': // キャリッジリターン
549 r = '\r';
550 break;
551 case 'e':
552 r = '\x1B';
553 break;
554 case '\\':
555 r = '\\';
556 break;
557 case 'x': // \xhh 16進数値を持つ文字
558 case 'u': // \uhhhh 16進数値を持つ文字
559 loop( i, (c == 'x') ? 2 : 4 )
560 {
561 int c = STRLIB::HexCharToInt(*P);
562 if ( c < 0 ) { break; }
563 if ( r < 0 ) { r = 0; }
564 r *= 16;
565 r += c;
566 P++;
567 }
568 break;
569 case '0':
570 if ( *P >= '0' && *P <= '9' )
571 {
572 loop ( i, 3 )
573 {
574 if ( *P < '0' || *P > '7' ) { break; }
575 if ( r < 0 ) { r = 0; }
576 r *= 8;
577 r += (*P - '0');
578 P++;
579 }
580 }
581 break;
582 default:
583 break;
584 }
585 if ( r >= 0 )
586 {
587 _lpsz = P;
588 }
589 return r;
590 }
591
592}; // STRLIB
593
594
595
596}; // TNB
597
598
599
600//T-TestCaseコードカバレッジEnable
601#pragma comment(user,"T-Coverage Enable")
602
TNBライブラリの定義ヘッダ
#define loop(VAR, CNT)
loop構文.
Definition: TnbDef.h:343
int GetCharSize(char c)
[取得] 文字のサイズ(ASCII/SJIS用)
Definition: TnbStrLib.h:341
int Compare(LPCSTR P1, LPCSTR P2, INT_PTR len=-1, DWORD dwCmpFlags=0)
[比較] 文字列比較(ASCII/SJIS用)
Definition: TnbStrLib.h:135
INT_PTR IndexOf(LPCSTR lpszText, char c, INDEX iFromIndex=0)
[検索] 文字検索(ASCII/SJIS用)
Definition: TnbStrLib.h:184
size_t GetLen(LPCSTR lpsz)
[計算] 文字列長計算(ASCII/SJIS用)
Definition: TnbStrLib.h:44
LONGLONG ToLonglong(LPCSTR lpsz)
[変換] LONGLONG変換(ASCII/SJIS用)
Definition: TnbStrLib.h:445
char IntToHexChar(int c)
[変換] 数値HEX文字変換
Definition: TnbStrLib.h:518
int HexCharToInt(int c)
[変換] HEX文字数値変換
Definition: TnbStrLib.h:492
INT_PTR PathIndexOf(LPCSTR lpszText)
[検索] パス区切り検索(ASCII/SJIS用)
Definition: TnbStrLib.h:310
INT_PTR IndexOneOf(LPCSTR lpszText, LPCSTR lpszMark, INDEX iFromIndex=0)
[検索] 文字セット検索(ASCII/SJIS用)
Definition: TnbStrLib.h:216
DWORD ToDword(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:395
void Copy(LPSTR _dst, LPCSTR src)
[複製] 文字列コピー(ASCII/SJIS用)
Definition: TnbStrLib.h:89
int EscCharToInt(const TYP *&_lpsz)
[変換] エスケープ文字表記変換
Definition: TnbStrLib.h:533
int ToInt(LPCSTR lpsz, int iBase=10)
[変換] INT変換(ASCII/SJIS用).
Definition: TnbStrLib.h:367
INT_PTR ReverseIndexOf(LPCSTR lpszText, char c)
[検索] 降順文字検索(ASCII/SJIS用)
Definition: TnbStrLib.h:249
bool IsAsciiOnly(LPCSTR lpsz)
[判断] ASCIIオンリー判断
Definition: TnbStrLib.h:471
double ToDouble(LPCSTR lpsz)
[変換] double変換(ASCII/SJIS用)
Definition: TnbStrLib.h:421
TNB Library
Definition: TnbDoxyTitle.txt:2