Skip to content

Commit 04ab647

Browse files
authored
[NFC][TableGen] Refactor StringToOffsetTable (#105655)
- Make `EmitString` const by not mutating `AggregateString`. - Use C++17 structured bindings in `GetOrAddStringOffset`. - Use StringExtras version of isDigit instead of std::isdigit.
1 parent 2051a7b commit 04ab647

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

llvm/include/llvm/TableGen/StringToOffsetTable.h

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -14,7 +14,6 @@
1414
#include "llvm/ADT/StringMap.h"
1515
#include "llvm/Support/FormatVariadic.h"
1616
#include "llvm/Support/raw_ostream.h"
17-
#include <cctype>
1817
#include <optional>
1918

2019
namespace llvm {
@@ -32,16 +31,15 @@ class StringToOffsetTable {
3231
size_t size() const { return AggregateString.size(); }
3332

3433
unsigned GetOrAddStringOffset(StringRef Str, bool appendZero = true) {
35-
auto IterBool =
36-
StringOffset.insert(std::make_pair(Str, AggregateString.size()));
37-
if (IterBool.second) {
34+
auto [II, Inserted] = StringOffset.insert({Str, size()});
35+
if (Inserted) {
3836
// Add the string to the aggregate if this is the first time found.
3937
AggregateString.append(Str.begin(), Str.end());
4038
if (appendZero)
4139
AggregateString += '\0';
4240
}
4341

44-
return IterBool.first->second;
42+
return II->second;
4543
}
4644

4745
// Returns the offset of `Str` in the table if its preset, else return
@@ -78,37 +76,35 @@ class StringToOffsetTable {
7876
}
7977

8078
// Emit the string as one single string.
81-
void EmitString(raw_ostream &O) {
79+
void EmitString(raw_ostream &O) const {
8280
// Escape the string.
83-
SmallString<256> Str;
84-
raw_svector_ostream(Str).write_escaped(AggregateString);
85-
AggregateString = std::string(Str);
81+
SmallString<256> EscapedStr;
82+
raw_svector_ostream(EscapedStr).write_escaped(AggregateString);
8683

8784
O << " \"";
8885
unsigned CharsPrinted = 0;
89-
for (unsigned i = 0, e = AggregateString.size(); i != e; ++i) {
86+
for (unsigned i = 0, e = EscapedStr.size(); i != e; ++i) {
9087
if (CharsPrinted > 70) {
9188
O << "\"\n \"";
9289
CharsPrinted = 0;
9390
}
94-
O << AggregateString[i];
91+
O << EscapedStr[i];
9592
++CharsPrinted;
9693

9794
// Print escape sequences all together.
98-
if (AggregateString[i] != '\\')
95+
if (EscapedStr[i] != '\\')
9996
continue;
10097

101-
assert(i + 1 < AggregateString.size() && "Incomplete escape sequence!");
102-
if (isdigit(AggregateString[i + 1])) {
103-
assert(isdigit(AggregateString[i + 2]) &&
104-
isdigit(AggregateString[i + 3]) &&
98+
assert(i + 1 < EscapedStr.size() && "Incomplete escape sequence!");
99+
if (isDigit(EscapedStr[i + 1])) {
100+
assert(isDigit(EscapedStr[i + 2]) && isDigit(EscapedStr[i + 3]) &&
105101
"Expected 3 digit octal escape!");
106-
O << AggregateString[++i];
107-
O << AggregateString[++i];
108-
O << AggregateString[++i];
102+
O << EscapedStr[++i];
103+
O << EscapedStr[++i];
104+
O << EscapedStr[++i];
109105
CharsPrinted += 3;
110106
} else {
111-
O << AggregateString[++i];
107+
O << EscapedStr[++i];
112108
++CharsPrinted;
113109
}
114110
}

0 commit comments

Comments
 (0)