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

24 unsigned char LHC = toLower(LC);

25 unsigned char RHC = toLower(RC);

26 if (LHC != RHC)

27 return LHC < RHC ? -1 : 1;

28 }

29 return 0;

30}

31

33 size_t Min = std::min(size(), RHS.size());

35 return Res;

36 if (size() == RHS.size())

37 return 0;

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

39}

40

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

44}

45

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

49}

50

55

56

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

59

61

62

63 size_t J;

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

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

67 if (ld != rd)

68 return rd ? -1 : 1;

69 if (!rd)

70 break;

71 }

72

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

74 return Res < 0 ? -1 : 1;

75

76 I = J - 1;

77 continue;

78 }

79 if (data()[I] != RHS.data()[I])

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

81 }

82 if (size() == RHS.size())

83 return 0;

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

85}

86

87

89 bool AllowReplacements,

90 unsigned MaxEditDistance) const {

93 AllowReplacements, MaxEditDistance);

94}

95

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

100 llvm::toLower, AllowReplacements, MaxEditDistance);

101}

102

103

104

105

106

111

116

117

118

119

120

121

122

123

124

125

127 if (From > size())

129

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

132

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

134 size_t N = Str.size();

135 if (N == 0)

136 return From;

139 if (N == 1) {

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

141 return Ptr == nullptr ? npos : Ptr - data();

142 }

143

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

145

146 if (N == 2) {

147

148

149

150 do {

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

152 return Start - data();

153 ++Start;

154 } while (Start < Stop);

156 }

157

158

160 do {

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

162 return Start - data();

163 ++Start;

164 } while (Start < Stop);

166 }

167

168

169 uint8_t BadCharSkip[256];

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

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

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

173

174 do {

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

178 return Start - data();

179

180

181 Start += BadCharSkip[Last];

182 } while (Start < Stop);

183

185}

186

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

190 if (This.starts_with_insensitive(Str))

191 return From;

192 This = This.drop_front();

193 ++From;

194 }

196}

197

199 From = std::min(From, size());

200 size_t i = From;

201 while (i != 0) {

202 --i;

204 return i;

205 }

207}

208

209

210

211

212

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

215}

216

218 size_t N = Str.size();

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

222 --i;

224 return i;

225 }

227}

228

229

230

231

232

234 size_t From) const {

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

236 for (char C : Chars)

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

238

239 for (size_type i = std::min(From, size()), e = size(); i != e; ++i)

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

241 return i;

243}

244

245

246

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

249}

250

251

252

253

254

256 size_t From) const {

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

258 for (char C : Chars)

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

260

261 for (size_type i = std::min(From, size()), e = size(); i != e; ++i)

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

263 return i;

265}

266

267

268

269

270

272 size_t From) const {

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

274 for (char C : Chars)

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

276

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

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

279 return i;

281}

282

283

284

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

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

288 return i;

290}

291

292

293

294

295

297 size_t From) const {

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

299 for (char C : Chars)

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

301

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

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

304 return i;

306}

307

309 StringRef Separator, int MaxSplit,

310 bool KeepEmpty) const {

312

313

314

315

316

317 while (MaxSplit-- != 0) {

318 size_t Idx = S.find(Separator);

319 if (Idx == npos)

320 break;

321

322

323 if (KeepEmpty || Idx > 0)

324 A.push_back(S.slice(0, Idx));

325

326

327 S = S.substr(Idx + Separator.size());

328 }

329

330

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

332 A.push_back(S);

333}

334

336 int MaxSplit, bool KeepEmpty) const {

338

339

340

341

342

343 while (MaxSplit-- != 0) {

344 size_t Idx = S.find(Separator);

345 if (Idx == npos)

346 break;

347

348

349 if (KeepEmpty || Idx > 0)

350 A.push_back(S.slice(0, Idx));

351

352

353 S = S.substr(Idx + 1);

354 }

355

356

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

358 A.push_back(S);

359}

360

361

362

363

364

365

366

368 size_t Count = 0;

369 size_t Pos = 0;

370 size_t N = Str.size();

371

372

373

374 if (N)

375 return 0;

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

378 Pos += N;

379 }

381}

382

384 if (Str.empty())

385 return 10;

386

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

388 return 16;

389

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

391 return 2;

392

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

394 return 8;

395

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

397 Str = Str.substr(1);

398 return 8;

399 }

400

401 return 10;

402}

403

405 unsigned long long &Result) {

406

407 if (Radix == 0)

409

410

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

412

413

415 Result = 0;

416 while (!Str2.empty()) {

417 unsigned CharVal;

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

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

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

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

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

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

424 else

425 break;

426

427

428

429 if (CharVal >= Radix)

430 break;

431

432

433 unsigned long long PrevResult = Result;

434 Result = Result * Radix + CharVal;

435

436

437 if (Result / Radix < PrevResult)

438 return true;

439

440 Str2 = Str2.substr(1);

441 }

442

443

444

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

446 return true;

447

448 Str = Str2;

449 return false;

450}

451

453 long long &Result) {

454 unsigned long long ULLVal;

455

456

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

459

460 (long long)ULLVal < 0)

