11#include <llvm/ADT/StringExtras.h>
12#include <llvm/ADT/Twine.h>
13#include <llvm/Support/Error.h>
14#include <llvm/Support/Path.h>
22 return Path.size() > 1 && llvm::isAlpha(
Path[0]) &&
Path[1] ==
':';
27 llvm::sys::path::is_separator(
Path[0]);
34class FileSystemScheme :
public URIScheme {
36 llvm::Expected<std::string>
37 getAbsolutePath(llvm::StringRef Authority, llvm::StringRef Body,
38 llvm::StringRef )
const override {
39 if (!Body.startswith(
"/"))
40 return error(
"File scheme: expect body to be an absolute path starting "
43 llvm::SmallString<128>
Path;
44 if (!Authority.empty()) {
49 Body.consume_front(
"/");
52 llvm::sys::path::native(
Path);
53 return std::string(
Path);
57 uriFromAbsolutePath(llvm::StringRef
AbsolutePath)
const override {
59 llvm::StringRef Authority;
63 Authority =
Root.drop_front(2);
70 return URI(
"file", Authority, Body);
74llvm::Expected<std::unique_ptr<URIScheme>>
77 return std::make_unique<FileSystemScheme>();
80 if (URIScheme.getName() != Scheme)
82 return URIScheme.instantiate();
84 return error(
"Can't find scheme: {0}", Scheme);
89 if ((
C >=
'a' &&
C <=
'z') || (
C >=
'A' &&
C <=
'Z') ||
90 (
C >=
'0' &&
C <=
'9'))
113 Out.push_back(llvm::hexdigit(
C / 16));
114 Out.push_back(llvm::hexdigit(
C % 16));
128 if (*
I ==
'%' &&
I + 2 <
Content.end() && llvm::isHexDigit(*(
I + 1)) &&
129 llvm::isHexDigit(*(
I + 2))) {
130 Result.push_back(llvm::hexFromNibbles(*(
I + 1), *(
I + 2)));
141 if (!llvm::isAlpha(Scheme[0]))
143 return llvm::all_of(llvm::drop_begin(Scheme), [](
char C) {
144 return llvm::isAlnum(
C) ||
C ==
'+' ||
C ==
'.' ||
C ==
'-';
150URI::URI(llvm::StringRef Scheme, llvm::StringRef Authority,
151 llvm::StringRef Body)
152 : Scheme(Scheme), Authority(Authority), Body(Body) {
154 assert((Authority.empty() || Body.startswith(
"/")) &&
155 "URI body must start with '/' when authority is present.");
162 if (Authority.empty() && Body.empty())
166 if (!Authority.empty() || llvm::StringRef(Body).startswith(
"/")) {
178 auto Pos =
Uri.find(
':');
179 if (Pos == llvm::StringRef::npos)
180 return error(
"Scheme must be provided in URI: {0}",
OrigUri);
184 return error(
"Invalid scheme: {0} (decoded: {1})",
SchemeStr,
U.Scheme);
185 Uri =
Uri.substr(Pos + 1);
186 if (
Uri.consume_front(
"//")) {
199 return Uri.takeError();
202 return Path.takeError();
207 llvm::StringRef Scheme) {
212 return S.takeError();
220 for (
auto &
Entry : URISchemeRegistry::entries()) {
226 llvm::consumeError(
URI.takeError());
229 return std::move(*
URI);
236 auto U = FileSystemScheme().uriFromAbsolutePath(
AbsolutePath);
239 return std::move(*
U);
246 return S.takeError();
252 if (!llvm::sys::path::is_absolute(
AbsPath))
254 for (
auto &
Entry : URISchemeRegistry::entries()) {
255 auto S =
Entry.instantiate();
256 auto U =
S->uriFromAbsolutePath(
AbsPath);
261 llvm::consumeError(
U.takeError());
264 return S->getAbsolutePath(
U->Authority,
U->Body,
HintPath);
273 return S.takeError();
274 return S->get()->getIncludeSpelling(
Uri);
static llvm::Expected< std::string > resolve(const URI &U, llvm::StringRef HintPath="")
static llvm::Expected< URI > parse(llvm::StringRef Uri)
static llvm::Expected< URI > create(llvm::StringRef AbsolutePath, llvm::StringRef Scheme)
std::string toString() const
Returns a string URI with all components percent-encoded.
static llvm::Expected< std::string > includeSpelling(const URI &U)
static URI createFile(llvm::StringRef AbsolutePath)
This creates a file:// URI for AbsolutePath. The path must be absolute.
static llvm::Expected< std::string > resolvePath(llvm::StringRef AbsPath, llvm::StringRef HintPath="")
Whether current platform treats paths case insensitively.
llvm::Error error(std::error_code EC, const char *Fmt, Ts &&...Vals)
bool fromJSON(const llvm::json::Value &, URIForFile &, llvm::json::Path)
llvm::Registry< URIScheme > URISchemeRegistry