diff options
author | Daniel Dunbar <daniel@zuster.org> | 2009-07-21 17:25:46 +0000 |
---|---|---|
committer | Daniel Dunbar <daniel@zuster.org> | 2009-07-21 17:25:46 +0000 |
commit | 7cb6860185187c74a1205deebff9e0f09a0ddae4 (patch) | |
tree | dfcd63bd9d3419bdd1b90f6677b1d2976cb73909 /include/llvm/ADT/StringRef.h | |
parent | 1010c21e4ef2580167d1d42989eca1bff859d346 (diff) |
Move StringRef comparison operators out of class.
Also, tweak the return type of size().
git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@76588 91177308-0d34-0410-b5e6-96231b3b80d8
Diffstat (limited to 'include/llvm/ADT/StringRef.h')
-rw-r--r-- | include/llvm/ADT/StringRef.h | 54 |
1 files changed, 38 insertions, 16 deletions
diff --git a/include/llvm/ADT/StringRef.h b/include/llvm/ADT/StringRef.h index 11903bef79..4e9bfa40a5 100644 --- a/include/llvm/ADT/StringRef.h +++ b/include/llvm/ADT/StringRef.h @@ -73,7 +73,14 @@ namespace llvm { bool empty() const { return Length == 0; } /// size - Get the string size. - unsigned size() const { return Length; } + size_t size() const { return Length; } + + /// equals - Check for string equality, this is more efficient than + /// compare() in when the relative ordering of inequal strings isn't needed. + bool equals(const StringRef &RHS) const { + return (Length == RHS.Length && + memcmp(Data, RHS.Data, Length) == 0); + } /// compare - Compare two strings; the result is -1, 0, or 1 if this string /// is lexicographically less than, equal to, or greater than the \arg RHS. @@ -95,20 +102,6 @@ namespace llvm { /// @name Operator Overloads /// @{ - bool operator==(const StringRef &RHS) const { - return Length == RHS.Length && memcmp(Data, RHS.Data, Length) == 0; - } - - bool operator!=(const StringRef &RHS) const { return !(*this == RHS); } - - bool operator<(const StringRef &RHS) const { return compare(RHS) == -1; } - - bool operator<=(const StringRef &RHS) const { return compare(RHS) != 1; } - - bool operator>(const StringRef &RHS) const { return compare(RHS) == 1; } - - bool operator>=(const StringRef &RHS) const { return compare(RHS) != -1; } - char operator[](size_t Index) const { assert(Index < Length && "Invalid index!"); return Data[Index]; @@ -142,12 +135,41 @@ namespace llvm { /// startswith - Check if this string starts with the given \arg Prefix. bool startswith(const StringRef &Prefix) const { - return substr(0, Prefix.Length) == Prefix; + return substr(0, Prefix.Length).equals(Prefix); } /// @} }; + /// @name StringRef Comparison Operators + /// @{ + + inline bool operator==(const StringRef &LHS, const StringRef &RHS) { + return LHS.equals(RHS); + } + + inline bool operator!=(const StringRef &LHS, const StringRef &RHS) { + return !(LHS == RHS); + } + + inline bool operator<(const StringRef &LHS, const StringRef &RHS) { + return LHS.compare(RHS) == -1; + } + + inline bool operator<=(const StringRef &LHS, const StringRef &RHS) { + return LHS.compare(RHS) != 1; + } + + inline bool operator>(const StringRef &LHS, const StringRef &RHS) { + return LHS.compare(RHS) == 1; + } + + inline bool operator>=(const StringRef &LHS, const StringRef &RHS) { + return LHS.compare(RHS) != -1; + } + + /// @} + } #endif |