2017-02-17 09:57:44 +00:00
|
|
|
#pragma once
|
|
|
|
|
2017-04-17 07:06:01 +00:00
|
|
|
#include <optional.h>
|
|
|
|
|
2017-04-14 22:30:33 +00:00
|
|
|
#include <algorithm>
|
2017-03-25 23:58:11 +00:00
|
|
|
#include <functional>
|
2017-04-08 22:54:36 +00:00
|
|
|
#include <memory>
|
2017-04-11 07:29:36 +00:00
|
|
|
#include <queue>
|
2017-02-17 09:57:44 +00:00
|
|
|
#include <string>
|
2017-04-09 02:27:07 +00:00
|
|
|
#include <unordered_map>
|
2017-02-17 09:57:44 +00:00
|
|
|
#include <vector>
|
|
|
|
|
2017-05-29 21:18:35 +00:00
|
|
|
// Trim from start (in place)
|
2017-12-22 16:48:12 +00:00
|
|
|
void TrimStartInPlace(std::string& s);
|
2017-05-29 21:18:35 +00:00
|
|
|
// Trim from end (in place)
|
2017-12-22 16:48:12 +00:00
|
|
|
void TrimEndInPlace(std::string& s);
|
2017-05-29 21:18:35 +00:00
|
|
|
// Trim from both ends (in place)
|
2017-12-22 16:48:12 +00:00
|
|
|
void TrimInPlace(std::string& s);
|
|
|
|
std::string Trim(std::string s);
|
2017-05-29 21:18:35 +00:00
|
|
|
|
2018-01-14 21:18:12 +00:00
|
|
|
uint64_t HashUsr(const std::string& s);
|
|
|
|
uint64_t HashUsr(const char* s);
|
|
|
|
uint64_t HashUsr(const char* s, size_t n);
|
2018-01-13 19:39:06 +00:00
|
|
|
|
2017-03-31 04:15:42 +00:00
|
|
|
// Returns true if |value| starts/ends with |start| or |ending|.
|
|
|
|
bool StartsWith(const std::string& value, const std::string& start);
|
|
|
|
bool EndsWith(const std::string& value, const std::string& ending);
|
2017-09-22 01:14:57 +00:00
|
|
|
bool AnyStartsWith(const std::vector<std::string>& values,
|
|
|
|
const std::string& start);
|
2017-10-18 08:24:52 +00:00
|
|
|
bool StartsWithAny(const std::string& value,
|
|
|
|
const std::vector<std::string>& startings);
|
2017-09-22 01:14:57 +00:00
|
|
|
bool EndsWithAny(const std::string& value,
|
|
|
|
const std::vector<std::string>& endings);
|
2018-01-12 17:37:33 +00:00
|
|
|
bool FindAnyPartial(const std::string& value,
|
|
|
|
const std::vector<std::string>& values);
|
2018-02-03 21:16:38 +00:00
|
|
|
// Returns the dirname of |path|, i.e. "foo/bar.cc" => "foo", "foo" => ".", "/foo" => "/".
|
|
|
|
std::string GetDirName(std::string path);
|
2018-01-11 02:33:20 +00:00
|
|
|
// Returns the basename of |path|, ie, "foo/bar.cc" => "bar.cc".
|
|
|
|
std::string GetBaseName(const std::string& path);
|
2018-01-12 17:37:33 +00:00
|
|
|
// Returns |path| without the filetype, ie, "foo/bar.cc" => "foo/bar".
|
|
|
|
std::string StripFileType(const std::string& path);
|
2018-01-11 02:33:20 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::string ReplaceAll(const std::string& source,
|
|
|
|
const std::string& from,
|
|
|
|
const std::string& to);
|
2017-03-31 04:15:42 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> SplitString(const std::string& str,
|
|
|
|
const std::string& delimiter);
|
2017-05-19 01:14:53 +00:00
|
|
|
|
2017-05-22 07:14:11 +00:00
|
|
|
std::string LowerPathIfCaseInsensitive(const std::string& path);
|
|
|
|
|
2017-09-14 06:39:32 +00:00
|
|
|
template <typename TValues, typename TMap>
|
2018-01-18 01:51:58 +00:00
|
|
|
std::string StringJoinMap(const TValues& values,
|
|
|
|
const TMap& map,
|
|
|
|
const std::string& sep = ", ") {
|
2017-04-26 04:03:22 +00:00
|
|
|
std::string result;
|
|
|
|
bool first = true;
|
|
|
|
for (auto& entry : values) {
|
|
|
|
if (!first)
|
2018-01-18 01:51:58 +00:00
|
|
|
result += sep;
|
2017-04-26 04:03:22 +00:00
|
|
|
first = false;
|
2017-09-14 06:39:32 +00:00
|
|
|
result += map(entry);
|
2017-04-26 04:03:22 +00:00
|
|
|
}
|
|
|
|
return result;
|
|
|
|
}
|
2017-04-24 01:01:51 +00:00
|
|
|
|
2017-09-14 06:39:32 +00:00
|
|
|
template <typename TValues>
|
2018-01-18 01:51:58 +00:00
|
|
|
std::string StringJoin(const TValues& values, const std::string& sep = ", ") {
|
|
|
|
return StringJoinMap(values, [](const std::string& entry) { return entry; },
|
|
|
|
sep);
|
2017-09-14 06:39:32 +00:00
|
|
|
}
|
|
|
|
|
2018-01-07 04:08:55 +00:00
|
|
|
template <typename TCollection, typename TValue>
|
|
|
|
bool ContainsValue(const TCollection& collection, const TValue& value) {
|
|
|
|
return collection.find(value) != collection.end();
|
|
|
|
}
|
|
|
|
|
2017-03-05 19:48:05 +00:00
|
|
|
// Finds all files in the given folder. This is recursive.
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> GetFilesInFolder(std::string folder,
|
|
|
|
bool recursive,
|
|
|
|
bool add_folder_to_path);
|
|
|
|
void GetFilesInFolder(std::string folder,
|
|
|
|
bool recursive,
|
|
|
|
bool add_folder_to_path,
|
|
|
|
const std::function<void(const std::string&)>& handler);
|
2017-05-21 19:51:15 +00:00
|
|
|
|
|
|
|
// Ensures that |path| ends in a slash.
|
|
|
|
void EnsureEndsInSlash(std::string& path);
|
|
|
|
|
2017-12-02 05:07:30 +00:00
|
|
|
// Converts a file path to one that can be used as filename.
|
|
|
|
// e.g. foo/bar.c => foo_bar.c
|
|
|
|
std::string EscapeFileName(std::string path);
|
|
|
|
|
2017-12-29 18:00:44 +00:00
|
|
|
// FIXME: Move ReadContent into ICacheManager?
|
2018-01-07 04:26:22 +00:00
|
|
|
bool FileExists(const std::string& filename);
|
2017-04-17 07:06:01 +00:00
|
|
|
optional<std::string> ReadContent(const std::string& filename);
|
2017-12-04 02:17:36 +00:00
|
|
|
std::vector<std::string> ReadLinesWithEnding(std::string filename);
|
2017-09-22 01:14:57 +00:00
|
|
|
std::vector<std::string> ToLines(const std::string& content,
|
|
|
|
bool trim_whitespace);
|
2017-04-16 08:09:12 +00:00
|
|
|
|
2017-12-22 16:48:12 +00:00
|
|
|
struct TextReplacer {
|
|
|
|
struct Replacement {
|
|
|
|
std::string from;
|
|
|
|
std::string to;
|
|
|
|
};
|
|
|
|
|
|
|
|
std::vector<Replacement> replacements;
|
|
|
|
|
|
|
|
std::string Apply(const std::string& content);
|
|
|
|
};
|
|
|
|
|
|
|
|
void ParseTestExpectation(
|
|
|
|
const std::string& filename,
|
|
|
|
const std::vector<std::string>& lines_with_endings,
|
|
|
|
TextReplacer* text_replacer,
|
|
|
|
std::vector<std::string>* flags,
|
|
|
|
std::unordered_map<std::string, std::string>* output_sections);
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
void UpdateTestExpectation(const std::string& filename,
|
|
|
|
const std::string& expectation,
|
|
|
|
const std::string& actual);
|
2017-02-23 08:18:54 +00:00
|
|
|
|
2017-03-04 01:45:20 +00:00
|
|
|
void WriteToFile(const std::string& filename, const std::string& content);
|
|
|
|
|
|
|
|
// note: this implementation does not disable this overload for array types
|
2017-09-22 01:14:57 +00:00
|
|
|
// See
|
|
|
|
// http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique#Possible_Implementatiog
|
|
|
|
template <typename T, typename... Args>
|
2017-03-04 01:45:20 +00:00
|
|
|
std::unique_ptr<T> MakeUnique(Args&&... args) {
|
2017-09-22 01:14:57 +00:00
|
|
|
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
|
2017-03-04 01:45:20 +00:00
|
|
|
}
|
2017-03-16 07:36:49 +00:00
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
template <typename T>
|
2017-04-11 07:29:36 +00:00
|
|
|
void AddRange(std::vector<T>* dest, const std::vector<T>& to_add) {
|
2017-12-11 02:59:32 +00:00
|
|
|
dest->insert(dest->end(), to_add.begin(), to_add.end());
|
2017-04-11 07:29:36 +00:00
|
|
|
}
|
|
|
|
|
2018-02-06 07:22:44 +00:00
|
|
|
template <typename T>
|
|
|
|
void AddRange(std::vector<T>* dest, std::vector<T>&& to_add) {
|
|
|
|
for (T& x : to_add)
|
|
|
|
dest->push_back(std::move(x));
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
template <typename T>
|
2017-04-11 07:29:36 +00:00
|
|
|
void PushRange(std::queue<T>* dest, const std::vector<T>& to_add) {
|
|
|
|
for (const T& e : to_add)
|
|
|
|
dest->push(e);
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
template <typename T>
|
2017-04-11 07:29:36 +00:00
|
|
|
void RemoveRange(std::vector<T>* dest, const std::vector<T>& to_remove) {
|
2017-09-22 01:14:57 +00:00
|
|
|
dest->erase(std::remove_if(dest->begin(), dest->end(),
|
|
|
|
[&](const T& t) {
|
|
|
|
// TODO: make to_remove a set?
|
|
|
|
return std::find(to_remove.begin(),
|
|
|
|
to_remove.end(),
|
|
|
|
t) != to_remove.end();
|
|
|
|
}),
|
|
|
|
dest->end());
|
2017-04-11 07:29:36 +00:00
|
|
|
}
|
2017-03-16 07:36:49 +00:00
|
|
|
|
|
|
|
// http://stackoverflow.com/a/38140932
|
|
|
|
//
|
|
|
|
// struct SomeHashKey {
|
|
|
|
// std::string key1;
|
|
|
|
// std::string key2;
|
|
|
|
// bool key3;
|
|
|
|
// };
|
|
|
|
// MAKE_HASHABLE(SomeHashKey, t.key1, t.key2, t.key3)
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
inline void hash_combine(std::size_t& seed) {}
|
2017-03-16 07:36:49 +00:00
|
|
|
|
|
|
|
template <typename T, typename... Rest>
|
|
|
|
inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) {
|
|
|
|
std::hash<T> hasher;
|
|
|
|
seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2);
|
|
|
|
hash_combine(seed, rest...);
|
|
|
|
}
|
|
|
|
|
2017-09-22 01:14:57 +00:00
|
|
|
#define MAKE_HASHABLE(type, ...) \
|
|
|
|
namespace std { \
|
|
|
|
template <> \
|
|
|
|
struct hash<type> { \
|
|
|
|
std::size_t operator()(const type& t) const { \
|
|
|
|
std::size_t ret = 0; \
|
|
|
|
hash_combine(ret, __VA_ARGS__); \
|
|
|
|
return ret; \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
}
|
|
|
|
|
|
|
|
#define MAKE_ENUM_HASHABLE(type) \
|
|
|
|
namespace std { \
|
|
|
|
template <> \
|
|
|
|
struct hash<type> { \
|
|
|
|
std::size_t operator()(const type& t) const { \
|
|
|
|
return hash<int>()(static_cast<int>(t)); \
|
|
|
|
} \
|
|
|
|
}; \
|
|
|
|
}
|
2017-05-25 02:04:19 +00:00
|
|
|
|
2017-06-14 06:59:40 +00:00
|
|
|
float GetProcessMemoryUsedInMb();
|
|
|
|
|
2017-11-30 18:40:42 +00:00
|
|
|
std::string FormatMicroseconds(long long microseconds);
|
2017-12-16 05:18:49 +00:00
|
|
|
|
2017-12-20 17:10:57 +00:00
|
|
|
std::string GetDefaultResourceDirectory();
|
2017-12-23 15:51:34 +00:00
|
|
|
|
|
|
|
// Makes sure all newlines in |output| are in \r\n format.
|
|
|
|
std::string UpdateToRnNewlines(std::string output);
|