aboutsummaryrefslogtreecommitdiffhomepage
path: root/Firestore/core/src/firebase/firestore/util/path.cc
diff options
context:
space:
mode:
authorGravatar Gil <mcg@google.com>2018-07-11 15:27:50 -0700
committerGravatar GitHub <noreply@github.com>2018-07-11 15:27:50 -0700
commite1ace703fbf4458d9e887ebf30050b1a0482a2d2 (patch)
tree63537e388b8d16a5625eedce0937cb50bf69d4c7 /Firestore/core/src/firebase/firestore/util/path.cc
parent79f663d5eaaa4b7f9fe46d176c82660d38e4be8a (diff)
Fix Firestore source errors under VS2017 (#1515)
* Project file updates from sync_project.rb * Fix misc compile errors under VS2017 * Fix util/hashing under VS2017 std::hash<int> is not just a pass through in Microsoft's STL. * Disable unsafe code warnings in VS2017 ... where comparing against a reference implementation that has no easy safe equivalent. * Handle drive letters in paths on Windows
Diffstat (limited to 'Firestore/core/src/firebase/firestore/util/path.cc')
-rw-r--r--Firestore/core/src/firebase/firestore/util/path.cc47
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('/');
}