io/BufferedOutputStream: use std::string_view

This commit is contained in:
Max Kellermann 2023-05-15 11:11:41 +02:00
parent 01c02a1ef8
commit e72d27566c
2 changed files with 22 additions and 25 deletions

View File

@ -45,12 +45,6 @@ BufferedOutputStream::Write(const void *data, std::size_t size)
os.Write(data, size); os.Write(data, size);
} }
void
BufferedOutputStream::Write(const char *p)
{
Write(p, strlen(p));
}
void void
BufferedOutputStream::VFmt(fmt::string_view format_str, fmt::format_args args) 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 #ifdef _UNICODE
void void
BufferedOutputStream::Write(const wchar_t *p) BufferedOutputStream::WriteWideToUTF8(std::wstring_view src)
{ {
WriteWideToUTF8(p, wcslen(p)); if (src.empty())
}
void
BufferedOutputStream::WriteWideToUTF8(const wchar_t *src,
std::size_t src_length)
{
if (src_length == 0)
return; return;
auto r = buffer.Write(); auto r = buffer.Write();
@ -86,7 +73,7 @@ BufferedOutputStream::WriteWideToUTF8(const wchar_t *src,
r = buffer.Write(); 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(), (char *)r.data(), r.size(),
nullptr, nullptr); nullptr, nullptr);
if (length <= 0) { if (length <= 0) {
@ -95,13 +82,13 @@ BufferedOutputStream::WriteWideToUTF8(const wchar_t *src,
throw MakeLastError(error, "UTF-8 conversion failed"); throw MakeLastError(error, "UTF-8 conversion failed");
/* how much buffer do we need? */ /* 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); nullptr, 0, nullptr, nullptr);
if (length <= 0) if (length <= 0)
throw MakeLastError(error, "UTF-8 conversion failed"); throw MakeLastError(error, "UTF-8 conversion failed");
/* grow the buffer and try again */ /* 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, (char *)buffer.Write(length), length,
nullptr, nullptr); nullptr, nullptr);
if (length <= 0) if (length <= 0)

View File

@ -5,6 +5,7 @@
#define BUFFERED_OUTPUT_STREAM_HXX #define BUFFERED_OUTPUT_STREAM_HXX
#include "util/DynamicFifoBuffer.hxx" #include "util/DynamicFifoBuffer.hxx"
#include "util/SpanCast.hxx"
#include <fmt/core.h> #include <fmt/core.h>
#if FMT_VERSION >= 80000 && FMT_VERSION < 90000 #if FMT_VERSION >= 80000 && FMT_VERSION < 90000
@ -12,6 +13,7 @@
#endif #endif
#include <cstddef> #include <cstddef>
#include <string_view>
#ifdef _UNICODE #ifdef _UNICODE
#include <wchar.h> #include <wchar.h>
@ -43,6 +45,10 @@ public:
*/ */
void Write(const void *data, std::size_t size); void Write(const void *data, std::size_t size);
void Write(std::span<const std::byte> src) {
Write(src.data(), src.size());
}
/** /**
* Write the given object. Note that this is only safe with * Write the given object. Note that this is only safe with
* POD types. Types with padding can expose sensitive data. * 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); void VFmt(fmt::string_view format_str, fmt::format_args args);
@ -80,16 +88,18 @@ public:
#ifdef _UNICODE #ifdef _UNICODE
/** /**
* Write one narrow character. * Write one wide character.
*/ */
void Write(const wchar_t &ch) { 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 #endif
/** /**
@ -108,7 +118,7 @@ private:
bool AppendToBuffer(const void *data, std::size_t size) noexcept; bool AppendToBuffer(const void *data, std::size_t size) noexcept;
#ifdef _UNICODE #ifdef _UNICODE
void WriteWideToUTF8(const wchar_t *p, std::size_t length); void WriteWideToUTF8(std::wstring_view src);
#endif #endif
}; };