SA_Logger
Sapphire's Suite's C++ Logger.
Loading...
Searching...
No Matches
StringFormat.hpp
1// Copyright (c) 2023 Sapphire's Suite. All Rights Reserved.
2
3#pragma once
4
5#ifndef SAPPHIRE_LOGGER_STRING_FORMAT_GUARD
6#define SAPPHIRE_LOGGER_STRING_FORMAT_GUARD
7
8#include <string>
9
10namespace SA
11{
12 namespace Intl
13 {
14 template <typename StringT>
15 uint32_t ReplaceAllInString(StringT& _str, const StringT& _what, const StringT& _with){
16
17 uint32_t count = 0u;
18
19 for(typename StringT::size_type pos{}; (pos = _str.find(_what, pos)) != _str.npos; pos += _with.size())
20 {
21 _str.replace(pos, _what.size(), _with);
22 ++count;
23 }
24
25 return count;
26 }
27
28 template <typename FirstT, typename... Args>
29 void StringFormatImpl(std::wstring& _str, uint32_t _depth, FirstT&& _first, Args&&... _args)
30 {
31 const uint32_t count = Intl::ReplaceAllInString(_str, L'%' + std::to_wstring(_depth), ToWString(_first));
32
33 // No more args to parse.
34 // Must be constexpr for valid recursive compilation.
35 if constexpr (sizeof...(Args) == 0)
36 return;
37 else
38 {
39 // No more token to parse.
40 if(!count)
41 return;
42
43 StringFormatImpl(_str, _depth + 1, std::forward<Args>(_args)...);
44 }
45 }
46 }
47
48 template <typename FirstT, typename... Args>
49 std::wstring StringFormat(FirstT _first, Args&&... _args)
50 {
51 // Force convert to wstring. Input might be string or any object.
52 std::wstring str = SA::ToWString(std::forward<FirstT>(_first));
53
54 // Arguments means string formatting.
55 if constexpr (sizeof...(Args) != 0)
56 Intl::StringFormatImpl(str, 1u, std::forward<Args>(_args)...);
57
58 return str;
59 }
60}
61
62#endif // GUARD
std::wstring ToWString(const Log &_log)
ToWString implementation for log.