#pragma once #include #include #include #include #include #include #include #include void TrimInPlace(std::string& s); std::string Trim(std::string s); uint64_t HashUsr(std::string_view s); // Returns true if |value| starts/ends with |start| or |ending|. bool StartsWith(std::string_view value, std::string_view start); bool EndsWith(std::string_view value, std::string_view ending); bool AnyStartsWith(const std::vector& xs, std::string_view prefix); bool StartsWithAny(std::string_view s, const std::vector& ps); bool EndsWithAny(std::string_view s, const std::vector& ss); bool FindAnyPartial(const std::string& value, const std::vector& values); // Returns the dirname of |path|, i.e. "foo/bar.cc" => "foo", "foo" => ".", // "/foo" => "/". std::string GetDirName(std::string path); // Returns the basename of |path|, ie, "foo/bar.cc" => "bar.cc". std::string GetBaseName(const std::string& path); // Returns |path| without the filetype, ie, "foo/bar.cc" => "foo/bar". std::string StripFileType(const std::string& path); std::vector SplitString(const std::string& str, const std::string& delimiter); std::string LowerPathIfInsensitive(const std::string& path); template std::string StringJoinMap(const TValues& values, const TMap& map, const std::string& sep = ", ") { std::string result; bool first = true; for (auto& entry : values) { if (!first) result += sep; first = false; result += map(entry); } return result; } template std::string StringJoin(const TValues& values, const std::string& sep = ", ") { return StringJoinMap(values, [](const std::string& entry) { return entry; }, sep); } template bool ContainsValue(const TCollection& collection, const TValue& value) { return collection.find(value) != collection.end(); } // Finds all files in the given folder. This is recursive. std::vector 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& handler); // Ensures that |path| ends in a slash. void EnsureEndsInSlash(std::string& path); // 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); // FIXME: Move ReadContent into ICacheManager? bool FileExists(const std::string& filename); std::optional ReadContent(const std::string& filename); std::vector ReadFileLines(std::string filename); std::vector ToLines(const std::string& content); struct TextReplacer { struct Replacement { std::string from; std::string to; }; std::vector replacements; std::string Apply(const std::string& content); }; void WriteToFile(const std::string& filename, const std::string& content); template void AddRange(std::vector* dest, const std::vector& to_add) { dest->insert(dest->end(), to_add.begin(), to_add.end()); } template void AddRange(std::vector* dest, std::vector&& to_add) { dest->insert(dest->end(), std::make_move_iterator(to_add.begin()), std::make_move_iterator(to_add.end())); } // 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 inline void hash_combine(std::size_t& seed, const T& v, Rest... rest) { std::hash hasher; seed ^= hasher(v) + 0x9e3779b9 + (seed << 6) + (seed >> 2); hash_combine(seed, rest...); } #define MAKE_HASHABLE(type, ...) \ namespace std { \ template <> \ struct hash { \ 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 { \ std::size_t operator()(const type& t) const { \ return hash()(static_cast(t)); \ } \ }; \ } std::string GetDefaultResourceDirectory();