UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
AsciiSet.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Misc/Char.h"
6
30{
31public:
32 template<typename CharType, int N>
33 constexpr FAsciiSet(const CharType(&Chars)[N])
34 : FAsciiSet(StringToBitset(Chars))
35 {}
36
38 template<typename CharType>
39 constexpr FORCEINLINE bool Contains(CharType Char) const
40 {
41 return !!TestImpl(TChar<CharType>::ToUnsigned(Char));
42 }
43
45 template<typename CharType>
46 constexpr FORCEINLINE uint64 Test(CharType Char) const
47 {
48 return TestImpl(TChar<CharType>::ToUnsigned(Char));
49 }
50
52 constexpr FORCEINLINE FAsciiSet operator+(char Char) const
53 {
54 InitData Bitset = { LoMask, HiMask };
56 return FAsciiSet(Bitset);
57 }
58
61 {
62 return FAsciiSet(LoMask | OtherSet.LoMask, HiMask | OtherSet.HiMask);
63 }
64
67 {
68 return FAsciiSet(LoMask & OtherSet.LoMask, HiMask & OtherSet.HiMask);
69 }
70
73 {
74 return FAsciiSet(~LoMask, ~HiMask);
75 }
76
78
80 template<class CharType>
81 static constexpr const CharType* FindFirstOrEnd(const CharType* Str, FAsciiSet Set)
82 {
83 for (FAsciiSet SetOrNil(Set.LoMask | NilMask, Set.HiMask); !SetOrNil.Test(*Str); ++Str);
84
85 return Str;
86 }
87
89 template<class CharType>
90 static constexpr const CharType* FindLastOrEnd(const CharType* Str, FAsciiSet Set)
91 {
92 const CharType* Last = FindFirstOrEnd(Str, Set);
93
94 for (const CharType* It = Last; *It; It = FindFirstOrEnd(It + 1, Set))
95 {
96 Last = It;
97 }
98
99 return Last;
100 }
101
103 template<typename CharType>
104 static constexpr const CharType* Skip(const CharType* Str, FAsciiSet Set)
105 {
106 while (Set.Contains(*Str))
107 {
108 ++Str;
109 }
110
111 return Str;
112 }
113
115 template<typename CharType>
116 static constexpr bool HasAny(const CharType* Str, FAsciiSet Set)
117 {
118 return *FindFirstOrEnd(Str, Set) != '\0';
119 }
120
122 template<typename CharType>
123 static constexpr bool HasNone(const CharType* Str, FAsciiSet Set)
124 {
125 return *FindFirstOrEnd(Str, Set) == '\0';
126 }
127
129 template<typename CharType>
130 static constexpr bool HasOnly(const CharType* Str, FAsciiSet Set)
131 {
132 return *Skip(Str, Set) == '\0';
133 }
134
136
138 template<class StringType>
139 static constexpr StringType FindPrefixWith(const StringType& Str, FAsciiSet Set)
140 {
142 }
143
145 template<class StringType>
146 static constexpr StringType FindPrefixWithout(const StringType& Str, FAsciiSet Set)
147 {
149 }
150
152 template<class StringType>
153 static constexpr StringType TrimPrefixWith(const StringType& Str, FAsciiSet Set)
154 {
156 }
157
159 template<class StringType>
160 static constexpr StringType TrimPrefixWithout(const StringType& Str, FAsciiSet Set)
161 {
163 }
164
166 template<class StringType>
167 static constexpr StringType FindSuffixWith(const StringType& Str, FAsciiSet Set)
168 {
170 }
171
173 template<class StringType>
174 static constexpr StringType FindSuffixWithout(const StringType& Str, FAsciiSet Set)
175 {
177 }
178
180 template<class StringType>
181 static constexpr StringType TrimSuffixWith(const StringType& Str, FAsciiSet Set)
182 {
184 }
185
187 template<class StringType>
188 static constexpr StringType TrimSuffixWithout(const StringType& Str, FAsciiSet Set)
189 {
191 }
192
194 template<class StringType>
195 static constexpr bool HasAny(const StringType& Str, FAsciiSet Set)
196 {
197 return !HasNone(Str, Set);
198 }
199
201 template<class StringType>
202 static constexpr bool HasNone(const StringType& Str, FAsciiSet Set)
203 {
204 uint64 Match = 0;
205 for (auto Char : Str)
206 {
207 Match |= Set.Test(Char);
208 }
209 return Match == 0;
210 }
211
213 template<class StringType>
214 static constexpr bool HasOnly(const StringType& Str, FAsciiSet Set)
215 {
216 auto End = GetData(Str) + GetNum(Str);
218 }
219
220private:
221 enum class EDir {Forward, Reverse};
222 enum class EInclude {Members, NonMembers};
223 enum class EKeep {Head, Tail};
224
225 template<EInclude Include, typename CharType>
226 static constexpr const CharType* FindFirst(FAsciiSet Set, const CharType* It, const CharType* End)
227 {
228 for (; It != End && (Include == EInclude::Members) == !!Set.Test(*It); ++It);
229 return It;
230 }
231
232 template<EInclude Include, typename CharType>
233 static constexpr const CharType* FindLast(FAsciiSet Set, const CharType* It, const CharType* End)
234 {
235 for (; It != End && (Include == EInclude::Members) == !!Set.Test(*It); --It);
236 return It;
237 }
238
239 template<EDir Dir, EInclude Include, EKeep Keep, class StringType>
240 static constexpr StringType Scan(const StringType& Str, FAsciiSet Set)
241 {
242 auto Begin = GetData(Str);
243 auto End = Begin + GetNum(Str);
244 auto It = Dir == EDir::Forward ? FindFirst<Include>(Set, Begin, End)
245 : FindLast<Include>(Set, End - 1, Begin - 1) + 1;
246
247 return Keep == EKeep::Head ? StringType(Begin, static_cast<int32>(It - Begin))
248 : StringType(It, static_cast<int32>(End - It));
249 }
250
251 // Work-around for constexpr limitations
252 struct InitData { uint64 Lo, Hi; };
253 static constexpr uint64 NilMask = uint64(1) << '\0';
254
255 static constexpr FORCEINLINE void SetImpl(InitData& Bitset, uint32 Char)
256 {
257 uint64 IsLo = uint64(0) - (Char >> 6 == 0);
258 uint64 IsHi = uint64(0) - (Char >> 6 == 1);
259 uint64 Bit = uint64(1) << uint8(Char & 0x3f);
260
261 Bitset.Lo |= Bit & IsLo;
262 Bitset.Hi |= Bit & IsHi;
263 }
264
265 constexpr FORCEINLINE uint64 TestImpl(uint32 Char) const
266 {
267 uint64 IsLo = uint64(0) - (Char >> 6 == 0);
268 uint64 IsHi = uint64(0) - (Char >> 6 == 1);
269 uint64 Bit = uint64(1) << (Char & 0x3f);
270
271 return (Bit & IsLo & LoMask) | (Bit & IsHi & HiMask);
272 }
273
274 template<typename CharType, int N>
275 static constexpr InitData StringToBitset(const CharType(&Chars)[N])
276 {
277 InitData Bitset = { 0, 0 };
278 for (int I = 0; I < N - 1; ++I)
279 {
280 SetImpl(Bitset, TChar<CharType>::ToUnsigned(Chars[I]));
281 }
282
283 return Bitset;
284 }
285
286 constexpr FAsciiSet(InitData Bitset)
287 : LoMask(Bitset.Lo), HiMask(Bitset.Hi)
288 {}
289
290 constexpr FAsciiSet(uint64 Lo, uint64 Hi)
291 : LoMask(Lo), HiMask(Hi)
292 {}
293
294 uint64 LoMask, HiMask;
295};
#define FORCEINLINE
Definition AndroidPlatform.h:140
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
FPlatformTypes::uint64 uint64
A 64-bit unsigned integer.
Definition Platform.h:1117
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
@ Char
Character type.
auto GetNum(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Length())
Definition StringConv.h:808
auto GetData(const TStringConversion< Converter, DefaultConversionSize > &Conversion) -> decltype(Conversion.Get())
Definition StringConv.h:802
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition AsciiSet.h:30
static constexpr StringType TrimSuffixWithout(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:188
static constexpr bool HasNone(const CharType *Str, FAsciiSet Set)
Definition AsciiSet.h:123
static constexpr const CharType * FindLastOrEnd(const CharType *Str, FAsciiSet Set)
Definition AsciiSet.h:90
static constexpr StringType TrimSuffixWith(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:181
constexpr FORCEINLINE FAsciiSet operator|(FAsciiSet OtherSet) const
Definition AsciiSet.h:60
static constexpr StringType FindPrefixWithout(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:146
static constexpr bool HasAny(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:195
constexpr FORCEINLINE FAsciiSet operator&(FAsciiSet OtherSet) const
Definition AsciiSet.h:66
constexpr FAsciiSet(const CharType(&Chars)[N])
Definition AsciiSet.h:33
static constexpr bool HasOnly(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:214
static constexpr StringType TrimPrefixWithout(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:160
static constexpr bool HasNone(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:202
static constexpr bool HasOnly(const CharType *Str, FAsciiSet Set)
Definition AsciiSet.h:130
static constexpr const CharType * Skip(const CharType *Str, FAsciiSet Set)
Definition AsciiSet.h:104
constexpr FORCEINLINE FAsciiSet operator+(char Char) const
Definition AsciiSet.h:52
static constexpr const CharType * FindFirstOrEnd(const CharType *Str, FAsciiSet Set)
Definition AsciiSet.h:81
static constexpr StringType TrimPrefixWith(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:153
constexpr FORCEINLINE FAsciiSet operator~() const
Definition AsciiSet.h:72
static constexpr StringType FindSuffixWithout(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:174
static constexpr StringType FindSuffixWith(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:167
static constexpr bool HasAny(const CharType *Str, FAsciiSet Set)
Definition AsciiSet.h:116
constexpr FORCEINLINE bool Contains(CharType Char) const
Definition AsciiSet.h:39
constexpr FORCEINLINE uint64 Test(CharType Char) const
Definition AsciiSet.h:46
static constexpr StringType FindPrefixWith(const StringType &Str, FAsciiSet Set)
Definition AsciiSet.h:139
Definition Char.h:76