LLVM: lib/Support/StringRef.cpp Source File (original) (raw)

1

2

3

4

5

6

7

8

16#include

17

18using namespace llvm;

19

20

21#ifndef _MSC_VER

23#endif

24

25

26

28 for (size_t I = 0; I < Length; ++I) {

29 unsigned char LHC = toLower(LHS[I]);

30 unsigned char RHC = toLower(RHS[I]);

31 if (LHC != RHC)

32 return LHC < RHC ? -1 : 1;

33 }

34 return 0;

35}

36

38 if (int Res =

40 return Res;

42 return 0;

43 return size() < RHS.size() ? -1 : 1;

44}

45

47 return size() >= Prefix.size() &&

49}

50

52 return size() >= Suffix.size() &&

54 Suffix.size()) == 0;

55}

56

58 char L = toLower(C);

59 return find_if([L](char D) { return toLower(D) == L; }, From);

60}

61

62

64 for (size_t I = 0, E = std::min(size(), RHS.size()); I != E; ++I) {

65

67

68

69 size_t J;

70 for (J = I + 1; J != E + 1; ++J) {

72 bool rd = J < RHS.size() && isDigit(RHS.data()[J]);

73 if (ld != rd)

74 return rd ? -1 : 1;

75 if (!rd)

76 break;

77 }

78

79 if (int Res = compareMemory(data() + I, RHS.data() + I, J - I))

80 return Res < 0 ? -1 : 1;

81

82 I = J - 1;

83 continue;

84 }

86 return (unsigned char)data()[I] < (unsigned char)RHS.data()[I] ? -1 : 1;

87 }

89 return 0;

90 return size() < RHS.size() ? -1 : 1;

91}

92

93

95 bool AllowReplacements,

96 unsigned MaxEditDistance) const {

99 AllowReplacements, MaxEditDistance);

100}

101

103 StringRef Other, bool AllowReplacements, unsigned MaxEditDistance) const {

106 llvm::toLower, AllowReplacements, MaxEditDistance);

107}

108

109

110

111

112

116}

117

121}

122

123

124

125

126

127

128

129

130

131

135

136 const char *Start = data() + From;

138

139 const char *Needle = Str.data();

140 size_t N = Str.size();

141 if (N == 0)

145 if (N == 1) {

146 const char *Ptr = (const char *)::memchr(Start, Needle[0], Size);

148 }

149

150 const char *Stop = Start + (Size - N + 1);

151

152 if (N == 2) {

153

154

155

156 do {

157 if (std::memcmp(Start, Needle, 2) == 0)

158 return Start - data();

159 ++Start;

160 } while (Start < Stop);

162 }

163

164

165 if (Size < 16 || N > 255) {

166 do {

167 if (std::memcmp(Start, Needle, N) == 0)

168 return Start - data();

169 ++Start;

170 } while (Start < Stop);

172 }

173

174

175 uint8_t BadCharSkip[256];

176 std::memset(BadCharSkip, N, 256);

177 for (unsigned i = 0; i != N-1; ++i)

178 BadCharSkip[(uint8_t)Str[i]] = N-1-i;

179

180 do {

183 if (std::memcmp(Start, Needle, N - 1) == 0)

184 return Start - data();

185

186

187 Start += BadCharSkip[Last];

188 } while (Start < Stop);

189

191}

192

195 while (This.size() >= Str.size()) {

196 if (This.starts_with_insensitive(Str))

198 This = This.drop_front();

200 }

202}

203

206 size_t i = From;

207 while (i != 0) {

208 --i;

209 if (toLower(data()[i]) == toLower(C))

210 return i;

211 }

213}

214

215

216

217

218

220 return std::string_view(*this).rfind(Str);

221}

222

224 size_t N = Str.size();

227 for (size_t i = size() - N + 1, e = 0; i != e;) {

228 --i;

230 return i;

231 }

233}

234

235

236

237

238

240 size_t From) const {

241 std::bitset<1 << CHAR_BIT> CharBits;

242 for (char C : Chars)

243 CharBits.set((unsigned char)C);

244

246 if (CharBits.test((unsigned char)data()[i]))

247 return i;

249}

