diff options
Diffstat (limited to 'Firestore/core/src/firebase/firestore/util/path.cc')
-rw-r--r-- | Firestore/core/src/firebase/firestore/util/path.cc | 47 |
1 files changed, 37 insertions, 10 deletions
diff --git a/Firestore/core/src/firebase/firestore/util/path.cc b/Firestore/core/src/firebase/firestore/util/path.cc index 940f12a..0da3b23 100644 --- a/Firestore/core/src/firebase/firestore/util/path.cc +++ b/Firestore/core/src/firebase/firestore/util/path.cc @@ -16,14 +16,45 @@ #include "Firestore/core/src/firebase/firestore/util/path.h" +#include "absl/strings/ascii.h" +#include "absl/strings/string_view.h" + namespace firebase { namespace firestore { namespace util { +namespace { + +static constexpr absl::string_view::size_type npos = absl::string_view::npos; + +/** Returns the given path with its leading drive letter removed. */ +inline absl::string_view StripDriveLetter(absl::string_view path) { +#if defined(_WIN32) + if (path.size() >= 2 && path[1] == ':' && absl::ascii_isalpha(path[0])) { + return path.substr(2); + } + return path; + +#else + return path; +#endif // defined(_WIN32) +} + +/** Returns true if the given character is a pathname separator. */ +inline bool IsSeparator(char c) { +#if defined(_WIN32) + return c == '/' || c == '\\'; +#else + return c == '/'; +#endif // defined(_WIN32) +} + +} // namespace + absl::string_view Path::Basename(absl::string_view pathname) { size_t slash = pathname.find_last_of('/'); - if (slash == absl::string_view::npos) { + if (slash == npos) { // No path separator found => the whole string. return pathname; } @@ -35,7 +66,7 @@ absl::string_view Path::Basename(absl::string_view pathname) { absl::string_view Path::Dirname(absl::string_view pathname) { size_t last_slash = pathname.find_last_of('/'); - if (last_slash == absl::string_view::npos) { + if (last_slash == npos) { // No path separator found => empty string. Conformance with POSIX would // have us return "." here. return pathname.substr(0, 0); @@ -43,7 +74,7 @@ absl::string_view Path::Dirname(absl::string_view pathname) { // Collapse runs of slashes. size_t nonslash = pathname.find_last_not_of('/', last_slash); - if (nonslash == absl::string_view::npos) { + if (nonslash == npos) { // All characters preceding the last path separator are slashes return pathname.substr(0, 1); } @@ -55,12 +86,8 @@ absl::string_view Path::Dirname(absl::string_view pathname) { } bool Path::IsAbsolute(absl::string_view path) { -#if defined(_WIN32) -#error "Handle drive letters" - -#else - return !path.empty() && path.front() == '/'; -#endif + path = StripDriveLetter(path); + return !path.empty() && IsSeparator(path.front()); } void Path::JoinAppend(std::string* base, absl::string_view path) { @@ -69,7 +96,7 @@ void Path::JoinAppend(std::string* base, absl::string_view path) { } else { size_t nonslash = base->find_last_not_of('/'); - if (nonslash != std::string::npos) { + if (nonslash != npos) { base->resize(nonslash + 1); base->push_back('/'); } |