LLVM: include/llvm/ADT/StringExtras.h Source File (original) (raw)

1

2

3

4

5

6

7

8

9

10

11

12

13

14#ifndef LLVM_ADT_STRINGEXTRAS_H

15#define LLVM_ADT_STRINGEXTRAS_H

16

23#include

24#include

25#include

26#include

27#include

28#include

29#include

30#include

31

32namespace llvm {

33

35

36

37

38inline char hexdigit(unsigned X, bool LowerCase = false) {

40 static const char LUT[] = "0123456789ABCDEF";

43}

44

45

46

47

48inline std::vector toStringRefArray(const char *const *Strings) {

49 std::vector Result;

50 while (*Strings)

51 Result.push_back(*Strings++);

52 return Result;

53}

54

55

57

58

65

66

67template

69 static_assert(std::is_same<CharT, char>::value ||

70 std::is_same<CharT, unsigned char>::value ||

71 std::is_same<CharT, signed char>::value,

72 "Expected byte type");

75}

76

77

78

79

80

82

83 static const int16_t LUT[256] = {

84 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

85 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

86 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

87 0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1,

88 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,

89 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

90 -1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1,

91 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

92 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

93 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

94 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

95 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

96 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

97 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

98 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

99 -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,

100 };

101

102 return LUT[static_cast<unsigned char>(C)];

103}

104

105

106inline bool isDigit(char C) { return C >= '0' && C <= '9'; }

107

108

110

111

112inline bool isLower(char C) { return 'a' <= C && C <= 'z'; }

113

114

115inline bool isUpper(char C) { return 'A' <= C && C <= 'Z'; }

116

117

119

120

121

123

124

125inline bool isASCII(char C) { return static_cast<unsigned char>(C) <= 127; }

126

127

129 for (char C : S)

131 return false;

132 return true;

133}

134

135

136

137

138

140 unsigned char UC = static_cast<unsigned char>(C);

141 return (0x20 <= UC) && (UC <= 0x7E);

142}

143

144

145

146

147

148

151 R"(!"#$%&'()*+,-./:;<=>?@[\]^_`{|}~)";

153}

154

155

156

157

159 return C == ' ' || C == '\f' || C == '\n' || C == '\r' || C == '\t' ||

160 C == '\v';

161}

162

163

166 return x - 'A' + 'a';

167 return x;

168}

169

170

173 return x - 'a' + 'A';

174 return x;

175}

176

178 unsigned Width = 0) {

179 char Buffer[17];

180 char *BufPtr = std::end(Buffer);

181

182 if (X == 0 && !Width)

183 *--BufPtr = '0';

184

185 for (unsigned i = 0; Width ? (i < Width) : X; ++i) {

186 unsigned char Mod = static_cast<unsigned char>(X) & 15;

188 X >>= 4;

189 }

190

191 return std::string(BufPtr, std::end(Buffer));

192}

193

194

195

200

201 for (size_t i = 0; i < Length; i++) {

203 Output[i * 2 ] = hexdigit(c >> 4, LowerCase);

204 Output[i * 2 + 1] = hexdigit(c & 15, LowerCase);

205 }

206}

207

211 return std::string(Output);

212}

213

217

218

219

220

221

225 if (U1 == ~0U || U2 == ~0U)

226 return false;

227

228 Hex = static_cast<uint8_t>((U1 << 4) | U2);

229 return true;

230}

231

232

233

237 (void)GotHex;

238 assert(GotHex && "MSB and/or LSB do not correspond to hex digits");

239 return Hex;

240}

241

242

243

244

245

247 if (Input.empty())

248 return true;

249

250

251

252 Output.resize((Input.size() + 1) / 2);

253 char *OutputPtr = const_cast<char *>(Output.data());

254 if (Input.size() % 2 == 1) {

257 return false;

258 *OutputPtr++ = Hex;

260 }

261

262

263

264

265 size_t InputSize = Input.size();

266 assert(InputSize % 2 == 0);

267 const char *InputPtr = Input.data();

268 for (size_t OutputIndex = 0; OutputIndex < InputSize / 2; ++OutputIndex) {

271 InputPtr[OutputIndex * 2 + 1],

272 Hex))

273 return false;

274 OutputPtr[OutputIndex] = Hex;

275 }

276 return true;

277}

278

279

280

282 std::string Hex;

284 (void)GotHex;

285 assert(GotHex && "Input contains non hex digits");

286 return Hex;

287}

288

289

290

291

295

297template

298inline bool to_float(const Twine &T, N &Num, N (*StrTo)(const char *, char **)) {

300 StringRef S = T.toNullTerminatedStringRef(Storage);

301 char *End;

302 N Temp = StrTo(S.data(), &End);

303 if (*End != '\0')

304 return false;

305 Num = Temp;

306 return true;

307}

