Clear the project (#116)

This commit is contained in:
ykiko
2025-03-30 15:38:14 +08:00
committed by GitHub
parent 7f5c10f667
commit 74f2ebc4f4
30 changed files with 195 additions and 586 deletions

View File

@@ -7,6 +7,7 @@
#include "llvm/Support/FileSystem.h"
#include "llvm/Support/VirtualFileSystem.h"
#include "llvm/Support/MemoryBuffer.h"
#include "llvm/ADT/StringExtras.h"
namespace clice {
@@ -78,6 +79,80 @@ inline std::expected<std::string, std::error_code> read(llvm::StringRef path) {
return buffer.get()->getBuffer().str();
}
inline std::string toURI(llvm::StringRef fspath) {
if(!path::is_absolute(fspath))
std::abort();
llvm::SmallString<128> path("file://");
#if defined(_WIN32)
path.append("/");
#endif
for(auto c: fspath) {
if(c == '\\') {
path.push_back('/');
} else if(std::isalnum(c) || c == '-' || c == '_' || c == '.' || c == '/') {
path.push_back(c);
} else {
path.push_back('%');
path.push_back(llvm::hexdigit(c >> 4));
path.push_back(llvm::hexdigit(c & 0xF));
}
}
return path.str().str();
}
inline std::string decodePercent(llvm::StringRef content) {
std::string result;
result.reserve(content.size());
for(auto iter = content.begin(), send = content.end(); iter != send; ++iter) {
auto c = *iter;
if(c == '%' && iter + 2 < send) {
auto m = *(iter + 1);
auto n = *(iter + 2);
if(llvm::isHexDigit(m) && llvm::isHexDigit(n)) {
result += llvm::hexFromNibbles(m, n);
iter += 2;
continue;
}
}
result += c;
}
return result;
}
inline std::string toPath(llvm::StringRef uri) {
llvm::StringRef cloned = uri;
#if defined(_WIN32)
if(cloned.starts_with("file:///")) {
cloned = cloned.drop_front(8);
} else {
std::abort();
}
#elif defined(__unix__)
if(cloned.starts_with("file://")) {
cloned = cloned.drop_front(7);
} else {
std::abort();
}
#else
#error "Unsupported platform"
#endif
auto decoded = decodePercent(cloned);
llvm::SmallString<128> result;
if(auto err = fs::real_path(decoded, result)) {
print("Failed to get real path: {}, Input is {}\n", err.message(), decoded);
std::abort();
}
return result.str().str();
}
} // namespace fs
namespace vfs = llvm::vfs;