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 +++++----- 7 files changed, 39 insertions(+), 28 deletions(-) (limited to 'include/winapi') 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; } -- cgit v1.3