1 //===-- StringRef.cpp - Lightweight String References ---------------------===//
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 #include "llvm/ADT/StringRef.h"
13 const size_t StringRef::npos;
15 //===----------------------------------------------------------------------===//
17 //===----------------------------------------------------------------------===//
20 /// find - Search for the first string \arg Str in the string.
22 /// \return - The index of the first occurence of \arg Str, or npos if not
24 size_t StringRef::find(const StringRef &Str) const {
25 size_t N = Str.size();
28 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
29 if (substr(i, N).equals(Str))
34 /// rfind - Search for the last string \arg Str in the string.
36 /// \return - The index of the last occurence of \arg Str, or npos if not
38 size_t StringRef::rfind(const StringRef &Str) const {
39 size_t N = Str.size();
42 for (size_t i = Length - N + 1, e = 0; i != e;) {
44 if (substr(i, N).equals(Str))
50 /// find_first_of - Find the first character from the string 'Chars' in the
51 /// current string or return npos if not in string.
52 StringRef::size_type StringRef::find_first_of(StringRef Chars) const {
53 for (size_type i = 0, e = Length; i != e; ++i)
54 if (Chars.find(Data[i]) != npos)
59 /// find_first_not_of - Find the first character in the string that is not
60 /// in the string 'Chars' or return npos if all are in string. Same as find.
61 StringRef::size_type StringRef::find_first_not_of(StringRef Chars) const {
62 for (size_type i = 0, e = Length; i != e; ++i)
63 if (Chars.find(Data[i]) == npos)
69 //===----------------------------------------------------------------------===//
71 //===----------------------------------------------------------------------===//
73 /// count - Return the number of non-overlapped occurrences of \arg Str in
75 size_t StringRef::count(const StringRef &Str) const {
77 size_t N = Str.size();
80 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
81 if (substr(i, N).equals(Str))
86 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
87 /// sequence of radix up to 36 to an unsigned long long value.
88 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
89 unsigned long long &Result) {
90 // Autosense radix if not specified.
101 } else if (Str[1] == 'b') {
111 // Empty strings (after the radix autosense) are invalid.
112 if (Str.empty()) return true;
114 // Parse all the bytes of the string given this radix. Watch for overflow.
116 while (!Str.empty()) {
118 if (Str[0] >= '0' && Str[0] <= '9')
119 CharVal = Str[0]-'0';
120 else if (Str[0] >= 'a' && Str[0] <= 'z')
121 CharVal = Str[0]-'a'+10;
122 else if (Str[0] >= 'A' && Str[0] <= 'Z')
123 CharVal = Str[0]-'A'+10;
127 // If the parsed value is larger than the integer radix, the string is
129 if (CharVal >= Radix)
132 // Add in this character.
133 unsigned long long PrevResult = Result;
134 Result = Result*Radix+CharVal;
136 // Check for overflow.
137 if (Result < PrevResult)
146 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
147 return GetAsUnsignedInteger(*this, Radix, Result);
151 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
152 unsigned long long ULLVal;
154 // Handle positive strings first.
155 if (empty() || front() != '-') {
156 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
157 // Check for value so large it overflows a signed value.
158 (long long)ULLVal < 0)
164 // Get the positive part of the value.
165 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
166 // Reject values so large they'd overflow as negative signed, but allow
167 // "-0". This negates the unsigned so that the negative isn't undefined
168 // on signed overflow.
169 (long long)-ULLVal > 0)
176 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
178 if (getAsInteger(Radix, Val) ||
185 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
186 unsigned long long Val;
187 if (getAsInteger(Radix, Val) ||
188 (unsigned)Val != Val)