LLVM API Documentation

SmallString.h

Go to the documentation of this file.
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/Support/DataTypes.h"
00019 #include <cstring>
00020 
00021 namespace llvm {
00022 
00023 /// SmallString - A SmallString is just a SmallVector with methods and accessors
00024 /// that make it work better as a string (e.g. operator+ etc).
00025 template<unsigned InternalLen>
00026 class SmallString : public SmallVector<char, InternalLen> {
00027 public:
00028   // Default ctor - Initialize to empty.
00029   SmallString() {}
00030 
00031   // Initialize with a range.
00032   template<typename ItTy>
00033   SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
00034   
00035   // Copy ctor.
00036   SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {}
00037 
00038   
00039   // Extra methods.
00040   const char *c_str() const {
00041     SmallString *This = const_cast<SmallString*>(this);
00042     // Ensure that there is a \0 at the end of the string.
00043     This->reserve(this->size()+1);
00044     This->End[0] = 0;
00045     return this->begin();
00046   }
00047   
00048   // Extra operators.
00049   const SmallString &operator=(const char *RHS) {
00050     this->clear();
00051     return *this += RHS;
00052   }
00053   
00054   SmallString &operator+=(const char *RHS) {
00055     this->append(RHS, RHS+strlen(RHS));
00056     return *this;
00057   }
00058   SmallString &operator+=(char C) {
00059     this->push_back(C);
00060     return *this;
00061   }
00062 
00063   SmallString &append_uint_32(uint32_t N) {
00064     char Buffer[20];
00065     char *BufPtr = Buffer+20;
00066     
00067     if (N == 0) *--BufPtr = '0';  // Handle special case.
00068     
00069     while (N) {
00070       *--BufPtr = '0' + char(N % 10);
00071       N /= 10;
00072     }
00073     this->append(BufPtr, Buffer+20);
00074     return *this;
00075   }
00076   
00077   SmallString &append_uint(uint64_t N) {
00078     if (N == uint32_t(N))
00079       return append_uint_32(uint32_t(N));
00080     
00081     char Buffer[40];
00082     char *BufPtr = Buffer+40;
00083     
00084     if (N == 0) *--BufPtr = '0';  // Handle special case...
00085     
00086     while (N) {
00087       *--BufPtr = '0' + char(N % 10);
00088       N /= 10;
00089     }
00090 
00091     this->append(BufPtr, Buffer+40);
00092     return *this;
00093   }
00094   
00095   SmallString &append_sint(int64_t N) {
00096     // TODO, wrong for minint64.
00097     if (N < 0) {
00098       this->push_back('-');
00099       N = -N;
00100     }
00101     return append_uint(N);
00102   }
00103   
00104 };
00105   
00106   
00107 }
00108 
00109 #endif



This web site is hosted by the Computer Science Department at the University of Illinois at Urbana-Champaign.