1
1
#pragma once
2
2
3
3
#include " fly/types/string/detail/classifier.hpp"
4
+ #include " fly/types/string/detail/format_specifier.hpp"
4
5
#include " fly/types/string/detail/string_traits.hpp"
5
6
#include " fly/types/string/string_formatters.hpp"
6
7
@@ -39,7 +40,11 @@ struct StringValue
39
40
const void *m_value;
40
41
std::size_t m_size;
41
42
42
- void (*m_format)(const void *value, std::size_t size, FormatContext &context);
43
+ void (*m_format)(
44
+ const void *value,
45
+ std::size_t size,
46
+ FormatContext &context,
47
+ BasicFormatSpecifier<typename FormatContext::char_type> &&specifier);
43
48
};
44
49
45
50
/* *
@@ -59,7 +64,10 @@ struct StandardValue
59
64
bool m_bool;
60
65
};
61
66
62
- void (*m_format)(StandardValue value, FormatContext &context);
67
+ void (*m_format)(
68
+ StandardValue value,
69
+ FormatContext &context,
70
+ BasicFormatSpecifier<typename FormatContext::char_type> &&specifier);
63
71
};
64
72
65
73
/* *
@@ -83,9 +91,14 @@ void format_user_defined_value(const void *value, FormatContext &context);
83
91
* @param value A pointer to the type-erased string to format.
84
92
* @param size The size of the string to format.
85
93
* @param context The context holding the formatting state.
94
+ * @param specifier The replacement field to be replaced.
86
95
*/
87
96
template <typename FormatContext, typename T>
88
- void format_string_value (const void *value, std::size_t size, FormatContext &context);
97
+ void format_string_value (
98
+ const void *value,
99
+ std::size_t size,
100
+ FormatContext &context,
101
+ BasicFormatSpecifier<typename FormatContext::char_type> &&specifier);
89
102
90
103
/* *
91
104
* Re-form a type-erased standard value and format that value.
@@ -95,9 +108,13 @@ void format_string_value(const void *value, std::size_t size, FormatContext &con
95
108
*
96
109
* @param value The container holding the type-erased value.
97
110
* @param context The context holding the formatting state.
111
+ * @param specifier The replacement field to be replaced.
98
112
*/
99
113
template <typename FormatContext, typename T>
100
- void format_standard_value (StandardValue<FormatContext> value, FormatContext &context);
114
+ void format_standard_value (
115
+ StandardValue<FormatContext> value,
116
+ FormatContext &context,
117
+ BasicFormatSpecifier<typename FormatContext::char_type> &&specifier);
101
118
102
119
/* *
103
120
* A container to hold a single type-erased format parameter.
@@ -108,6 +125,8 @@ void format_standard_value(StandardValue<FormatContext> value, FormatContext &co
108
125
template <typename FormatContext>
109
126
class BasicFormatParameter
110
127
{
128
+ using char_type = typename FormatContext::char_type;
129
+
111
130
public:
112
131
/* *
113
132
* Constructor. Initialize the format parameter to an invalid state.
@@ -169,8 +188,10 @@ class BasicFormatParameter
169
188
* Apply the type-erased formatting function to the stored format parameter.
170
189
*
171
190
* @param context The context holding the formatting state.
191
+ * @param specifier The replacement field to be replaced.
172
192
*/
173
- constexpr void format (FormatContext &context) const ;
193
+ constexpr void
194
+ format (FormatContext &context, BasicFormatSpecifier<char_type> &&specifier) const ;
174
195
175
196
/* *
176
197
* Apply the provided visitor to the stored format parameter.
@@ -265,19 +286,28 @@ inline void format_user_defined_value(const void *value, FormatContext &context)
265
286
266
287
// ==================================================================================================
267
288
template <typename FormatContext, typename T>
268
- inline void format_string_value (const void *value, std::size_t size, FormatContext &context)
289
+ inline void format_string_value (
290
+ const void *value,
291
+ std::size_t size,
292
+ FormatContext &context,
293
+ BasicFormatSpecifier<typename FormatContext::char_type> &&specifier)
269
294
{
270
- static_assert (detail::is_supported_character_v<T>);
295
+ using view_type = std::basic_string_view<T>;
296
+
297
+ Formatter<view_type, typename FormatContext::char_type> formatter (std::move (specifier));
271
298
272
- std::basic_string_view<T> view (static_cast <const T *>(value), size);
273
- Formatter< decltype (view), typename FormatContext::char_type>() .format (view, context);
299
+ view_type view (static_cast <const T *>(value), size);
300
+ formatter .format (view, context);
274
301
}
275
302
276
303
// ==================================================================================================
277
304
template <typename FormatContext, typename T>
278
- inline void format_standard_value (StandardValue<FormatContext> value, FormatContext &context)
305
+ inline void format_standard_value (
306
+ StandardValue<FormatContext> value,
307
+ FormatContext &context,
308
+ BasicFormatSpecifier<typename FormatContext::char_type> &&specifier)
279
309
{
280
- Formatter<T, typename FormatContext::char_type> formatter {} ;
310
+ Formatter<T, typename FormatContext::char_type> formatter ( std::move (specifier)) ;
281
311
282
312
if constexpr (BasicFormatTraits::is_pointer_v<T>)
283
313
{
@@ -347,25 +377,25 @@ constexpr inline BasicFormatParameter<FormatContext>::BasicFormatParameter(const
347
377
{
348
378
using U = std::remove_cvref_t <T>;
349
379
350
- using string_type = fly::detail::is_like_supported_string_t <T>;
351
- using char_type = typename string_type ::value_type;
352
- using view_type = typename fly::detail::BasicStringTraits<char_type >::view_type;
380
+ using string_like_type = fly::detail::is_like_supported_string_t <T>;
381
+ using char_like_type = typename string_like_type ::value_type;
382
+ using view_like_type = typename fly::detail::BasicStringTraits<char_like_type >::view_type;
353
383
354
- view_type view;
384
+ view_like_type view;
355
385
356
386
if constexpr (std::is_array_v<U> || std::is_pointer_v<U>)
357
387
{
358
- view = view_type (value, BasicClassifier<char_type >::size (value));
388
+ view = view_like_type (value, BasicClassifier<char_like_type >::size (value));
359
389
}
360
390
else
361
391
{
362
- view = view_type (value);
392
+ view = view_like_type (value);
363
393
}
364
394
365
395
m_value.m_string = {
366
396
static_cast <const void *>(view.data ()),
367
397
view.size (),
368
- format_string_value<FormatContext, char_type >};
398
+ format_string_value<FormatContext, char_like_type >};
369
399
}
370
400
371
401
// ==================================================================================================
@@ -426,15 +456,21 @@ constexpr inline BasicFormatParameter<FormatContext>::BasicFormatParameter(T val
426
456
427
457
// ==================================================================================================
428
458
template <typename FormatContext>
429
- constexpr inline void BasicFormatParameter<FormatContext>::format(FormatContext &context) const
459
+ constexpr inline void BasicFormatParameter<FormatContext>::format(
460
+ FormatContext &context,
461
+ BasicFormatSpecifier<char_type> &&specifier) const
430
462
{
431
463
switch (m_type)
432
464
{
433
465
case Type::UserDefined:
434
466
m_value.m_user_defined .m_format (m_value.m_user_defined .m_value , context);
435
467
break ;
436
468
case Type::String:
437
- m_value.m_string .m_format (m_value.m_string .m_value , m_value.m_string .m_size , context);
469
+ m_value.m_string .m_format (
470
+ m_value.m_string .m_value ,
471
+ m_value.m_string .m_size ,
472
+ context,
473
+ std::move (specifier));
438
474
break ;
439
475
case Type::Pointer:
440
476
case Type::SignedInt:
@@ -443,7 +479,7 @@ constexpr inline void BasicFormatParameter<FormatContext>::format(FormatContext
443
479
case Type::Double:
444
480
case Type::LongDouble:
445
481
case Type::Bool:
446
- m_value.m_standard .m_format (m_value.m_standard , context);
482
+ m_value.m_standard .m_format (m_value.m_standard , context, std::move (specifier) );
447
483
break ;
448
484
default :
449
485
break ;
0 commit comments