UDocumentation UE5.7 10.02.2026 (Source)
API documentation for Unreal Engine 5.7
UnrealString.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3// This needed to be UnrealString.h to avoid conflicting with
4// the Windows platform SDK string.h
5
6#pragma once
7
8// Include UnrealString.h.inl's includes before defining the macros, in case the macros 'poison' other headers or there are re-entrant includes.
9#include "Containers/UnrealStringIncludes.h.inl" // IWYU pragma: export
10
11#define UE_STRING_CLASS FString
12#define UE_STRING_CHARTYPE TCHAR
13#define UE_STRING_CHARTYPE_IS_TCHAR 1
14#define UE_STRING_PRINTF_FMT_CHARTYPE TCHAR
15#define UE_STRING_DEPRECATED(Version, Message)
16 #include "Containers/UnrealString.h.inl" // IWYU pragma: export
17#undef UE_STRING_DEPRECATED
18#undef UE_STRING_PRINTF_FMT_CHARTYPE
19#undef UE_STRING_CHARTYPE_IS_TCHAR
20#undef UE_STRING_CHARTYPE
21#undef UE_STRING_CLASS
22
29[[nodiscard]] inline FString BytesToString(const uint8* In, int32 Count)
30{
31 FString Result;
32 Result.Empty(Count);
33
34 while (Count)
35 {
36 // Put the byte into an int16 and add 1 to it, this keeps anything from being put into the string as a null terminator
37 int16 Value = *In;
38 Value += 1;
39
40 Result += FString::ElementType(Value);
41
42 ++In;
43 Count--;
44 }
45 return Result;
46}
47
55inline int32 StringToBytes( const FString& String, uint8* OutBytes, int32 MaxBufferSize )
56{
57 int32 NumBytes = 0;
58 const FString::ElementType* CharPos = *String;
59
60 while( *CharPos && NumBytes < MaxBufferSize)
61 {
62 OutBytes[ NumBytes ] = (int8)(*CharPos - 1);
63 CharPos++;
64 ++NumBytes;
65 }
66 return NumBytes;
67}
68
71{
72 if (Num > 9)
73 {
74 return (TCHAR)('A' + (Num - 10));
75 }
76 return (TCHAR)('0' + Num);
77}
78
81{
82 if (Num > 9)
83 {
84 return (TCHAR)('a' + (Num - 10));
85 }
86 return (TCHAR)('0' + Num);
87}
88
94inline void ByteToHex(uint8 In, FString& Result)
95{
96 Result += NibbleToTChar(In >> 4);
97 Result += NibbleToTChar(In & 15);
98}
99
101[[nodiscard]] inline FString BytesToHex(const uint8* Bytes, int32 NumBytes)
102{
103 FString Out;
104 BytesToHex(Bytes, NumBytes, Out);
105 return Out;
106}
107
109[[nodiscard]] inline FString BytesToHexLower(const uint8* Bytes, int32 NumBytes)
110{
111 FString Out;
112 BytesToHexLower(Bytes, NumBytes, Out);
113 return Out;
114}
115
121[[nodiscard]] inline const bool CheckTCharIsHex(const TCHAR Char)
122{
123 return (Char >= TEXT('0') && Char <= TEXT('9')) || (Char >= TEXT('A') && Char <= TEXT('F')) || (Char >= TEXT('a') && Char <= TEXT('f'));
124}
125
131[[nodiscard]] inline const uint8 TCharToNibble(const TCHAR Hex)
132{
133 if (Hex >= '0' && Hex <= '9')
134 {
135 return uint8(Hex - '0');
136 }
137 if (Hex >= 'A' && Hex <= 'F')
138 {
139 return uint8(Hex - 'A' + 10);
140 }
141 if (Hex >= 'a' && Hex <= 'f')
142 {
143 return uint8(Hex - 'a' + 10);
144 }
145 checkf(false, TEXT("'%c' (0x%02X) is not a valid hexadecimal digit"), Hex, Hex);
146 return 0;
147}
148
150template <
151 typename StringType = FString,
152 typename T
153 UE_REQUIRES(std::is_arithmetic_v<T>)
154>
155[[nodiscard]] StringType LexToString(const T& Value)
156{
157 // std::remove_cv_t to remove potential volatile decorations. Removing const is pointless, but harmless because it's specified in the param declaration.
158 return StringType::Printf(TFormatSpecifier<std::remove_cv_t<T>>::template GetFormatSpecifier<typename StringType::FmtCharType>(), Value);
159}
160
161template <
162 typename StringType = FString,
163 typename CharType
165>
166[[nodiscard]] StringType LexToString(const CharType* Ptr)
167{
168 return StringType(Ptr);
169}
170
171template <typename StringType = FString>
172[[nodiscard]] inline StringType LexToString(bool Value)
173{
174 using ElementType = typename StringType::ElementType;
175 return Value ? CHARTEXT(ElementType, "true") : CHARTEXT(ElementType, "false");
176}
177
179template <typename StringType = FString, typename T>
180[[nodiscard]] StringType LexToSanitizedString(const T& Value)
181{
183}
184
186template <typename StringType = FString>
187[[nodiscard]] inline StringType LexToSanitizedString(float Value)
188{
189 return StringType::SanitizeFloat(Value);
190}
191
193template <typename StringType = FString>
194[[nodiscard]] inline StringType LexToSanitizedString(double Value)
195{
196 return StringType::SanitizeFloat(Value);
197}
198
200template<typename T>
202{
203 template <typename StringType = FString>
204 [[nodiscard]] static StringType ToString(const T& Value)
205 {
207 }
208
209 template <typename StringType = FString>
210 [[nodiscard]] static StringType ToSanitizedString(const T& Value)
211 {
213 }
214};
215
216
217template<typename T>
219{
220 template <typename CharType>
221 static void FromString(T& Value, const CharType* Buffer)
222 {
223 return LexFromString(Value, Buffer);
224 }
225};
226
227namespace StringConv
228{
230 CORE_API void InlineCombineSurrogates(FString& Str);
231}
232
234{
238 {
239
240 }
241
248
249 inline bool UEOpEquals(const FTextRange& Other) const
250 {
251 return BeginIndex == Other.BeginIndex
252 && EndIndex == Other.EndIndex;
253 }
254
255 friend inline uint32 GetTypeHash(const FTextRange& Key)
256 {
257 uint32 KeyHash = 0;
258 KeyHash = HashCombine(KeyHash, GetTypeHash(Key.BeginIndex));
259 KeyHash = HashCombine(KeyHash, GetTypeHash(Key.EndIndex));
260 return KeyHash;
261 }
262
263 int32 Len() const { return EndIndex - BeginIndex; }
264 bool IsEmpty() const { return (EndIndex - BeginIndex) <= 0; }
265 void Offset(int32 Amount) { BeginIndex += Amount; BeginIndex = FMath::Max(0, BeginIndex); EndIndex += Amount; EndIndex = FMath::Max(0, EndIndex); }
266 bool Contains(int32 Index) const { return Index >= BeginIndex && Index < EndIndex; }
267 bool InclusiveContains(int32 Index) const { return Index >= BeginIndex && Index <= EndIndex; }
268
270 {
271 FTextRange Intersected(FMath::Max(BeginIndex, Other.BeginIndex), FMath::Min(EndIndex, Other.EndIndex));
272 if (Intersected.EndIndex <= Intersected.BeginIndex)
273 {
274 return FTextRange(0, 0);
275 }
276
277 return Intersected;
278 }
279
284
287};
288
289namespace UE::Core::Private
290{
291 CORE_API void StripNegativeZero(double& InFloat);
292}
293
294#include "Misc/StringFormatArg.h"
295
296#if UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_7
298#endif // UE_ENABLE_INCLUDE_ORDER_DEPRECATED_IN_5_7
#define checkf(expr, format,...)
Definition AssertionMacros.h:315
@ INDEX_NONE
Definition CoreMiscDefines.h:150
FPlatformTypes::int16 int16
A 16-bit signed integer.
Definition Platform.h:1123
FPlatformTypes::int8 int8
An 8-bit signed integer.
Definition Platform.h:1121
#define TEXT(x)
Definition Platform.h:1272
FPlatformTypes::TCHAR TCHAR
Either ANSICHAR or WIDECHAR, depending on whether the platform supports wide characters or the requir...
Definition Platform.h:1135
#define CHARTEXT(CharType, x)
Definition Platform.h:1291
FPlatformTypes::int32 int32
A 32-bit signed integer.
Definition Platform.h:1125
UE_FORCEINLINE_HINT TSharedRef< CastToType, Mode > StaticCastSharedRef(TSharedRef< CastFromType, Mode > const &InSharedRef)
Definition SharedPointer.h:127
void LexFromString(EAudioFeature &OutFeature, const TCHAR *String)
Definition IOSAppDelegate.cpp:163
@ Num
Definition MetalRHIPrivate.h:234
@ Char
Character type.
#define UE_REQUIRES(...)
Definition Requires.h:86
constexpr uint32 HashCombine(uint32 A, uint32 C)
Definition TypeHash.h:36
StringType LexToString(const T &Value)
Definition UnrealString.h:155
const bool CheckTCharIsHex(const TCHAR Char)
Definition UnrealString.h:121
FString BytesToHex(const uint8 *Bytes, int32 NumBytes)
Definition UnrealString.h:101
const uint8 TCharToNibble(const TCHAR Hex)
Definition UnrealString.h:131
void ByteToHex(uint8 In, FString &Result)
Definition UnrealString.h:94
FString BytesToHexLower(const uint8 *Bytes, int32 NumBytes)
Definition UnrealString.h:109
int32 StringToBytes(const FString &String, uint8 *OutBytes, int32 MaxBufferSize)
Definition UnrealString.h:55
TCHAR NibbleToTCharLower(uint8 Num)
Definition UnrealString.h:80
TCHAR NibbleToTChar(uint8 Num)
Definition UnrealString.h:70
StringType LexToSanitizedString(const T &Value)
Definition UnrealString.h:180
FString BytesToString(const uint8 *In, int32 Count)
Definition UnrealString.h:29
uint8_t uint8
Definition binka_ue_file_header.h:8
uint32_t uint32
Definition binka_ue_file_header.h:6
Definition Array.h:670
CORE_API void Empty()
Definition String.cpp.inl:321
Definition StringConv.h:51
CORE_API void InlineCombineSurrogates(FString &Str)
Definition String.cpp:49
implementation
Definition PlayInEditorLoadingScope.h:8
UE_DISABLE_OPTIMIZATION_SHIP void StripNegativeZero(double &InFloat)
Definition String.cpp:57
U16 Index
Definition radfft.cpp:71
Definition UnrealString.h:234
int32 EndIndex
Definition UnrealString.h:286
void Offset(int32 Amount)
Definition UnrealString.h:265
bool Contains(int32 Index) const
Definition UnrealString.h:266
FTextRange()
Definition UnrealString.h:235
FTextRange(int32 InBeginIndex, int32 InEndIndex)
Definition UnrealString.h:242
bool InclusiveContains(int32 Index) const
Definition UnrealString.h:267
FTextRange Intersect(const FTextRange &Other) const
Definition UnrealString.h:269
int32 Len() const
Definition UnrealString.h:263
bool UEOpEquals(const FTextRange &Other) const
Definition UnrealString.h:249
int32 BeginIndex
Definition UnrealString.h:285
bool IsEmpty() const
Definition UnrealString.h:264
static CORE_API void CalculateLineRangesFromString(const FString &Input, TArray< FTextRange > &LineRanges)
Definition String.cpp:16
friend uint32 GetTypeHash(const FTextRange &Key)
Definition UnrealString.h:255
Definition UnrealTypeTraits.h:83
Definition UnrealString.h:219
static void FromString(T &Value, const CharType *Buffer)
Definition UnrealString.h:221
Definition UnrealString.h:202
static StringType ToString(const T &Value)
Definition UnrealString.h:204
static StringType ToSanitizedString(const T &Value)
Definition UnrealString.h:210