00001 //===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===// 00002 // 00003 // The LLVM Compiler Infrastructure 00004 // 00005 // This file is distributed under the University of Illinois Open Source 00006 // License. See LICENSE.TXT for details. 00007 // 00008 //===----------------------------------------------------------------------===// 00009 // 00010 // This file defines the SmallString class. 00011 // 00012 //===----------------------------------------------------------------------===// 00013 00014 #ifndef LLVM_ADT_SMALLSTRING_H 00015 #define LLVM_ADT_SMALLSTRING_H 00016 00017 #include "llvm/ADT/SmallVector.h" 00018 #include "llvm/ADT/StringRef.h" 00019 00020 namespace llvm { 00021 00024 template<unsigned InternalLen> 00025 class SmallString : public SmallVector<char, InternalLen> { 00026 public: 00027 // Default ctor - Initialize to empty. 00028 SmallString() {} 00029 00030 // Initialize from a StringRef. 00031 SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {} 00032 00033 // Initialize with a range. 00034 template<typename ItTy> 00035 SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {} 00036 00037 // Copy ctor. 00038 SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {} 00039 00040 00041 // Extra methods. 00042 StringRef str() const { return StringRef(this->begin(), this->size()); } 00043 00044 // TODO: Make this const, if it's safe... 00045 const char* c_str() { 00046 this->push_back(0); 00047 this->pop_back(); 00048 return this->data(); 00049 } 00050 00051 // Implicit conversion to StringRef. 00052 operator StringRef() const { return str(); } 00053 00054 // Extra operators. 00055 const SmallString &operator=(StringRef RHS) { 00056 this->clear(); 00057 return *this += RHS; 00058 } 00059 00060 SmallString &operator+=(StringRef RHS) { 00061 this->append(RHS.begin(), RHS.end()); 00062 return *this; 00063 } 00064 SmallString &operator+=(char C) { 00065 this->push_back(C); 00066 return *this; 00067 } 00068 }; 00069 00070 } 00071 00072 #endif