WPILibC++  unspecified
 All Classes Files Functions Variables Typedefs Enumerations Enumerator Pages
StringRef.h
1 //===--- StringRef.h - Constant String Reference Wrapper --------*- 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 #ifndef LLVM_ADT_STRINGREF_H
11 #define LLVM_ADT_STRINGREF_H
12 
13 #include <algorithm>
14 #include <cassert>
15 #include <cstring>
16 #include <limits>
17 #include <ostream>
18 #include <string>
19 #include <utility>
20 
21 namespace llvm {
22  template <typename T>
24  class StringRef;
25 
27  bool getAsUnsignedInteger(StringRef Str, unsigned Radix,
28  unsigned long long &Result);
29 
30  bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result);
31 
39  class StringRef {
40  public:
41  typedef const char *iterator;
42  typedef const char *const_iterator;
43  static const size_t npos = ~size_t(0);
44  typedef size_t size_type;
45 
46  private:
48  const char *Data;
49 
51  size_t Length;
52 
53  // Workaround memcmp issue with null pointers (undefined behavior)
54  // by providing a specialized version
55  static int compareMemory(const char *Lhs, const char *Rhs, size_t Length) {
56  if (Length == 0) { return 0; }
57  return ::memcmp(Lhs,Rhs,Length);
58  }
59 
60  public:
63 
65  /*implicit*/ StringRef() : Data(nullptr), Length(0) {}
66 
68  /*implicit*/ StringRef(const char *Str)
69  : Data(Str) {
70  assert(Str && "StringRef cannot be built from a NULL argument");
71  Length = ::strlen(Str); // invoking strlen(NULL) is undefined behavior
72  }
73 
75  /*implicit*/ StringRef(const char *data, size_t length)
76  : Data(data), Length(length) {
77  assert((data || length == 0) &&
78  "StringRef cannot be built from a NULL argument with non-null length");
79  }
80 
82  /*implicit*/ StringRef(const std::string &Str)
83  : Data(Str.data()), Length(Str.length()) {}
84 
88 
89  iterator begin() const { return Data; }
90 
91  iterator end() const { return Data + Length; }
92 
93  const unsigned char *bytes_begin() const {
94  return reinterpret_cast<const unsigned char *>(begin());
95  }
96  const unsigned char *bytes_end() const {
97  return reinterpret_cast<const unsigned char *>(end());
98  }
99 
103 
106  const char *data() const { return Data; }
107 
109  bool empty() const { return Length == 0; }
110 
112  size_t size() const { return Length; }
113 
115  char front() const {
116  assert(!empty());
117  return Data[0];
118  }
119 
121  char back() const {
122  assert(!empty());
123  return Data[Length-1];
124  }
125 
126  // copy - Allocate copy in Allocator and return StringRef to it.
127  template <typename Allocator> StringRef copy(Allocator &A) const {
128  char *S = A.template Allocate<char>(Length);
129  std::copy(begin(), end(), S);
130  return StringRef(S, Length);
131  }
132 
135  bool equals(StringRef RHS) const {
136  return (Length == RHS.Length &&
137  compareMemory(Data, RHS.Data, RHS.Length) == 0);
138  }
139 
141  bool equals_lower(StringRef RHS) const {
142  return Length == RHS.Length && compare_lower(RHS) == 0;
143  }
144 
147  int compare(StringRef RHS) const {
148  // Check the prefix for a mismatch.
149  if (int Res = compareMemory(Data, RHS.Data, std::min(Length, RHS.Length)))
150  return Res < 0 ? -1 : 1;
151 
152  // Otherwise the prefixes match, so we only need to check the lengths.
153  if (Length == RHS.Length)
154  return 0;
155  return Length < RHS.Length ? -1 : 1;
156  }
157 
159  int compare_lower(StringRef RHS) const;
160 
163  int compare_numeric(StringRef RHS) const;
164 
166  std::string str() const {
167  if (!Data) return std::string();
168  return std::string(Data, Length);
169  }
170 
174 
175  char operator[](size_t Index) const {
176  assert(Index < Length && "Invalid index!");
177  return Data[Index];
178  }
179 
183 
184  operator std::string() const {
185  return str();
186  }
187 
191 
193  bool startswith(StringRef Prefix) const {
194  return Length >= Prefix.Length &&
195  compareMemory(Data, Prefix.Data, Prefix.Length) == 0;
196  }
197 
199  bool startswith_lower(StringRef Prefix) const;
200 
202  bool endswith(StringRef Suffix) const {
203  return Length >= Suffix.Length &&
204  compareMemory(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 0;
205  }
206 
208  bool endswith_lower(StringRef Suffix) const;
209 
213 
218  size_t find(char C, size_t From = 0) const {
219  size_t FindBegin = std::min(From, Length);
220  if (FindBegin < Length) { // Avoid calling memchr with nullptr.
221  // Just forward to memchr, which is faster than a hand-rolled loop.
222  if (const void *P = ::memchr(Data + FindBegin, C, Length - FindBegin))
223  return static_cast<const char *>(P) - Data;
224  }
225  return npos;
226  }
227 
232  size_t find(StringRef Str, size_t From = 0) const;
233 
238  size_t rfind(char C, size_t From = npos) const {
239  From = std::min(From, Length);
240  size_t i = From;
241  while (i != 0) {
242  --i;
243  if (Data[i] == C)
244  return i;
245  }
246  return npos;
247  }
248 
253  size_t rfind(StringRef Str) const;
254 
257  size_t find_first_of(char C, size_t From = 0) const {
258  return find(C, From);
259  }
260 
265  size_t find_first_of(StringRef Chars, size_t From = 0) const;
266 
269  size_t find_first_not_of(char C, size_t From = 0) const;
270 
275  size_t find_first_not_of(StringRef Chars, size_t From = 0) const;
276 
279  size_t find_last_of(char C, size_t From = npos) const {
280  return rfind(C, From);
281  }
282 
287  size_t find_last_of(StringRef Chars, size_t From = npos) const;
288 
291  size_t find_last_not_of(char C, size_t From = npos) const;
292 
297  size_t find_last_not_of(StringRef Chars, size_t From = npos) const;
298 
302 
304  size_t count(char C) const {
305  size_t Count = 0;
306  for (size_t i = 0, e = Length; i != e; ++i)
307  if (Data[i] == C)
308  ++Count;
309  return Count;
310  }
311 
314  size_t count(StringRef Str) const;
315 
323  template <typename T>
324  typename std::enable_if<std::numeric_limits<T>::is_signed, bool>::type
325  getAsInteger(unsigned Radix, T &Result) const {
326  long long LLVal;
327  if (getAsSignedInteger(*this, Radix, LLVal) ||
328  static_cast<T>(LLVal) != LLVal)
329  return true;
330  Result = LLVal;
331  return false;
332  }
333 
334  template <typename T>
335  typename std::enable_if<!std::numeric_limits<T>::is_signed, bool>::type
336  getAsInteger(unsigned Radix, T &Result) const {
337  unsigned long long ULLVal;
338  // The additional cast to unsigned long long is required to avoid the
339  // Visual C++ warning C4805: '!=' : unsafe mix of type 'bool' and type
340  // 'unsigned __int64' when instantiating getAsInteger with T = bool.
341  if (getAsUnsignedInteger(*this, Radix, ULLVal) ||
342  static_cast<unsigned long long>(static_cast<T>(ULLVal)) != ULLVal)
343  return true;
344  Result = ULLVal;
345  return false;
346  }
347 
351 
352  // Convert the given ASCII string to lowercase.
353  std::string lower() const;
354 
356  std::string upper() const;
357 
361 
371  StringRef substr(size_t Start, size_t N = npos) const {
372  Start = std::min(Start, Length);
373  return StringRef(Data + Start, std::min(N, Length - Start));
374  }
375 
378  StringRef drop_front(size_t N = 1) const {
379  assert(size() >= N && "Dropping more elements than exist");
380  return substr(N);
381  }
382 
385  StringRef drop_back(size_t N = 1) const {
386  assert(size() >= N && "Dropping more elements than exist");
387  return substr(0, size()-N);
388  }
389 
400  StringRef slice(size_t Start, size_t End) const {
401  Start = std::min(Start, Length);
402  End = std::min(std::max(Start, End), Length);
403  return StringRef(Data + Start, End - Start);
404  }
405 
416  std::pair<StringRef, StringRef> split(char Separator) const {
417  size_t Idx = find(Separator);
418  if (Idx == npos)
419  return std::make_pair(*this, StringRef());
420  return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
421  }
422 
433  std::pair<StringRef, StringRef> split(StringRef Separator) const {
434  size_t Idx = find(Separator);
435  if (Idx == npos)
436  return std::make_pair(*this, StringRef());
437  return std::make_pair(slice(0, Idx), slice(Idx + Separator.size(), npos));
438  }
439 
455  StringRef Separator, int MaxSplit = -1,
456  bool KeepEmpty = true) const;
457 
468  std::pair<StringRef, StringRef> rsplit(char Separator) const {
469  size_t Idx = rfind(Separator);
470  if (Idx == npos)
471  return std::make_pair(*this, StringRef());
472  return std::make_pair(slice(0, Idx), slice(Idx+1, npos));
473  }
474 
477  StringRef ltrim(StringRef Chars = " \t\n\v\f\r") const {
478  return drop_front(std::min(Length, find_first_not_of(Chars)));
479  }
480 
483  StringRef rtrim(StringRef Chars = " \t\n\v\f\r") const {
484  return drop_back(Length - std::min(Length, find_last_not_of(Chars) + 1));
485  }
486 
489  StringRef trim(StringRef Chars = " \t\n\v\f\r") const {
490  return ltrim(Chars).rtrim(Chars);
491  }
492 
494  };
495 
498 
499  inline bool operator==(StringRef LHS, StringRef RHS) {
500  return LHS.equals(RHS);
501  }
502 
503  inline bool operator!=(StringRef LHS, StringRef RHS) {
504  return !(LHS == RHS);
505  }
506 
507  inline bool operator<(StringRef LHS, StringRef RHS) {
508  return LHS.compare(RHS) == -1;
509  }
510 
511  inline bool operator<=(StringRef LHS, StringRef RHS) {
512  return LHS.compare(RHS) != 1;
513  }
514 
515  inline bool operator>(StringRef LHS, StringRef RHS) {
516  return LHS.compare(RHS) == 1;
517  }
518 
519  inline bool operator>=(StringRef LHS, StringRef RHS) {
520  return LHS.compare(RHS) != -1;
521  }
522 
523  inline std::string &operator+=(std::string &buffer, StringRef string) {
524  return buffer.append(string.data(), string.size());
525  }
526 
527  inline std::ostream &operator<<(std::ostream &os, StringRef string) {
528  os.write(string.data(), string.size());
529  return os;
530  }
531 
533 
534  // StringRefs can be treated like a POD type.
535  template <typename T> struct isPodLike;
536  template <> struct isPodLike<StringRef> { static const bool value = true; };
537 } // namespace llvm
538 
539 #endif
std::enable_if< std::numeric_limits< T >::is_signed, bool >::type getAsInteger(unsigned Radix, T &Result) const
Parse the current string as an integer of the specified radix.
Definition: StringRef.h:325
int compare_lower(StringRef RHS) const
compare_lower - Compare two strings, ignoring case.
Definition: StringRef.cpp:51
size_t size() const
size - Get the string size.
Definition: StringRef.h:112
size_t find(char C, size_t From=0) const
Search for the first character C in the string.
Definition: StringRef.h:218
bool endswith(StringRef Suffix) const
Check if this string ends with the given Suffix.
Definition: StringRef.h:202
std::pair< StringRef, StringRef > split(char Separator) const
Split into two substrings around the first occurrence of a separator character.
Definition: StringRef.h:416
size_t rfind(char C, size_t From=npos) const
Search for the last character C in the string.
Definition: StringRef.h:238
StringRef substr(size_t Start, size_t N=npos) const
Return a reference to the substring from [Start, Start + N).
Definition: StringRef.h:371
std::string str() const
str - Get the contents as an std::string.
Definition: StringRef.h:166
StringRef drop_back(size_t N=1) const
Return a StringRef equal to 'this' but with the last N elements dropped.
Definition: StringRef.h:385
std::pair< StringRef, StringRef > rsplit(char Separator) const
Split into two substrings around the last occurrence of a separator character.
Definition: StringRef.h:468
bool endswith_lower(StringRef Suffix) const
Check if this string ends with the given Suffix, ignoring case.
Definition: StringRef.cpp:66
int compare(StringRef RHS) const
compare - Compare two strings; the result is -1, 0, or 1 if this string is lexicographically less tha...
Definition: StringRef.h:147
StringRef rtrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the right removed.
Definition: StringRef.h:483
This class consists of common code factored out of the SmallVector class to reduce code duplication b...
Definition: StringRef.h:23
const char * data() const
data - Get a pointer to the start of the string (which may not be null terminated).
Definition: StringRef.h:106
StringRef()
Construct an empty string ref.
Definition: StringRef.h:65
size_t find_last_not_of(char C, size_t From=npos) const
Find the last character in the string that is not C, or npos if not found.
Definition: StringRef.cpp:242
StringRef trim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left and right removed...
Definition: StringRef.h:489
char back() const
back - Get the last character in the string.
Definition: StringRef.h:121
size_t count(char C) const
Return the number of occurrences of C in the string.
Definition: StringRef.h:304
StringRef(const char *data, size_t length)
Construct a string ref from a pointer and length.
Definition: StringRef.h:75
std::pair< StringRef, StringRef > split(StringRef Separator) const
Split into two substrings around the first occurrence of a separator string.
Definition: StringRef.h:433
size_t find_first_not_of(char C, size_t From=0) const
Find the first character in the string that is not C or npos if not found.
Definition: StringRef.cpp:201
StringRef(const std::string &Str)
Construct a string ref from an std::string.
Definition: StringRef.h:82
size_t find_last_of(char C, size_t From=npos) const
Find the last character in the string that is C, or npos if not found.
Definition: StringRef.h:279
isPodLike - This is a type trait that is used to determine whether a given type can be copied around ...
Definition: StringRef.h:535
bool startswith(StringRef Prefix) const
Check if this string starts with the given Prefix.
Definition: StringRef.h:193
StringRef drop_front(size_t N=1) const
Return a StringRef equal to 'this' but with the first N elements dropped.
Definition: StringRef.h:378
std::string upper() const
Convert the given ASCII string to uppercase.
Definition: StringRef.cpp:114
bool equals(StringRef RHS) const
equals - Check for string equality, this is more efficient than compare() when the relative ordering ...
Definition: StringRef.h:135
bool equals_lower(StringRef RHS) const
equals_lower - Check for string equality, ignoring case.
Definition: StringRef.h:141
StringRef(const char *Str)
Construct a string ref from a cstring.
Definition: StringRef.h:68
size_t find_first_of(char C, size_t From=0) const
Find the first character in the string that is C, or npos if not found.
Definition: StringRef.h:257
char front() const
front - Get the first character in the string.
Definition: StringRef.h:115
int compare_numeric(StringRef RHS) const
compare_numeric - Compare two strings, treating sequences of digits as numbers.
Definition: StringRef.cpp:72
StringRef - Represent a constant reference to a string, i.e.
Definition: StringRef.h:39
StringRef slice(size_t Start, size_t End) const
Return a reference to the substring from [Start, End).
Definition: StringRef.h:400
bool startswith_lower(StringRef Prefix) const
Check if this string starts with the given Prefix, ignoring case.
Definition: StringRef.cpp:60
StringRef ltrim(StringRef Chars=" \t\n\v\f\r") const
Return string with consecutive characters in Chars starting from the left removed.
Definition: StringRef.h:477
bool empty() const
empty - Check if the string is empty.
Definition: StringRef.h:109