250

251

252

254 return std::string_view(*this).find_first_not_of(C, From);

255}

256

257

258

259

260

262 size_t From) const {

263 std::bitset<1 << CHAR_BIT> CharBits;

264 for (char C : Chars)

265 CharBits.set((unsigned char)C);

266

268 if (!CharBits.test((unsigned char)data()[i]))

269 return i;

271}

272

273

274

275

276

278 size_t From) const {

279 std::bitset<1 << CHAR_BIT> CharBits;

280 for (char C : Chars)

281 CharBits.set((unsigned char)C);

282

283 for (size_type i = std::min(From, size()) - 1, e = -1; i != e; --i)

284 if (CharBits.test((unsigned char)data()[i]))

285 return i;

287}

288

289

290

292 for (size_type i = std::min(From, size()) - 1, e = -1; i != e; --i)

293 if (data()[i] != C)

294 return i;

296}

297

298

299

300

301

303 size_t From) const {

304 std::bitset<1 << CHAR_BIT> CharBits;

305 for (char C : Chars)

306 CharBits.set((unsigned char)C);

307

308 for (size_type i = std::min(From, size()) - 1, e = -1; i != e; --i)

309 if (!CharBits.test((unsigned char)data()[i]))

310 return i;

312}

313

315 StringRef Separator, int MaxSplit,

316 bool KeepEmpty) const {

318

319

320

321

322

323 while (MaxSplit-- != 0) {

324 size_t Idx = S.find(Separator);

326 break;

327

328

329 if (KeepEmpty || Idx > 0)

331

332

334 }

335

336

337 if (KeepEmpty || !S.empty())

338 A.push_back(S);

339}

340

342 int MaxSplit, bool KeepEmpty) const {

344

345

346

347

348

349 while (MaxSplit-- != 0) {

350 size_t Idx = S.find(Separator);

352 break;

353

354

355 if (KeepEmpty || Idx > 0)

357

358

360 }

361

362

363 if (KeepEmpty || !S.empty())

364 A.push_back(S);

365}

366

367

368

369

370

371

372

374 size_t Count = 0;

375 size_t Pos = 0;

376 size_t N = Str.size();

377

378

379

380 if (N)

381 return 0;

382 while ((Pos = find(Str, Pos)) != npos) {

383 ++Count;

384 Pos += N;

385 }

386 return Count;

387}

388

390 if (Str.empty())

391 return 10;

392

393 if (Str.consume_front_insensitive("0x"))

394 return 16;

395

396 if (Str.consume_front_insensitive("0b"))

397 return 2;

398

399 if (Str.consume_front("0o"))

400 return 8;

401

402 if (Str[0] == '0' && Str.size() > 1 && isDigit(Str[1])) {

403 Str = Str.substr(1);

404 return 8;

405 }

406

407 return 10;

408}

409

411 unsigned long long &Result) {

412

413 if (Radix == 0)

415

416

417 if (Str.empty()) return true;

418

419

421 Result = 0;

422 while (!Str2.empty()) {

423 unsigned CharVal;

424 if (Str2[0] >= '0' && Str2[0] <= '9')

425 CharVal = Str2[0] - '0';

426 else if (Str2[0] >= 'a' && Str2[0] <= 'z')

427 CharVal = Str2[0] - 'a' + 10;

428 else if (Str2[0] >= 'A' && Str2[0] <= 'Z')

429 CharVal = Str2[0] - 'A' + 10;

430 else

431 break;

432

433

434

435 if (CharVal >= Radix)

436 break;

437

438

439 unsigned long long PrevResult = Result;

440 Result = Result * Radix + CharVal;

441

442

443 if (Result / Radix < PrevResult)

444 return true;

445

446 Str2 = Str2.substr(1);

447 }

448

449

450

451 if (Str.size() == Str2.size())

452 return true;

453

454 Str = Str2;

455 return false;

456}

457

459 long long &Result) {

460 unsigned long long ULLVal;

461

462

463 if (!Str.starts_with("-")) {

465

466 (long long)ULLVal < 0)

467 return true;

468 Result = ULLVal;

469 return false;

470 }

