Home | History | Annotate | Download | only in ADT
      1 //===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===//
      2 //
      3 //                     The LLVM Compiler Infrastructure
      4 //
      5 // This file is distributed under the University of Illinois Open Source
      6 // License. See LICENSE.TXT for details.
      7 //
      8 //===----------------------------------------------------------------------===//
      9 //
     10 // This file defines the SmallString class.
     11 //
     12 //===----------------------------------------------------------------------===//
     13 
     14 #ifndef LLVM_ADT_SMALLSTRING_H
     15 #define LLVM_ADT_SMALLSTRING_H
     16 
     17 #include "llvm/ADT/SmallVector.h"
     18 #include "llvm/ADT/StringRef.h"
     19 
     20 namespace llvm {
     21 
     22 /// SmallString - A SmallString is just a SmallVector with methods and accessors
     23 /// that make it work better as a string (e.g. operator+ etc).
     24 template<unsigned InternalLen>
     25 class SmallString : public SmallVector<char, InternalLen> {
     26 public:
     27   // Default ctor - Initialize to empty.
     28   SmallString() {}
     29 
     30   // Initialize from a StringRef.
     31   SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
     32 
     33   // Initialize with a range.
     34   template<typename ItTy>
     35   SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
     36 
     37   // Copy ctor.
     38   SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {}
     39 
     40 
     41   // Extra methods.
     42   StringRef str() const { return StringRef(this->begin(), this->size()); }
     43 
     44   // TODO: Make this const, if it's safe...
     45   const char* c_str() {
     46     this->push_back(0);
     47     this->pop_back();
     48     return this->data();
     49   }
     50 
     51   // Implicit conversion to StringRef.
     52   operator StringRef() const { return str(); }
     53 
     54   // Extra operators.
     55   const SmallString &operator=(StringRef RHS) {
     56     this->clear();
     57     return *this += RHS;
     58   }
     59 
     60   SmallString &operator+=(StringRef RHS) {
     61     this->append(RHS.begin(), RHS.end());
     62     return *this;
     63   }
     64   SmallString &operator+=(char C) {
     65     this->push_back(C);
     66     return *this;
     67   }
     68 };
     69 
     70 }
     71 
     72 #endif
     73