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) const {
28 size_t N = Str.size();
31 for (size_t i = 0, e = Length - N + 1; 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 from the string 'Chars' in the
54 /// current string or return npos if not in string.
55 StringRef::size_type StringRef::find_first_of(StringRef Chars) const {
56 for (size_type i = 0, e = Length; i != e; ++i)
57 if (Chars.find(Data[i]) != npos)
62 /// find_first_not_of - Find the first character in the string that is not
63 /// in the string 'Chars' or return npos if all are in string. Same as find.
64 StringRef::size_type StringRef::find_first_not_of(StringRef Chars) const {
65 for (size_type i = 0, e = Length; i != e; ++i)
66 if (Chars.find(Data[i]) == npos)
72 //===----------------------------------------------------------------------===//
74 //===----------------------------------------------------------------------===//
76 /// count - Return the number of non-overlapped occurrences of \arg Str in
78 size_t StringRef::count(StringRef Str) const {
80 size_t N = Str.size();
83 for (size_t i = 0, e = Length - N + 1; i != e; ++i)
84 if (substr(i, N).equals(Str))
89 /// GetAsUnsignedInteger - Workhorse method that converts a integer character
90 /// sequence of radix up to 36 to an unsigned long long value.
91 static bool GetAsUnsignedInteger(StringRef Str, unsigned Radix,
92 unsigned long long &Result) {
93 // Autosense radix if not specified.
95 if (Str.startswith("0x")) {
98 } else if (Str.startswith("0b")) {
101 } else if (Str.startswith("0"))
107 // Empty strings (after the radix autosense) are invalid.
108 if (Str.empty()) return true;
110 // Parse all the bytes of the string given this radix. Watch for overflow.
112 while (!Str.empty()) {
114 if (Str[0] >= '0' && Str[0] <= '9')
115 CharVal = Str[0]-'0';
116 else if (Str[0] >= 'a' && Str[0] <= 'z')
117 CharVal = Str[0]-'a'+10;
118 else if (Str[0] >= 'A' && Str[0] <= 'Z')
119 CharVal = Str[0]-'A'+10;
123 // If the parsed value is larger than the integer radix, the string is
125 if (CharVal >= Radix)
128 // Add in this character.
129 unsigned long long PrevResult = Result;
130 Result = Result*Radix+CharVal;
132 // Check for overflow.
133 if (Result < PrevResult)
142 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
143 return GetAsUnsignedInteger(*this, Radix, Result);
147 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
148 unsigned long long ULLVal;
150 // Handle positive strings first.
151 if (empty() || front() != '-') {
152 if (GetAsUnsignedInteger(*this, Radix, ULLVal) ||
153 // Check for value so large it overflows a signed value.
154 (long long)ULLVal < 0)
160 // Get the positive part of the value.
161 if (GetAsUnsignedInteger(substr(1), Radix, ULLVal) ||
162 // Reject values so large they'd overflow as negative signed, but allow
163 // "-0". This negates the unsigned so that the negative isn't undefined
164 // on signed overflow.
165 (long long)-ULLVal > 0)
172 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
174 if (getAsInteger(Radix, Val) ||
181 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
182 unsigned long long Val;
183 if (getAsInteger(Radix, Val) ||
184 (unsigned)Val != Val)