From dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0 Mon Sep 17 00:00:00 2001 From: Chris Lattner Date: Mon, 30 Oct 2006 03:39:20 +0000 Subject: [PATCH] Add SmallString a (currently) minimal class that adapts SmallVector to be more string-like. git-svn-id: https://llvm.org/svn/llvm-project/llvm/trunk@31289 91177308-0d34-0410-b5e6-96231b3b80d8 --- include/llvm/ADT/SmallString.h | 57 ++++++++++++++++++++++++++++++++++ include/llvm/ADT/SmallVector.h | 1 + 2 files changed, 58 insertions(+) create mode 100644 include/llvm/ADT/SmallString.h diff --git a/include/llvm/ADT/SmallString.h b/include/llvm/ADT/SmallString.h new file mode 100644 index 00000000000..c7a292d8b90 --- /dev/null +++ b/include/llvm/ADT/SmallString.h @@ -0,0 +1,57 @@ +//===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===// +// +// The LLVM Compiler Infrastructure +// +// This file was developed by Chris Lattner and is distributed under +// the University of Illinois Open Source License. See LICENSE.TXT for details. +// +//===----------------------------------------------------------------------===// +// +// This file defines the SmallString class. +// +//===----------------------------------------------------------------------===// + +#ifndef LLVM_ADT_SMALLSTRING_H +#define LLVM_ADT_SMALLSTRING_H + +#include "llvm/ADT/SmallVector.h" +#include + +namespace llvm { + +/// SmallString - A SmallString is just a SmallVector with methods and accessors +/// that make it work better as a string (e.g. operator+ etc). +template +class SmallString : public SmallVector { +public: + // Default ctor - Initialize to empty. + SmallString() {} + + // Initialize with a range. + template + SmallString(ItTy S, ItTy E) : SmallVector(S, E) {} + + // Copy ctor. + SmallString(const SmallString &RHS) : SmallVector(RHS) {} + + + // Extra methods. + const char *c_str() const { + SmallString *This = const_cast(this); + // Ensure that there is a \0 at the end of the string. + This->reserve(this->size()+1); + This->End[0] = 0; + return this->begin(); + } + + // Extra operators. + SmallString &operator+=(const char *RHS) { + this->append(RHS, RHS+strlen(RHS)); + return *this; + } +}; + + +} + +#endif diff --git a/include/llvm/ADT/SmallVector.h b/include/llvm/ADT/SmallVector.h index 01d092c75d9..9f0255fc2b0 100644 --- a/include/llvm/ADT/SmallVector.h +++ b/include/llvm/ADT/SmallVector.h @@ -25,6 +25,7 @@ namespace llvm { /// template parameter. template class SmallVectorImpl { +protected: T *Begin, *End, *Capacity; // Allocate raw space for N elements of type T. If T has a ctor or dtor, we