diff --git a/include/fmt/format.h b/include/fmt/format.h index 2e782926..3c6b04fd 100644 --- a/include/fmt/format.h +++ b/include/fmt/format.h @@ -1431,6 +1431,22 @@ OutputIt write_int(OutputIt out, int num_digits, string_view prefix, }); } +template +OutputIt write(OutputIt out, basic_string_view s, + const basic_format_specs& specs = {}) { + auto data = s.data(); + auto size = s.size(); + if (specs.precision >= 0 && to_unsigned(specs.precision) < size) + size = code_point_index(s, to_unsigned(specs.precision)); + auto width = specs.width != 0 + ? count_code_points(basic_string_view(data, size)) + : 0; + using iterator = remove_reference_t; + return write_padded(out, specs, size, width, [=](iterator it) { + return copy_str(data, data + size, it); + }); +} + // The handle_int_type_spec handler that writes an integer. template struct int_writer { OutputIt out; @@ -1529,7 +1545,7 @@ template struct int_writer { if (group == groups.cend()) size += sep_size * ((n - 1) / groups.back()); char digits[40]; format_decimal(digits, abs_value, num_digits); - memory_buffer buffer; + basic_memory_buffer buffer; buffer.resize(size); basic_string_view s(&sep, sep_size); // Index of a decimal digit with the least significant digit having index 0. @@ -1537,7 +1553,7 @@ template struct int_writer { group = groups.cbegin(); auto p = buffer.data() + size; for (int i = num_digits - 1; i >= 0; --i) { - *--p = digits[i]; + *--p = static_cast(digits[i]); if (*group <= 0 || ++digit_index % *group != 0 || *group == max_value()) continue; @@ -1546,9 +1562,10 @@ template struct int_writer { ++group; } p -= s.size(); - std::uninitialized_copy(s.data(), s.data() + s.size(), p); + std::uninitialized_copy(s.data(), s.data() + s.size(), + make_checked(p, s.size())); } - write_bytes(out, {buffer.data(), buffer.size()}, specs); + write(out, basic_string_view(buffer.data(), buffer.size()), specs); } void on_chr() { *out++ = static_cast(abs_value); } @@ -1633,22 +1650,6 @@ OutputIt write_char(OutputIt out, Char value, }); } -template -OutputIt write(OutputIt out, basic_string_view s, - const basic_format_specs& specs = {}) { - auto data = s.data(); - auto size = s.size(); - if (specs.precision >= 0 && to_unsigned(specs.precision) < size) - size = code_point_index(s, to_unsigned(specs.precision)); - auto width = specs.width != 0 - ? count_code_points(basic_string_view(data, size)) - : 0; - using iterator = remove_reference_t; - return write_padded(out, specs, size, width, [=](iterator it) { - return copy_str(data, data + size, it); - }); -} - template OutputIt write_ptr(OutputIt out, UIntPtr value, const basic_format_specs* specs) {