[Lldb-commits] [lldb] Colorize output when searching for symbols in lldb (PR #69422)
David Spickett via lldb-commits
lldb-commits at lists.llvm.org
Tue Nov 14 01:34:18 PST 2023
================
@@ -403,9 +404,41 @@ bool Address::GetDescription(Stream &s, Target &target,
return false;
}
+void Address::DumpName(Stream *strm, llvm::StringRef text,
+ const char *pattern) {
+ if (!pattern) {
+ strm->PutCString(text.data());
+ return;
+ }
+
+ llvm::Regex reg_pattern(pattern);
+ llvm::SmallVector<llvm::StringRef, 1> matches;
+ llvm::StringRef remaining = text;
+ std::string red_start =
+ lldb_private::ansi::FormatAnsiTerminalCodes("${ansi.fg.red}");
+ std::string reset_color =
+ lldb_private::ansi::FormatAnsiTerminalCodes("${ansi.normal}");
+
+ size_t last_pos = 0;
+ while (reg_pattern.match(remaining, &matches)) {
+ llvm::StringRef match = matches[0];
+ size_t match_start_pos = match.data() - text.data();
+ size_t match_end_pos = match_start_pos + match.size();
+
+ strm->Write(text.data() + last_pos, match_start_pos - last_pos);
+ strm->PutCString(red_start.c_str());
+ strm->Write(text.data() + match_start_pos, match.size());
+ strm->PutCString(reset_color.c_str());
+ last_pos = match_end_pos;
+ remaining = text.substr(last_pos);
----------------
DavidSpickett wrote:
It may be clearer to `drop_front` (or `substr`, it's the same thing but my functional bias prefers drop) from `remaining` as we go, instead of referring to `text` all the time.
So you print up to where the match is, drop_front however much that was, print the match, drop_front the size of the match, etc, etc.
That way the algorithm is all centered on `remaining` and we aren't referring back to `text` the whole time.
And it can be summed up as "while there is remaining text keep printing it".
https://github.com/llvm/llvm-project/pull/69422
More information about the lldb-commits
mailing list