461 return true;

462 Result = ULLVal;

463 return false;

464 }

465

466

469

470

471

472 (long long)-ULLVal > 0)

473 return true;

474

475 Str = Str2;

476 Result = -ULLVal;

477 return false;

478}

479

480

481

483 unsigned long long &Result) {

485 return true;

486

487

488

489 return !Str.empty();

490}

491

493 long long &Result) {

495 return true;

496

497

498

499 return !Str.empty();

500}

501

504

505

506 if (Radix == 0)

508

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

510

511

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

513

514

515

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

517

518

519 if (Str.empty()) {

520 Result = APInt(64, 0);

521 *this = Str;

522 return false;

523 }

524

525

526 unsigned Log2Radix = 0;

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

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

529

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

531 if (BitWidth < Result.getBitWidth())

532 BitWidth = Result.getBitWidth();

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

534 Result = Result.zext(BitWidth);

535

536 APInt RadixAP, CharAP;

537 if (!IsPowerOf2Radix) {

538

541 }

542

543

544 Result = 0;

545 while (!Str.empty()) {

546 unsigned CharVal;

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

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

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

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

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

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

553 else

554 break;

555

556

557

558 if (CharVal >= Radix)

559 break;

560

561

562 if (IsPowerOf2Radix) {

563 Result <<= Log2Radix;

564 Result |= CharVal;

565 } else {

566 Result *= RadixAP;

567 CharAP = CharVal;

568 Result += CharAP;

569 }

570

571 Str = Str.substr(1);

572 }

573

574

575

576 if (size() == Str.size())

577 return true;

578

579 *this = Str;

580 return false;

581}

582

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

586 return true;

587

588

589

590 return !Str.empty();

591}

592

596 if (errorToBool(StatusOrErr.takeError()))

597 return true;

598

602 return true;

603 }

604

605 Result = F.convertToDouble();

606 return false;

607}

608

609

611

614 "Cannot hash the empty key!");

616 "Cannot hash the tombstone key!");

618}

assert(UImm &&(UImm !=~static_cast< T >(0)) &&"Invalid immediate!")

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...

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)

static int ascii_strncasecmp(StringRef LHS, StringRef RHS)

Definition StringRef.cpp:22

static constexpr roundingMode rmNearestTiesToEven

opStatus

IEEE-754R 7: Default exception handling.

Class for arbitrary precision integers.

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.

LLVM_ABI 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.

Definition StringRef.cpp:285

static constexpr size_t npos

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.

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

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

Definition StringRef.cpp:593

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.

LLVM_ABI bool starts_with_insensitive(StringRef Prefix) const

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

Definition StringRef.cpp:41

StringRef drop_front(size_t N=1) const

Return a StringRef equal to 'this' but with the first N elements dropped.

LLVM_ABI std::string upper() const

Convert the given ASCII string to uppercase.

Definition StringRef.cpp:112

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

Determine the edit distance between this string and another string.

Definition StringRef.cpp:88

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).

LLVM_ABI int compare_numeric(StringRef RHS) const

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

Definition StringRef.cpp:57

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.

StringRef()=default

Construct an empty string ref.

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

Search for the last character C in the string.

StringRef take_back(size_t N=1) const

Return a StringRef equal to 'this' but with only the last N elements remaining.

StringRef take_front(size_t N=1) const

Return a StringRef equal to 'this' but with only the first N elements remaining.

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

Search for the first character C in the string.

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

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

Definition StringRef.cpp:198

LLVM_ABI std::string lower() const

Definition StringRef.cpp:107

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

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

Definition StringRef.cpp:51

size_t count(char C) const

Return the number of occurrences of C in the string.

bool equals_insensitive(StringRef RHS) const

Check for string equality, ignoring case.

LLVM_ABI bool ends_with_insensitive(StringRef Suffix) const

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

Definition StringRef.cpp:46

LLVM_ABI 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.

Definition StringRef.cpp:247

LLVM_ABI int compare_insensitive(StringRef RHS) const

Compare two strings, ignoring case.

Definition StringRef.cpp:32

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

Definition StringRef.cpp:96

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.

char toLower(char x)

Returns the corresponding lowercase character if x is uppercase.

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

Definition StringRef.cpp:492

hash_code hash_value(const FixedPointSemantics &Val)

LLVM_ABI unsigned getAutoSenseRadix(StringRef &Str)

Definition StringRef.cpp:383

detail::zippy< detail::zip_first, T, U, Args... > zip_equal(T &&t, U &&u, Args &&...args)

zip iterator that assumes that all iteratees have the same length.

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

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

Definition StringRef.cpp:404

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

bool isDigit(char C)

Checks if character C is one of the 10 decimal digits.

FunctionAddr VTableAddr Count

char toUpper(char x)

Returns the corresponding uppercase character if x is lowercase.

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

Definition StringRef.cpp:452

ArrayRef(const T &OneElt) -> ArrayRef< T >

constexpr unsigned BitWidth

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

Helper functions for StringRef::getAsInteger.

Definition StringRef.cpp:482

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.

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