[Lldb-commits] [lldb] [lldb] colorize symbols in image lookup (PR #69422)

David Spickett via lldb-commits lldb-commits at lists.llvm.org
Wed Nov 29 04:04:55 PST 2023


=?utf-8?q?José?= L. Junior <josejunior at 10xengineers.ai>,taalhaataahir0102
 <23100293 at lums.edu.pk>
Message-ID:
In-Reply-To: <llvm.org/llvm/llvm-project/pull/69422 at github.com>


================
@@ -70,6 +72,32 @@ size_t Stream::PutCString(llvm::StringRef str) {
   return bytes_written;
 }
 
+void Stream::PutCStringColorHighlighted(llvm::StringRef text,
+                                        const char *pattern) {
+  if (!pattern) {
+    PutCString(text);
+    return;
+  }
+
+  // If pattern is not nullptr, we should use color
+  llvm::Regex reg_pattern(pattern);
+  llvm::SmallVector<llvm::StringRef, 1> matches;
+  llvm::StringRef remaining = text;
+  std::string format_str = lldb_private::ansi::FormatAnsiTerminalCodes(
+      "${ansi.fg.red}%s${ansi.normal}");
+  while (reg_pattern.match(remaining, &matches)) {
+    llvm::StringRef match = matches[0];
+    size_t match_start_pos = match.data() - remaining.data();
+    Write(remaining.data(), match_start_pos);
+    size_t last_pos = match_start_pos;
+    Printf(format_str.c_str(), match.str().c_str());
----------------
DavidSpickett wrote:

So because a StringRef doesn't point to a null terminated string (not always anyway) we have to make a std::string here. But...

Instead what you could do is include the string length in the format string using `%.*s`, then the arguments would be `format.c_str(), match.size(), match.data()`.

And the `*` becomes the match size and there's no need for `match.data()` to point to something null terminated. Therefore no need to make a std::string copy.

https://github.com/llvm/llvm-project/pull/69422


More information about the lldb-commits mailing list