Skip to content

Commit 29e2966

Browse files
committed
[ntuple] improve whitespace removal in type name normalization
1 parent 3c71f5e commit 29e2966

File tree

1 file changed

+31
-52
lines changed

1 file changed

+31
-52
lines changed

tree/ntuple/src/RFieldUtils.cxx

Lines changed: 31 additions & 52 deletions
Original file line numberDiff line numberDiff line change
@@ -119,62 +119,41 @@ std::vector<AnglePos> FindTemplateAngleBrackets(const std::string &typeName)
119119
}
120120

121121
// TClassEdit::CleanType and the name demangling insert blanks between closing angle brackets,
122-
// as they were required before C++11. We want to remove them for RNTuple.
123-
void RemoveSpaceBeforeClosingAngleBracket(std::string &typeName)
122+
// as they were required before C++11. Name demangling introduces a blank before array dimensions,
123+
// which should also be removed.
124+
void RemoveSpaceBefore(std::string &typeName, char beforeChar)
124125
{
125-
auto angle = typeName.find('<');
126-
if (angle != std::string::npos) {
127-
auto dst = typeName.begin() + angle;
128-
auto end = typeName.end();
129-
for (auto src = dst; src != end; ++src) {
130-
if (*src == ' ') {
131-
auto next = src + 1;
132-
if (next != end && *next == '>') {
133-
// Skip this space before a closing angle bracket.
134-
continue;
135-
}
126+
auto dst = typeName.begin();
127+
auto end = typeName.end();
128+
for (auto src = dst; src != end; ++src) {
129+
if (*src == ' ') {
130+
auto next = src + 1;
131+
if (next != end && *next == beforeChar) {
132+
// Skip this space before a closing angle bracket.
133+
continue;
136134
}
137-
*(dst++) = *src;
138135
}
139-
typeName.erase(dst, end);
136+
*(dst++) = *src;
140137
}
138+
typeName.erase(dst, end);
141139
}
142140

143141
// The demangled name adds spaces after commas
144-
void RemoveSpaceAfterComma(std::string &typeName)
142+
void RemoveSpaceAfter(std::string &typeName, char afterChar)
145143
{
146-
auto itr = typeName.begin();
147-
while (itr != typeName.end()) {
148-
auto c = *itr;
149-
itr++;
150-
151-
if (c != ',')
152-
continue;
153-
154-
R__ASSERT(itr != typeName.end());
155-
if (*itr == ' ') {
156-
itr = typeName.erase(itr);
157-
}
158-
}
159-
}
160-
161-
// The demangled name adds a space before array dimensions
162-
void RemoveSpaceBeforeBracket(std::string &typeName)
163-
{
164-
auto itr = typeName.begin();
165-
while (itr != typeName.end()) {
166-
auto last = itr;
167-
itr++;
168-
169-
if (*last != ' ')
170-
continue;
171-
172-
R__ASSERT(itr != typeName.end());
173-
if (*itr == '[') {
174-
itr = typeName.erase(last);
175-
itr++;
144+
auto dst = typeName.begin();
145+
auto end = typeName.end();
146+
for (auto src = dst; src != end; ++src) {
147+
*(dst++) = *src;
148+
if (*src == afterChar) {
149+
auto next = src + 1;
150+
if (next != end && *next == ' ') {
151+
// Skip this space before a closing angle bracket.
152+
++src;
153+
}
176154
}
177155
}
156+
typeName.erase(dst, end);
178157
}
179158

180159
// Map fundamental integer types to stdint integer types (e.g. int --> std::int32_t)
@@ -214,8 +193,8 @@ std::string GetDemangledTrimmedType(const std::type_info &ti)
214193
std::string result{str};
215194
free(str);
216195

217-
RemoveSpaceBeforeClosingAngleBracket(result);
218-
RemoveSpaceAfterComma(result);
196+
RemoveSpaceBefore(result, '>');
197+
RemoveSpaceAfter(result, ',');
219198

220199
return result;
221200
}
@@ -331,16 +310,16 @@ std::string GetRenormalizedDemangledTypeName(const std::string &demangledName)
331310
using ROOT::Internal::ParseArrayType;
332311

333312
std::string tn{demangledName};
334-
RemoveSpaceBeforeBracket(tn);
313+
RemoveSpaceBefore(tn, '[');
335314
auto [canonicalTypePrefix, dimensions] = ParseArrayType(tn);
336315
MapIntegerType(canonicalTypePrefix);
337316

338317
if (canonicalTypePrefix.find('<') == std::string::npos) {
339318
// If there are no templates, the function is done.
340319
return GetStandardArrayType(canonicalTypePrefix, dimensions);
341320
}
342-
RemoveSpaceBeforeClosingAngleBracket(canonicalTypePrefix);
343-
RemoveSpaceAfterComma(canonicalTypePrefix);
321+
RemoveSpaceBefore(canonicalTypePrefix, '>');
322+
RemoveSpaceAfter(canonicalTypePrefix, ',');
344323

345324
// Remove optional stdlib template arguments
346325
int maxTemplateArgs = 0;
@@ -381,7 +360,7 @@ std::tuple<std::string, std::vector<std::size_t>> ROOT::Internal::GetCanonicalTy
381360
canonicalType.erase(0, 2);
382361
}
383362

384-
RemoveSpaceBeforeClosingAngleBracket(canonicalType);
363+
RemoveSpaceBefore(canonicalType, '>');
385364

386365
if (canonicalType.substr(0, 6) == "array<") {
387366
canonicalType = "std::" + canonicalType;

0 commit comments

Comments
 (0)