From e72d27566c61865c8955024608508956f0f78f58 Mon Sep 17 00:00:00 2001 From: Max Kellermann Date: Mon, 15 May 2023 11:11:41 +0200 Subject: [PATCH] io/BufferedOutputStream: use std::string_view --- src/io/BufferedOutputStream.cxx | 23 +++++------------------ src/io/BufferedOutputStream.hxx | 24 +++++++++++++++++------- 2 files changed, 22 insertions(+), 25 deletions(-) diff --git a/src/io/BufferedOutputStream.cxx b/src/io/BufferedOutputStream.cxx index f63ed7f0b..52dfdd4df 100644 --- a/src/io/BufferedOutputStream.cxx +++ b/src/io/BufferedOutputStream.cxx @@ -45,12 +45,6 @@ BufferedOutputStream::Write(const void *data, std::size_t size) os.Write(data, size); } -void -BufferedOutputStream::Write(const char *p) -{ - Write(p, strlen(p)); -} - void BufferedOutputStream::VFmt(fmt::string_view format_str, fmt::format_args args) { @@ -68,16 +62,9 @@ BufferedOutputStream::VFmt(fmt::string_view format_str, fmt::format_args args) #ifdef _UNICODE void -BufferedOutputStream::Write(const wchar_t *p) +BufferedOutputStream::WriteWideToUTF8(std::wstring_view src) { - WriteWideToUTF8(p, wcslen(p)); -} - -void -BufferedOutputStream::WriteWideToUTF8(const wchar_t *src, - std::size_t src_length) -{ - if (src_length == 0) + if (src.empty()) return; auto r = buffer.Write(); @@ -86,7 +73,7 @@ BufferedOutputStream::WriteWideToUTF8(const wchar_t *src, r = buffer.Write(); } - int length = WideCharToMultiByte(CP_UTF8, 0, src, src_length, + int length = WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(), (char *)r.data(), r.size(), nullptr, nullptr); if (length <= 0) { @@ -95,13 +82,13 @@ BufferedOutputStream::WriteWideToUTF8(const wchar_t *src, throw MakeLastError(error, "UTF-8 conversion failed"); /* how much buffer do we need? */ - length = WideCharToMultiByte(CP_UTF8, 0, src, src_length, + length = WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(), nullptr, 0, nullptr, nullptr); if (length <= 0) throw MakeLastError(error, "UTF-8 conversion failed"); /* grow the buffer and try again */ - length = WideCharToMultiByte(CP_UTF8, 0, src, src_length, + length = WideCharToMultiByte(CP_UTF8, 0, src.data(), src.size(), (char *)buffer.Write(length), length, nullptr, nullptr); if (length <= 0) diff --git a/src/io/BufferedOutputStream.hxx b/src/io/BufferedOutputStream.hxx index 5395fc452..f37c99884 100644 --- a/src/io/BufferedOutputStream.hxx +++ b/src/io/BufferedOutputStream.hxx @@ -5,6 +5,7 @@ #define BUFFERED_OUTPUT_STREAM_HXX #include "util/DynamicFifoBuffer.hxx" +#include "util/SpanCast.hxx" #include #if FMT_VERSION >= 80000 && FMT_VERSION < 90000 @@ -12,6 +13,7 @@ #endif #include +#include #ifdef _UNICODE #include @@ -43,6 +45,10 @@ public: */ void Write(const void *data, std::size_t size); + void Write(std::span src) { + Write(src.data(), src.size()); + } + /** * Write the given object. Note that this is only safe with * POD types. Types with padding can expose sensitive data. @@ -60,9 +66,11 @@ public: } /** - * Write a null-terminated string. + * Write a string. */ - void Write(const char *p); + void Write(std::string_view src) { + Write(AsBytes(src)); + } void VFmt(fmt::string_view format_str, fmt::format_args args); @@ -80,16 +88,18 @@ public: #ifdef _UNICODE /** - * Write one narrow character. + * Write one wide character. */ void Write(const wchar_t &ch) { - WriteWideToUTF8(&ch, 1); + WriteWideToUTF8({&ch, 1}); } /** - * Write a null-terminated wide string. + * Write a wide string. */ - void Write(const wchar_t *p); + void Write(std::wstring_view src) { + WriteWideToUTF8(src); + } #endif /** @@ -108,7 +118,7 @@ private: bool AppendToBuffer(const void *data, std::size_t size) noexcept; #ifdef _UNICODE - void WriteWideToUTF8(const wchar_t *p, std::size_t length); + void WriteWideToUTF8(std::wstring_view src); #endif };