4#include "../Strings/StringView.h"
15struct StringFormatterFor;
52 size_t backupSize = 0;
77template <
typename RangeIterator>
86 template <
typename... Types>
90 struct Implementation;
99template <
typename RangeIterator>
102 template <
int Total,
int N,
typename T,
typename... Rest>
103 static bool formatArgument(StringFormatOutput& data, StringView specifier,
int position, T&& arg, Rest&&... rest)
105 if (position == Total - N)
107 using First =
typename TypeTraits::RemoveConst<typename TypeTraits::RemoveReference<T>::type>::type;
108 return StringFormatterFor<First>::format(data, specifier, arg);
112 return formatArgument<Total, N - 1>(data, specifier, position, forward<Rest>(rest)...);
116 template <
int Total,
int N,
typename... Args>
118 StringView,
int, Args...)
123 template <
typename... Types>
124 static bool parsePosition(StringFormatOutput& data, RangeIterator& it,
int32_t& parsedPosition, Types&&... args)
126 const auto startOfSpecifier = it;
127 if (it.advanceUntilMatches(
'}'))
129 auto specifier = startOfSpecifier.sliceFromStartUntil(it);
130 auto specifierPosition = specifier;
131 if (specifier.advanceUntilMatches(
':'))
133 specifierPosition = startOfSpecifier.sliceFromStartUntil(specifier);
134 (void)specifier.stepForward();
136 (void)specifierPosition.stepForward();
137 (void)it.stepForward();
140 if (not positionString.isEmpty())
142 if (not positionString.parseInt32(parsedPosition))
147 constexpr auto maxArgs =
sizeof...(args);
148 return formatArgument<maxArgs, maxArgs>(data, specifierString, parsedPosition, forward<Types>(args)...);
153 template <
typename... Types>
154 static bool executeFormat(StringFormatOutput& data, RangeIterator it, Types&&... args)
163 if (it.advanceUntilMatchesAny({
'{',
'}'}, matchedChar))
165 if (it.isFollowedBy(matchedChar))
168 (void)it.stepForward();
171 (void)it.stepForward();
174 else if (matchedChar ==
'{')
179 int32_t parsedPosition = position;
180 if (not parsePosition(data, it, parsedPosition, forward<Types>(args)...))
184 maxPosition =
max(maxPosition, parsedPosition + 1);
195 return maxPosition ==
static_cast<int32_t>(
sizeof...(args));
201template <
typename RangeIterator>
202template <
typename... Types>
206 if (Implementation::executeFormat(data, fmt.
getIterator<RangeIterator>(), forward<Types>(args)...))
219template <>
struct SC_COMPILER_EXPORT StringFormatterFor<double> {
static bool format(StringFormatOutput&,
const StringView,
const double);};
220#if SC_COMPILER_MSVC || SC_COMPILER_CLANG_CL
221#if SC_PLATFORM_64_BIT == 0
225#if !SC_PLATFORM_LINUX
238template <>
struct SC_COMPILER_EXPORT StringFormatterFor<char> {
static bool format(StringFormatOutput&,
const StringView,
const char);};
239template <>
struct SC_COMPILER_EXPORT StringFormatterFor<bool> {
static bool format(StringFormatOutput&,
const StringView,
const bool);};
240template <>
struct SC_COMPILER_EXPORT StringFormatterFor<StringView> {
static bool format(StringFormatOutput&,
const StringView,
const StringView);};
241template <>
struct SC_COMPILER_EXPORT StringFormatterFor<String> {
static bool format(StringFormatOutput&,
const StringView,
const String&);};
242template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const char*> {
static bool format(StringFormatOutput&,
const StringView,
const char*);};
243template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const void*> {
static bool format(StringFormatOutput&,
const StringView,
const void*);};
244#if SC_PLATFORM_WINDOWS
245template <>
struct SC_COMPILER_EXPORT StringFormatterFor<wchar_t> {
static bool format(StringFormatOutput&,
const StringView,
const wchar_t);};
246template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const wchar_t*> {
static bool format(StringFormatOutput&,
const StringView,
const wchar_t*);};
249template <
int N>
struct SC_COMPILER_EXPORT StringFormatterFor<SmallString<N>> {
static bool format(StringFormatOutput& sfo,
const StringView sv,
const SmallString<N>& s){
return StringFormatterFor<StringView>::format(sfo,sv,s.view());}};
253struct StringFormatterFor<char[N]>
255 static bool format(StringFormatOutput& data,
const StringView specifier,
const char* str)
258 return StringFormatterFor<StringView>::format(data, specifier, sv);
constexpr const T & max(const T &t1, const T &t2)
Finds the maximum of two values.
Definition: Compiler.h:302
#define SC_COMPILER_EXPORT
Macro for symbol visibility in non-MSVC compilers.
Definition: Compiler.h:78
int int32_t
Platform independent (4) bytes signed int.
Definition: PrimitiveTypes.h:46
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition: PrimitiveTypes.h:36
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition: PrimitiveTypes.h:42
signed char int8_t
Platform independent (1) byte signed int.
Definition: PrimitiveTypes.h:44
long long int64_t
Platform independent (8) bytes signed int.
Definition: PrimitiveTypes.h:50
unsigned long size_t
Platform independent unsigned size type.
Definition: PrimitiveTypes.h:56
unsigned int uint32_t
Platform independent (4) bytes unsigned int.
Definition: PrimitiveTypes.h:38
short int16_t
Platform independent (2) bytes signed int.
Definition: PrimitiveTypes.h:45
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition: PrimitiveTypes.h:37
signed long ssize_t
Platform independent signed size type.
Definition: PrimitiveTypes.h:57
uint32_t StringCodePoint
UTF code point (32 bit)
Definition: StringIterator.h:13
StringEncoding
String Encoding (Ascii, Utf8, Utf16)
Definition: StringIterator.h:17
@ Ascii
Encoding is ASCII.
Writes to console using SC::StringFormat.
Definition: Console.h:27
Non-owning view over a range of characters with UTF Encoding.
Definition: StringView.h:47
constexpr StringIterator getIterator() const
Returns a StringIterator from current StringView.
Definition: StringView.h:742
static StringView fromIterators(StringIterator from, StringIterator to)
Returns a StringView from two iterators. The from iterator will be shortened until the start of to.
static StringView fromIteratorUntilEnd(StringIterator it)
Returns a section of a string, from it to end of StringView.
EnableIf conditionally defines a type if a boolean template parameter is true.
Definition: TypeTraits.h:25
A contiguous sequence of heap allocated elements.
Definition: Vector.h:51