SmallString.h revision 04087d069a17265b964b30e8210262bbdbc4fbec
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#include "llvm/Support/DataTypes.h"
20#include <cstring>
21
22namespace llvm {
23
24/// SmallString - A SmallString is just a SmallVector with methods and accessors
25/// that make it work better as a string (e.g. operator+ etc).
26template<unsigned InternalLen>
27class SmallString : public SmallVector<char, InternalLen> {
28public:
29  // Default ctor - Initialize to empty.
30  SmallString() {}
31
32  // Initialize with a range.
33  template<typename ItTy>
34  SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
35
36  // Copy ctor.
37  SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {}
38
39
40  // Extra methods.
41  const char *c_str() const {
42    SmallString *This = const_cast<SmallString*>(this);
43    // Ensure that there is a \0 at the end of the string.
44    This->reserve(this->size()+1);
45    This->End[0] = 0;
46    return this->begin();
47  }
48
49  StringRef str() const { return StringRef(this->begin(), this->size()); }
50
51  // Extra operators.
52  const SmallString &operator=(const char *RHS) {
53    this->clear();
54    return *this += RHS;
55  }
56
57  SmallString &operator+=(const char *RHS) {
58    this->append(RHS, RHS+strlen(RHS));
59    return *this;
60  }
61  SmallString &operator+=(char C) {
62    this->push_back(C);
63    return *this;
64  }
65};
66
67
68}
69
70#endif
71