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 AllocatedPath(string &&_value) noexcept
:value(std::move(_value)) {} :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: public:
/** /**
* Construct a "nulled" instance. Its IsNull() method will * Construct a "nulled" instance. Its IsNull() method will
@ -93,14 +89,13 @@ public:
/** /**
* Join two path components with the path separator. * Join two path components with the path separator.
*/ */
gcc_pure gcc_nonnull_all gcc_pure
static AllocatedPath Build(const_pointer a, const_pointer b) noexcept { static AllocatedPath Build(string_view a, string_view b) noexcept {
return Build(a, Traits::GetLength(a), return AllocatedPath(Traits::Build(a, b));
b, Traits::GetLength(b));
} }
gcc_pure gcc_nonnull_all 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); return Build(a.c_str(), b);
} }
@ -110,24 +105,21 @@ public:
} }
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
static AllocatedPath Build(const_pointer a, static AllocatedPath Build(string_view a,
const AllocatedPath &b) noexcept { const AllocatedPath &b) noexcept {
return Build(a, Traits::GetLength(a), return Build(a, b.value);
b.value.c_str(), b.value.size());
} }
gcc_pure gcc_nonnull_all gcc_pure gcc_nonnull_all
static AllocatedPath Build(const AllocatedPath &a, static AllocatedPath Build(const AllocatedPath &a,
const_pointer b) noexcept { string_view b) noexcept {
return Build(a.value.c_str(), a.value.size(), return Build(a.value, b);
b, Traits::GetLength(b));
} }
gcc_pure gcc_pure
static AllocatedPath Build(const AllocatedPath &a, static AllocatedPath Build(const AllocatedPath &a,
const AllocatedPath &b) noexcept { const AllocatedPath &b) noexcept {
return Build(a.value.c_str(), a.value.size(), return Build(a.value, b.value);
b.value.c_str(), b.value.size());
} }
gcc_pure gcc_pure

View File

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

View File

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