Coverage Report

Created: 2019-07-24 05:18

/Users/buildslave/jenkins/workspace/clang-stage2-coverage-R/llvm/lib/Support/StringRef.cpp
Line
Count
Source (jump to first uncovered line)
1
//===-- StringRef.cpp - Lightweight String References ---------------------===//
2
//
3
// Part of the LLVM Project, under the Apache License v2.0 with LLVM Exceptions.
4
// See https://llvm.org/LICENSE.txt for license information.
5
// SPDX-License-Identifier: Apache-2.0 WITH LLVM-exception
6
//
7
//===----------------------------------------------------------------------===//
8
9
#include "llvm/ADT/StringRef.h"
10
#include "llvm/ADT/APFloat.h"
11
#include "llvm/ADT/APInt.h"
12
#include "llvm/ADT/Hashing.h"
13
#include "llvm/ADT/StringExtras.h"
14
#include "llvm/ADT/edit_distance.h"
15
#include <bitset>
16
17
using namespace llvm;
18
19
// MSVC emits references to this into the translation units which reference it.
20
#ifndef _MSC_VER
21
const size_t StringRef::npos;
22
#endif
23
24
// strncasecmp() is not available on non-POSIX systems, so define an
25
// alternative function here.
26
31.1M
static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length) {
27
66.2M
  for (size_t I = 0; I < Length; 
++I35.1M
) {
28
65.5M
    unsigned char LHC = toLower(LHS[I]);
29
65.5M
    unsigned char RHC = toLower(RHS[I]);
30
65.5M
    if (LHC != RHC)
31
30.4M
      return LHC < RHC ? 
-119.6M
:
110.7M
;
32
65.5M
  }
33
31.1M
  
return 0736k
;
34
31.1M
}
35
36
/// compare_lower - Compare strings, ignoring case.
37
31.0M
int StringRef::compare_lower(StringRef RHS) const {
38
31.0M
  if (int Res = ascii_strncasecmp(Data, RHS.Data, std::min(Length, RHS.Length)))
39
30.3M
    return Res;
40
711k
  if (Length == RHS.Length)
41
707k
    return 0;
42
4.17k
  return Length < RHS.Length ? 
-11.97k
:
12.19k
;
43
4.17k
}
44
45
/// Check if this string starts with the given \p Prefix, ignoring case.
46
128k
bool StringRef::startswith_lower(StringRef Prefix) const {
47
128k
  return Length >= Prefix.Length &&
48
128k
      
ascii_strncasecmp(Data, Prefix.Data, Prefix.Length) == 0107k
;
49
128k
}
50
51
/// Check if this string ends with the given \p Suffix, ignoring case.
52
21.8k
bool StringRef::endswith_lower(StringRef Suffix) const {
53
21.8k
  return Length >= Suffix.Length &&
54
21.8k
      
ascii_strncasecmp(end() - Suffix.Length, Suffix.Data, Suffix.Length) == 020.3k
;
55
21.8k
}
56
57
14
size_t StringRef::find_lower(char C, size_t From) const {
58
14
  char L = toLower(C);
59
50
  return find_if([L](char D) { return toLower(D) == L; }, From);
60
14
}
61
62
/// compare_numeric - Compare strings, handle embedded numbers.
63
443k
int StringRef::compare_numeric(StringRef RHS) const {
64
3.79M
  for (size_t I = 0, E = std::min(Length, RHS.Length); I != E; 
++I3.35M
) {
65
3.76M
    // Check for sequences of digits.
66
3.76M
    if (isDigit(Data[I]) && 
isDigit(RHS.Data[I])279k
) {
67
275k
      // The longer sequence of numbers is considered larger.
68
275k
      // This doesn't really handle prefixed zeros well.
69
275k
      size_t J;
70
929k
      for (J = I + 1; J != E + 1; 
++J653k
) {
71
929k
        bool ld = J < Length && 
isDigit(Data[J])739k
;
72
929k
        bool rd = J < RHS.Length && 
isDigit(RHS.Data[J])739k
;
73
929k
        if (ld != rd)
74
16.1k
          return rd ? 
-19.67k
:
16.43k
;
75
913k
        if (!rd)
76
259k
          break;
77
913k
      }
78
275k
      // The two number sequences have the same length (J-I), just memcmp them.
79
275k
      
if (int 259k
Res259k
= compareMemory(Data + I, RHS.Data + I, J - I))
80
203k
        return Res < 0 ? 
-1103k
:
199.8k
;
81
56.3k
      // Identical number sequences, continue search after the numbers.
82
56.3k
      I = J - 1;
83
56.3k
      continue;
84
56.3k
    }
85
3.48M
    if (Data[I] != RHS.Data[I])
86
195k
      return (unsigned char)Data[I] < (unsigned char)RHS.Data[I] ? 
-185.7k
:
1109k
;
87
3.48M
  }
88
443k
  
if (27.7k
Length == RHS.Length27.7k
)
89
24.4k
    return 0;
90
3.24k
  return Length < RHS.Length ? 
-11.34k
:
11.90k
;
91
3.24k
}
92
93
// Compute the edit distance between the two given strings.
94
unsigned StringRef::edit_distance(llvm::StringRef Other,
95
                                  bool AllowReplacements,
96
2.26M
                                  unsigned MaxEditDistance) const {
97
2.26M
  return llvm::ComputeEditDistance(
98
2.26M
      makeArrayRef(data(), size()),
99
2.26M
      makeArrayRef(Other.data(), Other.size()),
100
2.26M
      AllowReplacements, MaxEditDistance);
101
2.26M
}
102
103
//===----------------------------------------------------------------------===//
104
// String Operations
105
//===----------------------------------------------------------------------===//
106
107
7.33M
std::string StringRef::lower() const {
108
7.33M
  std::string Result(size(), char());
109
224M
  for (size_type i = 0, e = size(); i != e; 
++i217M
) {
110
217M
    Result[i] = toLower(Data[i]);
111
217M
  }
112
7.33M
  return Result;
113
7.33M
}
114
115
483k
std::string StringRef::upper() const {
116
483k
  std::string Result(size(), char());
117
2.13M
  for (size_type i = 0, e = size(); i != e; 
++i1.64M
) {
118
1.64M
    Result[i] = toUpper(Data[i]);
119
1.64M
  }
120
483k
  return Result;
121
483k
}
122
123
//===----------------------------------------------------------------------===//
124
// String Searching
125
//===----------------------------------------------------------------------===//
126
127
128
/// find - Search for the first string \arg Str in the string.
129
///
130
/// \return - The index of the first occurrence of \arg Str, or npos if not
131
/// found.
132
11.6M
size_t StringRef::find(StringRef Str, size_t From) const {
133
11.6M
  if (From > Length)
134
0
    return npos;
135
11.6M
136
11.6M
  const char *Start = Data + From;
137
11.6M
  size_t Size = Length - From;
138
11.6M
139
11.6M
  const char *Needle = Str.data();
140
11.6M
  size_t N = Str.size();
141
11.6M
  if (N == 0)
142
729
    return From;
143
11.6M
  if (Size < N)
144
707k
    return npos;
145
10.9M
  if (N == 1) {
146
7.16M
    const char *Ptr = (const char *)::memchr(Start, Needle[0], Size);
147
7.16M
    return Ptr == nullptr ? 
npos3.07M
:
Ptr - Data4.09M
;
148
7.16M
  }
149
3.75M
150
3.75M
  const char *Stop = Start + (Size - N + 1);
151
3.75M
152
3.75M
  // For short haystacks or unsupported needles fall back to the naive algorithm
153
3.75M
  if (Size < 16 || 
N > 2551.47M
) {
154
9.96M
    do {
155
9.96M
      if (std::memcmp(Start, Needle, N) == 0)
156
29.5k
        return Start - Data;
157
9.93M
      ++Start;
158
9.93M
    } while (Start < Stop);
159
2.27M
    
return npos2.24M
;
160
1.47M
  }
161
1.47M
162
1.47M
  // Build the bad char heuristic table, with uint8_t to reduce cache thrashing.
163
1.47M
  uint8_t BadCharSkip[256];
164
1.47M
  std::memset(BadCharSkip, N, 256);
165
18.0M
  for (unsigned i = 0; i != N-1; 
++i16.5M
)
166
16.5M
    BadCharSkip[(uint8_t)Str[i]] = N-1-i;
167
1.47M
168
11.8M
  do {
169
11.8M
    uint8_t Last = Start[N - 1];
170
11.8M
    if (LLVM_UNLIKELY(Last == (uint8_t)Needle[N - 1]))
171
11.8M
      
if (1.39M
std::memcmp(Start, Needle, N - 1) == 01.39M
)
172
474k
        return Start - Data;
173
11.3M
174
11.3M
    // Otherwise skip the appropriate number of bytes.
175
11.3M
    Start += BadCharSkip[Last];
176
11.3M
  } while (Start < Stop);
177
1.47M
178
1.47M
  
return npos1.00M
;
179
1.47M
}
180
181
32.6k
size_t StringRef::find_lower(StringRef Str, size_t From) const {
182
32.6k
  StringRef This = substr(From);
183
95.1k
  while (This.size() >= Str.size()) {
184
63.4k
    if (This.startswith_lower(Str))
185
1.00k
      return From;
186
62.4k
    This = This.drop_front();
187
62.4k
    ++From;
188
62.4k
  }
189
32.6k
  
return npos31.6k
;
190
32.6k
}
191
192
3
size_t StringRef::rfind_lower(char C, size_t From) const {
193
3
  From = std::min(From, Length);
194
3
  size_t i = From;
195
15
  while (i != 0) {
196
14
    --i;
197
14
    if (toLower(Data[i]) == toLower(C))
198
2
      return i;
199
14
  }
200
3
  
return npos1
;
201
3
}
202
203
/// rfind - Search for the last string \arg Str in the string.
204
///
205
/// \return - The index of the last occurrence of \arg Str, or npos if not
206
/// found.
207
467k
size_t StringRef::rfind(StringRef Str) const {
208
467k
  size_t N = Str.size();
209
467k
  if (N > Length)
210
137
    return npos;
211
4.26M
  
for (size_t i = Length - N + 1, e = 0; 467k
i != e;) {
212
4.10M
    --i;
213
4.10M
    if (substr(i, N).equals(Str))
214
307k
      return i;
215
4.10M
  }
216
467k
  
return npos159k
;
217
467k
}
218
219
1
size_t StringRef::rfind_lower(StringRef Str) const {
220
1
  size_t N = Str.size();
221
1
  if (N > Length)
222
0
    return npos;
223
2
  
for (size_t i = Length - N + 1, e = 0; 1
i != e;) {
224
1
    --i;
225
1
    if (substr(i, N).equals_lower(Str))
226
0
      return i;
227
1
  }
228
1
  return npos;
229
1
}
230
231
/// find_first_of - Find the first character in the string that is in \arg
232
/// Chars, or npos if not found.
233
///
234
/// Note: O(size() + Chars.size())
235
StringRef::size_type StringRef::find_first_of(StringRef Chars,
236
44.7M
                                              size_t From) const {
237
44.7M
  std::bitset<1 << CHAR_BIT> CharBits;
238
119M
  for (size_type i = 0; i != Chars.size(); 
++i74.4M
)
239
74.4M
    CharBits.set((unsigned char)Chars[i]);
240
44.7M
241
412M
  for (size_type i = std::min(From, Length), e = Length; i != e; 
++i367M
)
242
393M
    if (CharBits.test((unsigned char)Data[i]))
243
25.4M
      return i;
244
44.7M
  
return npos19.2M
;
245
44.7M
}
246
247
/// find_first_not_of - Find the first character in the string that is not
248
/// \arg C or npos if not found.
249
4.67k
StringRef::size_type StringRef::find_first_not_of(char C, size_t From) const {
250
6.54k
  for (size_type i = std::min(From, Length), e = Length; i != e; 
++i1.87k
)
251
6.50k
    if (Data[i] != C)
252
4.63k
      return i;
253
4.67k
  
return npos39
;
254
4.67k
}
255
256
/// find_first_not_of - Find the first character in the string that is not
257
/// in the string \arg Chars, or npos if not found.
258
///
259
/// Note: O(size() + Chars.size())
260
StringRef::size_type StringRef::find_first_not_of(StringRef Chars,
261
10.7M
                                                  size_t From) const {
262
10.7M
  std::bitset<1 << CHAR_BIT> CharBits;
263
57.7M
  for (size_type i = 0; i != Chars.size(); 
++i47.0M
)
264
47.0M
    CharBits.set((unsigned char)Chars[i]);
265
10.7M
266
17.8M
  for (size_type i = std::min(From, Length), e = Length; i != e; 
++i7.13M
)
267
16.6M
    if (!CharBits.test((unsigned char)Data[i]))
268
9.52M
      return i;
269
10.7M
  
return npos1.23M
;
270
10.7M
}
271
272
/// find_last_of - Find the last character in the string that is in \arg C,
273
/// or npos if not found.
274
///
275
/// Note: O(size() + Chars.size())
276
StringRef::size_type StringRef::find_last_of(StringRef Chars,
277
5.87M
                                             size_t From) const {
278
5.87M
  std::bitset<1 << CHAR_BIT> CharBits;
279
11.9M
  for (size_type i = 0; i != Chars.size(); 
++i6.06M
)
280
6.06M
    CharBits.set((unsigned char)Chars[i]);
281
5.87M
282
52.5M
  for (size_type i = std::min(From, Length) - 1, e = -1; i != e; 
--i46.6M
)
283
52.1M
    if (CharBits.test((unsigned char)Data[i]))
284
5.47M
      return i;
285
5.87M
  
return npos398k
;
286
5.87M
}
287
288
/// find_last_not_of - Find the last character in the string that is not
289
/// \arg C, or npos if not found.
290
423k
StringRef::size_type StringRef::find_last_not_of(char C, size_t From) const {
291
450k
  for (size_type i = std::min(From, Length) - 1, e = -1; i != e; 
--i26.6k
)
292
450k
    if (Data[i] != C)
293
423k
      return i;
294
423k
  
return npos7
;
295
423k
}
296
297
/// find_last_not_of - Find the last character in the string that is not in
298
/// \arg Chars, or npos if not found.
299
///
300
/// Note: O(size() + Chars.size())
301
StringRef::size_type StringRef::find_last_not_of(StringRef Chars,
302
2.01M
                                                 size_t From) const {
303
2.01M
  std::bitset<1 << CHAR_BIT> CharBits;
304
13.4M
  for (size_type i = 0, e = Chars.size(); i != e; 
++i11.4M
)
305
11.4M
    CharBits.set((unsigned char)Chars[i]);
306
2.01M
307
2.09M
  for (size_type i = std::min(From, Length) - 1, e = -1; i != e; 
--i82.2k
)
308
1.65M
    if (!CharBits.test((unsigned char)Data[i]))
309
1.57M
      return i;
310
2.01M
  
return npos444k
;
311
2.01M
}
312
313
void StringRef::split(SmallVectorImpl<StringRef> &A,
314
                      StringRef Separator, int MaxSplit,
315
875k
                      bool KeepEmpty) const {
316
875k
  StringRef S = *this;
317
875k
318
875k
  // Count down from MaxSplit. When MaxSplit is -1, this will just split
319
875k
  // "forever". This doesn't support splitting more than 2^31 times
320
875k
  // intentionally; if we ever want that we can make MaxSplit a 64-bit integer
321
875k
  // but that seems unlikely to be useful.
322
1.02M
  while (MaxSplit-- != 0) {
323
1.02M
    size_t Idx = S.find(Separator);
324
1.02M
    if (Idx == npos)
325
875k
      break;
326
149k
327
149k
    // Push this split.
328
149k
    if (KeepEmpty || 
Idx > 02.00k
)
329
149k
      A.push_back(S.slice(0, Idx));
330
149k
331
149k
    // Jump forward.
332
149k
    S = S.slice(Idx + Separator.size(), npos);
333
149k
  }
334
875k
335
875k
  // Push the tail.
336
875k
  if (KeepEmpty || 
!S.empty()28.6k
)
337
864k
    A.push_back(S);
338
875k
}
339
340
void StringRef::split(SmallVectorImpl<StringRef> &A, char Separator,
341
2.56M
                      int MaxSplit, bool KeepEmpty) const {
342
2.56M
  StringRef S = *this;
343
2.56M
344
2.56M
  // Count down from MaxSplit. When MaxSplit is -1, this will just split
345
2.56M
  // "forever". This doesn't support splitting more than 2^31 times
346
2.56M
  // intentionally; if we ever want that we can make MaxSplit a 64-bit integer
347
2.56M
  // but that seems unlikely to be useful.
348
7.81M
  while (MaxSplit-- != 0) {
349
7.27M
    size_t Idx = S.find(Separator);
350
7.27M
    if (Idx == npos)
351
2.02M
      break;
352
5.25M
353
5.25M
    // Push this split.
354
5.25M
    if (KeepEmpty || 
Idx > 0674k
)
355
5.25M
      A.push_back(S.slice(0, Idx));
356
5.25M
357
5.25M
    // Jump forward.
358
5.25M
    S = S.slice(Idx + 1, npos);
359
5.25M
  }
360
2.56M
361
2.56M
  // Push the tail.
362
2.56M
  if (KeepEmpty || 
!S.empty()249k
)
363
2.47M
    A.push_back(S);
364
2.56M
}
365
366
//===----------------------------------------------------------------------===//
367
// Helpful Algorithms
368
//===----------------------------------------------------------------------===//
369
370
/// count - Return the number of non-overlapped occurrences of \arg Str in
371
/// the string.
372
3.56k
size_t StringRef::count(StringRef Str) const {
373
3.56k
  size_t Count = 0;
374
3.56k
  size_t N = Str.size();
375
3.56k
  if (N > Length)
376
19
    return 0;
377
89.5k
  
for (size_t i = 0, e = Length - N + 1; 3.54k
i != e;
++i85.9k
)
378
85.9k
    if (substr(i, N).equals(Str))
379
26.1k
      ++Count;
380
3.54k
  return Count;
381
3.54k
}
382
383
5.65M
static unsigned GetAutoSenseRadix(StringRef &Str) {
384
5.65M
  if (Str.empty())
385
826
    return 10;
386
5.65M
387
5.65M
  if (Str.startswith("0x") || 
Str.startswith("0X")4.29M
) {
388
1.36M
    Str = Str.substr(2);
389
1.36M
    return 16;
390
1.36M
  }
391
4.29M
392
4.29M
  if (Str.startswith("0b") || 
Str.startswith("0B")4.28M
) {
393
38
    Str = Str.substr(2);
394
38
    return 2;
395
38
  }
396
4.28M
397
4.28M
  if (Str.startswith("0o")) {
398
1
    Str = Str.substr(2);
399
1
    return 8;
400
1
  }
401
4.28M
402
4.28M
  if (Str[0] == '0' && 
Str.size() > 13.37M
&&
isDigit(Str[1])14.2k
) {
403
43
    Str = Str.substr(1);
404
43
    return 8;
405
43
  }
406
4.28M
407
4.28M
  return 10;
408
4.28M
}
409
410
bool llvm::consumeUnsignedInteger(StringRef &Str, unsigned Radix,
411
9.48M
                                  unsigned long long &Result) {
412
9.48M
  // Autosense radix if not specified.
413
9.48M
  if (Radix == 0)
414
5.55M
    Radix = GetAutoSenseRadix(Str);
415
9.48M
416
9.48M
  // Empty strings (after the radix autosense) are invalid.
417
9.48M
  if (Str.empty()) 
return true22.6k
;
418
9.45M
419
9.45M
  // Parse all the bytes of the string given this radix.  Watch for overflow.
420
9.45M
  StringRef Str2 = Str;
421
9.45M
  Result = 0;
422
23.6M
  while (!Str2.empty()) {
423
14.5M
    unsigned CharVal;
424
14.5M
    if (Str2[0] >= '0' && 
Str2[0] <= '9'14.4M
)
425
13.5M
      CharVal = Str2[0] - '0';
426
968k
    else if (Str2[0] >= 'a' && 
Str2[0] <= 'z'896k
)
427
893k
      CharVal = Str2[0] - 'a' + 10;
428
74.9k
    else if (Str2[0] >= 'A' && 
Str2[0] <= 'Z'11.1k
)
429
6.95k
      CharVal = Str2[0] - 'A' + 10;
430
67.9k
    else
431
67.9k
      break;
432
14.4M
433
14.4M
    // If the parsed value is larger than the integer radix, we cannot
434
14.4M
    // consume any more characters.
435
14.4M
    if (CharVal >= Radix)
436
280k
      break;
437
14.1M
438
14.1M
    // Add in this character.
439
14.1M
    unsigned long long PrevResult = Result;
440
14.1M
    Result = Result * Radix + CharVal;
441
14.1M
442
14.1M
    // Check for overflow by shifting back and seeing if bits were lost.
443
14.1M
    if (Result / Radix < PrevResult)
444
5
      return true;
445
14.1M
446
14.1M
    Str2 = Str2.substr(1);
447
14.1M
  }
448
9.45M
449
9.45M
  // We consider the operation a failure if no characters were consumed
450
9.45M
  // successfully.
451
9.45M
  
if (9.45M
Str.size() == Str2.size()9.45M
)
452
295k
    return true;
453
9.16M
454
9.16M
  Str = Str2;
455
9.16M
  return false;
456
9.16M
}
457
458
bool llvm::consumeSignedInteger(StringRef &Str, unsigned Radix,
459
225k
                                long long &Result) {
460
225k
  unsigned long long ULLVal;
461
225k
462
225k
  // Handle positive strings first.
463
225k
  if (Str.empty() || 
Str.front() != '-'225k
) {
464
223k
    if (consumeUnsignedInteger(Str, Radix, ULLVal) ||
465
223k
        // Check for value so large it overflows a signed value.
466
223k
        
(long long)ULLVal < 0144k
)
467
79.1k
      return true;
468
144k
    Result = ULLVal;
469
144k
    return false;
470
144k
  }
471
1.19k
472
1.19k
  // Get the positive part of the value.
473
1.19k
  StringRef Str2 = Str.drop_front(1);
474
1.19k
  if (consumeUnsignedInteger(Str2, Radix, ULLVal) ||
475
1.19k
      // Reject values so large they'd overflow as negative signed, but allow
476
1.19k
      // "-0".  This negates the unsigned so that the negative isn't undefined
477
1.19k
      // on signed overflow.
478
1.19k
      (long long)-ULLVal > 0)
479
2
    return true;
480
1.19k
481
1.19k
  Str = Str2;
482
1.19k
  Result = -ULLVal;
483
1.19k
  return false;
484
1.19k
}
485
486
/// GetAsUnsignedInteger - Workhorse method that converts a integer character
487
/// sequence of radix up to 36 to an unsigned long long value.
488
bool llvm::getAsUnsignedInteger(StringRef Str, unsigned Radix,
489
9.00M
                                unsigned long long &Result) {
490
9.00M
  if (consumeUnsignedInteger(Str, Radix, Result))
491
217k
    return true;
492
8.78M
493
8.78M
  // For getAsUnsignedInteger, we require the whole string to be consumed or
494
8.78M
  // else we consider it a failure.
495
8.78M
  return !Str.empty();
496
8.78M
}
497
498
bool llvm::getAsSignedInteger(StringRef Str, unsigned Radix,
499
224k
                              long long &Result) {
500
224k
  if (consumeSignedInteger(Str, Radix, Result))
501
79.1k
    return true;
502
145k
503
145k
  // For getAsSignedInteger, we require the whole string to be consumed or else
504
145k
  // we consider it a failure.
505
145k
  return !Str.empty();
506
145k
}
507
508
1.11M
bool StringRef::getAsInteger(unsigned Radix, APInt &Result) const {
509
1.11M
  StringRef Str = *this;
510
1.11M
511
1.11M
  // Autosense radix if not specified.
512
1.11M
  if (Radix == 0)
513
101k
    Radix = GetAutoSenseRadix(Str);
514
1.11M
515
1.11M
  assert(Radix > 1 && Radix <= 36);
516
1.11M
517
1.11M
  // Empty strings (after the radix autosense) are invalid.
518
1.11M
  if (Str.empty()) 
return true0
;
519
1.11M
520
1.11M
  // Skip leading zeroes.  This can be a significant improvement if
521
1.11M
  // it means we don't need > 64 bits.
522
1.25M
  
while (1.11M
!Str.empty() &&
Str.front() == '0'1.12M
)
523
139k
    Str = Str.substr(1);
524
1.11M
525
1.11M
  // If it was nothing but zeroes....
526
1.11M
  if (Str.empty()) {
527
131k
    Result = APInt(64, 0);
528
131k
    return false;
529
131k
  }
530
982k
531
982k
  // (Over-)estimate the required number of bits.
532
982k
  unsigned Log2Radix = 0;
533
4.91M
  while ((1U << Log2Radix) < Radix) 
Log2Radix++3.92M
;
534
982k
  bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);
