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