]> git-server-git.apps.pok.os.sepia.ceph.com Git - googletest.git/commitdiff
Improve FilePath::Normalize method
authorRuslan Manaev <manavrion@gmail.com>
Wed, 7 Oct 2020 18:39:50 +0000 (23:39 +0500)
committerRuslan Manaev <manavrion@gmail.com>
Wed, 7 Oct 2020 18:39:50 +0000 (23:39 +0500)
googletest/src/gtest-filepath.cc

index 062b95b1ff7d5249030d062d97963e87c7f7f576..af297684031cd257b321fa984bb570af4c9a23c7 100644 (file)
@@ -349,21 +349,19 @@ FilePath FilePath::RemoveTrailingPathSeparator() const {
 // For example, "bar///foo" becomes "bar/foo". Does not eliminate other
 // redundancies that might be in a pathname involving "." or "..".
 void FilePath::Normalize() {
-  std::string normalized_pathname;
-  normalized_pathname.reserve(pathname_.length());
+  auto out = pathname_.begin();
 
   for (const char character : pathname_) {
     if (!IsPathSeparator(character)) {
-      normalized_pathname.push_back(character);
-    } else if (normalized_pathname.empty() ||
-               normalized_pathname.back() != kPathSeparator) {
-      normalized_pathname.push_back(kPathSeparator);
+      *(out++) = character;
+    } else if (out == pathname_.begin() || *std::prev(out) != kPathSeparator) {
+      *(out++) = kPathSeparator;
     } else {
       continue;
     }
   }
 
-  pathname_ = normalized_pathname;
+  pathname_.erase(out, pathname_.end());
 }
 
 }  // namespace internal