535
982k
536
982k
  unsigned BitWidth = Log2Radix * Str.size();
537
982k
  if (BitWidth < Result.getBitWidth())
538
982k
    BitWidth = Result.getBitWidth(); // don't shrink the result
539
55
  else if (BitWidth > Result.getBitWidth())
540
45
    Result = Result.zext(BitWidth);
541
982k
542
982k
  APInt RadixAP, CharAP; // unused unless !IsPowerOf2Radix
543
982k
  if (!IsPowerOf2Radix) {
544
916k
    // These must have the same bit-width as Result.
545
916k
    RadixAP = APInt(BitWidth, Radix);
546
916k
    CharAP = APInt(BitWidth, 0);
547
916k
  }
548
982k
549
982k
  // Parse all the bytes of the string given this radix.
550
982k
  Result = 0;
551
2.73M
  while (!Str.empty()) {
552
1.75M
    unsigned CharVal;
553
1.75M
    if (Str[0] >= '0' && Str[0] <= '9')
554
1.68M
      CharVal = Str[0]-'0';
555
64.5k
    else if (Str[0] >= 'a' && 
Str[0] <= 'z'60.5k
)
556
60.5k
      CharVal = Str[0]-'a'+10;
557
4.05k
    else if (Str[0] >= 'A' && Str[0] <= 'Z')
558
4.05k
      CharVal = Str[0]-'A'+10;
559
0
    else
560
0
      return true;
561
1.75M
562
1.75M
    // If the parsed value is larger than the integer radix, the string is
563
1.75M
    // invalid.
564
1.75M
    if (CharVal >= Radix)
565
25
      return true;
566
1.75M
567
1.75M
    // Add in this character.
568
1.75M
    if (IsPowerOf2Radix) {
569
249k
      Result <<= Log2Radix;
570
249k
      Result |= CharVal;
571
1.50M
    } else {
572
1.50M
      Result *= RadixAP;
573
1.50M
      CharAP = CharVal;
574
1.50M
      Result += CharAP;
575
1.50M
    }
576
1.75M
577
1.75M
    Str = Str.substr(1);
578
1.75M
  }
579
982k
580
982k
  
return false982k
;
581
982k
}
582
583
19
bool StringRef::getAsDouble(double &Result, bool AllowInexact) const {
584
19
  APFloat F(0.0);
585
19
  APFloat::opStatus Status =
586
19
      F.convertFromString(*this, APFloat::rmNearestTiesToEven);
587
19
  if (Status != APFloat::opOK) {
588
6
    if (!AllowInexact || 
!(Status & APFloat::opInexact)3
)
589
3
      return true;
590
16
  }
591
16
592
16
  Result = F.convertToDouble();
593
16
  return false;
594
16
}
595
596
// Implementation of StringRef hashing.
597
7.80M
hash_code llvm::hash_value(StringRef S) {
598
7.80M
  return hash_combine_range(S.begin(), S.end());
599
7.80M
}