SmallString.h revision 55738761b393cc844e230a74e29edf4cdc37e9b8
1dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//===- llvm/ADT/SmallString.h - 'Normally small' strings --------*- C++ -*-===//
2dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//
3dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//                     The LLVM Compiler Infrastructure
4dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//
57ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// This file is distributed under the University of Illinois Open Source
67ed47a13356daed2a34cd2209a31f92552e3bdd8Chris Lattner// License. See LICENSE.TXT for details.
7dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//
8dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//===----------------------------------------------------------------------===//
9dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//
10dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner// This file defines the SmallString class.
11dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//
12dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner//===----------------------------------------------------------------------===//
13dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner
14dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner#ifndef LLVM_ADT_SMALLSTRING_H
15dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner#define LLVM_ADT_SMALLSTRING_H
16dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner
17dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner#include "llvm/ADT/SmallVector.h"
1804087d069a17265b964b30e8210262bbdbc4fbecDaniel Dunbar#include "llvm/ADT/StringRef.h"
19dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner
20dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattnernamespace llvm {
21dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner
22dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner/// SmallString - A SmallString is just a SmallVector with methods and accessors
23dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner/// that make it work better as a string (e.g. operator+ etc).
24dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattnertemplate<unsigned InternalLen>
25dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattnerclass SmallString : public SmallVector<char, InternalLen> {
26dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattnerpublic:
272527188a42b3250671b69dc979102565be1f910fTalin  /// Default ctor - Initialize to empty.
28dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner  SmallString() {}
29dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner
302527188a42b3250671b69dc979102565be1f910fTalin  /// Initialize from a StringRef.
31965841cfe4de695fc56cab4821fd7e032ff85b83Michael J. Spencer  SmallString(StringRef S) : SmallVector<char, InternalLen>(S.begin(), S.end()) {}
32965841cfe4de695fc56cab4821fd7e032ff85b83Michael J. Spencer
332527188a42b3250671b69dc979102565be1f910fTalin  /// Initialize with a range.
34dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner  template<typename ItTy>
35dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner  SmallString(ItTy S, ItTy E) : SmallVector<char, InternalLen>(S, E) {}
363a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
372527188a42b3250671b69dc979102565be1f910fTalin  /// Copy ctor.
38dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner  SmallString(const SmallString &RHS) : SmallVector<char, InternalLen>(RHS) {}
39dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner
402527188a42b3250671b69dc979102565be1f910fTalin  // Note that in order to add new overloads for append & assign, we have to
412527188a42b3250671b69dc979102565be1f910fTalin  // duplicate the inherited versions so as not to inadvertently hide them.
422527188a42b3250671b69dc979102565be1f910fTalin
432527188a42b3250671b69dc979102565be1f910fTalin  /// @}
442527188a42b3250671b69dc979102565be1f910fTalin  /// @name String Assignment
452527188a42b3250671b69dc979102565be1f910fTalin  /// @{
462527188a42b3250671b69dc979102565be1f910fTalin
472527188a42b3250671b69dc979102565be1f910fTalin  /// Assign from a repeated element
4855738761b393cc844e230a74e29edf4cdc37e9b8Chris Lattner  void assign(size_t NumElts, char Elt) {
492527188a42b3250671b69dc979102565be1f910fTalin    this->SmallVectorImpl<char>::assign(NumElts, Elt);
502527188a42b3250671b69dc979102565be1f910fTalin  }
512527188a42b3250671b69dc979102565be1f910fTalin
522527188a42b3250671b69dc979102565be1f910fTalin  /// Assign from an iterator pair
532527188a42b3250671b69dc979102565be1f910fTalin  template<typename in_iter>
542527188a42b3250671b69dc979102565be1f910fTalin  void assign(in_iter S, in_iter E) {
552527188a42b3250671b69dc979102565be1f910fTalin    this->clear();
562527188a42b3250671b69dc979102565be1f910fTalin    SmallVectorImpl<char>::append(S, E);
572527188a42b3250671b69dc979102565be1f910fTalin  }
582527188a42b3250671b69dc979102565be1f910fTalin
592527188a42b3250671b69dc979102565be1f910fTalin  /// Assign from a StringRef
602527188a42b3250671b69dc979102565be1f910fTalin  void assign(StringRef RHS) {
612527188a42b3250671b69dc979102565be1f910fTalin    this->clear();
622527188a42b3250671b69dc979102565be1f910fTalin    SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
632527188a42b3250671b69dc979102565be1f910fTalin  }
642527188a42b3250671b69dc979102565be1f910fTalin
652527188a42b3250671b69dc979102565be1f910fTalin  /// Assign from a SmallVector
662527188a42b3250671b69dc979102565be1f910fTalin  void assign(const SmallVectorImpl<char> &RHS) {
672527188a42b3250671b69dc979102565be1f910fTalin    this->clear();
682527188a42b3250671b69dc979102565be1f910fTalin    SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
692527188a42b3250671b69dc979102565be1f910fTalin  }
702527188a42b3250671b69dc979102565be1f910fTalin
712527188a42b3250671b69dc979102565be1f910fTalin  /// @}
722527188a42b3250671b69dc979102565be1f910fTalin  /// @name String Concatenation
732527188a42b3250671b69dc979102565be1f910fTalin  /// @{
742527188a42b3250671b69dc979102565be1f910fTalin
752527188a42b3250671b69dc979102565be1f910fTalin  /// Append from an iterator pair
762527188a42b3250671b69dc979102565be1f910fTalin  template<typename in_iter>
772527188a42b3250671b69dc979102565be1f910fTalin  void append(in_iter S, in_iter E) {
782527188a42b3250671b69dc979102565be1f910fTalin    SmallVectorImpl<char>::append(S, E);
792527188a42b3250671b69dc979102565be1f910fTalin  }
8055738761b393cc844e230a74e29edf4cdc37e9b8Chris Lattner
8155738761b393cc844e230a74e29edf4cdc37e9b8Chris Lattner  void append(size_t NumInputs, char Elt) {
8255738761b393cc844e230a74e29edf4cdc37e9b8Chris Lattner    SmallVectorImpl<char>::append(NumInputs, Elt);
8355738761b393cc844e230a74e29edf4cdc37e9b8Chris Lattner  }
8455738761b393cc844e230a74e29edf4cdc37e9b8Chris Lattner
852527188a42b3250671b69dc979102565be1f910fTalin
862527188a42b3250671b69dc979102565be1f910fTalin  /// Append from a StringRef
872527188a42b3250671b69dc979102565be1f910fTalin  void append(StringRef RHS) {
882527188a42b3250671b69dc979102565be1f910fTalin    SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
892527188a42b3250671b69dc979102565be1f910fTalin  }
902527188a42b3250671b69dc979102565be1f910fTalin
912527188a42b3250671b69dc979102565be1f910fTalin  /// Append from a SmallVector
922527188a42b3250671b69dc979102565be1f910fTalin  void append(const SmallVectorImpl<char> &RHS) {
932527188a42b3250671b69dc979102565be1f910fTalin    SmallVectorImpl<char>::append(RHS.begin(), RHS.end());
942527188a42b3250671b69dc979102565be1f910fTalin  }
952527188a42b3250671b69dc979102565be1f910fTalin
962527188a42b3250671b69dc979102565be1f910fTalin  /// @}
972527188a42b3250671b69dc979102565be1f910fTalin  /// @name String Comparison
982527188a42b3250671b69dc979102565be1f910fTalin  /// @{
992527188a42b3250671b69dc979102565be1f910fTalin
1002527188a42b3250671b69dc979102565be1f910fTalin  /// equals - Check for string equality, this is more efficient than
1012527188a42b3250671b69dc979102565be1f910fTalin  /// compare() when the relative ordering of inequal strings isn't needed.
1022527188a42b3250671b69dc979102565be1f910fTalin  bool equals(StringRef RHS) const {
1032527188a42b3250671b69dc979102565be1f910fTalin    return str().equals(RHS);
1042527188a42b3250671b69dc979102565be1f910fTalin  }
1052527188a42b3250671b69dc979102565be1f910fTalin
1062527188a42b3250671b69dc979102565be1f910fTalin  /// equals_lower - Check for string equality, ignoring case.
1072527188a42b3250671b69dc979102565be1f910fTalin  bool equals_lower(StringRef RHS) const {
1082527188a42b3250671b69dc979102565be1f910fTalin    return str().equals_lower(RHS);
1092527188a42b3250671b69dc979102565be1f910fTalin  }
1102527188a42b3250671b69dc979102565be1f910fTalin
1112527188a42b3250671b69dc979102565be1f910fTalin  /// compare - Compare two strings; the result is -1, 0, or 1 if this string
1122527188a42b3250671b69dc979102565be1f910fTalin  /// is lexicographically less than, equal to, or greater than the \arg RHS.
1132527188a42b3250671b69dc979102565be1f910fTalin  int compare(StringRef RHS) const {
1142527188a42b3250671b69dc979102565be1f910fTalin    return str().compare(RHS);
1152527188a42b3250671b69dc979102565be1f910fTalin  }
1162527188a42b3250671b69dc979102565be1f910fTalin
1172527188a42b3250671b69dc979102565be1f910fTalin  /// compare_lower - Compare two strings, ignoring case.
1182527188a42b3250671b69dc979102565be1f910fTalin  int compare_lower(StringRef RHS) const {
1192527188a42b3250671b69dc979102565be1f910fTalin    return str().compare_lower(RHS);
1202527188a42b3250671b69dc979102565be1f910fTalin  }
1212527188a42b3250671b69dc979102565be1f910fTalin
1222527188a42b3250671b69dc979102565be1f910fTalin  /// compare_numeric - Compare two strings, treating sequences of digits as
1232527188a42b3250671b69dc979102565be1f910fTalin  /// numbers.
1242527188a42b3250671b69dc979102565be1f910fTalin  int compare_numeric(StringRef RHS) const {
1252527188a42b3250671b69dc979102565be1f910fTalin    return str().compare_numeric(RHS);
1262527188a42b3250671b69dc979102565be1f910fTalin  }
1272527188a42b3250671b69dc979102565be1f910fTalin
1282527188a42b3250671b69dc979102565be1f910fTalin  /// @}
1292527188a42b3250671b69dc979102565be1f910fTalin  /// @name String Predicates
1302527188a42b3250671b69dc979102565be1f910fTalin  /// @{
1312527188a42b3250671b69dc979102565be1f910fTalin
1322527188a42b3250671b69dc979102565be1f910fTalin  /// startswith - Check if this string starts with the given \arg Prefix.
1332527188a42b3250671b69dc979102565be1f910fTalin  bool startswith(StringRef Prefix) const {
1342527188a42b3250671b69dc979102565be1f910fTalin    return str().startswith(Prefix);
1352527188a42b3250671b69dc979102565be1f910fTalin  }
1362527188a42b3250671b69dc979102565be1f910fTalin
1372527188a42b3250671b69dc979102565be1f910fTalin  /// endswith - Check if this string ends with the given \arg Suffix.
1382527188a42b3250671b69dc979102565be1f910fTalin  bool endswith(StringRef Suffix) const {
1392527188a42b3250671b69dc979102565be1f910fTalin    return str().endswith(Suffix);
1402527188a42b3250671b69dc979102565be1f910fTalin  }
1412527188a42b3250671b69dc979102565be1f910fTalin
1422527188a42b3250671b69dc979102565be1f910fTalin  /// @}
1432527188a42b3250671b69dc979102565be1f910fTalin  /// @name String Searching
1442527188a42b3250671b69dc979102565be1f910fTalin  /// @{
1452527188a42b3250671b69dc979102565be1f910fTalin
1462527188a42b3250671b69dc979102565be1f910fTalin  /// find - Search for the first character \arg C in the string.
1472527188a42b3250671b69dc979102565be1f910fTalin  ///
1482527188a42b3250671b69dc979102565be1f910fTalin  /// \return - The index of the first occurrence of \arg C, or npos if not
1492527188a42b3250671b69dc979102565be1f910fTalin  /// found.
1502527188a42b3250671b69dc979102565be1f910fTalin  size_t find(char C, size_t From = 0) const {
1512527188a42b3250671b69dc979102565be1f910fTalin    return str().find(C, From);
1522527188a42b3250671b69dc979102565be1f910fTalin  }
1532527188a42b3250671b69dc979102565be1f910fTalin
1542527188a42b3250671b69dc979102565be1f910fTalin  /// find - Search for the first string \arg Str in the string.
1552527188a42b3250671b69dc979102565be1f910fTalin  ///
1562527188a42b3250671b69dc979102565be1f910fTalin  /// \return - The index of the first occurrence of \arg Str, or npos if not
1572527188a42b3250671b69dc979102565be1f910fTalin  /// found.
1582527188a42b3250671b69dc979102565be1f910fTalin  size_t find(StringRef Str, size_t From = 0) const {
1592527188a42b3250671b69dc979102565be1f910fTalin    return str().find(Str, From);
1602527188a42b3250671b69dc979102565be1f910fTalin  }
1612527188a42b3250671b69dc979102565be1f910fTalin
1622527188a42b3250671b69dc979102565be1f910fTalin  /// rfind - Search for the last character \arg C in the string.
1632527188a42b3250671b69dc979102565be1f910fTalin  ///
1642527188a42b3250671b69dc979102565be1f910fTalin  /// \return - The index of the last occurrence of \arg C, or npos if not
1652527188a42b3250671b69dc979102565be1f910fTalin  /// found.
1662527188a42b3250671b69dc979102565be1f910fTalin  size_t rfind(char C, size_t From = StringRef::npos) const {
1672527188a42b3250671b69dc979102565be1f910fTalin    return str().rfind(C, From);
1682527188a42b3250671b69dc979102565be1f910fTalin  }
1692527188a42b3250671b69dc979102565be1f910fTalin
1702527188a42b3250671b69dc979102565be1f910fTalin  /// rfind - Search for the last string \arg Str in the string.
1712527188a42b3250671b69dc979102565be1f910fTalin  ///
1722527188a42b3250671b69dc979102565be1f910fTalin  /// \return - The index of the last occurrence of \arg Str, or npos if not
1732527188a42b3250671b69dc979102565be1f910fTalin  /// found.
1742527188a42b3250671b69dc979102565be1f910fTalin  size_t rfind(StringRef Str) const {
1752527188a42b3250671b69dc979102565be1f910fTalin    return str().rfind(Str);
1762527188a42b3250671b69dc979102565be1f910fTalin  }
1772527188a42b3250671b69dc979102565be1f910fTalin
1782527188a42b3250671b69dc979102565be1f910fTalin  /// find_first_of - Find the first character in the string that is \arg C,
1792527188a42b3250671b69dc979102565be1f910fTalin  /// or npos if not found. Same as find.
1802527188a42b3250671b69dc979102565be1f910fTalin  size_t find_first_of(char C, size_t From = 0) const {
1812527188a42b3250671b69dc979102565be1f910fTalin    return str().find_first_of(C, From);
1822527188a42b3250671b69dc979102565be1f910fTalin  }
1832527188a42b3250671b69dc979102565be1f910fTalin
1842527188a42b3250671b69dc979102565be1f910fTalin  /// find_first_of - Find the first character in the string that is in \arg
1852527188a42b3250671b69dc979102565be1f910fTalin  /// Chars, or npos if not found.
1862527188a42b3250671b69dc979102565be1f910fTalin  ///
1872527188a42b3250671b69dc979102565be1f910fTalin  /// Note: O(size() + Chars.size())
1882527188a42b3250671b69dc979102565be1f910fTalin  size_t find_first_of(StringRef Chars, size_t From = 0) const {
1892527188a42b3250671b69dc979102565be1f910fTalin    return str().find_first_of(Chars, From);
1902527188a42b3250671b69dc979102565be1f910fTalin  }
1912527188a42b3250671b69dc979102565be1f910fTalin
1922527188a42b3250671b69dc979102565be1f910fTalin  /// find_first_not_of - Find the first character in the string that is not
1932527188a42b3250671b69dc979102565be1f910fTalin  /// \arg C or npos if not found.
1942527188a42b3250671b69dc979102565be1f910fTalin  size_t find_first_not_of(char C, size_t From = 0) const {
1952527188a42b3250671b69dc979102565be1f910fTalin    return str().find_first_not_of(C, From);
1962527188a42b3250671b69dc979102565be1f910fTalin  }
1972527188a42b3250671b69dc979102565be1f910fTalin
1982527188a42b3250671b69dc979102565be1f910fTalin  /// find_first_not_of - Find the first character in the string that is not
1992527188a42b3250671b69dc979102565be1f910fTalin  /// in the string \arg Chars, or npos if not found.
2002527188a42b3250671b69dc979102565be1f910fTalin  ///
2012527188a42b3250671b69dc979102565be1f910fTalin  /// Note: O(size() + Chars.size())
2022527188a42b3250671b69dc979102565be1f910fTalin  size_t find_first_not_of(StringRef Chars, size_t From = 0) const {
2032527188a42b3250671b69dc979102565be1f910fTalin    return str().find_first_not_of(Chars, From);
2042527188a42b3250671b69dc979102565be1f910fTalin  }
2052527188a42b3250671b69dc979102565be1f910fTalin
2062527188a42b3250671b69dc979102565be1f910fTalin  /// find_last_of - Find the last character in the string that is \arg C, or
2072527188a42b3250671b69dc979102565be1f910fTalin  /// npos if not found.
2082527188a42b3250671b69dc979102565be1f910fTalin  size_t find_last_of(char C, size_t From = StringRef::npos) const {
2092527188a42b3250671b69dc979102565be1f910fTalin    return str().find_last_of(C, From);
2102527188a42b3250671b69dc979102565be1f910fTalin  }
2112527188a42b3250671b69dc979102565be1f910fTalin
2122527188a42b3250671b69dc979102565be1f910fTalin  /// find_last_of - Find the last character in the string that is in \arg C,
2132527188a42b3250671b69dc979102565be1f910fTalin  /// or npos if not found.
2142527188a42b3250671b69dc979102565be1f910fTalin  ///
2152527188a42b3250671b69dc979102565be1f910fTalin  /// Note: O(size() + Chars.size())
2162527188a42b3250671b69dc979102565be1f910fTalin  size_t find_last_of(
2172527188a42b3250671b69dc979102565be1f910fTalin      StringRef Chars, size_t From = StringRef::npos) const {
2182527188a42b3250671b69dc979102565be1f910fTalin    return str().find_last_of(Chars, From);
2192527188a42b3250671b69dc979102565be1f910fTalin  }
2202527188a42b3250671b69dc979102565be1f910fTalin
2212527188a42b3250671b69dc979102565be1f910fTalin  /// @}
2222527188a42b3250671b69dc979102565be1f910fTalin  /// @name Helpful Algorithms
2232527188a42b3250671b69dc979102565be1f910fTalin  /// @{
2242527188a42b3250671b69dc979102565be1f910fTalin
2252527188a42b3250671b69dc979102565be1f910fTalin  /// count - Return the number of occurrences of \arg C in the string.
2262527188a42b3250671b69dc979102565be1f910fTalin  size_t count(char C) const {
2272527188a42b3250671b69dc979102565be1f910fTalin    return str().count(C);
2282527188a42b3250671b69dc979102565be1f910fTalin  }
2292527188a42b3250671b69dc979102565be1f910fTalin
2302527188a42b3250671b69dc979102565be1f910fTalin  /// count - Return the number of non-overlapped occurrences of \arg Str in
2312527188a42b3250671b69dc979102565be1f910fTalin  /// the string.
2322527188a42b3250671b69dc979102565be1f910fTalin  size_t count(StringRef Str) const {
2332527188a42b3250671b69dc979102565be1f910fTalin    return str().count(Str);
2342527188a42b3250671b69dc979102565be1f910fTalin  }
2352527188a42b3250671b69dc979102565be1f910fTalin
2362527188a42b3250671b69dc979102565be1f910fTalin  /// @}
2372527188a42b3250671b69dc979102565be1f910fTalin  /// @name Substring Operations
2382527188a42b3250671b69dc979102565be1f910fTalin  /// @{
2392527188a42b3250671b69dc979102565be1f910fTalin
2402527188a42b3250671b69dc979102565be1f910fTalin  /// substr - Return a reference to the substring from [Start, Start + N).
2412527188a42b3250671b69dc979102565be1f910fTalin  ///
2422527188a42b3250671b69dc979102565be1f910fTalin  /// \param Start - The index of the starting character in the substring; if
2432527188a42b3250671b69dc979102565be1f910fTalin  /// the index is npos or greater than the length of the string then the
2442527188a42b3250671b69dc979102565be1f910fTalin  /// empty substring will be returned.
2452527188a42b3250671b69dc979102565be1f910fTalin  ///
2462527188a42b3250671b69dc979102565be1f910fTalin  /// \param N - The number of characters to included in the substring. If N
2472527188a42b3250671b69dc979102565be1f910fTalin  /// exceeds the number of characters remaining in the string, the string
2482527188a42b3250671b69dc979102565be1f910fTalin  /// suffix (starting with \arg Start) will be returned.
2492527188a42b3250671b69dc979102565be1f910fTalin  StringRef substr(size_t Start, size_t N = StringRef::npos) const {
2502527188a42b3250671b69dc979102565be1f910fTalin    return str().substr(Start, N);
2512527188a42b3250671b69dc979102565be1f910fTalin  }
2522527188a42b3250671b69dc979102565be1f910fTalin
2532527188a42b3250671b69dc979102565be1f910fTalin  /// slice - Return a reference to the substring from [Start, End).
2542527188a42b3250671b69dc979102565be1f910fTalin  ///
2552527188a42b3250671b69dc979102565be1f910fTalin  /// \param Start - The index of the starting character in the substring; if
2562527188a42b3250671b69dc979102565be1f910fTalin  /// the index is npos or greater than the length of the string then the
2572527188a42b3250671b69dc979102565be1f910fTalin  /// empty substring will be returned.
2582527188a42b3250671b69dc979102565be1f910fTalin  ///
2592527188a42b3250671b69dc979102565be1f910fTalin  /// \param End - The index following the last character to include in the
2602527188a42b3250671b69dc979102565be1f910fTalin  /// substring. If this is npos, or less than \arg Start, or exceeds the
2612527188a42b3250671b69dc979102565be1f910fTalin  /// number of characters remaining in the string, the string suffix
2622527188a42b3250671b69dc979102565be1f910fTalin  /// (starting with \arg Start) will be returned.
2632527188a42b3250671b69dc979102565be1f910fTalin  StringRef slice(size_t Start, size_t End) const {
2642527188a42b3250671b69dc979102565be1f910fTalin    return str().slice(Start, End);
2652527188a42b3250671b69dc979102565be1f910fTalin  }
2663a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
267dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner  // Extra methods.
2682527188a42b3250671b69dc979102565be1f910fTalin
2692527188a42b3250671b69dc979102565be1f910fTalin  /// Explicit conversion to StringRef
27004087d069a17265b964b30e8210262bbdbc4fbecDaniel Dunbar  StringRef str() const { return StringRef(this->begin(), this->size()); }
27104087d069a17265b964b30e8210262bbdbc4fbecDaniel Dunbar
27258fe86dc0ecb7efff01abe2b0024a6a53ebb2c81Michael J. Spencer  // TODO: Make this const, if it's safe...
27358fe86dc0ecb7efff01abe2b0024a6a53ebb2c81Michael J. Spencer  const char* c_str() {
27458fe86dc0ecb7efff01abe2b0024a6a53ebb2c81Michael J. Spencer    this->push_back(0);
27558fe86dc0ecb7efff01abe2b0024a6a53ebb2c81Michael J. Spencer    this->pop_back();
27658fe86dc0ecb7efff01abe2b0024a6a53ebb2c81Michael J. Spencer    return this->data();
27758fe86dc0ecb7efff01abe2b0024a6a53ebb2c81Michael J. Spencer  }
27858fe86dc0ecb7efff01abe2b0024a6a53ebb2c81Michael J. Spencer
2792527188a42b3250671b69dc979102565be1f910fTalin  /// Implicit conversion to StringRef.
280983c7fe847dd3f46945f0117ab19345b9c68e88fDaniel Dunbar  operator StringRef() const { return str(); }
281983c7fe847dd3f46945f0117ab19345b9c68e88fDaniel Dunbar
282dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner  // Extra operators.
28339db3439bfcdca4073dd513879f8ce12ee8c593bDaniel Dunbar  const SmallString &operator=(StringRef RHS) {
2840fbdfc3664830e8387c13bf817c44e8b71085142Chris Lattner    this->clear();
2850fbdfc3664830e8387c13bf817c44e8b71085142Chris Lattner    return *this += RHS;
2860fbdfc3664830e8387c13bf817c44e8b71085142Chris Lattner  }
2873a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
28839db3439bfcdca4073dd513879f8ce12ee8c593bDaniel Dunbar  SmallString &operator+=(StringRef RHS) {
28939db3439bfcdca4073dd513879f8ce12ee8c593bDaniel Dunbar    this->append(RHS.begin(), RHS.end());
290dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner    return *this;
291dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner  }
292703f5291c4f7199a95274df5e3381b36f8faf38cChris Lattner  SmallString &operator+=(char C) {
293703f5291c4f7199a95274df5e3381b36f8faf38cChris Lattner    this->push_back(C);
294703f5291c4f7199a95274df5e3381b36f8faf38cChris Lattner    return *this;
295703f5291c4f7199a95274df5e3381b36f8faf38cChris Lattner  }
296dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner};
2973a54b3dc87a581c203b18050b4f787b4ca28a12cMisha Brukman
298dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner}
299dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner
300dd94c8d6b2afb9c33c364ac8f0c8f8ed5d4c04a0Chris Lattner#endif
301