File: utils.h

package info (click to toggle)
asymptote 3.02%2Bds-1
  • links: PTS, VCS
  • area: main
  • in suites: trixie
  • size: 33,400 kB
  • sloc: cpp: 172,516; ansic: 69,728; python: 14,967; sh: 5,599; javascript: 4,866; lisp: 1,507; perl: 1,417; makefile: 1,028; yacc: 610; lex: 449; xml: 182; asm: 8
file content (123 lines) | stat: -rw-r--r-- 4,607 bytes parent folder | download | duplicates (2)
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
#pragma once
#include <algorithm>
#include <codecvt>
#include <functional>
#include <iterator>
#include <locale>
#include <memory>
#include <string>
#include <vector>
#include <optional>
#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(std::vector<std::string> const& values, std::string const& start);
bool StartsWithAny(std::string const& value, std::vector<std::string> const& startings);
bool EndsWithAny(std::string const& value, std::vector<std::string> const& endings);
bool FindAnyPartial(std::string const& value, std::vector<std::string> const& 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(std::string const& path);
// Returns |path| without the filetype, ie, "foo/bar.cc" => "foo/bar".
std::string StripFileType(std::string const& path);

std::string ReplaceAll(std::string const& source, std::string const& from, std::string const& to);

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

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

template<typename TCollection, typename TValue>
bool ContainsValue(TCollection const& collection, TValue const& 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(std::string const& filename);
optional<std::string> ReadContent(AbsolutePath const& filename);
std::vector<std::string> ReadLinesWithEnding(AbsolutePath const& filename);

bool WriteToFile(std::string const& filename, std::string const& 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(std::string const& path);
bool IsUnixAbsolutePath(std::string const& path);
bool IsWindowsAbsolutePath(std::string const& path);

bool IsDirectory(std::string const& 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(std::string const& path, bool ensure_exists = true, bool force_lower_on_windows = true);

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

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

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

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

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

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

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

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

} // namespace lsp