471

472

473 StringRef Str2 = Str.drop_front(1);

475

476

477

478 (long long)-ULLVal > 0)

479 return true;

480

481 Str = Str2;

482 Result = -ULLVal;

483 return false;

484}

485

486

487

489 unsigned long long &Result) {

491 return true;

492

493

494

495 return !Str.empty();

496}

497

499 long long &Result) {

501 return true;

502

503

504

505 return !Str.empty();

506}

507

510

511

512 if (Radix == 0)

514

515 assert(Radix > 1 && Radix <= 36);

516

517

518 if (Str.empty()) return true;

519

520

521

522 Str = Str.ltrim('0');

523

524

525 if (Str.empty()) {

526 Result = APInt(64, 0);

527 *this = Str;

528 return false;

529 }

530

531

532 unsigned Log2Radix = 0;

533 while ((1U << Log2Radix) < Radix) Log2Radix++;

534 bool IsPowerOf2Radix = ((1U << Log2Radix) == Radix);

535

536 unsigned BitWidth = Log2Radix * Str.size();

537 if (BitWidth < Result.getBitWidth())

538 BitWidth = Result.getBitWidth();

539 else if (BitWidth > Result.getBitWidth())

540 Result = Result.zext(BitWidth);

541

542 APInt RadixAP, CharAP;

543 if (!IsPowerOf2Radix) {

544

547 }

548

549

550 Result = 0;

551 while (!Str.empty()) {

552 unsigned CharVal;

553 if (Str[0] >= '0' && Str[0] <= '9')

554 CharVal = Str[0]-'0';

555 else if (Str[0] >= 'a' && Str[0] <= 'z')

556 CharVal = Str[0]-'a'+10;

557 else if (Str[0] >= 'A' && Str[0] <= 'Z')

558 CharVal = Str[0]-'A'+10;

559 else

560 break;

561

562

563

564 if (CharVal >= Radix)

565 break;

566

567

568 if (IsPowerOf2Radix) {

569 Result <<= Log2Radix;

570 Result |= CharVal;

571 } else {

572 Result *= RadixAP;

573 CharAP = CharVal;

574 Result += CharAP;

575 }

576

577 Str = Str.substr(1);

578 }

579

580

581

582 if (size() == Str.size())

583 return true;

584

585 *this = Str;

586 return false;

587}

588

591 if (Str.consumeInteger(Radix, Result))

592 return true;

593

594

595

596 return !Str.empty();

597}

598

602 if (errorToBool(StatusOrErr.takeError()))

603 return true;

604

608 return true;

609 }

610

611 Result = F.convertToDouble();

612 return false;

613}

614

615

618}

619

621 assert(Val.data() != getEmptyKey().data() &&

622 "Cannot hash the empty key!");

623 assert(Val.data() != getTombstoneKey().data() &&

624 "Cannot hash the tombstone key!");

626}

This file declares a class to represent arbitrary precision floating point values and provide a varie...

This file implements a class to represent arbitrary precision integral constant values and operations...

BlockVerifier::State From

static GCRegistry::Add< ErlangGC > A("erlang", "erlang-compatible garbage collector")

static GCRegistry::Add< StatepointGC > D("statepoint-example", "an example strategy for statepoint")

#define LLVM_UNLIKELY(EXPR)

Returns the sub type a function will return at a given Idx Should correspond to the result type of an ExtractValue instruction executed with just that one unsigned Idx

static bool isDigit(const char C)

assert(ImpDefSCC.getReg()==AMDGPU::SCC &&ImpDefSCC.isDef())

static unsigned GetAutoSenseRadix(StringRef &Str)

static int ascii_strncasecmp(const char *LHS, const char *RHS, size_t Length)

Class for arbitrary precision integers.

ArrayRef - Represent a constant reference to an array (0 or more elements consecutively in memory),...

This class consists of common code factored out of the SmallVector class to reduce code duplication b...

StringRef - Represent a constant reference to a string, i.e.

std::pair< StringRef, StringRef > split(char Separator) const

Split into two substrings around the first occurrence of a separator character.

size_t find_last_not_of(char C, size_t From=npos) const

