| 1 |
#pragma once |
| 2 |
#include <algorithm> |
| 3 |
#include <codecvt> |
| 4 |
#include <functional> |
| 5 |
#include <iterator> |
| 6 |
#include <locale> |
| 7 |
#include <memory> |
| 8 |
#include <string> |
| 9 |
#include <vector> |
| 10 |
#include <optional> |
| 11 |
#include <LibLsp/lsp/AbsolutePath.h> |
| 12 |
|
| 13 |
#include "lsPosition.h" |
| 14 |
|
| 15 |
namespace lsp |
| 16 |
{ |
| 17 |
|
| 18 |
// Returns true if |value| starts/ends with |start| or |ending|. |
| 19 |
bool StartsWith(std::string value, std::string start); |
| 20 |
bool EndsWith(std::string value, std::string ending); |
| 21 |
bool AnyStartsWith(std::vector<std::string> const& values, std::string const& start); |
| 22 |
bool StartsWithAny(std::string const& value, std::vector<std::string> const& startings); |
| 23 |
bool EndsWithAny(std::string const& value, std::vector<std::string> const& endings); |
| 24 |
bool FindAnyPartial(std::string const& value, std::vector<std::string> const& values); |
| 25 |
// Returns the dirname of |path|, i.e. "foo/bar.cc" => "foo/", "foo" => "./", |
| 26 |
// "/foo" => "/". The result always ends in '/'. |
| 27 |
std::string GetDirName(std::string path); |
| 28 |
// Returns the basename of |path|, ie, "foo/bar.cc" => "bar.cc". |
| 29 |
std::string GetBaseName(std::string const& path); |
| 30 |
// Returns |path| without the filetype, ie, "foo/bar.cc" => "foo/bar". |
| 31 |
std::string StripFileType(std::string const& path); |
| 32 |
|
| 33 |
std::string ReplaceAll(std::string const& source, std::string const& from, std::string const& to); |
| 34 |
|
| 35 |
std::vector<std::string> SplitString(std::string const& str, std::string const& delimiter); |
| 36 |
|
| 37 |
template<typename TValues, typename TMap> |
| 38 |
std::string StringJoinMap(TValues const& values, TMap const& map, std::string const& sep = ", ") |
| 39 |
{ |
| 40 |
std::string result; |
| 41 |
bool first = true; |
| 42 |
for (auto& entry : values) |
| 43 |
{ |
| 44 |
if (!first) |
| 45 |
{ |
| 46 |
result += sep; |
| 47 |
} |
| 48 |
first = false; |
| 49 |
result += map(entry); |
| 50 |
} |
| 51 |
return result; |
| 52 |
} |
| 53 |
|
| 54 |
template<typename TValues> |
| 55 |
std::string StringJoin(TValues const& values, std::string const& sep = ", ") |
| 56 |
{ |
| 57 |
return StringJoinMap(values, [](std::string const& entry) { return entry; }, sep); |
| 58 |
} |
| 59 |
|
| 60 |
template<typename TCollection, typename TValue> |
| 61 |
bool ContainsValue(TCollection const& collection, TValue const& value) |
| 62 |
{ |
| 63 |
return std::find(std::begin(collection), std::end(collection), value) != std::end(collection); |
| 64 |
} |
| 65 |
|
| 66 |
// Ensures that |path| ends in a slash. |
| 67 |
void EnsureEndsInSlash(std::string& path); |
| 68 |
|
| 69 |
// Converts a file path to one that can be used as filename. |
| 70 |
// e.g. foo/bar.c => foo_bar.c |
| 71 |
std::string EscapeFileName(std::string path); |
| 72 |
|
| 73 |
// FIXME: Move ReadContent into ICacheManager? |
| 74 |
bool FileExists(std::string const& filename); |
| 75 |
optional<std::string> ReadContent(AbsolutePath const& filename); |
| 76 |
std::vector<std::string> ReadLinesWithEnding(AbsolutePath const& filename); |
| 77 |
|
| 78 |
bool WriteToFile(std::string const& filename, std::string const& content); |
| 79 |
|
| 80 |
template<typename T, typename Fn> |
| 81 |
void RemoveIf(std::vector<T>* vec, Fn predicate) |
| 82 |
{ |
| 83 |
vec->erase(std::remove_if(vec->begin(), vec->end(), predicate), vec->end()); |
| 84 |
} |
| 85 |
|
| 86 |
std::string FormatMicroseconds(long long microseconds); |
| 87 |
|
| 88 |
// Makes sure all newlines in |output| are in \r\n format. |
| 89 |
std::string UpdateToRnNewlines(std::string output); |
| 90 |
|
| 91 |
// Utility methods to check if |path| is absolute. |
| 92 |
bool IsAbsolutePath(std::string const& path); |
| 93 |
bool IsUnixAbsolutePath(std::string const& path); |
| 94 |
bool IsWindowsAbsolutePath(std::string const& path); |
| 95 |
|
| 96 |
bool IsDirectory(std::string const& path); |
| 97 |
|
| 98 |
// string <-> wstring conversion (UTF-16), e.g. for use with Window's wide APIs. |
| 99 |
std::string ws2s(std::wstring const& wstr); |
| 100 |
std::wstring s2ws(std::string const& str); |
| 101 |
|
| 102 |
AbsolutePath NormalizePath(std::string const& path, bool ensure_exists = true, bool force_lower_on_windows = true); |
| 103 |
|
| 104 |
int GetOffsetForPosition(lsPosition position, std::string const& content); |
| 105 |
|
| 106 |
// Finds the position for an |offset| in |content|. |
| 107 |
lsPosition GetPositionForOffset(int offset, std::string const& content); |
| 108 |
|
| 109 |
// Utility method to find a position for the given character. |
| 110 |
lsPosition CharPos(std::string const& search, char character, int character_offset = 0); |
| 111 |
|
| 112 |
void scanDirsNoRecursive(std::wstring const& rootPath, std::vector<std::wstring>& ret); |
| 113 |
|
| 114 |
void scanFilesUseRecursive(std::wstring const& rootPath, std::vector<std::wstring>& ret, std::wstring strSuf = L""); |
| 115 |
|
| 116 |
void scanFileNamesUseRecursive(std::wstring const& rootPath, std::vector<std::wstring>& ret, std::wstring strSuf = L""); |
| 117 |
void scanFileNamesUseRecursive(std::string const& rootPath, std::vector<std::string>& ret, std::string strSuf = ""); |
| 118 |
|
| 119 |
void scanFilesUseRecursive(std::string const& rootPath, std::vector<std::string>& ret, std::string strSuf = ""); |
| 120 |
|
| 121 |
void scanDirsUseRecursive(std::wstring const& rootPath, std::vector<std::wstring>& ret); |
| 122 |
|
| 123 |
} // namespace lsp |