- 001
- 002
- 003
- 004
- 005
- 006
- 007
- 008
- 009
- 010
- 011
- 012
- 013
- 014
- 015
- 016
- 017
- 018
- 019
- 020
- 021
- 022
- 023
- 024
- 025
- 026
- 027
- 028
- 029
- 030
- 031
- 032
- 033
- 034
- 035
- 036
- 037
- 038
- 039
- 040
- 041
- 042
- 043
- 044
- 045
- 046
- 047
- 048
- 049
- 050
- 051
- 052
- 053
- 054
- 055
- 056
- 057
- 058
- 059
- 060
- 061
- 062
- 063
- 064
- 065
- 066
- 067
- 068
- 069
- 070
- 071
- 072
- 073
- 074
- 075
- 076
- 077
- 078
- 079
- 080
- 081
- 082
- 083
- 084
- 085
- 086
- 087
- 088
- 089
- 090
- 091
- 092
- 093
- 094
- 095
- 096
- 097
- 098
- 099
- 100
template<typename CharType>
class Formatter
{
private:
static int _ToString(Int32 Value){
return CString<CharType>::Snprintf(
(CharType*)GSupportToStringBuffer,
TO_STRING_BUFFER_SIZE,
"%" PRId32,
Value);
}
static int _ToString(float Value){
return CString<CharType>::Snprintf(
(CharType*)GSupportToStringBuffer,
TO_STRING_BUFFER_SIZE,
"%f",
Value);
}
template<typename First, typename ... Args>
static void _ConvertArgs(Array<GenericString<CharType>>& _ArgBuffer, const First& _First, const Args& ... _Args) {
_ArgBuffer.Add(ToString(_First));
_ConvertArgs(_ArgBuffer, _Args ...);
}
template<typename First>
static void _ConvertArgs(Array<GenericString<CharType>>& _ArgBuffer, const First& _First) {
_ArgBuffer.Add(ToString(_First));
}
static bool _ScanPlaceholder(const CharType* Format, size_t Index, size_t Length, size_t& Placeholder, size_t& LastIndex) {
size_t i = Index;
size_t j = 0;
while (i < Length && Format[i] != FormatterPlaceholder<CharType>::End)
{
if (!Char<CharType>::IsDigit(Format[i]))
{
return false;
}
else
{
GSupportToStringBuffer[j] = Format[i];
j++;
}
i++;
}
if (i == Length)
return false;
GSupportToStringBuffer[j] = 0;
#if defined(64BIT)
Placeholder = CString<CharType>::Atoi64((const CharType*)GSupportToStringBuffer);
#else
Placeholder = CString<CharType>::Atoi((const CharType*)GSupportToStringBuffer);
#endif
LastIndex = i;
return true;
}
public:
template<typename T>
static GenericString<CharType> ToString(const T& Value) {
int Length = Formatter<CharType>::_ToString(Value);
return GenericString<CharType>((char*)GSupportToStringBuffer, Length);
}
template<typename ... Args>
static GenericString<CharType> Format(const CharType* Format, const Args& ... _Args) {
Array<GenericString<CharType>> _FormatedArgs;
_FormatedArgs.Reserve(sizeof...(Args));
Formatter<CharType>::_ConvertArgs(_FormatedArgs, _Args ...);
const size_t _Length = CString<CharType>::Strlen(Format);
size_t Index = 0;
for (size_t i = 0; i < _Length; i++)
{
if (Format[i] == FormatterPlaceholder<CharType>::Begin)
{
size_t Placeholder = 0;
size_t LastIndex = 0;
if (_ScanPlaceholder(Format, i + 1, _Length, Placeholder, LastIndex) && Placeholder < sizeof...(Args))
{
Memory::Copy(GSupportFormatBuffer + Index, _FormatedArgs[Placeholder].Data(), _FormatedArgs[Placeholder].Length() * sizeof(CharType));
Index += _FormatedArgs[Placeholder].Length();
i = LastIndex;
}
}
else
{
GSupportFormatBuffer[Index] = Format[i];
Index++;
}
}
GSupportFormatBuffer[Index] = 0;
return GenericString<CharType>((const CharType*)GSupportFormatBuffer);
}
};
template<typename T>
forceinline String ToString(const T& Value){
return Formatter<char>::ToString<T>(Value);
}
template<typename ... Args>
forceinline String Format(const char* Format, const Args& ... _Args){
return Formatter<char>::Format(Format, _Args ...);
}
Три года назад писал printf аля Console.WriteLine в C#. Тут порезал до ста строк. https://pastebin.com/8BCLuBEm