From 7fcbd6e474235957f2785c3d9b5344b2acf3ff77 Mon Sep 17 00:00:00 2001 From: Egor Tensin Date: Thu, 4 Jun 2026 22:39:44 +0200 Subject: upgrade to std::string_view --- include/winapi/buffer.hpp | 11 ++++++----- include/winapi/cmd_line.hpp | 7 ++++--- include/winapi/file.hpp | 10 +++++----- include/winapi/handle.hpp | 17 +++++++++++++---- include/winapi/path.hpp | 4 ++-- include/winapi/process_io.hpp | 8 ++++---- include/winapi/shmem.hpp | 10 +++++----- src/cmd_line.cpp | 7 ++++--- src/error.cpp | 10 ++++++---- src/file.cpp | 18 +++++++++--------- src/path.cpp | 10 +++++----- src/process_io.cpp | 8 ++++---- src/shmem.cpp | 6 +++--- 13 files changed, 70 insertions(+), 56 deletions(-) diff --git a/include/winapi/buffer.hpp b/include/winapi/buffer.hpp index a9243cb..bebd05f 100644 --- a/include/winapi/buffer.hpp +++ b/include/winapi/buffer.hpp @@ -11,6 +11,7 @@ #include #include #include +#include #include #include @@ -33,19 +34,19 @@ public: /** Construct a buffer from an instance of `std::vector`. */ explicit Buffer(Parent&& src) : Parent{std::move(src)} {} - /** Construct a buffer from an instance of `std::basic_string`. */ + /** Construct a buffer from a string view. */ template - explicit Buffer(const std::basic_string& src) { + explicit Buffer(std::basic_string_view src) { set(src); } /** Construct a buffer from a memory region. */ Buffer(const void* src, std::size_t nb) { set(src, nb); } - /** Replace the buffer's contents with the data from `std::basic_string`. */ + /** Replace the buffer's contents with the data from a string view. */ template - void set(const std::basic_string& src) { - set(src.c_str(), src.length() * sizeof(std::basic_string::char_type)); + void set(std::basic_string_view src) { + set(src.data(), src.length() * sizeof(std::basic_string_view::char_type)); } /** Replace the buffer's contents with the data from a memory region. */ diff --git a/include/winapi/cmd_line.hpp b/include/winapi/cmd_line.hpp index d1a4a7e..af83cac 100644 --- a/include/winapi/cmd_line.hpp +++ b/include/winapi/cmd_line.hpp @@ -6,6 +6,7 @@ #pragma once #include +#include #include #include @@ -27,7 +28,7 @@ public: * Parse a command line from a string. * @param src UTF-8 encoded string. */ - static CommandLine parse(const std::string& src); + static CommandLine parse(std::string_view src); /** * Build a command line from main() arguments. @@ -64,9 +65,9 @@ public: */ explicit CommandLine(std::vector argv); - static std::string escape(const std::string&); + static std::string escape(std::string_view); - static std::string escape_cmd(const std::string&); + static std::string escape_cmd(std::string_view); /** * Build a string that represents this command line. diff --git a/include/winapi/file.hpp b/include/winapi/file.hpp index b1c8ce3..4ec717b 100644 --- a/include/winapi/file.hpp +++ b/include/winapi/file.hpp @@ -14,7 +14,7 @@ #include #include -#include +#include #include namespace winapi { @@ -38,20 +38,20 @@ public: }; /** Open file for reading. */ - static File open_r(const std::string&); + static File open_r(std::string_view); /** @overload */ static File open_r(const CanonicalPath&); /** Open file for reading (inc. ability to read its attributes). */ - static File open_read_attributes(const std::string&); + static File open_read_attributes(std::string_view); /** @overload */ static File open_read_attributes(const CanonicalPath&); /** Open file for writing. */ - static File open_w(const std::string&); + static File open_w(std::string_view); /** @overload */ static File open_w(const CanonicalPath&); /** Delete a file. */ - static void remove(const std::string&); + static void remove(std::string_view); /** @overload */ static void remove(const CanonicalPath&); diff --git a/include/winapi/handle.hpp b/include/winapi/handle.hpp index b5a7547..7af7252 100644 --- a/include/winapi/handle.hpp +++ b/include/winapi/handle.hpp @@ -11,7 +11,7 @@ #include #include -#include +#include #include namespace winapi { @@ -69,14 +69,23 @@ public: * @param buffer Binary data to write. */ void write(const Buffer& buffer) const; - /** * Write data to this handle. * @param src Binary data to write. */ template - void write(const std::basic_string& src) const { - write(src.c_str(), src.size() * sizeof(CharT)); + void write(std::basic_string_view src) const { + write(src.data(), src.length() * sizeof(CharT)); + } + /** + * Write data to this handle. + * @param src Binary data to write. + */ + template , + typename Allocator = std::allocator> + void write(const std::basic_string src) const { + write(std::basic_string_view{src}); } void inherit(bool yes = true) const; diff --git a/include/winapi/path.hpp b/include/winapi/path.hpp index 26d89ce..8de2559 100644 --- a/include/winapi/path.hpp +++ b/include/winapi/path.hpp @@ -13,9 +13,9 @@ namespace winapi { class CanonicalPath { public: /** Make an absolute, canonical path. */ - static std::string canonicalize(const std::string&); + static std::string canonicalize(std::string_view); - explicit CanonicalPath(const std::string&); + explicit CanonicalPath(std::string_view); std::string get() const { return m_path; } std::string path() const { return get(); } diff --git a/include/winapi/process_io.hpp b/include/winapi/process_io.hpp index 9df145c..9da442f 100644 --- a/include/winapi/process_io.hpp +++ b/include/winapi/process_io.hpp @@ -9,7 +9,7 @@ #include "path.hpp" #include "pipe.hpp" -#include +#include #include namespace winapi { @@ -26,7 +26,7 @@ struct Stdin : Stream { /** No redirection. */ Stdin(); /** Make child process read form a file. */ - explicit Stdin(const std::string& file); + explicit Stdin(std::string_view file); /** @overload */ explicit Stdin(const CanonicalPath& file); /** Make child process read form a pipe. */ @@ -38,7 +38,7 @@ struct Stdout : Stream { /** No redirection. */ Stdout(); /** Redirect child process's stdout to a file. */ - explicit Stdout(const std::string& file); + explicit Stdout(std::string_view file); /** @overload */ explicit Stdout(const CanonicalPath& file); /** Redirect child process's stdout to a pipe. */ @@ -50,7 +50,7 @@ struct Stderr : Stream { /** No redirection. */ Stderr(); /** Redirect child process's stderr to a file. */ - explicit Stderr(const std::string& file); + explicit Stderr(std::string_view file); /** @overload */ explicit Stderr(const CanonicalPath& file); /** Redirect child process's stderr to a pipe. */ diff --git a/include/winapi/shmem.hpp b/include/winapi/shmem.hpp index 4040241..3290cf3 100644 --- a/include/winapi/shmem.hpp +++ b/include/winapi/shmem.hpp @@ -9,7 +9,7 @@ #include #include -#include +#include #include #include @@ -23,12 +23,12 @@ public: * @param name UTF-8 string. * @param nb Number of bytes. */ - static SharedMemory create(const std::string& name, std::size_t nb); + static SharedMemory create(std::string_view name, std::size_t nb); /** * Opens a shared memory region. * @param name UTF-8 string. */ - static SharedMemory open(const std::string& name); + static SharedMemory open(std::string_view name); /** Get pointer to the data. */ void* get() const { return m_addr.get(); } @@ -60,7 +60,7 @@ public: * @param args Arguments to construct the object. */ template - static SharedObject create(const std::string& name, Args&&... args) { + static SharedObject create(std::string_view name, Args&&... args) { SharedObject obj{SharedMemory::create(name, sizeof(AlignedType))}; new (obj.ptr()) T(std::forward(args)...); obj.m_destruct = true; @@ -71,7 +71,7 @@ public: * Open a shared memory region that stores the object. * @param name UTF-8 string, name of the shared memory region. */ - static SharedObject open(const std::string& name) { + static SharedObject open(std::string_view name) { SharedObject obj{SharedMemory::open(name)}; return obj; } diff --git a/src/cmd_line.cpp b/src/cmd_line.cpp index dd33bb4..46281e2 100644 --- a/src/cmd_line.cpp +++ b/src/cmd_line.cpp @@ -20,6 +20,7 @@ #include #include #include +#include #include #include @@ -76,7 +77,7 @@ CommandLine CommandLine::query() { return do_parse(::GetCommandLineW()); } -CommandLine CommandLine::parse(const std::string& src) { +CommandLine CommandLine::parse(std::string_view src) { return do_parse(widen(src)); } @@ -90,7 +91,7 @@ CommandLine::CommandLine(std::vector argv) : m_args{std::move(argv) m_argv0 = split_argv0(m_args); } -std::string CommandLine::escape(const std::string& arg) { +std::string CommandLine::escape(std::string_view arg) { std::ostringstream safe; safe << '"'; @@ -122,7 +123,7 @@ std::string CommandLine::escape(const std::string& arg) { return safe.str(); } -std::string CommandLine::escape_cmd(const std::string& arg) { +std::string CommandLine::escape_cmd(std::string_view arg) { static constexpr auto escape_symbol = '^'; static const std::string dangerous_symbols{"^!\"%&()<>|"}; diff --git a/src/error.cpp b/src/error.cpp index d0c8a53..3fd18ea 100644 --- a/src/error.cpp +++ b/src/error.cpp @@ -11,20 +11,22 @@ #include #include #include +#include #include namespace winapi { namespace error { namespace { -std::wstring trim_trailing_newline(const std::wstring& s) { +std::wstring trim_trailing_newline(std::wstring_view s) { const auto last_pos = s.find_last_not_of(L"\r\n"); - if (std::wstring::npos == last_pos) + if (std::wstring_view::npos == last_pos) return {}; - return s.substr(0, last_pos + 1); + const auto sub = s.substr(0, last_pos + 1); + return {sub.data(), sub.length()}; } -std::string build_what(DWORD code, const char* function) { +std::string build_what(DWORD code, std::string_view function) { std::ostringstream what; what << "Function " << function << " failed with error code " << code; return what.str(); diff --git a/src/file.cpp b/src/file.cpp index 4f1d2a9..275f0fc 100644 --- a/src/file.cpp +++ b/src/file.cpp @@ -18,7 +18,7 @@ namespace winapi { namespace { -std::wstring to_system_path(const std::string& path) { +std::wstring to_system_path(std::string_view path) { return widen(path); } @@ -58,13 +58,13 @@ private: CreateFileParams() = default; }; -File open_file(const std::wstring& path, const CreateFileParams& params) { +File open_file(std::wstring_view path, const CreateFileParams& params) { SECURITY_ATTRIBUTES attributes; std::memset(&attributes, 0, sizeof(attributes)); attributes.nLength = sizeof(attributes); attributes.bInheritHandle = TRUE; - const auto handle = ::CreateFileW(path.c_str(), + const auto handle = ::CreateFileW(path.data(), params.dwDesiredAccess, params.dwShareMode, &attributes, @@ -79,8 +79,8 @@ File open_file(const std::wstring& path, const CreateFileParams& params) { return File{Handle{handle}}; } -void remove_file(const std::wstring& path) { - const auto ret = ::DeleteFileW(path.c_str()); +void remove_file(std::wstring_view path) { + const auto ret = ::DeleteFileW(path.data()); if (!ret) { throw error::windows(GetLastError(), "DeleteFileW"); @@ -89,7 +89,7 @@ void remove_file(const std::wstring& path) { } // namespace -File File::open_r(const std::string& path) { +File File::open_r(std::string_view path) { return open_file(to_system_path(path), CreateFileParams::read()); } @@ -97,7 +97,7 @@ File File::open_r(const CanonicalPath& path) { return open_file(to_system_path(path), CreateFileParams::read()); } -File File::open_read_attributes(const std::string& path) { +File File::open_read_attributes(std::string_view path) { return open_file(to_system_path(path), CreateFileParams::read_attributes()); } @@ -105,7 +105,7 @@ File File::open_read_attributes(const CanonicalPath& path) { return open_file(to_system_path(path), CreateFileParams::read_attributes()); } -File File::open_w(const std::string& path) { +File File::open_w(std::string_view path) { return open_file(to_system_path(path), CreateFileParams::write()); } @@ -113,7 +113,7 @@ File File::open_w(const CanonicalPath& path) { return open_file(to_system_path(path), CreateFileParams::write()); } -void File::remove(const std::string& path) { +void File::remove(std::string_view path) { remove_file(to_system_path(path)); } diff --git a/src/path.cpp b/src/path.cpp index 409fc44..5c78fe9 100644 --- a/src/path.cpp +++ b/src/path.cpp @@ -18,7 +18,7 @@ namespace winapi { namespace { -std::wstring do_canonicalize(const std::wstring& path) { +std::wstring do_canonicalize(std::wstring_view path) { static constexpr std::size_t init_buffer_size = MAX_PATH; static_assert(init_buffer_size > 0, "init_buffer_size must be positive"); @@ -28,8 +28,8 @@ std::wstring do_canonicalize(const std::wstring& path) { while (true) { if (buffer.size() > std::numeric_limits::max()) throw std::range_error{"Path buffer is too large"}; - const auto nch = ::GetFullPathNameW( - path.c_str(), static_cast(buffer.size()), buffer.data(), NULL); + const auto nch = + ::GetFullPathNameW(path.data(), static_cast(buffer.size()), buffer.data(), NULL); if (nch == 0) { throw error::windows(GetLastError(), "GetFullPathNameW"); @@ -47,9 +47,9 @@ std::wstring do_canonicalize(const std::wstring& path) { } // namespace -CanonicalPath::CanonicalPath(const std::string& path) : m_path(canonicalize(path)) {} +CanonicalPath::CanonicalPath(std::string_view path) : m_path{canonicalize(path)} {} -std::string CanonicalPath::canonicalize(const std::string& path) { +std::string CanonicalPath::canonicalize(std::string_view path) { return narrow(do_canonicalize(widen(path))); } diff --git a/src/process_io.cpp b/src/process_io.cpp index 149081b..1162049 100644 --- a/src/process_io.cpp +++ b/src/process_io.cpp @@ -8,7 +8,7 @@ #include #include -#include +#include #include namespace winapi { @@ -20,15 +20,15 @@ Stdout::Stdout() : Stream{Handle::std_out()} {} Stderr::Stderr() : Stream{Handle::std_err()} {} -Stdin::Stdin(const std::string& path) : Stream{File::open_r(path)} {} +Stdin::Stdin(std::string_view path) : Stream{File::open_r(path)} {} Stdin::Stdin(const CanonicalPath& path) : Stream{File::open_r(path)} {} -Stdout::Stdout(const std::string& path) : Stream{File::open_w(path)} {} +Stdout::Stdout(std::string_view path) : Stream{File::open_w(path)} {} Stdout::Stdout(const CanonicalPath& path) : Stream{File::open_w(path)} {} -Stderr::Stderr(const std::string& path) : Stream{File::open_w(path)} {} +Stderr::Stderr(std::string_view path) : Stream{File::open_w(path)} {} Stderr::Stderr(const CanonicalPath& path) : Stream{File::open_w(path)} {} diff --git a/src/shmem.cpp b/src/shmem.cpp index a636f4e..0d747b3 100644 --- a/src/shmem.cpp +++ b/src/shmem.cpp @@ -13,7 +13,7 @@ #include #include -#include +#include #include namespace winapi { @@ -37,7 +37,7 @@ void SharedMemory::Unmap::operator()(void* ptr) const { WINAPI_UNUSED_PARAMETER(ret); }; -SharedMemory SharedMemory::create(const std::string& name, std::size_t nb) { +SharedMemory SharedMemory::create(std::string_view name, std::size_t nb) { const auto nb64 = static_cast(nb); static_assert(sizeof(nb64) == 2 * sizeof(DWORD), "sizeof(DWORD) != 32"); @@ -56,7 +56,7 @@ SharedMemory SharedMemory::create(const std::string& name, std::size_t nb) { return {std::move(mapping), addr}; } -SharedMemory SharedMemory::open(const std::string& name) { +SharedMemory SharedMemory::open(std::string_view name) { const auto mapping_impl = ::OpenFileMappingW(FILE_MAP_ALL_ACCESS, FALSE, widen(name).c_str()); if (mapping_impl == NULL) { -- cgit v1.3