1 //===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===//
3 // The LLVM Compiler Infrastructure
5 // This file is distributed under the University of Illinois Open Source
6 // License. See LICENSE.TXT for details.
8 //===----------------------------------------------------------------------===//
10 // This file defines the SmallString class.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_SMALLSTRING_H
15 #define LLVM_ADT_SMALLSTRING_H
17 #include "llvm/ADT/SmallVector.h"
18 #include "llvm/Support/DataTypes.h"
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).
25 template<unsigned InternalLen>
26 class SmallString : public SmallVector<char, InternalLen> {
28 // Default ctor - Initialize to empty.
31 // Initialize with a range.
32 template<typename ItTy>
33 SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
36 SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {}
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);
49 const SmallString &operator=(const char *RHS) {
54 SmallString &operator+=(const char *RHS) {
55 this->append(RHS, RHS+strlen(RHS));
58 SmallString &operator+=(char C) {
63 SmallString &append_uint_32(uint32_t N) {
65 char *BufPtr = Buffer+20;
67 if (N == 0) *--BufPtr = '0'; // Handle special case.
70 *--BufPtr = '0' + char(N % 10);
73 this->append(BufPtr, Buffer+20);
77 SmallString &append_uint(uint64_t N) {
79 return append_uint_32(uint32_t(N));
82 char *BufPtr = Buffer+40;
84 if (N == 0) *--BufPtr = '0'; // Handle special case...
87 *--BufPtr = '0' + char(N % 10);
91 this->append(BufPtr, Buffer+40);
95 SmallString &append_sint(int64_t N) {
96 // TODO, wrong for minint64.
101 return append_uint(N);