4#include "../Strings/StringView.h"
14struct StringFormatterFor;
50 StringEncoding encoding;
51 size_t backupSize = 0;
76template <
typename RangeIterator>
85 template <
typename... Types>
89 struct Implementation;
98template <
typename RangeIterator>
101 template <
int Total,
int N,
typename T,
typename... Rest>
102 static bool formatArgument(StringFormatOutput& data, StringView specifier,
int position, T&& arg, Rest&&... rest)
104 if (position == Total - N)
106 using First =
typename TypeTraits::RemoveConst<typename TypeTraits::RemoveReference<T>::type>::type;
107 return StringFormatterFor<First>::format(data, specifier, arg);
111 return formatArgument<Total, N - 1>(data, specifier, position,
forward<Rest>(rest)...);
115 template <
int Total,
int N,
typename... Args>
117 StringView,
int, Args...)
122 template <
typename... Types>
123 static bool parsePosition(StringFormatOutput& data, RangeIterator& it,
int32_t& parsedPosition, Types&&... args)
125 const auto startOfSpecifier = it;
126 if (it.advanceUntilMatches(
'}'))
128 auto specifier = startOfSpecifier.sliceFromStartUntil(it);
129 auto specifierPosition = specifier;
130 if (specifier.advanceUntilMatches(
':'))
132 specifierPosition = startOfSpecifier.sliceFromStartUntil(specifier);
133 (void)specifier.stepForward();
135 (void)specifierPosition.stepForward();
136 (void)it.stepForward();
139 if (not positionString.isEmpty())
141 if (not positionString.parseInt32(parsedPosition))
146 constexpr auto maxArgs =
sizeof...(args);
147 return formatArgument<maxArgs, maxArgs>(data, specifierString, parsedPosition,
forward<Types>(args)...);
152 template <
typename... Types>
153 static bool executeFormat(StringFormatOutput& data, RangeIterator it, Types&&... args)
162 if (it.advanceUntilMatchesAny({
'{',
'}'}, matchedChar))
164 if (it.isFollowedBy(matchedChar))
167 (void)it.stepForward();
170 (void)it.stepForward();
173 else if (matchedChar ==
'{')
178 int32_t parsedPosition = position;
179 if (not parsePosition(data, it, parsedPosition,
forward<Types>(args)...))
183 maxPosition =
max(maxPosition, parsedPosition + 1);
194 return maxPosition ==
static_cast<int32_t>(
sizeof...(args));
200template <
typename RangeIterator>
201template <
typename... Types>
205 if (Implementation::executeFormat(data, fmt.
getIterator<RangeIterator>(), forward<Types>(args)...))
218template <>
struct SC_COMPILER_EXPORT StringFormatterFor<double> {
static bool format(StringFormatOutput&,
const StringView,
const double);};
219#if SC_COMPILER_MSVC || SC_COMPILER_CLANG_CL
220#if SC_PLATFORM_64_BIT == 0
224#if !SC_PLATFORM_LINUX
237template <>
struct SC_COMPILER_EXPORT StringFormatterFor<char> {
static bool format(StringFormatOutput&,
const StringView,
const char);};
238template <>
struct SC_COMPILER_EXPORT StringFormatterFor<bool> {
static bool format(StringFormatOutput&,
const StringView,
const bool);};
239template <>
struct SC_COMPILER_EXPORT StringFormatterFor<StringView> {
static bool format(StringFormatOutput&,
const StringView,
const StringView);};
240template <>
struct SC_COMPILER_EXPORT StringFormatterFor<String> {
static bool format(StringFormatOutput&,
const StringView,
const String&);};
241template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const char*> {
static bool format(StringFormatOutput&,
const StringView,
const char*);};
242template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const void*> {
static bool format(StringFormatOutput&,
const StringView,
const void*);};
243#if SC_PLATFORM_WINDOWS
244template <>
struct SC_COMPILER_EXPORT StringFormatterFor<wchar_t> {
static bool format(StringFormatOutput&,
const StringView,
const wchar_t);};
245template <>
struct SC_COMPILER_EXPORT StringFormatterFor<const wchar_t*> {
static bool format(StringFormatOutput&,
const StringView,
const wchar_t*);};
247template <>
struct SC_COMPILER_EXPORT StringFormatterFor<StringSpan> {
static bool format(StringFormatOutput&,
const StringView,
const StringSpan);};
249template <
int N>
struct 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)
257 const StringView sv({str, N - 1},
true, StringEncoding::Ascii);
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
unsigned short uint16_t
Platform independent (2) bytes unsigned int.
Definition PrimitiveTypes.h:37
constexpr T && forward(typename TypeTraits::RemoveReference< T >::type &value)
Forwards an lvalue or an rvalue as an rvalue reference.
Definition Compiler.h:272
unsigned char uint8_t
Platform independent (1) byte unsigned int.
Definition PrimitiveTypes.h:36
unsigned long size_t
Platform independent unsigned size type.
Definition PrimitiveTypes.h:56
unsigned long long uint64_t
Platform independent (8) bytes unsigned int.
Definition PrimitiveTypes.h:42
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
long long int64_t
Platform independent (8) bytes signed int.
Definition PrimitiveTypes.h:50
signed char int8_t
Platform independent (1) byte signed int.
Definition PrimitiveTypes.h:44
signed long ssize_t
Platform independent signed size type.
Definition PrimitiveTypes.h:57
int int32_t
Platform independent (4) bytes signed int.
Definition PrimitiveTypes.h:46
uint32_t StringCodePoint
UTF code point (32 bit)
Definition StringIterator.h:14
An heap allocated byte buffer that can optionally use an inline buffer.
Definition Buffer.h:29
Writes to console using SC::StringFormat.
Definition Console.h:27
String with compile time configurable inline storage (small string optimization)
Definition StringFormat.h:12
Non-owning view over a range of characters with UTF Encoding.
Definition StringView.h:48
constexpr StringIterator getIterator() const
Returns a StringIterator from current StringView.
Definition StringView.h:654
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