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 /src | |
| 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-- | src/cmd_line.cpp | 7 | ||||
| -rw-r--r-- | src/error.cpp | 10 | ||||
| -rw-r--r-- | src/file.cpp | 18 | ||||
| -rw-r--r-- | src/path.cpp | 10 | ||||
| -rw-r--r-- | src/process_io.cpp | 8 | ||||
| -rw-r--r-- | src/shmem.cpp | 6 |
6 files changed, 31 insertions, 28 deletions
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 <sstream> #include <stdexcept> #include <string> +#include <string_view> #include <utility> #include <vector> @@ -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<std::string> 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 <cstdint> #include <sstream> #include <string> +#include <string_view> #include <system_error> 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<DWORD>::max()) throw std::range_error{"Path buffer is too large"}; - const auto nch = ::GetFullPathNameW( - path.c_str(), static_cast<DWORD>(buffer.size()), buffer.data(), NULL); + const auto nch = + ::GetFullPathNameW(path.data(), static_cast<DWORD>(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 <winapi/path.hpp> #include <winapi/process_io.hpp> -#include <string> +#include <string_view> #include <utility> 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 <cstddef> #include <cstdint> -#include <string> +#include <string_view> #include <utility> 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<std::uint64_t>(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) { |
