comparison lib/Support/StringRef.cpp @ 121:803732b1fca8

LLVM 5.0
author kono
date Fri, 27 Oct 2017 17:07:41 +0900
parents 1172e4bd9c6f
children
comparison
equal deleted inserted replaced
120:1172e4bd9c6f 121:803732b1fca8
6 // License. See LICENSE.TXT for details. 6 // License. See LICENSE.TXT for details.
7 // 7 //
8 //===----------------------------------------------------------------------===// 8 //===----------------------------------------------------------------------===//
9 9
10 #include "llvm/ADT/StringRef.h" 10 #include "llvm/ADT/StringRef.h"
11 #include "llvm/ADT/APFloat.h"
11 #include "llvm/ADT/APInt.h" 12 #include "llvm/ADT/APInt.h"
12 #include "llvm/ADT/Hashing.h" 13 #include "llvm/ADT/Hashing.h"
13 #include "llvm/ADT/edit_distance.h" 14 #include "llvm/ADT/edit_distance.h"
14 #include <bitset> 15 #include <bitset>
15 16
146 /// found. 147 /// found.
147 size_t StringRef::find(StringRef Str, size_t From) const { 148 size_t StringRef::find(StringRef Str, size_t From) const {
148 if (From > Length) 149 if (From > Length)
149 return npos; 150 return npos;
150 151
152 const char *Start = Data + From;
153 size_t Size = Length - From;
154
151 const char *Needle = Str.data(); 155 const char *Needle = Str.data();
152 size_t N = Str.size(); 156 size_t N = Str.size();
153 if (N == 0) 157 if (N == 0)
154 return From; 158 return From;
155
156 size_t Size = Length - From;
157 if (Size < N) 159 if (Size < N)
158 return npos; 160 return npos;
159 161 if (N == 1) {
160 const char *Start = Data + From; 162 const char *Ptr = (const char *)::memchr(Start, Needle[0], Size);
163 return Ptr == nullptr ? npos : Ptr - Data;
164 }
165
161 const char *Stop = Start + (Size - N + 1); 166 const char *Stop = Start + (Size - N + 1);
162 167
163 // For short haystacks or unsupported needles fall back to the naive algorithm 168 // For short haystacks or unsupported needles fall back to the naive algorithm
164 if (Size < 16 || N > 255) { 169 if (Size < 16 || N > 255) {
165 do { 170 do {
175 std::memset(BadCharSkip, N, 256); 180 std::memset(BadCharSkip, N, 256);
176 for (unsigned i = 0; i != N-1; ++i) 181 for (unsigned i = 0; i != N-1; ++i)
177 BadCharSkip[(uint8_t)Str[i]] = N-1-i; 182 BadCharSkip[(uint8_t)Str[i]] = N-1-i;
178 183
179 do { 184 do {
180 if (std::memcmp(Start, Needle, N) == 0) 185 uint8_t Last = Start[N - 1];
181 return Start - Data; 186 if (LLVM_UNLIKELY(Last == (uint8_t)Needle[N - 1]))
187 if (std::memcmp(Start, Needle, N - 1) == 0)
188 return Start - Data;
182 189
183 // Otherwise skip the appropriate number of bytes. 190 // Otherwise skip the appropriate number of bytes.
184 Start += BadCharSkip[(uint8_t)Start[N-1]]; 191 Start += BadCharSkip[Last];
185 } while (Start < Stop); 192 } while (Start < Stop);
186 193
187 return npos; 194 return npos;
188 } 195 }
189 196
587 } 594 }
588 595
589 return false; 596 return false;
590 } 597 }
591 598
599 bool StringRef::getAsDouble(double &Result, bool AllowInexact) const {
600 APFloat F(0.0);
601 APFloat::opStatus Status =
602 F.convertFromString(*this, APFloat::rmNearestTiesToEven);
603 if (Status != APFloat::opOK) {
604 if (!AllowInexact || Status != APFloat::opInexact)
605 return true;
606 }
607
608 Result = F.convertToDouble();
609 return false;
610 }
592 611
593 // Implementation of StringRef hashing. 612 // Implementation of StringRef hashing.
594 hash_code llvm::hash_value(StringRef S) { 613 hash_code llvm::hash_value(StringRef S) {
595 return hash_combine_range(S.begin(), S.end()); 614 return hash_combine_range(S.begin(), S.end());
596 } 615 }