SmallString.h revision 0861d1fa5c1c8ca2adb8af1df0b3fce8f6d3ba5b
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/Support/DataTypes.h" 19#include <cstring> 20 21namespace llvm { 22 23/// SmallString - A SmallString is just a SmallVector with methods and accessors 24/// that make it work better as a string (e.g. operator+ etc). 25template<unsigned InternalLen> 26class SmallString : public SmallVector<char, InternalLen> { 27public: 28 // Default ctor - Initialize to empty. 29 SmallString() {} 30 31 // Initialize with a range. 32 template<typename ItTy> 33 SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {} 34 35 // Copy ctor. 36 SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {} 37 38 39 // Extra methods. 40 const char *c_str() const { 41 SmallString *This = const_cast<SmallString*>(this); 42 // Ensure that there is a \0 at the end of the string. 43 This->reserve(this->size()+1); 44 This->End[0] = 0; 45 return this->begin(); 46 } 47 48 // Extra operators. 49 SmallString &operator+=(const char *RHS) { 50 this->append(RHS, RHS+strlen(RHS)); 51 return *this; 52 } 53 SmallString &operator+=(char C) { 54 this->push_back(C); 55 return *this; 56 } 57 58 SmallString &append_uint_32(uint32_t N) { 59 char Buffer[20]; 60 char *BufPtr = Buffer+20; 61 62 if (N == 0) *--BufPtr = '0'; // Handle special case. 63 64 while (N) { 65 *--BufPtr = '0' + char(N % 10); 66 N /= 10; 67 } 68 this->append(BufPtr, Buffer+20); 69 return *this; 70 } 71 72 SmallString &append_uint(uint64_t N) { 73 if (N == uint32_t(N)) 74 return append_uint_32(uint32_t(N)); 75 76 char Buffer[40]; 77 char *BufPtr = Buffer+40; 78 79 if (N == 0) *--BufPtr = '0'; // Handle special case... 80 81 while (N) { 82 *--BufPtr = '0' + char(N % 10); 83 N /= 10; 84 } 85 86 this->append(BufPtr, Buffer+40); 87 return *this; 88 } 89 90 SmallString &append_sint(int64_t N) { 91 // TODO, wrong for minint64. 92 if (N < 0) { 93 this->push_back('-'); 94 N = -N; 95 } 96 return append_uint(N); 97 } 98 99}; 100 101 102} 103 104#endif 105