Find the last character in the string that is not C, or npos if not found.

bool consumeInteger(unsigned Radix, T &Result)

Parse the current string as an integer of the specified radix.

bool getAsInteger(unsigned Radix, T &Result) const

Parse the current string as an integer of the specified radix.

bool getAsDouble(double &Result, bool AllowInexact=true) const

Parse the current string as an IEEE double-precision floating point value.

size_t find_if(function_ref< bool(char)> F, size_t From=0) const

Search for the first character satisfying the predicate F.

constexpr StringRef substr(size_t Start, size_t N=npos) const

Return a reference to the substring from [Start, Start + N).

constexpr bool empty() const

empty - Check if the string is empty.

bool starts_with_insensitive(StringRef Prefix) const

Check if this string starts with the given Prefix, ignoring case.

std::string upper() const

Convert the given ASCII string to uppercase.

unsigned edit_distance(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const

Determine the edit distance between this string and another string.

StringRef slice(size_t Start, size_t End) const

Return a reference to the substring from [Start, End).

constexpr size_t size() const

size - Get the string size.

size_t find_last_of(char C, size_t From=npos) const

Find the last character in the string that is C, or npos if not found.

constexpr const char * data() const

data - Get a pointer to the start of the string (which may not be null terminated).

int compare_numeric(StringRef RHS) const

compare_numeric - Compare two strings, treating sequences of digits as numbers.

size_t find_first_of(char C, size_t From=0) const

Find the first character in the string that is C, or npos if not found.

size_t rfind(char C, size_t From=npos) const

Search for the last character C in the string.

size_t find(char C, size_t From=0) const

Search for the first character C in the string.

size_t rfind_insensitive(char C, size_t From=npos) const

Search for the last character C in the string, ignoring case.

std::string lower() const

size_t find_insensitive(char C, size_t From=0) const

Search for the first character C in the string, ignoring case.

size_t count(char C) const

Return the number of occurrences of C in the string.

static constexpr size_t npos

bool equals_insensitive(StringRef RHS) const

Check for string equality, ignoring case.

bool ends_with_insensitive(StringRef Suffix) const

Check if this string ends with the given Suffix, ignoring case.

size_t find_first_not_of(char C, size_t From=0) const

Find the first character in the string that is not C or npos if not found.

int compare_insensitive(StringRef RHS) const

Compare two strings, ignoring case.

unsigned edit_distance_insensitive(StringRef Other, bool AllowReplacements=true, unsigned MaxEditDistance=0) const

An opaque object representing a hash code.

This file defines a Levenshtein distance function that works for any two sequences,...

@ C

The default llvm calling convention, compatible with C.

This is an optimization pass for GlobalISel generic memory operations.

bool errorToBool(Error Err)

Helper for converting an Error to a bool.

bool getAsSignedInteger(StringRef Str, unsigned Radix, long long &Result)

hash_code hash_value(const FixedPointSemantics &Val)

mapped_iterator< ItTy, FuncTy > map_iterator(ItTy I, FuncTy F)

bool consumeUnsignedInteger(StringRef &Str, unsigned Radix, unsigned long long &Result)

unsigned ComputeEditDistance(ArrayRef< T > FromArray, ArrayRef< T > ToArray, bool AllowReplacements=true, unsigned MaxEditDistance=0)

bool consumeSignedInteger(StringRef &Str, unsigned Radix, long long &Result)

constexpr unsigned BitWidth

bool getAsUnsignedInteger(StringRef Str, unsigned Radix, unsigned long long &Result)

Helper functions for StringRef::getAsInteger.

unsigned ComputeMappedEditDistance(ArrayRef< T > FromArray, ArrayRef< T > ToArray, Functor Map, bool AllowReplacements=true, unsigned MaxEditDistance=0)

Determine the edit distance between two sequences.

hash_code hash_combine_range(InputIteratorT first, InputIteratorT last)

Compute a hash_code for a sequence of values.

static constexpr roundingMode rmNearestTiesToEven

opStatus

IEEE-754R 7: Default exception handling.

An information struct used to provide DenseMap with the various necessary components for a given valu...