X-Git-Url: http://demsky.eecs.uci.edu/git/?a=blobdiff_plain;f=lib%2FSupport%2FPath.cpp;h=4d179218f78c7781d7b5cfc0a4be6abb306ff283;hb=285133714f0d995e0e14a77e23a0abe5dfc32d17;hp=535394c5b3ba1136b3845b736941233d4b1d8373;hpb=f21de942f3785d86d1f82cd19072ca26de9167da;p=oota-llvm.git diff --git a/lib/Support/Path.cpp b/lib/Support/Path.cpp index 535394c5b3b..4d179218f78 100644 --- a/lib/Support/Path.cpp +++ b/lib/Support/Path.cpp @@ -308,7 +308,30 @@ const_iterator &const_iterator::operator++() { return *this; } -const_iterator &const_iterator::operator--() { +bool const_iterator::operator==(const const_iterator &RHS) const { + return Path.begin() == RHS.Path.begin() && Position == RHS.Position; +} + +ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { + return Position - RHS.Position; +} + +reverse_iterator rbegin(StringRef Path) { + reverse_iterator I; + I.Path = Path; + I.Position = Path.size(); + return ++I; +} + +reverse_iterator rend(StringRef Path) { + reverse_iterator I; + I.Path = Path; + I.Component = Path.substr(0, 0); + I.Position = 0; + return I; +} + +reverse_iterator &reverse_iterator::operator++() { // If we're at the end and the previous char was a '/', return '.' unless // we are the root path. size_t root_dir_pos = root_dir_start(Path); @@ -335,19 +358,11 @@ const_iterator &const_iterator::operator--() { return *this; } -bool const_iterator::operator==(const const_iterator &RHS) const { - return Path.begin() == RHS.Path.begin() && +bool reverse_iterator::operator==(const reverse_iterator &RHS) const { + return Path.begin() == RHS.Path.begin() && Component == RHS.Component && Position == RHS.Position; } -bool const_iterator::operator!=(const const_iterator &RHS) const { - return !(*this == RHS); -} - -ptrdiff_t const_iterator::operator-(const const_iterator &RHS) const { - return Position - RHS.Position; -} - const StringRef root_path(StringRef path) { const_iterator b = begin(path), pos = b, @@ -525,14 +540,24 @@ void native(const Twine &path, SmallVectorImpl &result) { native(result); } -void native(SmallVectorImpl &path) { +void native(SmallVectorImpl &Path) { #ifdef LLVM_ON_WIN32 - std::replace(path.begin(), path.end(), '/', '\\'); + std::replace(Path.begin(), Path.end(), '/', '\\'); +#else + for (auto PI = Path.begin(), PE = Path.end(); PI < PE; ++PI) { + if (*PI == '\\') { + auto PN = PI + 1; + if (PN < PE && *PN == '\\') + ++PI; // increment once, the for loop will move over the escaped slash + else + *PI = '/'; + } + } #endif } const StringRef filename(StringRef path) { - return *(--end(path)); + return *rbegin(path); } const StringRef stem(StringRef path) { @@ -927,7 +952,7 @@ void directory_entry::replace_filename(const Twine &filename, file_status st) { } /// @brief Identify the magic in magic. - file_magic identify_magic(StringRef Magic) { +file_magic identify_magic(StringRef Magic) { if (Magic.size() < 4) return file_magic::unknown; switch ((unsigned char)Magic[0]) { @@ -1065,7 +1090,7 @@ std::error_code identify_magic(const Twine &Path, file_magic &Result) { char Buffer[32]; int Length = read(FD, Buffer, sizeof(Buffer)); - if (Length < 0) + if (close(FD) != 0 || Length < 0) return std::error_code(errno, std::generic_category()); Result = identify_magic(StringRef(Buffer, Length));