308}

309

313

317

321

323 char Buffer[21];

324 char *BufPtr = std::end(Buffer);

325

326 if (X == 0) *--BufPtr = '0';

327

328 while (X) {

329 *--BufPtr = '0' + char(X % 10);

330 X /= 10;

331 }

332

333 if (isNeg) *--BufPtr = '-';

334 return std::string(BufPtr, std::end(Buffer));

335}

336

337inline std::string itostr(int64_t X) {

338 if (X < 0)

340 else

342}

343

345 bool formatAsCLiteral = false,

346 bool UpperCase = true,

347 bool InsertSeparators = false) {

349 I.toString(S, Radix, Signed, formatAsCLiteral, UpperCase, InsertSeparators);

350 return std::string(S);

351}

352

356

357

358

359

361

362

363

364

365

366

367

368LLVM_ABI std::pair<StringRef, StringRef>

369getToken(StringRef Source, StringRef Delimiters = " \t\n\v\f\r");

370

371

372

374 SmallVectorImpl &OutFragments,

375 StringRef Delimiters = " \t\n\v\f\r");

376

377

379

380

381 switch (Val % 100) {

382 case 11:

383 case 12:

384 case 13:

385 return "th";

386 default:

387 switch (Val % 10) {

388 case 1: return "st";

389 case 2: return "nd";

390 case 3: return "rd";

391 default: return "th";

392 }

393 }

394}

395

396

397

399

400

401

403

404

406

407

408

409

411

412

413

414

415

417 bool capitalizeFirst = false);

418

420

421template

422inline std::string join_impl(IteratorT Begin, IteratorT End,

423 StringRef Separator, std::input_iterator_tag) {

424 std::string S;

425 if (Begin == End)

426 return S;

427

428 S += (*Begin);

429 while (++Begin != End) {

430 S += Separator;

431 S += (*Begin);

432 }

433 return S;

434}

435

436template

437inline std::string join_impl(IteratorT Begin, IteratorT End,

438 StringRef Separator, std::forward_iterator_tag) {

439 std::string S;

440 if (Begin == End)

441 return S;

442

443 size_t Len = (std::distance(Begin, End) - 1) * Separator.size();

444 for (IteratorT I = Begin; I != End; ++I)

446 S.reserve(Len);

447 size_t PrevCapacity = S.capacity();

448 (void)PrevCapacity;

449 S += (*Begin);

450 while (++Begin != End) {

451 S += Separator;

452 S += (*Begin);

453 }

454 assert(PrevCapacity == S.capacity() && "String grew during building");

455 return S;

456}

457

458template

460

461template <typename Sep, typename Arg>

463 const Arg &Item) {

464 Result += Item;

465}

466

467template <typename Sep, typename Arg1, typename... Args>

468inline void join_items_impl(std::string &Result, Sep Separator, const Arg1 &A1,

469 Args &&... Items) {

470 Result += A1;

471 Result += Separator;

472 join_items_impl(Result, Separator, std::forward(Items)...);

473}

474

477

479 return Str.size();

480}

481

485

486}

487

488

489

490template

491inline std::string join(IteratorT Begin, IteratorT End, StringRef Separator) {

492 using tag = typename std::iterator_traits::iterator_category;

494}

495

496

497

498template

500 return join(R.begin(), R.end(), Separator);

501}

502

503

504

505

506

507template <typename Sep, typename... Args>

508inline std::string join_items(Sep Separator, Args &&... Items) {

509 std::string Result;

510 if (sizeof...(Items) == 0)

511 return Result;

512

515 Result.reserve(NI + (sizeof...(Items) - 1) * NS + 1);

517 return Result;

518}

519

520

521

522

523

524

525

526

527

528

530 bool First = true;

533

534public:

536 : Separator(Separator), Prefix(Prefix) {}

538 if (First) {

539 First = false;

540 return Prefix;

541 }

542 return Separator;

543 }

544};

545

546

549 StringRef> {

550 char SeparatorStorage;

554

555public:

557 : Next(Str), Separator(Separator) {

558 ++*this;

559 }

560

562 : SeparatorStorage(Separator), Next(Str),

563 Separator(&SeparatorStorage, 1) {

564 ++*this;

565 }

566

568 : SeparatorStorage(R.SeparatorStorage), Current(R.Current), Next(R.Next),

569 Separator(R.Separator) {

570 if (R.Separator.data() == &R.SeparatorStorage)

571 Separator = StringRef(&SeparatorStorage, 1);

572 }

573

575 if (this == &R)

576 return *this;

577

578 SeparatorStorage = R.SeparatorStorage;

579 Current = R.Current;

580 Next = R.Next;

581 Separator = R.Separator;

582 if (R.Separator.data() == &R.SeparatorStorage)

583 Separator = StringRef(&SeparatorStorage, 1);

584 return *this;

585 }

