Gray C++ Libraries  0.0.2
A set of C++ libraries for MSVC, GNU on Windows, WinCE, Linux
StrChar.h
Go to the documentation of this file.
1 //
4 //
5 
6 #ifndef _INC_StrChar_H
7 #define _INC_StrChar_H
8 #ifndef NO_PRAGMA_ONCE
9 #pragma once
10 #endif
11 
12 #include "cUnitTestDecl.h"
13 #include "cValT.h"
14 
15 #ifdef _WIN32
16 #include <wtypes.h> // BSTR
17 
18 #elif defined(__linux__)
19 #include <wchar.h> // wcslen()
20 #else
21 
22 #error NOOS
23 #endif
24 
25 namespace Gray
26 {
27  typedef WORD RADIX_t;
28 
29 #ifdef _WIN32
30  typedef UINT CODEPAGE_t;
31 #else
33  {
34  CP_ACP = 0,
35  CP_OEMCP = 1,
36  CP_UTF8 = 65001,
37  };
38 #endif
39 
40  struct GRAYCORE_LINK StrChar // static
41  {
46 
47  static const char k_BEL = '\a';
48  static const char k_BS = '\b';
49  static const char k_HT = '\t';
50  static const char k_NL = '\n';
51  static const char k_VT = '\v';
52  static const char k_FF = '\f';
53  static const char k_CR = '\r';
54 
55  // Other C escape chars are "\'\"\?\\"
56 
57  static const char k_Space = ' ';
58  static const char k_ASCII = 127;
59 
60  static const RADIX_t k_uRadixMin = 2;
61  static const RADIX_t k_uRadixDef = 10;
62  static const RADIX_t k_uRadixMax = 10 + 26;
63 
64  static const char k_Vowels[5];
65 
66  static constexpr bool IsAscii(wchar_t ch) noexcept
67  {
69  return ((unsigned)ch) <= k_ASCII;
70  }
71 
72  static constexpr bool IsPrint(wchar_t ch) noexcept
73  {
75  return ch >= k_Space && ch < k_ASCII;
76  }
77  static inline bool IsAlNum(wchar_t ch) noexcept
78  {
80  return IsAlphaA(ch) || IsDigit(ch);
81  }
82 
83  // single space, tab, vertical tab, form feed, carriage return, or newline
84  static inline bool IsNL(wchar_t ch) noexcept
85  {
87  return ch == k_NL || ch == k_CR;
88  }
89  static inline bool IsSpace(wchar_t ch) noexcept
90  {
92  return ch == k_HT || ch == k_Space;
93  }
94  static inline bool IsSpaceX(wchar_t ch) noexcept
95  {
102 
103  return ch == k_Space || (ch >= k_BS && ch <= k_CR);
104  }
105 
106  static inline bool IsDigit(wchar_t ch) noexcept
107  {
109  return ch >= '0' && ch <= '9';
110  }
111  static inline bool IsUpperA(wchar_t ch) noexcept
112  {
114  return ch >= 'A' && ch <= 'Z';
115  }
116  static inline bool IsLowerA(wchar_t ch) noexcept
117  {
119  return ch >= 'a' && ch <= 'z';
120  }
121  static inline bool IsAlphaA(wchar_t ch) noexcept
122  {
124  return IsUpperA(ch) || IsLowerA(ch);
125  }
126 
127  static const BYTE k_AXU = 0xC0;
128  static const BYTE k_AXL = 0xE0;
129 
130  // Is upper case in extended ASCII, NOT UTF8
131  static inline bool IsUpperAXSet(wchar_t ch) noexcept
132  {
134  return ((unsigned)ch) >= k_AXU && ((unsigned)ch) <= 0xDF;
135  }
136  static inline bool IsLowerAXSet(wchar_t ch) noexcept
137  {
139  return ((unsigned)ch) >= k_AXL && ((unsigned)ch) <= 0xFF;
140  }
141 
142  static inline bool IsAlphaUSet(wchar_t ch) noexcept
143  {
145  return ((unsigned)ch) >= 0x100 && ((unsigned)ch) <= 0x1FF;
146  }
147  static inline bool IsUpperUSet(wchar_t ch) noexcept
148  {
150  return IsAlphaUSet(ch) && (ch & 1) == 0;
151  }
152  static inline bool IsLowerUSet(wchar_t ch) noexcept
153  {
155  return IsAlphaUSet(ch) && (ch & 1) == 1;
156  }
157 
158  static inline bool IsUpperAX(wchar_t ch) noexcept
159  {
161  return IsUpperA(ch) || IsUpperAXSet(ch);
162  }
163  static inline bool IsLowerAX(wchar_t ch) noexcept
164  {
166  return IsLowerA(ch) || IsLowerAXSet(ch);
167  }
168 
169  static inline bool IsUpper(wchar_t ch) noexcept
170  {
172  if (IsAscii(ch))
173  {
174  return IsUpperA(ch);
175  }
176  return IsUpperAXSet(ch) || IsUpperUSet(ch);
177  }
178  static inline bool IsLower(wchar_t ch) noexcept
179  {
181  if (IsAscii(ch))
182  {
183  return IsLowerA(ch);
184  }
185  return IsLowerAXSet(ch) || IsLowerUSet(ch);
186  }
187 
188  static inline bool IsAlpha(wchar_t ch) noexcept
189  {
192  return IsLowerAX(ch) || IsUpperAX(ch) || IsAlphaUSet(ch);
193  }
194  static inline bool IsCSymF(wchar_t ch) noexcept
195  {
198  return IsAlphaA(ch) || ch == '_';
199  }
200  static inline bool IsCSym(wchar_t ch) noexcept
201  {
204  return IsAlphaA(ch) || ch == '_' || IsDigit(ch);
205  }
206 
207  static inline wchar_t ToUpperA(wchar_t ch) noexcept
208  {
210  if (IsLowerA(ch))
211  return ((ch)-'a') + 'A';
212  return ch;
213  }
214  static inline wchar_t ToUpperW(wchar_t ch) noexcept
215  {
217  if (IsAscii(ch))
218  {
219  if (IsLowerA(ch))
220  return ((ch)-'a') + 'A';
221  }
222  else
223  {
224  if (IsLowerAXSet(ch))
225  return ((ch)-k_AXL) + k_AXU;
226  if (IsLowerUSet(ch))
227  return (wchar_t)(ch + 1);
228  }
229  return ch;
230  }
231  static inline wchar_t ToLowerA(wchar_t ch) noexcept
232  {
234  if (IsUpperA(ch))
235  return ((ch)-'A') + 'a';
236  return ch;
237  }
238  static inline wchar_t ToLowerW(wchar_t ch) noexcept
239  {
241  if (IsAscii(ch))
242  {
243  if (IsUpperA(ch))
244  return ((ch)-'A') + 'a';
245  }
246  else
247  {
248  if (IsUpperAXSet(ch))
249  return ((ch)-k_AXU) + k_AXL;
250  else if (IsUpperUSet(ch))
251  return (wchar_t)(ch - 1);
252  }
253  return ch;
254  }
255 
256  static inline COMPARE_t CmpI(char a, char b) noexcept
257  {
261  if (a == b)
262  return 0;
263  wchar_t ch1 = ToLowerA(a);
264  wchar_t ch2 = ToLowerA(b);
265  return ch1 - ch2;
266  }
267 
268  static inline COMPARE_t CmpI(wchar_t a, wchar_t b) noexcept
269  {
273  if (a == b)
274  return 0;
275  wchar_t ch1 = ToLowerW(a);
276  wchar_t ch2 = ToLowerW(b);
277  return ch1 - ch2;
278  }
279 
280  static inline int Dec2U(wchar_t ch) noexcept
281  {
283  // ASSERT(ch>='0'&&ch<=??);
284  return(ch - '0');
285  }
286 
287  static bool GRAYCALL IsDigitF(wchar_t ch);
288  static bool GRAYCALL IsDigitX(wchar_t ch, RADIX_t uRadix = 0x10);
289  static bool GRAYCALL IsVowel(wchar_t ch);
290  static char GRAYCALL U2Hex(UINT uVal);
291  static int GRAYCALL Hex2U(wchar_t ch);
292  static char GRAYCALL U2Radix(UINT uVal, RADIX_t uRadix = 10);
293  static int GRAYCALL Radix2U(wchar_t ch, RADIX_t uRadix = 10);
294 
296  };
297 }
298 #endif // StrChar
#define GRAYCORE_LINK
Definition: GrayCore.h:47
#define GRAYCALL
declare calling convention for static functions so everyone knows the arg passing scheme....
Definition: GrayCore.h:36
#define UNITTEST_FRIEND(n)
Define this in the class body to be unit tested. Allow the unit test to access private/protected stuf...
Definition: cUnitTestDecl.h:17
< The main namespace for all Core functions.
Definition: GrayCore.cpp:14
int COMPARE_t
result of compare. 0=same, 1=a>b, -1=a<b
Definition: cValT.h:17
WORD RADIX_t
Base for convert of numbers to strings. e.g. 10 base vs 16 base hex numbers.
Definition: StrChar.h:27
CODEPAGE_t
Definition: StrChar.h:33
@ CP_UTF8
UTF-8 translation.
Definition: StrChar.h:36
@ CP_OEMCP
default to OEM code page
Definition: StrChar.h:35
@ CP_ACP
default to ANSI code page. All the _WIN32 A suffix functions.
Definition: StrChar.h:34
Definition: StrChar.h:41
static wchar_t ToUpperA(wchar_t ch) noexcept
Definition: StrChar.h:207
static wchar_t ToUpperW(wchar_t ch) noexcept
Definition: StrChar.h:214
static bool IsCSym(wchar_t ch) noexcept
Definition: StrChar.h:200
static bool IsUpper(wchar_t ch) noexcept
Definition: StrChar.h:169
static COMPARE_t CmpI(char a, char b) noexcept
Definition: StrChar.h:256
static bool IsLowerUSet(wchar_t ch) noexcept
Definition: StrChar.h:152
static bool IsCSymF(wchar_t ch) noexcept
Definition: StrChar.h:194
static bool IsAlpha(wchar_t ch) noexcept
Definition: StrChar.h:188
static bool IsNL(wchar_t ch) noexcept
Definition: StrChar.h:84
static COMPARE_t CmpI(wchar_t a, wchar_t b) noexcept
Definition: StrChar.h:268
static bool IsLowerA(wchar_t ch) noexcept
Definition: StrChar.h:116
static bool IsSpace(wchar_t ch) noexcept
Definition: StrChar.h:89
static constexpr bool IsAscii(wchar_t ch) noexcept
Definition: StrChar.h:66
static int Dec2U(wchar_t ch) noexcept
Definition: StrChar.h:280
static bool IsAlphaA(wchar_t ch) noexcept
Definition: StrChar.h:121
static constexpr bool IsPrint(wchar_t ch) noexcept
Definition: StrChar.h:72
static bool IsDigit(wchar_t ch) noexcept
Definition: StrChar.h:106
static bool IsLowerAXSet(wchar_t ch) noexcept
Definition: StrChar.h:136
static bool IsLower(wchar_t ch) noexcept
Definition: StrChar.h:178
static bool IsUpperAX(wchar_t ch) noexcept
Definition: StrChar.h:158
static bool IsAlNum(wchar_t ch) noexcept
Definition: StrChar.h:77
static wchar_t ToLowerA(wchar_t ch) noexcept
Definition: StrChar.h:231
static bool IsLowerAX(wchar_t ch) noexcept
Definition: StrChar.h:163
static bool IsUpperAXSet(wchar_t ch) noexcept
Definition: StrChar.h:131
static bool IsSpaceX(wchar_t ch) noexcept
Definition: StrChar.h:94
static bool IsAlphaUSet(wchar_t ch) noexcept
Definition: StrChar.h:142
static bool IsUpperA(wchar_t ch) noexcept
Definition: StrChar.h:111
static bool IsUpperUSet(wchar_t ch) noexcept
Definition: StrChar.h:147
static wchar_t ToLowerW(wchar_t ch) noexcept
Definition: StrChar.h:238