From: Joel Rosdahl Date: Sun, 13 Oct 2019 20:34:37 +0000 (+0200) Subject: Use nonstd::string_view where appropriate X-Git-Tag: v4.0~735 X-Git-Url: http://git.ipfire.org/gitweb.cgi?a=commitdiff_plain;h=985193a5606659917029cd8fc6228c5329cedd78;p=thirdparty%2Fccache.git Use nonstd::string_view where appropriate --- diff --git a/dev.mk.in b/dev.mk.in index f15defbaa..79ec28f74 100644 --- a/dev.mk.in +++ b/dev.mk.in @@ -42,10 +42,11 @@ non_third_party_headers = \ src/Checksum.hpp \ src/Compression.hpp \ src/Compressor.hpp \ - src/Decompressor.hpp \ src/Config.hpp \ + src/Decompressor.hpp \ src/Error.hpp \ src/File.hpp \ + src/FormatNonStdStringView.hpp \ src/NonCopyable.hpp \ src/NullCompressor.hpp \ src/NullDecompressor.hpp \ diff --git a/src/FormatNonstdStringView.hpp b/src/FormatNonstdStringView.hpp new file mode 100644 index 000000000..63f7ae1be --- /dev/null +++ b/src/FormatNonstdStringView.hpp @@ -0,0 +1,42 @@ +// Copyright (C) 2019 Joel Rosdahl and other contributors +// +// See doc/AUTHORS.adoc for a complete list of contributors. +// +// This program is free software; you can redistribute it and/or modify it +// under the terms of the GNU General Public License as published by the Free +// Software Foundation; either version 3 of the License, or (at your option) +// any later version. +// +// This program is distributed in the hope that it will be useful, but WITHOUT +// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or +// FITNESS FOR A PARTICULAR PURPOSE. See the GNU General Public License for +// more details. +// +// You should have received a copy of the GNU General Public License along with +// this program; if not, write to the Free Software Foundation, Inc., 51 +// Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA + +#pragma once + +// Specialization of fmt::formatter for nonstd::string_view. +namespace fmt { + +template<> struct formatter +{ + template + constexpr auto + parse(ParseContext& ctx) -> decltype(ctx.begin()) + { + return ctx.begin(); + } + + template + auto + format(const nonstd::string_view& sv, FormatContext& ctx) + -> decltype(ctx.out()) + { + return format_to(ctx.out(), "{:{}}", sv.data(), sv.length()); + } +}; + +} // namespace fmt diff --git a/src/Util.cpp b/src/Util.cpp index 20aff655f..801291e02 100644 --- a/src/Util.cpp +++ b/src/Util.cpp @@ -18,6 +18,7 @@ #include "Util.hpp" +#include "FormatNonstdStringView.hpp" #include "ccache.hpp" #include @@ -39,14 +40,14 @@ get_cache_files_internal(const std::string& dir, std::vector directories; dirent* de; while ((de = readdir(d))) { - std::string name = de->d_name; + nonstd::string_view name(de->d_name); if (name == "" || name == "." || name == ".." || name == "CACHEDIR.TAG" - || name == "stats" || Util::starts_with(name, ".nfs")) { + || name == "stats" || name.starts_with(".nfs")) { continue; } if (name.length() == 1) { - directories.push_back(name); + directories.emplace_back(name); } else { files.push_back( std::make_shared(fmt::format("{}/{}", dir, name))); @@ -71,8 +72,8 @@ get_cache_files_internal(const std::string& dir, namespace Util { -std::string -base_name(const std::string& path) +nonstd::string_view +base_name(nonstd::string_view path) { size_t n = path.rfind('/'); #ifdef _WIN32 @@ -85,10 +86,11 @@ base_name(const std::string& path) } bool -create_dir(const std::string& dir) +create_dir(nonstd::string_view dir) { + std::string dir_str(dir); struct stat st; - if (stat(dir.c_str(), &st) == 0) { + if (stat(dir_str.c_str(), &st) == 0) { if (S_ISDIR(st.st_mode)) { return true; } else { @@ -99,7 +101,7 @@ create_dir(const std::string& dir) if (!create_dir(Util::dir_name(dir))) { return false; } - int result = mkdir(dir.c_str(), 0777); + int result = mkdir(dir_str.c_str(), 0777); // Treat an already existing directory as OK since the file system could // have changed in between calling stat and actually creating the // directory. This can happen when there are multiple instances of ccache @@ -112,17 +114,17 @@ create_dir(const std::string& dir) } std::pair -create_temp_fd(const std::string& path_prefix) +create_temp_fd(nonstd::string_view path_prefix) { - char* tmp_path = x_strdup(path_prefix.c_str()); + char* tmp_path = x_strndup(path_prefix.data(), path_prefix.length()); int fd = create_tmp_fd(&tmp_path); std::string actual_path = tmp_path; free(tmp_path); return {fd, actual_path}; } -std::string -dir_name(const std::string& path) +nonstd::string_view +dir_name(nonstd::string_view path) { size_t n = path.rfind('/'); #ifdef _WIN32 @@ -138,12 +140,9 @@ dir_name(const std::string& path) } bool -ends_with(const std::string& string, const std::string& suffix) +ends_with(nonstd::string_view string, nonstd::string_view suffix) { - return suffix.length() <= string.length() - && string.compare( - string.length() - suffix.length(), suffix.length(), suffix) - == 0; + return string.ends_with(suffix); } void @@ -212,10 +211,9 @@ read_file(const std::string& path) } bool -starts_with(const std::string& string, const std::string& prefix) +starts_with(nonstd::string_view string, nonstd::string_view prefix) { - return prefix.length() <= string.length() - && string.compare(0, prefix.length(), prefix) == 0; + return string.starts_with(prefix); } std::string diff --git a/src/Util.hpp b/src/Util.hpp index f75a51dda..53a388014 100644 --- a/src/Util.hpp +++ b/src/Util.hpp @@ -22,6 +22,8 @@ #include "CacheFile.hpp" +#include "third_party/nonstd/string_view.hpp" + #include #include #include @@ -37,7 +39,7 @@ typedef std::function create_temp_fd(const std::string& path_prefix); +std::pair create_temp_fd(nonstd::string_view path_prefix); // Get directory name of path. -std::string dir_name(const std::string& path); +nonstd::string_view dir_name(nonstd::string_view path); // Return true if suffix is a suffix of string. -bool ends_with(const std::string& string, const std::string& suffix); +bool ends_with(nonstd::string_view string, nonstd::string_view suffix); // Call a function for each subdir (0-9a-f) in the cache. // @@ -163,7 +165,7 @@ int parse_int(const std::string& value); std::string read_file(const std::string& path); // Return true if prefix is a prefix of string. -bool starts_with(const std::string& string, const std::string& prefix); +bool starts_with(nonstd::string_view string, nonstd::string_view prefix); // Strip whitespace from left and right side of a string. [[gnu::warn_unused_result]] std::string