586

588 assert(Separator == R.Separator);

589 return Current.data() == R.Current.data();

590 }

591

593

595

597 std::tie(Current, Next) = Next.split(Separator);

598 return *this;

599 }

600};

601

602

603

604

605

606

607

608

609

610

611

612

617

622

623}

624

625#endif

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

This file implements the APSInt class, which is a simple class that represents an arbitrary sized int...

static GCRegistry::Add< OcamlGC > B("ocaml", "ocaml 3.10-compatible GC")

#define LLVM_UNLIKELY(EXPR)

static bool isNeg(Value *V)

Returns true if the operation is a negation of V, and it works for both integers and floats.

ConstantRange Range(APInt(BitWidth, Low), APInt(BitWidth, High))

This file defines the SmallString class.

static TableGen::Emitter::OptClass< SkeletonEmitter > X("gen-skeleton-class", "Generate example skeleton class")

The Input class is used to parse a yaml document into in-memory structs and vectors.

Class for arbitrary precision integers.

An arbitrary precision integer that knows its signedness.

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

ListSeparator(StringRef Separator=", ", StringRef Prefix="")

Definition StringExtras.h:535

SmallString - A SmallString is just a SmallVector with methods and accessors that make it work better...

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

void resize_for_overwrite(size_type N)

Like resize, but T is POD, the new values won't be initialized.

A forward iterator over partitions of string over a separator.

Definition StringExtras.h:549

const StringRef & operator*() const

Definition StringExtras.h:592

SplittingIterator(StringRef Str, char Separator)

Definition StringExtras.h:561

StringRef & operator*()

Definition StringExtras.h:594

SplittingIterator(StringRef Str, StringRef Separator)

Definition StringExtras.h:556

bool operator==(const SplittingIterator &R) const

Definition StringExtras.h:587

SplittingIterator & operator=(const SplittingIterator &R)

Definition StringExtras.h:574

SplittingIterator & operator++()

Definition StringExtras.h:596

SplittingIterator(const SplittingIterator &R)

Definition StringExtras.h:567

A wrapper around a string literal that serves as a proxy for constructing global tables of StringRefs...

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

bool getAsInteger(unsigned Radix, T &Result) const

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

constexpr size_t size() const

size - Get the string size.

constexpr const char * data() const

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

bool contains(StringRef Other) const

Return true if the given string is a substring of *this, and false otherwise.

Twine - A lightweight data structure for efficiently representing the concatenation of temporary valu...

CRTP base class which implements the entire standard iterator facade in terms of a minimal subset of ...

A range adaptor for a pair of iterators.

This class implements an extremely fast bulk output stream that can only output to a stream.

@ C

The default llvm calling convention, compatible with C.

bool to_float(const Twine &T, N &Num, N(*StrTo)(const char *, char **))

Definition StringExtras.h:298

std::string join_impl(IteratorT Begin, IteratorT End, StringRef Separator, std::input_iterator_tag)

Definition StringExtras.h:422

size_t join_one_item_size(char)

Definition StringExtras.h:475

size_t join_items_size(Args &&...Items)

Definition StringExtras.h:482

void join_items_impl(std::string &Result, Sep Separator)

Definition StringExtras.h:459

This is an optimization pass for GlobalISel generic memory operations.

LLVM_ABI StringRef::size_type StrInStrNoCase(StringRef s1, StringRef s2)

StrInStrNoCase - Portable version of strcasestr. Locates the first occurrence of string 's1' in strin...

char toLower(char x)

Returns the corresponding lowercase character if x is uppercase.

Definition StringExtras.h:164

std::string fromHex(StringRef Input)

Convert hexadecimal string Input to its binary representation. The return string is half the size of ...

Definition StringExtras.h:281

LLVM_ABI void printHTMLEscaped(StringRef String, raw_ostream &Out)

Print each character of the specified string, escaping HTML special characters.

ArrayRef< CharT > arrayRefFromStringRef(StringRef Input)

Construct a string ref from an array ref of unsigned chars.

Definition StringExtras.h:68

LLVM_ABI void printLowerCase(StringRef String, raw_ostream &Out)

printLowerCase - Print each character as lowercase if it is uppercase.

LLVM_ABI std::pair< StringRef, StringRef > getToken(StringRef Source, StringRef Delimiters=" \t\n\v\f\r")

getToken - This function extracts one token from source, ignoring any leading characters that appear ...

unsigned hexDigitValue(char C)

Interpret the given character C as a hexadecimal digit and return its value.

Definition StringExtras.h:81

