Skip to content
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
34 changes: 27 additions & 7 deletions llvm/lib/Support/Mustache.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -34,6 +34,32 @@ static bool isContextFalsey(const json::Value *V) {
return isFalsey(*V);
}

static void splitAndTrim(StringRef Str, SmallVectorImpl<StringRef> &Tokens) {
size_t CurrentPos = 0;
while (CurrentPos < Str.size()) {
// Find the next delimiter.
size_t DelimiterPos = Str.find('.', CurrentPos);

// If no delimiter is found, process the rest of the string.
if (DelimiterPos == StringRef::npos) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

nit: can omit the curly braces here

DelimiterPos = Str.size();
}

// Get the current part, which may have whitespace.
StringRef Part = Str.slice(CurrentPos, DelimiterPos);

// Manually trim the part without creating a new string object.
size_t Start = Part.find_first_not_of(" \t\r\n");
if (Start != StringRef::npos) {
size_t End = Part.find_last_not_of(" \t\r\n");
Tokens.push_back(Part.slice(Start, End + 1));
}

// Move past the delimiter for the next iteration.
CurrentPos = DelimiterPos + 1;
}
}

static Accessor splitMustacheString(StringRef Str, MustacheContext &Ctx) {
// We split the mustache string into an accessor.
// For example:
Expand All @@ -46,13 +72,7 @@ static Accessor splitMustacheString(StringRef Str, MustacheContext &Ctx) {
// It's a literal, so it doesn't need to be saved.
Tokens.push_back(".");
} else {
while (!Str.empty()) {
StringRef Part;
std::tie(Part, Str) = Str.split('.');
// Each part of the accessor needs to be saved to the arena
// to ensure it has a stable address.
Tokens.push_back(Part.trim());
}
splitAndTrim(Str, Tokens);
}
// Now, allocate memory for the array of StringRefs in the arena.
StringRef *ArenaTokens = Ctx.Allocator.Allocate<StringRef>(Tokens.size());
Expand Down
Loading