summaryrefslogtreecommitdiff
path: root/graphics/asymptote/LspCpp/include/LibLsp/lsp/utils.h
blob: 95064c34d63ea41d3ffaf0dc97e8b7e09179b57b (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
#pragma once
#include <algorithm>
#include <codecvt>
#include <functional>
#include <iterator>
#include <locale>
#include <memory>
#include <string>
#include <vector>
#include <boost/optional/optional.hpp>
#include <LibLsp/lsp/AbsolutePath.h>

#include "lsPosition.h"


namespace lsp
{


// Returns true if |value| starts/ends with |start| or |ending|.
bool StartsWith(std::string value, std::string start);
bool EndsWith(std::string value, std::string ending);
bool AnyStartsWith(const std::vector<std::string>& values,
                   const std::string& start);
bool StartsWithAny(const std::string& value,
                   const std::vector<std::string>& startings);
bool EndsWithAny(const std::string& value,
                 const std::vector<std::string>& endings);
bool FindAnyPartial(const std::string& value,
                    const std::vector<std::string>& values);
// Returns the dirname of |path|, i.e. "foo/bar.cc" => "foo/", "foo" => "./",
// "/foo" => "/". The result always ends in '/'.
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::string ReplaceAll(const std::string& source,
                       const std::string& from,
                       const std::string& to);

std::vector<std::string> SplitString(const std::string& str,
                                     const std::string& delimiter);

template <typename TValues, typename TMap>
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 <typename TValues>
std::string StringJoin(const TValues& values, const std::string& sep = ", ") {
  return StringJoinMap(values, [](const std::string& entry) { return entry; },
                       sep);
}

template <typename TCollection, typename TValue>
bool ContainsValue(const TCollection& collection, const TValue& value) {
  return std::find(std::begin(collection), std::end(collection), value) !=
         std::end(collection);
}


// 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);
boost::optional<std::string> ReadContent(const AbsolutePath& filename);
std::vector<std::string> ReadLinesWithEnding(const AbsolutePath& filename);

bool WriteToFile(const std::string& filename, const std::string& content);


template <typename T, typename Fn>
void RemoveIf(std::vector<T>* vec, Fn predicate) {
  vec->erase(std::remove_if(vec->begin(), vec->end(), predicate), vec->end());
}

std::string FormatMicroseconds(long long microseconds);

// Makes sure all newlines in |output| are in \r\n format.
std::string UpdateToRnNewlines(std::string output);

// Utility methods to check if |path| is absolute.
bool IsAbsolutePath(const std::string& path);
bool IsUnixAbsolutePath(const std::string& path);
bool IsWindowsAbsolutePath(const std::string& path);

bool IsDirectory(const std::string& path);

// string <-> wstring conversion (UTF-16), e.g. for use with Window's wide APIs.
 std::string ws2s(std::wstring const& wstr);
 std::wstring s2ws(std::string const& str);

AbsolutePath NormalizePath(const std::string& path,
                            bool ensure_exists = true,
                            bool force_lower_on_windows = true);


int GetOffsetForPosition(lsPosition position, const std::string& content);

// Finds the position for an |offset| in |content|.
lsPosition GetPositionForOffset(int offset, const std::string& content);

// Utility method to find a position for the given character.
lsPosition CharPos(const std::string& search,
    char character,
    int character_offset = 0);


 void scanDirsNoRecursive(const std::wstring& rootPath, std::vector<std::wstring>& ret);

 void scanFilesUseRecursive(const std::wstring& rootPath, std::vector<std::wstring>& ret,
    std::wstring strSuf = L"");

 void scanFileNamesUseRecursive(const std::wstring& rootPath, std::vector<std::wstring>& ret,
    std::wstring strSuf = L"");
 void scanFileNamesUseRecursive(const std::string& rootPath, std::vector<std::string>& ret,
    std::string strSuf = "");

 void scanFilesUseRecursive(const std::string& rootPath, std::vector<std::string>& ret,
    std::string strSuf = "");

 void scanDirsUseRecursive(const std::wstring& rootPath, std::vector<std::wstring>& ret);

}