fs/Traits: add IsAbsolute(string_view)

This commit is contained in:
Max Kellermann 2025-01-30 11:52:56 +01:00
parent 66ee03741d
commit f6cee35896
3 changed files with 20 additions and 2 deletions

@ -94,7 +94,7 @@ print_storage_uri(Client &client, Response &r, const Storage &storage)
if (uri.empty()) if (uri.empty())
return; return;
if (PathTraitsUTF8::IsAbsolute(uri.c_str())) { if (PathTraitsUTF8::IsAbsolute(uri)) {
/* storage points to local directory */ /* storage points to local directory */
if (!client.IsLocal()) if (!client.IsLocal())

@ -337,7 +337,7 @@ public:
[[gnu::pure]] [[gnu::pure]]
bool IsAbsolute() const noexcept { bool IsAbsolute() const noexcept {
return Traits::IsAbsolute(c_str()); return Traits::IsAbsolute(value);
} }
}; };

@ -130,6 +130,15 @@ struct PathTraitsFS {
return IsSeparator(*p); return IsSeparator(*p);
} }
[[gnu::pure]]
static bool IsAbsolute(string_view p) noexcept {
#ifdef _WIN32
if (IsDrive(p) && IsSeparator(p[2]))
return true;
#endif
return !p.empty() && IsSeparator(p.front());
}
[[gnu::pure]] [[gnu::nonnull]] [[gnu::pure]] [[gnu::nonnull]]
static bool IsSpecialFilename(const_pointer name) noexcept { static bool IsSpecialFilename(const_pointer name) noexcept {
return (name[0] == '.' && name[1] == 0) || return (name[0] == '.' && name[1] == 0) ||
@ -274,6 +283,15 @@ struct PathTraitsUTF8 {
return IsSeparator(*p); return IsSeparator(*p);
} }
[[gnu::pure]]
static bool IsAbsolute(string_view p) noexcept {
#ifdef _WIN32
if (IsDrive(p) && IsSeparator(p[2]))
return true;
#endif
return !p.empty() && IsSeparator(p.front());
}
/** /**
* Is this any kind of absolute URI? (Unlike IsAbsolute(), * Is this any kind of absolute URI? (Unlike IsAbsolute(),
* this includes URIs/URLs with a scheme) * this includes URIs/URLs with a scheme)