@@ -412,7 +412,7 @@ class BasicString
412
412
* 3. An optional colon ":" following by formatting options.
413
413
* 4. A final "}" character.
414
414
*
415
- * For a detailed description of replacement fields see fly::detail::BasicFormatSpecifier.
415
+ * For a detailed description of replacement fields, see fly::detail::BasicFormatSpecifier.
416
416
*
417
417
* This implementation differs from std::format in the following ways:
418
418
*
@@ -456,6 +456,27 @@ class BasicString
456
456
static string_type
457
457
format (FormatString<ParameterTypes...> &&fmt, ParameterTypes &&...parameters);
458
458
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
+
459
480
/* *
460
481
* Concatenate a list of objects with the given separator.
461
482
*
@@ -849,26 +870,44 @@ auto BasicString<CharType>::generate_random_string(size_type length) -> string_t
849
870
// ==================================================================================================
850
871
template <typename CharType>
851
872
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,
853
893
FormatString<ParameterTypes...> &&fmt,
854
- ParameterTypes &&...parameters) -> string_type
894
+ ParameterTypes &&...parameters)
855
895
{
856
- using FormatContext =
857
- detail::BasicFormatContext<std::back_insert_iterator<string_type>, char_type>;
896
+ using FormatContext = detail::BasicFormatContext<OutputIterator, char_type>;
858
897
859
898
if (fmt.context ().has_error ())
860
899
{
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 ());
862
904
}
863
905
864
906
const view_type view = fmt.context ().view ();
865
907
866
- string_type formatted;
867
- formatted.reserve (view.size () * 2 );
868
-
869
908
auto params =
870
909
detail::make_format_parameters<FormatContext>(std::forward<ParameterTypes>(parameters)...);
871
- FormatContext context (std::back_inserter (formatted) , params);
910
+ FormatContext context (output , params);
872
911
873
912
for (std::size_t pos = 0 ; pos < view.size ();)
874
913
{
@@ -901,8 +940,6 @@ auto BasicString<CharType>::format(
901
940
break ;
902
941
}
903
942
}
904
-
905
- return formatted;
906
943
}
907
944
908
945
// ==================================================================================================
0 commit comments