Ark Server API (ASA) - Wiki
Loading...
Searching...
No Matches
StringFormatter.h
Go to the documentation of this file.
1// Copyright Epic Games, Inc. All Rights Reserved.
2
3#pragma once
4
5#include "Containers/Array.h"
6#include "Containers/Map.h"
7#include "Containers/UnrealString.h"
8#include "CoreTypes.h"
9#include "Misc/AssertionMacros.h"
10#include "Misc/ExpressionParserTypes.h"
11#include "Templates/UnrealTemplate.h"
12#include "Templates/ValueOrError.h"
13
14struct FStringFormatArg;
15template<typename KeyType,typename ValueType,typename SetAllocator ,typename KeyFuncs > class TMap;
16
17/** A string formatter is responsible for formatting string patterns using a set of named, or ordered arguments */
19{
20public:
21
23
24 /**
25 * Format the specified string using the specified arguments. Replaces instances of { Argument } with keys in the map matching 'Argument'
26 * @param InExpression A string representing the format expression
27 * @param InArgs A map of named arguments that match the tokens specified in InExpression
28 * @return A string containing the formatted text
29 */
30 FString Format(const TCHAR* InExpression, const TMap<FString, FStringFormatArg>& InArgs) const
31 {
32 auto Result = FormatInternal(InExpression, InArgs, false);
33 if (ensure(Result.IsValid()))
34 {
35 return MoveTemp(Result.GetValue());
36 }
37
38 return InExpression;
39 }
40
41 /**
42 * Format the specified string using the specified arguments. Replaces instances of {0} with indices from the given array matching the index specified in the token
43 * @param InExpression A string representing the format expression
44 * @param InArgs An array of ordered arguments that match the tokens specified in InExpression
45 * @return A string containing the formatted text
46 */
47 FString Format(const TCHAR* InExpression, const TArray<FStringFormatArg>& InArgs) const
48 {
49 auto Result = FormatInternal(InExpression, InArgs, false);
50 if (ensure(Result.IsValid()))
51 {
52 return MoveTemp(Result.GetValue());
53 }
54
55 return InExpression;
56 }
57
58 /**
59 * Format the specified string using the specified arguments. Replaces instances of { Argument } with keys in the map matching 'Argument'
60 * @param InExpression A string representing the format expression
61 * @param InArgs A map of named arguments that match the tokens specified in InExpression
62 * @return A string containing the formatted text, or an error where InExpression is ill-formed, or contains undefined arguments
63 */
64 TValueOrError<FString, FExpressionError> FormatStrict(const TCHAR* InExpression, const TMap<FString, FStringFormatArg>& InArgs) const
65 {
66 return FormatInternal(InExpression, InArgs, true);
67 }
68
69 /**
70 * Format the specified string using the specified arguments. Replaces instances of {0} with indices from the given array matching the index specified in the token
71 * @param InExpression A string representing the format expression
72 * @param InArgs An array of ordered arguments that match the tokens specified in InExpression
73 * @return A string containing the formatted text, or an error where InExpression is ill-formed, or contains undefined arguments
74 */
75 TValueOrError<FString, FExpressionError> FormatStrict(const TCHAR* InExpression, const TArray<FStringFormatArg>& InArgs) const
76 {
77 return FormatInternal(InExpression, InArgs, true);
78 }
79
80private:
81
82 /** Internal formatting logic */
83 TValueOrError<FString, FExpressionError> FormatInternal(const TCHAR* InExpression, const TMap<FString, FStringFormatArg>& InArgs, bool bStrict) const;
84 TValueOrError<FString, FExpressionError> FormatInternal(const TCHAR* InExpression, const TArray<FStringFormatArg>& InArgs, bool bStrict) const;
85
86 /** Token definitions for lenient lexers */
89
90 /** Token definitions for strict lexers */
93};
#define ensure( InExpression)
FTokenDefinitions OrderedDefinitions
FTokenDefinitions NamedDefinitions
TValueOrError< FString, FExpressionError > FormatStrict(const TCHAR *InExpression, const TMap< FString, FStringFormatArg > &InArgs) const
FTokenDefinitions StrictOrderedDefinitions
FTokenDefinitions StrictNamedDefinitions
FString Format(const TCHAR *InExpression, const TMap< FString, FStringFormatArg > &InArgs) const
TValueOrError< FString, FExpressionError > FormatInternal(const TCHAR *InExpression, const TMap< FString, FStringFormatArg > &InArgs, bool bStrict) const