UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
Char.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "CoreTypes.h"
6#include "Traits/IntType.h"
7#include <ctype.h>
8#include <wctype.h>
9#include <type_traits>
10
11/*-----------------------------------------------------------------------------
12 Character type functions.
13-----------------------------------------------------------------------------*/
14
19template <typename T> struct TLiteral
20{
21 static const ANSICHAR Select(const ANSICHAR ansi, const WIDECHAR ) { return ansi; }
22 static const ANSICHAR* Select(const ANSICHAR* ansi, const WIDECHAR*) { return ansi; }
23};
24
25template <> struct TLiteral<WIDECHAR>
26{
27 static const WIDECHAR Select(const ANSICHAR, const WIDECHAR wide) { return wide; }
28 static const WIDECHAR* Select(const ANSICHAR*, const WIDECHAR* wide) { return wide; }
29};
30
31#define LITERAL(CharType, StringLiteral) TLiteral<CharType>::Select(StringLiteral, TEXT(StringLiteral))
32
33
34template <typename CharType, const unsigned int Size>
36{
37 static constexpr CharType LineFeed = (CharType)0xa;
38 static constexpr CharType VerticalTab = (CharType)0xb;
39 static constexpr CharType FormFeed = (CharType)0xc;
40 static constexpr CharType CarriageReturn = (CharType)0xd;
41 static constexpr CharType NextLine = (CharType)0x85;
42 static constexpr CharType LineSeparator = (CharType)0x2028;
43 static constexpr CharType ParagraphSeparator = (CharType)0x2029;
44
45 static bool IsLinebreak(CharType Char)
46 {
47 return ((uint32(Char) - LineFeed) <= uint32(CarriageReturn - LineFeed)) |
49 }
50
51};
52
53template <typename CharType>
54struct TCharBase<CharType, 1>
55{
56 static constexpr CharType LineFeed = (CharType)0xa;
57 static constexpr CharType VerticalTab = (CharType)0xb;
58 static constexpr CharType FormFeed = (CharType)0xc;
59 static constexpr CharType CarriageReturn = (CharType)0xd;
60
61 static bool IsLinebreak(CharType Char)
62 {
64 }
65};
66
67
74template <typename CharType>
75struct TChar : TCharBase<CharType, sizeof(CharType)>
76{
80 static CharType ToUpper(CharType Char)
81 {
82 return (CharType)(ToUnsigned(Char) - ((uint32(Char) - 'a' < 26u) << 5));
83 }
84
88 static CharType ToLower(CharType Char)
89 {
90 return (CharType)(ToUnsigned(Char) + ((uint32(Char) - 'A' < 26u) << 5));
91 }
92
93 static bool IsUpper(CharType Char)
94 {
95 if constexpr (std::is_same_v<CharType, ANSICHAR>)
96 {
97 return ::isupper((unsigned char)Char) != 0;
98 }
99 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
100 {
101 return ::iswupper(Char) != 0;
102 }
103 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
104 {
105 return ::isupper((unsigned char)Char) != 0;
106 }
107 else
108 {
109 static_assert(sizeof(CharType) == 0, "Not supported");
110 return false;
111 }
112 }
113
114 static bool IsLower(CharType Char)
115 {
116 if constexpr (std::is_same_v<CharType, ANSICHAR>)
117 {
118 return ::islower((unsigned char)Char) != 0;
119 }
120 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
121 {
122 return ::iswlower(Char) != 0;
123 }
124 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
125 {
126 return ::islower((unsigned char)Char) != 0;
127 }
128 else
129 {
130 static_assert(sizeof(CharType) == 0, "Not supported");
131 return false;
132 }
133 }
134
135 static bool IsAlpha(CharType Char)
136 {
137 if constexpr (std::is_same_v<CharType, ANSICHAR>)
138 {
139 return ::isalpha((unsigned char)Char) != 0;
140 }
141 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
142 {
143 return ::iswalpha(Char) != 0;
144 }
145 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
146 {
147 return ::isalpha((unsigned char)Char) != 0;
148 }
149 else
150 {
151 static_assert(sizeof(CharType) == 0, "Not supported");
152 return false;
153 }
154 }
155
156 static bool IsGraph(CharType Char)
157 {
158 if constexpr (std::is_same_v<CharType, ANSICHAR>)
159 {
160 return ::isgraph((unsigned char)Char) != 0;
161 }
162 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
163 {
164 return ::iswgraph(Char) != 0;
165 }
166 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
167 {
168 return ::isgraph((unsigned char)Char) != 0;
169 }
170 else
171 {
172 static_assert(sizeof(CharType) == 0, "Not supported");
173 return false;
174 }
175 }
176
177 static bool IsPrint(CharType Char)
178 {
179 if constexpr (std::is_same_v<CharType, ANSICHAR>)
180 {
181 return ::isprint((unsigned char)Char) != 0;
182 }
183 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
184 {
185 return ::iswprint(Char) != 0;
186 }
187 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
188 {
189 return ::isprint((unsigned char)Char) != 0;
190 }
191 else
192 {
193 static_assert(sizeof(CharType) == 0, "Not supported");
194 return false;
195 }
196 }
197
198 static bool IsPunct(CharType Char)
199 {
200 if constexpr (std::is_same_v<CharType, ANSICHAR>)
201 {
202 return ::ispunct((unsigned char)Char) != 0;
203 }
204 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
205 {
206 return ::iswpunct(Char) != 0;
207 }
208 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
209 {
210 return ::ispunct((unsigned char)Char) != 0;
211 }
212 else
213 {
214 static_assert(sizeof(CharType) == 0, "Not supported");
215 return false;
216 }
217 }
218
219 static bool IsAlnum(CharType Char)
220 {
221 if constexpr (std::is_same_v<CharType, ANSICHAR>)
222 {
223 return ::isalnum((unsigned char)Char) != 0;
224 }
225 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
226 {
227 return ::iswalnum(Char) != 0;
228 }
229 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
230 {
231 return ::isalnum((unsigned char)Char) != 0;
232 }
233 else
234 {
235 static_assert(sizeof(CharType) == 0, "Not supported");
236 return false;
237 }
238 }
239
240 static bool IsDigit(CharType Char)
241 {
242 if constexpr (std::is_same_v<CharType, ANSICHAR>)
243 {
244 return ::isdigit((unsigned char)Char) != 0;
245 }
246 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
247 {
248 return ::iswdigit(Char) != 0;
249 }
250 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
251 {
252 return ::isdigit((unsigned char)Char) != 0;
253 }
254 else
255 {
256 static_assert(sizeof(CharType) == 0, "Not supported");
257 return false;
258 }
259 }
260
261 static bool IsHexDigit(CharType Char)
262 {
263 if constexpr (std::is_same_v<CharType, ANSICHAR>)
264 {
265 return ::isxdigit((unsigned char)Char) != 0;
266 }
267 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
268 {
269 return ::iswxdigit(Char) != 0;
270 }
271 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
272 {
273 return ::isxdigit((unsigned char)Char) != 0;
274 }
275 else
276 {
277 static_assert(sizeof(CharType) == 0, "Not supported");
278 return false;
279 }
280 }
281
282 static bool IsWhitespace(CharType Char)
283 {
284 if constexpr (std::is_same_v<CharType, ANSICHAR>)
285 {
286 return ::isspace((unsigned char)Char) != 0;
287 }
288 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
289 {
290 return ::iswspace(Char) != 0;
291 }
292 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
293 {
294 return ::isspace((unsigned char)Char) != 0;
295 }
296 else
297 {
298 static_assert(sizeof(CharType) == 0, "Not supported");
299 return false;
300 }
301 }
302
303 static bool IsControl(CharType Char)
304 {
305 if constexpr (std::is_same_v<CharType, ANSICHAR>)
306 {
307 return ::iscntrl((unsigned char)Char) != 0;
308 }
309 else if constexpr (std::is_same_v<CharType, WIDECHAR>)
310 {
311 return ::iswcntrl(Char) != 0;
312 }
313 else if constexpr (std::is_same_v<CharType, UTF8CHAR>)
314 {
315 return ::iscntrl((unsigned char)Char) != 0;
316 }
317 else
318 {
319 static_assert(sizeof(CharType) == 0, "Not supported");
320 return false;
321 }
322 }
323
324 static bool IsOctDigit(CharType Char)
325 {
326 return uint32(Char) - '0' < 8u;
327 }
328
330 {
331 return static_cast<int32>(Char) - static_cast<int32>('0');
332 }
333
334 static bool IsIdentifier(CharType Char)
335 {
336 return IsAlnum(Char) || IsUnderscore(Char);
337 }
338
339 static bool IsUnderscore(CharType Char)
340 {
341 return Char == LITERAL(CharType, '_');
342 }
343
352 static constexpr UE_FORCEINLINE_HINT uint32 ToUnsigned(CharType Char)
353 {
354 return (typename TUnsignedIntType<sizeof(CharType)>::Type)Char;
355 }
356};
357
TChar< WIDECHAR > FCharWide
Definition Char.h:359
#define LITERAL(CharType, StringLiteral)
Definition Char.h:31
TChar< ANSICHAR > FCharAnsi
Definition Char.h:360
TChar< TCHAR > FChar
Definition Char.h:358
FPlatformTypes::WIDECHAR WIDECHAR
A wide character. Normally a signed type.
Definition Platform.h:1133
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
#define UE_FORCEINLINE_HINT
Definition Platform.h:723
FPlatformTypes::ANSICHAR ANSICHAR
An ANSI character. Normally a signed type.
Definition Platform.h:1131
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Char
Character type.
uint32_t uint32
Definition binka_ue_file_header.h:6
static bool IsLinebreak(CharType Char)
Definition Char.h:61
Definition Char.h:36
static bool IsLinebreak(CharType Char)
Definition Char.h:45
static constexpr CharType ParagraphSeparator
Definition Char.h:43
static constexpr CharType CarriageReturn
Definition Char.h:40
static constexpr CharType VerticalTab
Definition Char.h:38
static constexpr CharType NextLine
Definition Char.h:41
static constexpr CharType LineFeed
Definition Char.h:37
static constexpr CharType FormFeed
Definition Char.h:39
static constexpr CharType LineSeparator
Definition Char.h:42
Definition Char.h:76
static bool IsDigit(CharType Char)
Definition Char.h:240
static bool IsHexDigit(CharType Char)
Definition Char.h:261
static bool IsOctDigit(CharType Char)
Definition Char.h:324
static bool IsPrint(CharType Char)
Definition Char.h:177
static bool IsPunct(CharType Char)
Definition Char.h:198
static bool IsAlnum(CharType Char)
Definition Char.h:219
static bool IsUpper(CharType Char)
Definition Char.h:93
static bool IsAlpha(CharType Char)
Definition Char.h:135
static CharType ToLower(CharType Char)
Definition Char.h:88
static bool IsIdentifier(CharType Char)
Definition Char.h:334
static int32 ConvertCharDigitToInt(CharType Char)
Definition Char.h:329
static bool IsControl(CharType Char)
Definition Char.h:303
static CharType ToUpper(CharType Char)
Definition Char.h:80
static bool IsLower(CharType Char)
Definition Char.h:114
static bool IsUnderscore(CharType Char)
Definition Char.h:339
static constexpr UE_FORCEINLINE_HINT uint32 ToUnsigned(CharType Char)
Definition Char.h:352
static bool IsWhitespace(CharType Char)
Definition Char.h:282
static bool IsGraph(CharType Char)
Definition Char.h:156
static const WIDECHAR * Select(const ANSICHAR *, const WIDECHAR *wide)
Definition Char.h:28
static const WIDECHAR Select(const ANSICHAR, const WIDECHAR wide)
Definition Char.h:27
Definition Char.h:20
static const ANSICHAR Select(const ANSICHAR ansi, const WIDECHAR)
Definition Char.h:21
static const ANSICHAR * Select(const ANSICHAR *ansi, const WIDECHAR *)
Definition Char.h:22
Definition IntType.h:34