This patch removes a bit of code from StringRef by making it derive
from ArrayRef<char>.
Comments? OK to commit? The savings aren't huge, so I'm not
particularly wedded to the patch.
Incidentally, I didn't touch StringRef::front(), back() or
operator[](), because they return char, whereas the ArrayRef<char>
implementations would return const char &. My C++ isn't good enough to
work out whether that's an important difference or not.
ArrayRef.h | 2 +-
StringRef.h | 39 +++++++--------------------------------
2 files changed, 8 insertions(+), 33 deletions(-)
Thanks,
Jay.
Index: include/llvm/ADT/StringRef.h
===================================================================
--- include/llvm/ADT/StringRef.h (revision 129508)
+++ include/llvm/ADT/StringRef.h (working copy)
@@ -15,6 +15,8 @@
#include <utility>
#include <string>
+#include "llvm/ADT/ArrayRef.h"
+
namespace llvm {
template<typename T>
class SmallVectorImpl;
@@ -27,20 +29,11 @@
/// situations where the character data resides in some other buffer, whose
/// lifetime extends past that of the StringRef. For this reason, it is not in
/// general safe to store a StringRef.
- class StringRef {
+ class StringRef : public ArrayRef<char> {
public:
- typedef const char *iterator;
- typedef const char *const_iterator;
static const size_t npos = ~size_t(0);
- typedef size_t size_type;
private:
- /// The start of the string, in an external buffer.
- const char *Data;
-
- /// The length of the string.
- size_t Length;
-
// Workaround PR5482: nearly all gcc 4.x miscompile StringRef and std::min()
// Changing the arg of min to be an integer, instead of a reference to an
// integer works around this bug.
@@ -52,42 +45,24 @@
/// @{
/// Construct an empty string ref.
- /*implicit*/ StringRef() : Data(0), Length(0) {}
+ /*implicit*/ StringRef() : ArrayRef<char>() {}
/// Construct a string ref from a cstring.
/*implicit*/ StringRef(const char *Str)
- : Data(Str), Length(::strlen(Str)) {}
+ : ArrayRef<char>(Str, ::strlen(Str)) {}
/// Construct a string ref from a pointer and length.
/*implicit*/ StringRef(const char *data, size_t length)
- : Data(data), Length(length) {}
+ : ArrayRef<char>(data, length) {}
/// Construct a string ref from an std::string.
/*implicit*/ StringRef(const std::string &Str)
- : Data(Str.data()), Length(Str.length()) {}
+ : ArrayRef<char>(Str.data(), Str.length()) {}
/// @}
- /// @name Iterators
- /// @{
-
- iterator begin() const { return Data; }
-
- iterator end() const { return Data + Length; }
-
- /// @}
/// @name String Operations
/// @{
- /// data - Get a pointer to the start of the string (which may not be null
- /// terminated).
- const char *data() const { return Data; }
-
- /// empty - Check if the string is empty.
- bool empty() const { return Length == 0; }
-
- /// size - Get the string size.
- size_t size() const { return Length; }
-
/// front - Get the first character in the string.
char front() const {
assert(!empty());
Index: include/llvm/ADT/ArrayRef.h
===================================================================
--- include/llvm/ADT/ArrayRef.h (revision 129508)
+++ include/llvm/ADT/ArrayRef.h (working copy)
@@ -34,7 +34,7 @@
typedef const T *const_iterator;
typedef size_t size_type;
- private:
+ protected:
/// The start of the array, in an external buffer.
const T *Data;
_______________________________________________
cfe-commits mailing list
[email protected]
http://lists.cs.uiuc.edu/mailman/listinfo/cfe-commits