1 //===-- llvm/ADT/StringExtras.h - Useful string functions -------*- 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 contains some functions that are useful when dealing with strings.
12 //===----------------------------------------------------------------------===//
14 #ifndef LLVM_ADT_STRINGEXTRAS_H
15 #define LLVM_ADT_STRINGEXTRAS_H
17 #include "llvm/ADT/StringRef.h"
18 #include "llvm/Support/DataTypes.h"
22 template<typename T> class SmallVectorImpl;
24 /// hexdigit - Return the hexadecimal character for the
25 /// given number \p X (which should be less than 16).
26 static inline char hexdigit(unsigned X, bool LowerCase = false) {
27 const char HexChar = LowerCase ? 'a' : 'A';
28 return X < 10 ? '0' + X : HexChar + X - 10;
31 /// Construct a string ref from a boolean.
32 static inline StringRef toStringRef(bool B) {
33 return StringRef(B ? "true" : "false");
36 /// Interpret the given character \p C as a hexadecimal digit and return its
39 /// If \p C is not a valid hex digit, -1U is returned.
40 static inline unsigned hexDigitValue(char C) {
41 if (C >= '0' && C <= '9') return C-'0';
42 if (C >= 'a' && C <= 'f') return C-'a'+10U;
43 if (C >= 'A' && C <= 'F') return C-'A'+10U;
47 /// utohex_buffer - Emit the specified number into the buffer specified by
48 /// BufferEnd, returning a pointer to the start of the string. This can be used
49 /// like this: (note that the buffer must be large enough to handle any number):
51 /// printf("0x%s", utohex_buffer(X, Buffer+40));
53 /// This should only be used with unsigned types.
55 template<typename IntTy>
56 static inline char *utohex_buffer(IntTy X, char *BufferEnd) {
57 char *BufPtr = BufferEnd;
58 *--BufPtr = 0; // Null terminate buffer.
60 *--BufPtr = '0'; // Handle special case.
65 unsigned char Mod = static_cast<unsigned char>(X) & 15;
66 *--BufPtr = hexdigit(Mod);
72 static inline std::string utohexstr(uint64_t X) {
74 return utohex_buffer(X, Buffer+17);
77 static inline std::string utostr_32(uint32_t X, bool isNeg = false) {
79 char *BufPtr = Buffer+11;
81 if (X == 0) *--BufPtr = '0'; // Handle special case...
84 *--BufPtr = '0' + char(X % 10);
88 if (isNeg) *--BufPtr = '-'; // Add negative sign...
90 return std::string(BufPtr, Buffer+11);
93 static inline std::string utostr(uint64_t X, bool isNeg = false) {
95 char *BufPtr = Buffer+21;
97 if (X == 0) *--BufPtr = '0'; // Handle special case...
100 *--BufPtr = '0' + char(X % 10);
104 if (isNeg) *--BufPtr = '-'; // Add negative sign...
105 return std::string(BufPtr, Buffer+21);
109 static inline std::string itostr(int64_t X) {
111 return utostr(static_cast<uint64_t>(-X), true);
113 return utostr(static_cast<uint64_t>(X));
116 /// StrInStrNoCase - Portable version of strcasestr. Locates the first
117 /// occurrence of string 's1' in string 's2', ignoring case. Returns
118 /// the offset of s2 in s1 or npos if s2 cannot be found.
119 StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2);
121 /// getToken - This function extracts one token from source, ignoring any
122 /// leading characters that appear in the Delimiters string, and ending the
123 /// token at any of the characters that appear in the Delimiters string. If
124 /// there are no tokens in the source string, an empty string is returned.
125 /// The function returns a pair containing the extracted token and the
126 /// remaining tail string.
127 std::pair<StringRef, StringRef> getToken(StringRef Source,
128 StringRef Delimiters = " \t\n\v\f\r");
130 /// SplitString - Split up the specified string according to the specified
131 /// delimiters, appending the result fragments to the output list.
132 void SplitString(StringRef Source,
133 SmallVectorImpl<StringRef> &OutFragments,
134 StringRef Delimiters = " \t\n\v\f\r");
136 /// HashString - Hash function for strings.
138 /// This is the Bernstein hash function.
140 // FIXME: Investigate whether a modified bernstein hash function performs
141 // better: http://eternallyconfuzzled.com/tuts/algorithms/jsw_tut_hashing.aspx
143 static inline unsigned HashString(StringRef Str, unsigned Result = 0) {
144 for (StringRef::size_type i = 0, e = Str.size(); i != e; ++i)
145 Result = Result * 33 + (unsigned char)Str[i];
149 /// Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).
150 static inline StringRef getOrdinalSuffix(unsigned Val) {
151 // It is critically important that we do this perfectly for
152 // user-written sequences with over 100 elements.
163 default: return "th";
168 template <typename IteratorT>
169 inline std::string join_impl(IteratorT Begin, IteratorT End,
170 StringRef Separator, std::input_iterator_tag) {
176 while (++Begin != End) {
183 template <typename IteratorT>
184 inline std::string join_impl(IteratorT Begin, IteratorT End,
185 StringRef Separator, std::forward_iterator_tag) {
190 size_t Len = (std::distance(Begin, End) - 1) * Separator.size();
191 for (IteratorT I = Begin; I != End; ++I)
192 Len += (*Begin).size();
195 while (++Begin != End) {
202 /// Joins the strings in the range [Begin, End), adding Separator between
204 template <typename IteratorT>
205 inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {
206 typedef typename std::iterator_traits<IteratorT>::iterator_category tag;
207 return join_impl(Begin, End, Separator, tag());
210 } // End llvm namespace