Make Path use StringRef instead of std::string where possible.

git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@91620 91177308-0d34-0410-b5e6-96231b3b80d8
This commit is contained in:
Jeffrey Yasskin
2009-12-17 21:02:39 +00:00
parent aeb79aea8f
commit 88cd3582b6
5 changed files with 69 additions and 67 deletions

View File

@@ -14,6 +14,7 @@
#ifndef LLVM_SYSTEM_PATH_H #ifndef LLVM_SYSTEM_PATH_H
#define LLVM_SYSTEM_PATH_H #define LLVM_SYSTEM_PATH_H
#include "llvm/ADT/StringRef.h"
#include "llvm/System/TimeValue.h" #include "llvm/System/TimeValue.h"
#include <set> #include <set>
#include <string> #include <string>
@@ -159,7 +160,7 @@ namespace sys {
/// between processes. /// between processes.
/// @returns The dynamic link library suffix for the current platform. /// @returns The dynamic link library suffix for the current platform.
/// @brief Return the dynamic link library suffix. /// @brief Return the dynamic link library suffix.
static std::string GetDLLSuffix(); static StringRef GetDLLSuffix();
/// GetMainExecutable - Return the path to the main executable, given the /// GetMainExecutable - Return the path to the main executable, given the
/// value of argv[0] from program startup and the address of main itself. /// value of argv[0] from program startup and the address of main itself.
@@ -174,12 +175,12 @@ namespace sys {
Path() : path() {} Path() : path() {}
Path(const Path &that) : path(that.path) {} Path(const Path &that) : path(that.path) {}
/// This constructor will accept a std::string as a path. No checking is /// This constructor will accept a char* or std::string as a path. No
/// done on this path to determine if it is valid. To determine validity /// checking is done on this path to determine if it is valid. To
/// of the path, use the isValid method. /// determine validity of the path, use the isValid method.
/// @param p The path to assign. /// @param p The path to assign.
/// @brief Construct a Path from a string. /// @brief Construct a Path from a string.
explicit Path(const std::string& p); explicit Path(StringRef p);
/// This constructor will accept a character range as a path. No checking /// This constructor will accept a character range as a path. No checking
/// is done on this path to determine if it is valid. To determine /// is done on this path to determine if it is valid. To determine
@@ -202,10 +203,10 @@ namespace sys {
} }
/// Makes a copy of \p that to \p this. /// Makes a copy of \p that to \p this.
/// @param \p that A std::string denoting the path /// @param \p that A StringRef denoting the path
/// @returns \p this /// @returns \p this
/// @brief Assignment Operator /// @brief Assignment Operator
Path &operator=(const std::string &that); Path &operator=(StringRef that);
/// Compares \p this Path with \p that Path for equality. /// Compares \p this Path with \p that Path for equality.
/// @returns true if \p this and \p that refer to the same thing. /// @returns true if \p this and \p that refer to the same thing.
@@ -251,28 +252,28 @@ namespace sys {
/// component is the file or directory name occuring after the last /// component is the file or directory name occuring after the last
/// directory separator. If no directory separator is present, the entire /// directory separator. If no directory separator is present, the entire
/// path name is returned (i.e. same as toString). /// path name is returned (i.e. same as toString).
/// @returns std::string containing the last component of the path name. /// @returns StringRef containing the last component of the path name.
/// @brief Returns the last component of the path name. /// @brief Returns the last component of the path name.
std::string getLast() const; StringRef getLast() const;
/// This function strips off the path and suffix of the file or directory /// This function strips off the path and suffix of the file or directory
/// name and returns just the basename. For example /a/foo.bar would cause /// name and returns just the basename. For example /a/foo.bar would cause
/// this function to return "foo". /// this function to return "foo".
/// @returns std::string containing the basename of the path /// @returns StringRef containing the basename of the path
/// @brief Get the base name of the path /// @brief Get the base name of the path
std::string getBasename() const; StringRef getBasename() const;
/// This function strips off the suffix of the path beginning with the /// This function strips off the suffix of the path beginning with the
/// path separator ('/' on Unix, '\' on Windows) and returns the result. /// path separator ('/' on Unix, '\' on Windows) and returns the result.
std::string getDirname() const; StringRef getDirname() const;
/// This function strips off the path and basename(up to and /// This function strips off the path and basename(up to and
/// including the last dot) of the file or directory name and /// including the last dot) of the file or directory name and
/// returns just the suffix. For example /a/foo.bar would cause /// returns just the suffix. For example /a/foo.bar would cause
/// this function to return "bar". /// this function to return "bar".
/// @returns std::string containing the suffix of the path /// @returns StringRef containing the suffix of the path
/// @brief Get the suffix of the path /// @brief Get the suffix of the path
std::string getSuffix() const; StringRef getSuffix() const;
/// Obtain a 'C' string for the path name. /// Obtain a 'C' string for the path name.
/// @returns a 'C' string containing the path name. /// @returns a 'C' string containing the path name.
@@ -315,7 +316,7 @@ namespace sys {
/// cases (file not found, file not accessible, etc.) it returns false. /// cases (file not found, file not accessible, etc.) it returns false.
/// @returns true if the magic number of the file matches \p magic. /// @returns true if the magic number of the file matches \p magic.
/// @brief Determine if file has a specific magic number /// @brief Determine if file has a specific magic number
bool hasMagicNumber(const std::string& magic) const; bool hasMagicNumber(StringRef magic) const;
/// This function retrieves the first \p len bytes of the file associated /// This function retrieves the first \p len bytes of the file associated
/// with \p this. These bytes are returned as the "magic number" in the /// with \p this. These bytes are returned as the "magic number" in the
@@ -422,8 +423,8 @@ namespace sys {
/// Path object takes on the path value of \p unverified_path /// Path object takes on the path value of \p unverified_path
/// @returns true if the path was set, false otherwise. /// @returns true if the path was set, false otherwise.
/// @param unverified_path The path to be set in Path object. /// @param unverified_path The path to be set in Path object.
/// @brief Set a full path from a std::string /// @brief Set a full path from a StringRef
bool set(const std::string& unverified_path); bool set(StringRef unverified_path);
/// One path component is removed from the Path. If only one component is /// One path component is removed from the Path. If only one component is
/// present in the path, the Path object becomes empty. If the Path object /// present in the path, the Path object becomes empty. If the Path object
@@ -437,7 +438,7 @@ namespace sys {
/// needed. /// needed.
/// @returns false if the path component could not be added. /// @returns false if the path component could not be added.
/// @brief Appends one path component to the Path. /// @brief Appends one path component to the Path.
bool appendComponent( const std::string& component ); bool appendComponent(StringRef component);
/// A period and the \p suffix are appended to the end of the pathname. /// A period and the \p suffix are appended to the end of the pathname.
/// The precondition for this function is that the Path reference a file /// The precondition for this function is that the Path reference a file
@@ -446,7 +447,7 @@ namespace sys {
/// become invalid for the host operating system, false is returned. /// become invalid for the host operating system, false is returned.
/// @returns false if the suffix could not be added, true if it was. /// @returns false if the suffix could not be added, true if it was.
/// @brief Adds a period and the \p suffix to the end of the pathname. /// @brief Adds a period and the \p suffix to the end of the pathname.
bool appendSuffix(const std::string& suffix); bool appendSuffix(StringRef suffix);
/// The suffix of the filename is erased. The suffix begins with and /// The suffix of the filename is erased. The suffix begins with and
/// includes the last . character in the filename after the last directory /// includes the last . character in the filename after the last directory
@@ -620,12 +621,12 @@ namespace sys {
PathWithStatus(const Path &other) PathWithStatus(const Path &other)
: Path(other), status(), fsIsValid(false) {} : Path(other), status(), fsIsValid(false) {}
/// This constructor will accept a std::string as a path. No checking is /// This constructor will accept a char* or std::string as a path. No
/// done on this path to determine if it is valid. To determine validity /// checking is done on this path to determine if it is valid. To
/// of the path, use the isValid method. /// determine validity of the path, use the isValid method.
/// @brief Construct a Path from a string. /// @brief Construct a Path from a string.
explicit PathWithStatus( explicit PathWithStatus(
const std::string& p ///< The path to assign. StringRef p ///< The path to assign.
) : Path(p), status(), fsIsValid(false) {} ) : Path(p), status(), fsIsValid(false) {}
/// This constructor will accept a character range as a path. No checking /// This constructor will accept a character range as a path. No checking

View File

@@ -35,7 +35,7 @@ namespace llvmc {
const std::string& LanguageMap::GetLanguage(const sys::Path& File) const { const std::string& LanguageMap::GetLanguage(const sys::Path& File) const {
LanguageMap::const_iterator Lang = this->find(File.getSuffix()); LanguageMap::const_iterator Lang = this->find(File.getSuffix());
if (Lang == this->end()) if (Lang == this->end())
throw std::runtime_error("Unknown suffix: " + File.getSuffix()); throw std::runtime_error(("Unknown suffix: " + File.getSuffix()).str());
return Lang->second; return Lang->second;
} }
} }

View File

@@ -176,7 +176,7 @@ Path::FindLibrary(std::string& name) {
return sys::Path(); return sys::Path();
} }
std::string Path::GetDLLSuffix() { StringRef Path::GetDLLSuffix() {
return LTDL_SHLIB_EXT; return LTDL_SHLIB_EXT;
} }
@@ -191,7 +191,7 @@ Path::isBitcodeFile() const {
return FT == Bitcode_FileType; return FT == Bitcode_FileType;
} }
bool Path::hasMagicNumber(const std::string &Magic) const { bool Path::hasMagicNumber(StringRef Magic) const {
std::string actualMagic; std::string actualMagic;
if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size()))) if (getMagicNumber(actualMagic, static_cast<unsigned>(Magic.size())))
return Magic == actualMagic; return Magic == actualMagic;
@@ -217,8 +217,9 @@ static void getPathList(const char*path, std::vector<Path>& Paths) {
Paths.push_back(tmpPath); Paths.push_back(tmpPath);
} }
static std::string getDirnameCharSep(const std::string& path, char Sep) { static StringRef getDirnameCharSep(StringRef path, const char *Sep) {
assert(Sep[0] != '\0' && Sep[1] == '\0' &&
"Sep must be a 1-character string literal.");
if (path.empty()) if (path.empty())
return "."; return ".";
@@ -227,31 +228,31 @@ static std::string getDirnameCharSep(const std::string& path, char Sep) {
signed pos = static_cast<signed>(path.size()) - 1; signed pos = static_cast<signed>(path.size()) - 1;
while (pos >= 0 && path[pos] == Sep) while (pos >= 0 && path[pos] == Sep[0])
--pos; --pos;
if (pos < 0) if (pos < 0)
return path[0] == Sep ? std::string(1, Sep) : std::string("."); return path[0] == Sep[0] ? Sep : ".";
// Any slashes left? // Any slashes left?
signed i = 0; signed i = 0;
while (i < pos && path[i] != Sep) while (i < pos && path[i] != Sep[0])
++i; ++i;
if (i == pos) // No slashes? Return "." if (i == pos) // No slashes? Return "."
return "."; return ".";
// There is at least one slash left. Remove all trailing non-slashes. // There is at least one slash left. Remove all trailing non-slashes.
while (pos >= 0 && path[pos] != Sep) while (pos >= 0 && path[pos] != Sep[0])
--pos; --pos;
// Remove any trailing slashes. // Remove any trailing slashes.
while (pos >= 0 && path[pos] == Sep) while (pos >= 0 && path[pos] == Sep[0])
--pos; --pos;
if (pos < 0) if (pos < 0)
return path[0] == Sep ? std::string(1, Sep) : std::string("."); return path[0] == Sep[0] ? Sep : ".";
return path.substr(0, pos+1); return path.substr(0, pos+1);
} }

View File

@@ -78,15 +78,15 @@ using namespace sys;
const char sys::PathSeparator = ':'; const char sys::PathSeparator = ':';
Path::Path(const std::string& p) Path::Path(StringRef p)
: path(p) {} : path(p) {}
Path::Path(const char *StrStart, unsigned StrLen) Path::Path(const char *StrStart, unsigned StrLen)
: path(StrStart, StrLen) {} : path(StrStart, StrLen) {}
Path& Path&
Path::operator=(const std::string &that) { Path::operator=(StringRef that) {
path = that; path.assign(that.data(), that.size());
return *this; return *this;
} }
@@ -377,11 +377,11 @@ Path Path::GetMainExecutable(const char *argv0, void *MainAddr) {
} }
std::string Path::getDirname() const { StringRef Path::getDirname() const {
return getDirnameCharSep(path, '/'); return getDirnameCharSep(path, "/");
} }
std::string StringRef
Path::getBasename() const { Path::getBasename() const {
// Find the last slash // Find the last slash
std::string::size_type slash = path.rfind('/'); std::string::size_type slash = path.rfind('/');
@@ -392,12 +392,12 @@ Path::getBasename() const {
std::string::size_type dot = path.rfind('.'); std::string::size_type dot = path.rfind('.');
if (dot == std::string::npos || dot < slash) if (dot == std::string::npos || dot < slash)
return path.substr(slash); return StringRef(path).substr(slash);
else else
return path.substr(slash, dot - slash); return StringRef(path).substr(slash, dot - slash);
} }
std::string StringRef
Path::getSuffix() const { Path::getSuffix() const {
// Find the last slash // Find the last slash
std::string::size_type slash = path.rfind('/'); std::string::size_type slash = path.rfind('/');
@@ -408,9 +408,9 @@ Path::getSuffix() const {
std::string::size_type dot = path.rfind('.'); std::string::size_type dot = path.rfind('.');
if (dot == std::string::npos || dot < slash) if (dot == std::string::npos || dot < slash)
return std::string(); return StringRef("");
else else
return path.substr(dot + 1); return StringRef(path).substr(dot + 1);
} }
bool Path::getMagicNumber(std::string &Magic, unsigned len) const { bool Path::getMagicNumber(std::string &Magic, unsigned len) const {
@@ -478,7 +478,7 @@ Path::canExecute() const {
return true; return true;
} }
std::string StringRef
Path::getLast() const { Path::getLast() const {
// Find the last slash // Find the last slash
size_t pos = path.rfind('/'); size_t pos = path.rfind('/');
@@ -492,12 +492,12 @@ Path::getLast() const {
// Find the second to last slash // Find the second to last slash
size_t pos2 = path.rfind('/', pos-1); size_t pos2 = path.rfind('/', pos-1);
if (pos2 == std::string::npos) if (pos2 == std::string::npos)
return path.substr(0,pos); return StringRef(path).substr(0,pos);
else else
return path.substr(pos2+1,pos-pos2-1); return StringRef(path).substr(pos2+1,pos-pos2-1);
} }
// Return everything after the last slash // Return everything after the last slash
return path.substr(pos+1); return StringRef(path).substr(pos+1);
} }
const FileStatus * const FileStatus *
@@ -589,7 +589,7 @@ Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
} }
bool bool
Path::set(const std::string& a_path) { Path::set(StringRef a_path) {
if (a_path.empty()) if (a_path.empty())
return false; return false;
std::string save(path); std::string save(path);
@@ -602,7 +602,7 @@ Path::set(const std::string& a_path) {
} }
bool bool
Path::appendComponent(const std::string& name) { Path::appendComponent(StringRef name) {
if (name.empty()) if (name.empty())
return false; return false;
std::string save(path); std::string save(path);
@@ -634,7 +634,7 @@ Path::eraseComponent() {
} }
bool bool
Path::appendSuffix(const std::string& suffix) { Path::appendSuffix(StringRef suffix) {
std::string save(path); std::string save(path);
path.append("."); path.append(".");
path.append(suffix); path.append(suffix);

View File

@@ -58,8 +58,8 @@ Path::Path(const char *StrStart, unsigned StrLen)
} }
Path& Path&
Path::operator=(const std::string &that) { Path::operator=(StringRef that) {
path = that; path.assign(that.data(), that.size());
FlipBackSlashes(path); FlipBackSlashes(path);
return *this; return *this;
} }
@@ -287,11 +287,11 @@ Path::isRootDirectory() const {
return len > 0 && path[len-1] == '/'; return len > 0 && path[len-1] == '/';
} }
std::string Path::getDirname() const { StringRef Path::getDirname() const {
return getDirnameCharSep(path, '/'); return getDirnameCharSep(path, "/");
} }
std::string StringRef
Path::getBasename() const { Path::getBasename() const {
// Find the last slash // Find the last slash
size_t slash = path.rfind('/'); size_t slash = path.rfind('/');
@@ -302,12 +302,12 @@ Path::getBasename() const {
size_t dot = path.rfind('.'); size_t dot = path.rfind('.');
if (dot == std::string::npos || dot < slash) if (dot == std::string::npos || dot < slash)
return path.substr(slash); return StringRef(path).substr(slash);
else else
return path.substr(slash, dot - slash); return StringRef(path).substr(slash, dot - slash);
} }
std::string StringRef
Path::getSuffix() const { Path::getSuffix() const {
// Find the last slash // Find the last slash
size_t slash = path.rfind('/'); size_t slash = path.rfind('/');
@@ -318,9 +318,9 @@ Path::getSuffix() const {
size_t dot = path.rfind('.'); size_t dot = path.rfind('.');
if (dot == std::string::npos || dot < slash) if (dot == std::string::npos || dot < slash)
return std::string(); return StringRef("");
else else
return path.substr(dot + 1); return StringRef(path).substr(dot + 1);
} }
bool bool
@@ -364,7 +364,7 @@ Path::isRegularFile() const {
return true; return true;
} }
std::string StringRef
Path::getLast() const { Path::getLast() const {
// Find the last slash // Find the last slash
size_t pos = path.rfind('/'); size_t pos = path.rfind('/');
@@ -378,7 +378,7 @@ Path::getLast() const {
return path; return path;
// Return everything after the last slash // Return everything after the last slash
return path.substr(pos+1); return StringRef(path).substr(pos+1);
} }
const FileStatus * const FileStatus *
@@ -490,7 +490,7 @@ Path::getDirectoryContents(std::set<Path>& result, std::string* ErrMsg) const {
} }
bool bool
Path::set(const std::string& a_path) { Path::set(StringRef a_path) {
if (a_path.empty()) if (a_path.empty())
return false; return false;
std::string save(path); std::string save(path);
@@ -504,7 +504,7 @@ Path::set(const std::string& a_path) {
} }
bool bool
Path::appendComponent(const std::string& name) { Path::appendComponent(StringRef name) {
if (name.empty()) if (name.empty())
return false; return false;
std::string save(path); std::string save(path);
@@ -536,7 +536,7 @@ Path::eraseComponent() {
} }
bool bool
Path::appendSuffix(const std::string& suffix) { Path::appendSuffix(StringRef suffix) {
std::string save(path); std::string save(path);
path.append("."); path.append(".");
path.append(suffix); path.append(suffix);