Skip to content

Commit 559a8ba

Browse files
committed
Add formatting method to format to an existing output iterator
1 parent 30743a8 commit 559a8ba

File tree

1 file changed

+49
-12
lines changed

1 file changed

+49
-12
lines changed

fly/types/string/string.hpp

+49-12
Original file line numberDiff line numberDiff line change
@@ -412,7 +412,7 @@ class BasicString
412412
* 3. An optional colon ":" following by formatting options.
413413
* 4. A final "}" character.
414414
*
415-
* For a detailed description of replacement fields see fly::detail::BasicFormatSpecifier.
415+
* For a detailed description of replacement fields, see fly::detail::BasicFormatSpecifier.
416416
*
417417
* This implementation differs from std::format in the following ways:
418418
*
@@ -456,6 +456,27 @@ class BasicString
456456
static string_type
457457
format(FormatString<ParameterTypes...> &&fmt, ParameterTypes &&...parameters);
458458

459+
/**
460+
* Format a string with a set of format parameters to an existing output iterator. Based
461+
* strongly upon: https://en.cppreference.com/w/cpp/utility/format/format.
462+
*
463+
* For a detailed description of string formatting, see fly::BasicString::format.
464+
*
465+
* @tparam OutputIterator The type of the output iterator.
466+
* @tparam ParameterTypes Variadic format parameter types.
467+
*
468+
* @param output The output iterator to write to.
469+
* @param fmt The string to format.
470+
* @param parameters The variadic list of format parameters to be formatted.
471+
*
472+
* @return A string that has been formatted with the given format parameters.
473+
*/
474+
template <typename OutputIterator, typename... ParameterTypes>
475+
static void format_to(
476+
OutputIterator output,
477+
FormatString<ParameterTypes...> &&fmt,
478+
ParameterTypes &&...parameters);
479+
459480
/**
460481
* Concatenate a list of objects with the given separator.
461482
*
@@ -849,26 +870,44 @@ auto BasicString<CharType>::generate_random_string(size_type length) -> string_t
849870
//==================================================================================================
850871
template <typename CharType>
851872
template <typename... ParameterTypes>
852-
auto BasicString<CharType>::format(
873+
inline auto
874+
BasicString<CharType>::format(FormatString<ParameterTypes...> &&fmt, ParameterTypes &&...parameters)
875+
-> string_type
876+
{
877+
string_type formatted;
878+
formatted.reserve(fmt.context().view().size() * 2);
879+
880+
format_to(
881+
std::back_inserter(formatted),
882+
std::move(fmt),
883+
std::forward<ParameterTypes>(parameters)...);
884+
885+
return formatted;
886+
}
887+
888+
//==================================================================================================
889+
template <typename CharType>
890+
template <typename OutputIterator, typename... ParameterTypes>
891+
void BasicString<CharType>::format_to(
892+
OutputIterator output,
853893
FormatString<ParameterTypes...> &&fmt,
854-
ParameterTypes &&...parameters) -> string_type
894+
ParameterTypes &&...parameters)
855895
{
856-
using FormatContext =
857-
detail::BasicFormatContext<std::back_insert_iterator<string_type>, char_type>;
896+
using FormatContext = detail::BasicFormatContext<OutputIterator, char_type>;
858897

859898
if (fmt.context().has_error())
860899
{
861-
return format(FLY_ARR(char_type, "Ignored invalid formatter: {}"), fmt.context().error());
900+
format_to(
901+
output,
902+
FLY_ARR(char_type, "Ignored invalid formatter: {}"),
903+
fmt.context().error());
862904
}
863905

864906
const view_type view = fmt.context().view();
865907

866-
string_type formatted;
867-
formatted.reserve(view.size() * 2);
868-
869908
auto params =
870909
detail::make_format_parameters<FormatContext>(std::forward<ParameterTypes>(parameters)...);
871-
FormatContext context(std::back_inserter(formatted), params);
910+
FormatContext context(output, params);
872911

873912
for (std::size_t pos = 0; pos < view.size();)
874913
{
@@ -901,8 +940,6 @@ auto BasicString<CharType>::format(
901940
break;
902941
}
903942
}
904-
905-
return formatted;
906943
}
907944

908945
//==================================================================================================

0 commit comments

Comments
 (0)