Skip to content

Commit cc22d1b

Browse files
committed
Move parameter type inference into the replacement field itself
1 parent 80d258a commit cc22d1b

File tree

2 files changed

+25
-22
lines changed

2 files changed

+25
-22
lines changed

fly/types/string/detail/format_specifier.hpp

+24-16
Original file line numberDiff line numberDiff line change
@@ -122,6 +122,14 @@ struct BasicFormatSpecifier
122122

123123
BasicFormatSpecifier() = default;
124124

125+
/**
126+
* Constructor. Initializes the replacement field's format parameter position and infers its
127+
* presentation type.
128+
*
129+
* @param context The context holding the format string parsing state.
130+
*/
131+
explicit constexpr BasicFormatSpecifier(FormatParseContext &context);
132+
125133
BasicFormatSpecifier(BasicFormatSpecifier &&) = default;
126134
BasicFormatSpecifier &operator=(BasicFormatSpecifier &&) = default;
127135

@@ -174,17 +182,6 @@ struct BasicFormatSpecifier
174182
*/
175183
constexpr void parse(FormatParseContext &context);
176184

177-
/**
178-
* Infer a presentation type for a replacement field based on the corresponding format
179-
* parameter's type.
180-
*
181-
* TODO: This should be private. Once the standard fly::Formatters are updated to inherit from
182-
* this structure, they may infer their presentation type.
183-
*
184-
* @param context The context holding the format string parsing state.
185-
*/
186-
constexpr void infer_type(FormatParseContext &context);
187-
188185
/**
189186
* The width formatting option may either be a number or a nested replacement field. If a
190187
* numeric value was specified, return that value. If a nested replacement field was specified,
@@ -337,6 +334,14 @@ struct BasicFormatSpecifier
337334
*/
338335
constexpr void parse_type(FormatParseContext &context);
339336

337+
/**
338+
* Infer a presentation type for a replacement field based on the corresponding format
339+
* parameter's type.
340+
*
341+
* @param context The context holding the format string parsing state.
342+
*/
343+
constexpr void infer_type(FormatParseContext &context);
344+
340345
/**
341346
* After parsing a single replacement field, validate all options that were parsed. Raise an
342347
* error if the field is invalid.
@@ -416,6 +421,14 @@ struct BasicFormatSpecifier
416421
static constexpr const auto s_decimal = FLY_CHR(CharType, '.');
417422
};
418423

424+
//==================================================================================================
425+
template <typename CharType>
426+
constexpr BasicFormatSpecifier<CharType>::BasicFormatSpecifier(FormatParseContext &context) :
427+
m_position(context.next_position())
428+
{
429+
infer_type(context);
430+
}
431+
419432
//==================================================================================================
420433
template <typename CharType>
421434
constexpr void BasicFormatSpecifier<CharType>::parse(FormatParseContext &context)
@@ -587,11 +600,6 @@ constexpr void BasicFormatSpecifier<CharType>::parse_type(FormatParseContext &co
587600
}
588601
}
589602
}
590-
591-
if (m_type == Type::None)
592-
{
593-
infer_type(context);
594-
}
595603
}
596604

597605
//==================================================================================================

fly/types/string/detail/format_string.hpp

+1-6
Original file line numberDiff line numberDiff line change
@@ -164,8 +164,7 @@ constexpr auto BasicFormatString<CharType, ParameterTypes...>::parse_specifier()
164164
// The opening { will have already been consumed, so the starting position is one less.
165165
const auto starting_position = m_context.lexer().position() - 1;
166166

167-
FormatSpecifier specifier {};
168-
specifier.m_position = m_context.next_position();
167+
FormatSpecifier specifier(m_context);
169168

170169
if (m_context.parameter_type(specifier.m_position) == ParameterType::UserDefined)
171170
{
@@ -200,10 +199,6 @@ BasicFormatString<CharType, ParameterTypes...>::parse_standard_specifier(FormatS
200199
{
201200
specifier.parse(m_context);
202201
}
203-
else
204-
{
205-
specifier.infer_type(m_context);
206-
}
207202

208203
if (!m_context.has_error() && !m_context.lexer().consume_if(s_right_brace))
209204
{

0 commit comments

Comments
 (0)