ccls/src/utils.h

123 lines
3.8 KiB
C
Raw Normal View History

2017-02-17 09:57:44 +00:00
#pragma once
#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>
#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>
using std::experimental::optional;
using std::experimental::nullopt;
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);
bool AnyStartsWith(const std::vector<std::string>& values, const std::string& start);
bool EndsWithAny(const std::string& value, const std::vector<std::string>& endings);
std::string ReplaceAll(const std::string& source, const std::string& from, const std::string& to);
2017-03-31 04:15:42 +00:00
std::vector<std::string> SplitString(const std::string& str, const std::string& delimiter);
template <typename TValues>
std::string StringJoin(const TValues& values) {
std::string result;
bool first = true;
for (auto& entry : values) {
if (!first)
result += ", ";
first = false;
result += entry;
}
return result;
}
2017-03-05 19:48:05 +00:00
// Finds all files in the given folder. This is recursive.
2017-03-11 02:24:51 +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);
// Ensures that |path| ends in a slash.
void EnsureEndsInSlash(std::string& path);
optional<std::string> ReadContent(const std::string& filename);
2017-02-17 09:57:44 +00:00
std::vector<std::string> ReadLines(std::string filename);
std::vector<std::string> ToLines(const std::string& content, bool trim_whitespace);
2017-04-08 22:54:36 +00:00
std::unordered_map<std::string, std::string> ParseTestExpectation(std::string filename);
void UpdateTestExpectation(const std::string& filename, const std::string& expectation, const std::string& actual);
2017-02-23 08:18:54 +00:00
2017-03-01 08:36:11 +00:00
void Fail(const std::string& message);
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
// See http://en.cppreference.com/w/cpp/memory/unique_ptr/make_unique#Possible_Implementatiog
template<typename T, typename... Args>
std::unique_ptr<T> MakeUnique(Args&&... args) {
return std::unique_ptr<T>(new T(std::forward<Args>(args)...));
}
2017-03-16 07:36:49 +00:00
template<typename T>
void AddRange(std::vector<T>* dest, const std::vector<T>& to_add) {
for (const T& e : to_add)
dest->push_back(e);
}
template<typename T>
void PushRange(std::queue<T>* dest, const std::vector<T>& to_add) {
for (const T& e : to_add)
dest->push(e);
}
template<typename T>
void RemoveRange(std::vector<T>* dest, const std::vector<T>& to_remove) {
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-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)
inline void hash_combine(std::size_t& seed) { }
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...);
}
#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;\
}\
};\
2017-03-25 01:01:41 +00:00
}
#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));\
}\
};\
}