4#include "../Strings/StringView.h"
13struct StringFormatterFor;
50 size_t backupSize = 0;
75template <
typename RangeIterator>
84 template <
typename... Types>
88 struct Implementation;
97template <
typename RangeIterator>
100 template <
int Total,
int N,
typename T,
typename... Rest>
101 static bool formatArgument(StringFormatOutput& data, StringView specifier,
int position, T&& arg, Rest&&... rest)
103 if (position == Total - N)
105 using First =
typename TypeTraits::RemoveConst<typename TypeTraits::RemoveReference<T>::type>::type;
106 return StringFormatterFor<First>::format(data, specifier, arg);
110 return formatArgument<Total, N - 1>(data, specifier, position, forward<Rest>(rest)...);
114 template <
int Total,
int N,
typename... Args>
116 StringView,
int, Args...)
121 template <
typename... Types>
122 static bool parsePosition(StringFormatOutput& data, RangeIterator& it,
int32_t& parsedPosition, Types&&... args)
124 const auto startOfSpecifier = it;
125 if (it.advanceUntilMatches(
'}'))
127 auto specifier = startOfSpecifier.sliceFromStartUntil(it);
128 auto specifierPosition = specifier;
129 if (specifier.advanceUntilMatches(
':'))
131 specifierPosition = startOfSpecifier.sliceFromStartUntil(specifier);
132 (void)specifier.stepForward();
134 (void)specifierPosition.stepForward();
135 (void)it.stepForward();
138 if (not positionString.isEmpty())
140 if (not positionString.parseInt32(parsedPosition))
145 constexpr auto maxArgs =
sizeof...(args);
146 return formatArgument<maxArgs, maxArgs>(data, specifierString, parsedPosition, forward<Types>(args)...);
151 template <
typename... Types>
152 static bool executeFormat(StringFormatOutput& data, RangeIterator it, Types&&... args)
161 if (it.advanceUntilMatchesAny({
'{',
'}'}, matchedChar))
163 if (it.isFollowedBy(matchedChar))
166 (void)it.stepForward();
169 (void)it.stepForward();
172 else if (matchedChar ==
'{')
177 int32_t parsedPosition = position;
178 if (not parsePosition(data, it, parsedPosition, forward<Types>(args)...))
182 maxPosition =
max(maxPosition, parsedPosition + 1);
193 return maxPosition ==
static_cast<int32_t>(
sizeof...(args));
199template <
typename RangeIterator>
200template <
typename... Types>
204 if (Implementation::executeFormat(data, fmt.
getIterator<RangeIterator>(), forward<Types>(args)...))
217template <>
struct SC_COMPILER_EXPORT StringFormatterFor<double> {
static bool format(StringFormatOutput&,
const StringView,
const double);};
218#if SC_COMPILER_MSVC || SC_COMPILER_CLANG_CL
219#if SC_PLATFORM_64_BIT == 0
223#if !SC_PLATFORM_LINUX
236template <>
struct SC_COMPILER_EXPORT StringFormatterFor<char> {
static bool format(StringFormatOutput&,
const StringView,
const char);};
237template <>
struct SC_COMPILER_EXPORT StringFormatterFor<bool> {
static bool format(StringFormatOutput&,
const StringView,
const bool);};
238template <>
struct SC_COMPILER_EXPORT StringFormatterFor<StringView> {
static bool format(StringFormatOutput&,
const StringView,
const StringView);};
239template <>
struct SC_COMPILER_EXPORT StringFormatterFor<String> {
static bool format(StringFormatOutput&,
const StringView,
const String&);};
240template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const char*> {
static bool format(StringFormatOutput&,
const StringView,
const char*);};
241template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const void*> {
static bool format(StringFormatOutput&,
const StringView,
const void*);};
242#if SC_PLATFORM_WINDOWS
243template <>
struct SC_COMPILER_EXPORT StringFormatterFor<wchar_t> {
static bool format(StringFormatOutput&,
const StringView,
const wchar_t);};
244template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const wchar_t*> {
static bool format(StringFormatOutput&,
const StringView,
const wchar_t*);};
247template <
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());}};
251struct StringFormatterFor<char[N]>
253 static bool format(StringFormatOutput& data,
const StringView specifier,
const char* str)
256 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.
An heap allocated byte buffer that can optionally use an inline buffer.
Definition: Buffer.h:25
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:743
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