bool isLower(char C)

Checks if character C is a lowercase letter as classified by "C" locale.

Definition StringExtras.h:112

bool tryGetFromHex(StringRef Input, std::string &Output)

Convert hexadecimal string Input to its binary representation and store the result in Output....

Definition StringExtras.h:246

std::string utohexstr(uint64_t X, bool LowerCase=false, unsigned Width=0)

Definition StringExtras.h:177

std::string utostr(uint64_t X, bool isNeg=false)

Definition StringExtras.h:322

LLVM_ABI void printEscapedString(StringRef Name, raw_ostream &Out)

Print each character of the specified string, escaping it if it is not printable or if it is an escap...

LLVM_ABI void SplitString(StringRef Source, SmallVectorImpl< StringRef > &OutFragments, StringRef Delimiters=" \t\n\v\f\r")

SplitString - Split up the specified string according to the specified delimiters,...

bool to_float(const Twine &T, float &Num)

Definition StringExtras.h:310

bool isAlpha(char C)

Checks if character C is a valid letter as classified by "C" locale.

Definition StringExtras.h:118

bool tryGetHexFromNibbles(char MSB, char LSB, uint8_t &Hex)

Store the binary representation of the two provided values, MSB and LSB, that make up the nibbles of ...

Definition StringExtras.h:222

LLVM_ABI std::string convertToSnakeFromCamelCase(StringRef input)

Converts a string from camel-case to snake-case by replacing all uppercase letters with '_' followed ...

bool isUpper(char C)

Checks if character C is a uppercase letter as classified by "C" locale.

Definition StringExtras.h:115

uint8_t hexFromNibbles(char MSB, char LSB)

Return the binary representation of the two provided values, MSB and LSB, that make up the nibbles of...

Definition StringExtras.h:234

char hexdigit(unsigned X, bool LowerCase=false)

hexdigit - Return the hexadecimal character for the given number X (which should be less than 16).

Definition StringExtras.h:38

bool isDigit(char C)

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

Definition StringExtras.h:106

bool isAlnum(char C)

Checks whether character C is either a decimal digit or an uppercase or lowercase letter as classifie...

Definition StringExtras.h:122

iterator_range< SplittingIterator > split(StringRef Str, StringRef Separator)

Split the specified string over a separator and return a range-compatible iterable over its partition...

Definition StringExtras.h:613

@ Mod

The access may modify the value stored in memory.

std::string join(IteratorT Begin, IteratorT End, StringRef Separator)

Joins the strings in the range [Begin, End), adding Separator between the elements.

Definition StringExtras.h:491

char toUpper(char x)

Returns the corresponding uppercase character if x is lowercase.

Definition StringExtras.h:171

StringRef getOrdinalSuffix(unsigned Val)

Returns the English suffix for an ordinal integer (-st, -nd, -rd, -th).

Definition StringExtras.h:378

std::vector< StringRef > toStringRefArray(const char *const *Strings)

Given an array of c-style strings terminated by a null pointer, construct a vector of StringRefs repr...

Definition StringExtras.h:48

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

std::string toString(const APInt &I, unsigned Radix, bool Signed, bool formatAsCLiteral=false, bool UpperCase=true, bool InsertSeparators=false)

Definition StringExtras.h:344

LLVM_ABI std::string convertToCamelFromSnakeCase(StringRef input, bool capitalizeFirst=false)

Converts a string from snake-case to camel-case by replacing all occurrences of '_' followed by a low...

void toHex(ArrayRef< uint8_t > Input, bool LowerCase, SmallVectorImpl< char > &Output)

Convert buffer Input to its hexadecimal representation. The returned string is double the size of Inp...

Definition StringExtras.h:196

std::string join_items(Sep Separator, Args &&... Items)

Joins the strings in the parameter pack Items, adding Separator between the elements....

Definition StringExtras.h:508

bool isASCII(char C)

Checks whether character C is valid ASCII (high bit is zero).

Definition StringExtras.h:125

bool isPrint(char C)

Checks whether character C is printable.

Definition StringExtras.h:139

bool isSpace(char C)

Checks whether character C is whitespace in the "C" locale.

Definition StringExtras.h:158

bool isHexDigit(char C)

Checks if character C is a hexadecimal numeric character.

Definition StringExtras.h:109

bool to_integer(StringRef S, N &Num, unsigned Base=0)

Convert the string S to an integer of the specified type using the radix Base. If Base is 0,...

Definition StringExtras.h:292

bool isPunct(char C)

Checks whether character C is a punctuation character.

Definition StringExtras.h:149

StringRef toStringRef(bool B)

Construct a string ref from a boolean.

Definition StringExtras.h:56

std::string itostr(int64_t X)

Definition StringExtras.h:337