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 static char ascii_tolower(char x) {
19 if (x >= 'A' && x <= 'Z')
24 /// compare_lower - Compare strings, ignoring case.
25 int StringRef::compare_lower(StringRef RHS) const {
26 for (size_t I = 0, E = min(Length, RHS.Length); I != E; ++I) {
27 char LHC = ascii_tolower(Data[I]);
28 char RHC = ascii_tolower(RHS.Data[I]);
30 return LHC < RHC ? -1 : 1;
33 if (Length == RHS.Length)
35 return Length < RHS.Length ? -1 : 1;
38 //===----------------------------------------------------------------------===//
40 //===----------------------------------------------------------------------===//
43 /// find - Search for the first string \arg Str in the string.
45 /// \return - The index of the first occurence of \arg Str, or npos if not
47 size_t StringRef::find(StringRef Str, size_t From) const {
48 size_t N = Str.size();
51 for (size_t e = Length - N + 1, i = min(From, e); i != e; ++i)
52 if (substr(i, N).equals(Str))
57 /// rfind - Search for the last string \arg Str in the string.
59 /// \return - The index of the last occurence of \arg Str, or npos if not
61 size_t StringRef::rfind(StringRef Str) const {
62 size_t N = Str.size();
65 for (size_t i = Length - N + 1, e = 0; i != e;) {
67 if (substr(i, N).equals(Str))
73 /// find_first_of - Find the first character in the string that is in \arg
74 /// Chars, or npos if not found.
76 /// Note: O(size() * Chars.size())
77 StringRef::size_type StringRef::find_first_of(StringRef Chars,
79 for (size_type i = min(From, Length), e = Length; i != e; ++i)
80 if (Chars.find(Data[i]) != npos)
85 /// find_first_not_of - Find the first character in the string that is not
86 /// \arg C or npos if not found.
87 StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
88 for (size_type i = min(From, Length), e = Length; i != e; ++i)
94 /// find_first_not_of - Find the first character in the string that is not
95 /// in the string \arg Chars, or npos if not found.
97 /// Note: O(size() * Chars.size())
98 StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
100 for (size_type i = min(From, Length), e = Length; i != e; ++i)
101 if (Chars.find(Data[i]) == npos)
107 //===----------------------------------------------------------------------===//
108 // Helpful Algorithms
109 //===----------------------------------------------------------------------===//
111 /// count - Return the number of non-overlapped occurrences of \arg Str in
113 size_t StringRef::count(StringRef Str) const {
115 size_t N = Str.size();
118 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
119 if (substr(i, N).equals(Str))
124 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
125 /// sequence of radix up to 36 to an unsigned long long value.
126 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
127 unsigned long long &Result) {
128 // Autosense radix if not specified.
130 if (Str.startswith("0x")) {
133 } else if (Str.startswith("0b")) {
136 } else if (Str.startswith("0"))
142 // Empty strings (after the radix autosense) are invalid.
143 if (Str.empty()) return true;
145 // Parse all the bytes of the string given this radix. Watch for overflow.
147 while (!Str.empty()) {
149 if (Str[0] >= '0' && Str[0] <= '9')
150 CharVal = Str[0]-'0';
151 else if (Str[0] >= 'a' && Str[0] <= 'z')
152 CharVal = Str[0]-'a'+10;
153 else if (Str[0] >= 'A' && Str[0] <= 'Z')
154 CharVal = Str[0]-'A'+10;
158 // If the parsed value is larger than the integer radix, the string is
160 if (CharVal >= Radix)
163 // Add in this character.
164 unsigned long long PrevResult = Result;
165 Result = Result*Radix+CharVal;
167 // Check for overflow.
168 if (Result < PrevResult)
177 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
178 return GetAsUnsignedInteger(*this, Radix, Result);
182 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
183 unsigned long long ULLVal;
185 // Handle positive strings first.
186 if (empty() || front() != '-') {
187 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
188 // Check for value so large it overflows a signed value.
189 (long long)ULLVal < 0)
195 // Get the positive part of the value.
196 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
197 // Reject values so large they'd overflow as negative signed, but allow
198 // "-0". This negates the unsigned so that the negative isn't undefined
199 // on signed overflow.
200 (long long)-ULLVal > 0)
207 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
209 if (getAsInteger(Radix, Val) ||
216 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
217 unsigned long long Val;
218 if (getAsInteger(Radix, Val) ||
219 (unsigned)Val != Val)