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 \
--- /dev/null
+// 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<nonstd::string_view>
+{
+ template<typename ParseContext>
+ constexpr auto
+ parse(ParseContext& ctx) -> decltype(ctx.begin())
+ {
+ return ctx.begin();
+ }
+
+ template<typename FormatContext>
+ auto
+ format(const nonstd::string_view& sv, FormatContext& ctx)
+ -> decltype(ctx.out())
+ {
+ return format_to(ctx.out(), "{:{}}", sv.data(), sv.length());
+ }
+};
+
+} // namespace fmt
#include "Util.hpp"
+#include "FormatNonstdStringView.hpp"
#include "ccache.hpp"
#include <algorithm>
std::vector<std::string> 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<CacheFile>(fmt::format("{}/{}", dir, name)));
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
}
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 {
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
}
std::pair<int, std::string>
-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
}
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
}
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
#include "CacheFile.hpp"
+#include "third_party/nonstd/string_view.hpp"
+
#include <functional>
#include <memory>
#include <string>
SubdirVisitor;
// Get base name of path.
-std::string base_name(const std::string& path);
+nonstd::string_view base_name(nonstd::string_view path);
// Get an integer value from bytes in big endian order.
//
// Create a directory if needed, including its parents if needed.
//
// Returns true if the directory exists or could be created, otherwise false.
-bool create_dir(const std::string& dir);
+bool create_dir(nonstd::string_view dir);
// Create a unique temporary file.
//
//
// Returns the open file descriptor (in read/write mode) and the actual
// filename.
-std::pair<int, std::string> create_temp_fd(const std::string& path_prefix);
+std::pair<int, std::string> 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.
//
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