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);
}
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)

View File

@ -5,6 +5,7 @@
#define BUFFERED_OUTPUT_STREAM_HXX
#include "util/DynamicFifoBuffer.hxx"
#include "util/SpanCast.hxx"
#include <fmt/core.h>
#if FMT_VERSION >= 80000 && FMT_VERSION < 90000
@ -12,6 +13,7 @@
#endif
#include <cstddef>
#include <string_view>
#ifdef _UNICODE
#include <wchar.h>
@ -43,6 +45,10 @@ public:
*/
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
* 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
};