Pass StringRef by value.
[oota-llvm.git] / lib / Support / StringRef.cpp
1 //===-- StringRef.cpp - Lightweight String References ---------------------===//
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 #include "llvm/ADT/StringRef.h"
11 using namespace llvm;
12
13 // MSVC emits references to this into the translation units which reference it.
14 #ifndef _MSC_VER
15 const size_t StringRef::npos;
16 #endif
17
18 //===----------------------------------------------------------------------===//
19 // String Searching
20 //===----------------------------------------------------------------------===//
21
22
23 /// find - Search for the first string \arg Str in the string.
24 ///
25 /// \return - The index of the first occurence of \arg Str, or npos if not
26 /// found.
27 size_t StringRef::find(StringRef Str) const {
28   size_t N = Str.size();
29   if (N > Length)
30     return npos;
31   for (size_t i = 0, e = Length - N + 1; i != e; ++i)
32     if (substr(i, N).equals(Str))
33       return i;
34   return npos;
35 }
36
37 /// rfind - Search for the last string \arg Str in the string.
38 ///
39 /// \return - The index of the last occurence of \arg Str, or npos if not
40 /// found.
41 size_t StringRef::rfind(StringRef Str) const {
42   size_t N = Str.size();
43   if (N > Length)
44     return npos;
45   for (size_t i = Length - N + 1, e = 0; i != e;) {
46     --i;
47     if (substr(i, N).equals(Str))
48       return i;
49   }
50   return npos;
51 }
52
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)
58       return i;
59   return npos;
60 }
61
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)
67       return i;
68   return npos;
69 }
70
71
72 //===----------------------------------------------------------------------===//
73 // Helpful Algorithms
74 //===----------------------------------------------------------------------===//
75
76 /// count - Return the number of non-overlapped occurrences of \arg Str in
77 /// the string.
78 size_t StringRef::count(StringRef Str) const {
79   size_t Count = 0;
80   size_t N = Str.size();
81   if (N > Length)
82     return 0;
83   for (size_t i = 0, e = Length - N + 1; i != e; ++i)
84     if (substr(i, N).equals(Str))
85       ++Count;
86   return Count;
87 }
88
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.
94   if (Radix == 0) {
95     if (Str.startswith("0x")) {
96       Str = Str.substr(2);
97       Radix = 16;
98     } else if (Str.startswith("0b")) {
99       Str = Str.substr(2);
100       Radix = 2;
101     } else if (Str.startswith("0"))
102       Radix = 8;
103     else
104       Radix = 10;
105   }
106   
107   // Empty strings (after the radix autosense) are invalid.
108   if (Str.empty()) return true;
109   
110   // Parse all the bytes of the string given this radix.  Watch for overflow.
111   Result = 0;
112   while (!Str.empty()) {
113     unsigned CharVal;
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;
120     else
121       return true;
122     
123     // If the parsed value is larger than the integer radix, the string is
124     // invalid.
125     if (CharVal >= Radix)
126       return true;
127     
128     // Add in this character.
129     unsigned long long PrevResult = Result;
130     Result = Result*Radix+CharVal;
131     
132     // Check for overflow.
133     if (Result < PrevResult)
134       return true;
135
136     Str = Str.substr(1);
137   }
138   
139   return false;
140 }
141
142 bool StringRef::getAsInteger(unsigned Radix, unsigned long long &Result) const {
143   return GetAsUnsignedInteger(*this, Radix, Result);
144 }
145
146
147 bool StringRef::getAsInteger(unsigned Radix, long long &Result) const {
148   unsigned long long ULLVal;
149   
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)
155       return true;
156     Result = ULLVal;
157     return false;
158   }
159   
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)
166     return true;
167   
168   Result = -ULLVal;
169   return false;
170 }
171
172 bool StringRef::getAsInteger(unsigned Radix, int &Result) const {
173   long long Val;
174   if (getAsInteger(Radix, Val) ||
175       (int)Val != Val)
176     return true;
177   Result = Val;
178   return false;
179 }
180
181 bool StringRef::getAsInteger(unsigned Radix, unsigned &Result) const {
182   unsigned long long Val;
183   if (getAsInteger(Radix, Val) ||
184       (unsigned)Val != Val)
185     return true;
186   Result = Val;
187   return false;
188 }