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 // MSVC emits references to this into the translation units which reference it.
15 const size_t StringRef::npos;
18 //===----------------------------------------------------------------------===//
20 //===----------------------------------------------------------------------===//
23 /// find - Search for the first string \arg Str in the string.
25 /// \return - The index of the first occurence of \arg Str, or npos if not
27 size_t StringRef::find(StringRef Str, size_t From) const {
28 size_t N = Str.size();
31 for (size_t e = Length - N + 1, i = std::min(From, e); i != e; ++i)
32 if (substr(i, N).equals(Str))
37 /// rfind - Search for the last string \arg Str in the string.
39 /// \return - The index of the last occurence of \arg Str, or npos if not
41 size_t StringRef::rfind(StringRef Str) const {
42 size_t N = Str.size();
45 for (size_t i = Length - N + 1, e = 0; i != e;) {
47 if (substr(i, N).equals(Str))
53 /// find_first_of - Find the first character in the string that is in \arg
54 /// Chars, or npos if not found.
56 /// Note: O(size() * Chars.size())
57 StringRef::size_type StringRef::find_first_of(StringRef Chars,
59 for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
60 if (Chars.find(Data[i]) != npos)
65 /// find_first_not_of - Find the first character in the string that is not
66 /// \arg C or npos if not found.
67 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
68 for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
74 /// find_first_not_of - Find the first character in the string that is not
75 /// in the string \arg Chars, or npos if not found.
77 /// Note: O(size() * Chars.size())
78 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
80 for (size_type i = std::min(From, Length), e = Length; i != e; ++i)
81 if (Chars.find(Data[i]) == npos)
87 //===----------------------------------------------------------------------===//
89 //===----------------------------------------------------------------------===//
91 /// count - Return the number of non-overlapped occurrences of \arg Str in
93 size_t StringRef::count(StringRef Str) const {
95 size_t N = Str.size();
98 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
99 if (substr(i, N).equals(Str))
104 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
105 /// sequence of radix up to 36 to an unsigned long long value.
106 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
107 unsigned long long &Result) {
108 // Autosense radix if not specified.
110 if (Str.startswith("0x")) {
113 } else if (Str.startswith("0b")) {
116 } else if (Str.startswith("0"))
122 // Empty strings (after the radix autosense) are invalid.
123 if (Str.empty()) return true;
125 // Parse all the bytes of the string given this radix. Watch for overflow.
127 while (!Str.empty()) {
129 if (Str[0] >= '0' && Str[0] <= '9')
130 CharVal = Str[0]-'0';
131 else if (Str[0] >= 'a' && Str[0] <= 'z')
132 CharVal = Str[0]-'a'+10;
133 else if (Str[0] >= 'A' && Str[0] <= 'Z')
134 CharVal = Str[0]-'A'+10;
138 // If the parsed value is larger than the integer radix, the string is
140 if (CharVal >= Radix)
143 // Add in this character.
144 unsigned long long PrevResult = Result;
145 Result = Result*Radix+CharVal;
147 // Check for overflow.
148 if (Result < PrevResult)
157 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
158 return GetAsUnsignedInteger(*this, Radix, Result);
162 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
163 unsigned long long ULLVal;
165 // Handle positive strings first.
166 if (empty() || front() != '-') {
167 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
168 // Check for value so large it overflows a signed value.
169 (long long)ULLVal < 0)
175 // Get the positive part of the value.
176 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
177 // Reject values so large they'd overflow as negative signed, but allow
178 // "-0". This negates the unsigned so that the negative isn't undefined
179 // on signed overflow.
180 (long long)-ULLVal > 0)
187 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
189 if (getAsInteger(Radix, Val) ||
196 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
197 unsigned long long Val;
198 if (getAsInteger(Radix, Val) ||
199 (unsigned)Val != Val)