diff options
| author | Egor Tensin <egor@tensin.name> | 2026-06-04 22:39:44 +0200 |
|---|---|---|
| committer | Egor Tensin <egor@tensin.name> | 2026-06-04 22:42:12 +0200 |
| commit | 7fcbd6e474235957f2785c3d9b5344b2acf3ff77 (patch) | |
| tree | 4c1a2d5971a9db5330963ac7d77501638efeb4c0 /include | |
| parent | upgrade to `using` instead of typedef (diff) | |
| download | winapi-common-7fcbd6e474235957f2785c3d9b5344b2acf3ff77.tar.gz winapi-common-7fcbd6e474235957f2785c3d9b5344b2acf3ff77.zip | |
upgrade to std::string_view
Diffstat (limited to '')
| -rw-r--r-- | include/winapi/buffer.hpp | 11 | ||||
| -rw-r--r-- | include/winapi/cmd_line.hpp | 7 | ||||
| -rw-r--r-- | include/winapi/file.hpp | 10 | ||||
| -rw-r--r-- | include/winapi/handle.hpp | 17 | ||||
| -rw-r--r-- | include/winapi/path.hpp | 4 | ||||
| -rw-r--r-- | include/winapi/process_io.hpp | 8 | ||||
| -rw-r--r-- | include/winapi/shmem.hpp | 10 |
7 files changed, 39 insertions, 28 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 <sstream> #include <stdexcept> #include <string> +#include <string_view> #include <utility> #include <vector> @@ -33,19 +34,19 @@ public: /** Construct a buffer from an instance of `std::vector<unsigned char>`. */ 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 <typename CharT> - explicit Buffer(const std::basic_string<CharT>& src) { + explicit Buffer(std::basic_string_view<CharT> 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 <typename CharT> - void set(const std::basic_string<CharT>& src) { - set(src.c_str(), src.length() * sizeof(std::basic_string<CharT>::char_type)); + void set(std::basic_string_view<CharT> src) { + set(src.data(), src.length() * sizeof(std::basic_string_view<CharT>::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 <string> +#include <string_view> #include <utility> #include <vector> @@ -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<std::string> 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 <cstddef> #include <functional> -#include <string> +#include <string_view> #include <utility> 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 <cstddef> #include <memory> -#include <string> +#include <string_view> #include <utility> 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 <typename CharT> - void write(const std::basic_string<CharT>& src) const { - write(src.c_str(), src.size() * sizeof(CharT)); + void write(std::basic_string_view<CharT> src) const { + write(src.data(), src.length() * sizeof(CharT)); + } + /** + * Write data to this handle. + * @param src Binary data to write. + */ + template <typename CharT, + typename Traits = std::char_traits<CharT>, + typename Allocator = std::allocator<CharT>> + void write(const std::basic_string<CharT, Traits, Allocator> src) const { + write(std::basic_string_view<CharT>{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 <string> +#include <string_view> #include <utility> 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 <cstddef> #include <memory> -#include <string> +#include <string_view> #include <type_traits> #include <utility> @@ -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 <typename... Args> - 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>(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; } |
