fs/Traits: pass string_view to Build()

This commit is contained in:
Max Kellermann 2020-03-13 19:49:46 +01:00
parent b6b15afb5a
commit a885bdba4c
3 changed files with 29 additions and 54 deletions

View File

@ -56,10 +56,6 @@ class AllocatedPath {
AllocatedPath(string &&_value) noexcept
:value(std::move(_value)) {}
static AllocatedPath Build(const_pointer a, size_t a_size,
const_pointer b, size_t b_size) noexcept {
return AllocatedPath(Traits::Build(a, a_size, b, b_size));
}
public:
/**
* Construct a "nulled" instance. Its IsNull() method will
@ -93,14 +89,13 @@ public:
/**
* Join two path components with the path separator.
*/
gcc_pure gcc_nonnull_all
static AllocatedPath Build(const_pointer a, const_pointer b) noexcept {
return Build(a, Traits::GetLength(a),
b, Traits::GetLength(b));
gcc_pure
static AllocatedPath Build(string_view a, string_view b) noexcept {
return AllocatedPath(Traits::Build(a, b));
}
gcc_pure gcc_nonnull_all
static AllocatedPath Build(Path a, const_pointer b) noexcept {
static AllocatedPath Build(Path a, string_view b) noexcept {
return Build(a.c_str(), b);
}
@ -110,24 +105,21 @@ public:
}
gcc_pure gcc_nonnull_all
static AllocatedPath Build(const_pointer a,
static AllocatedPath Build(string_view a,
const AllocatedPath &b) noexcept {
return Build(a, Traits::GetLength(a),
b.value.c_str(), b.value.size());
return Build(a, b.value);
}
gcc_pure gcc_nonnull_all
static AllocatedPath Build(const AllocatedPath &a,
const_pointer b) noexcept {
return Build(a.value.c_str(), a.value.size(),
b, Traits::GetLength(b));
string_view b) noexcept {
return Build(a.value, b);
}
gcc_pure
static AllocatedPath Build(const AllocatedPath &a,
const AllocatedPath &b) noexcept {
return Build(a.value.c_str(), a.value.size(),
b.value.c_str(), b.value.size());
return Build(a.value, b.value);
}
gcc_pure

View File

@ -24,29 +24,26 @@
template<typename Traits>
typename Traits::string
BuildPathImpl(typename Traits::const_pointer a, size_t a_size,
typename Traits::const_pointer b, size_t b_size) noexcept
BuildPathImpl(typename Traits::string_view a,
typename Traits::string_view b) noexcept
{
assert(a != nullptr);
assert(b != nullptr);
if (a_size == 0)
return typename Traits::string(b, b_size);
if (b_size == 0)
return typename Traits::string(a, a_size);
if (a.empty())
return typename Traits::string(b);
if (b.empty())
return typename Traits::string(a);
typename Traits::string result;
result.reserve(a_size + 1 + b_size);
result.reserve(a.length() + 1 + b.length());
result.append(a, a_size);
result.append(a);
if (!Traits::IsSeparator(a[a_size - 1]))
if (!Traits::IsSeparator(a.back()))
result.push_back(Traits::SEPARATOR);
if (Traits::IsSeparator(b[0]))
result.append(b + 1, b_size - 1);
if (Traits::IsSeparator(b.front()))
result.append(b.substr(1));
else
result.append(b, b_size);
result.append(b);
return result;
}
@ -122,10 +119,9 @@ RelativePathImpl(typename Traits::const_pointer base,
}
PathTraitsFS::string
PathTraitsFS::Build(const_pointer a, size_t a_size,
const_pointer b, size_t b_size) noexcept
PathTraitsFS::Build(string_view a, string_view b) noexcept
{
return BuildPathImpl<PathTraitsFS>(a, a_size, b, b_size);
return BuildPathImpl<PathTraitsFS>(a, b);
}
PathTraitsFS::const_pointer
@ -161,10 +157,9 @@ PathTraitsFS::Apply(const_pointer base, const_pointer path) noexcept
}
PathTraitsUTF8::string
PathTraitsUTF8::Build(const_pointer a, size_t a_size,
const_pointer b, size_t b_size) noexcept
PathTraitsUTF8::Build(string_view a, string_view b) noexcept
{
return BuildPathImpl<PathTraitsUTF8>(a, a_size, b, b_size);
return BuildPathImpl<PathTraitsUTF8>(a, b);
}
PathTraitsUTF8::const_pointer

View File

@ -156,14 +156,8 @@ struct PathTraitsFS {
* remaining component is returned unchanged.
* If both components are empty strings, empty string is returned.
*/
gcc_pure gcc_nonnull_all
static string Build(const_pointer a, size_t a_size,
const_pointer b, size_t b_size) noexcept;
gcc_pure gcc_nonnull_all
static string Build(const_pointer a, const_pointer b) noexcept {
return Build(a, GetLength(a), b, GetLength(b));
}
gcc_pure
static string Build(string_view a, string_view b) noexcept;
/**
* Interpret the given path as being relative to the given
@ -271,14 +265,8 @@ struct PathTraitsUTF8 {
* remaining component is returned unchanged.
* If both components are empty strings, empty string is returned.
*/
gcc_pure gcc_nonnull_all
static string Build(const_pointer a, size_t a_size,
const_pointer b, size_t b_size) noexcept;
gcc_pure gcc_nonnull_all
static string Build(const_pointer a, const_pointer b) noexcept {
return Build(a, GetLength(a), b, GetLength(b));
}
gcc_pure
static string Build(string_view a, string_view b) noexcept;
};
#endif