diff options
author | Derek Mauro <dmauro@google.com> | 2020-05-28 19:54:29 -0400 |
---|---|---|
committer | Derek Mauro <dmauro@google.com> | 2020-05-28 19:54:29 -0400 |
commit | 731d908c09e3fc5357008dbce3e13ea049f29820 (patch) | |
tree | 13638532d45d5bf3496a6cab3ca9e40312329c53 | |
parent | 99ea9ca3fedc03b87d62fd3326ed0f71d6ef749e (diff) | |
parent | c09fbb239338da11d81e4a854ae805b8e87b381b (diff) | |
download | googletest-731d908c09e3fc5357008dbce3e13ea049f29820.tar.gz |
Merge pull request #2677 from IYP-Programer-Yeah:fix-file-path-normalize-function
PiperOrigin-RevId: 312486861
-rw-r--r-- | googletest/src/gtest-filepath.cc | 36 |
1 files changed, 12 insertions, 24 deletions
diff --git a/googletest/src/gtest-filepath.cc b/googletest/src/gtest-filepath.cc index 9aad12fb..062b95b1 100644 --- a/googletest/src/gtest-filepath.cc +++ b/googletest/src/gtest-filepath.cc @@ -349,33 +349,21 @@ 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() { - if (pathname_.c_str() == nullptr) { - pathname_ = ""; - return; - } - const char* src = pathname_.c_str(); - char* const dest = new char[pathname_.length() + 1]; - char* dest_ptr = dest; - memset(dest_ptr, 0, pathname_.length() + 1); - - while (*src != '\0') { - *dest_ptr = *src; - if (!IsPathSeparator(*src)) { - src++; + std::string normalized_pathname; + normalized_pathname.reserve(pathname_.length()); + + 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); } else { -#if GTEST_HAS_ALT_PATH_SEP_ - if (*dest_ptr == kAlternatePathSeparator) { - *dest_ptr = kPathSeparator; - } -#endif - while (IsPathSeparator(*src)) - src++; + continue; } - dest_ptr++; } - *dest_ptr = '\0'; - pathname_ = dest; - delete[] dest; + + pathname_ = normalized_pathname; } } // namespace internal |