aboutsummaryrefslogtreecommitdiff
path: root/lldb/source/Utility/Stream.cpp
diff options
context:
space:
mode:
Diffstat (limited to 'lldb/source/Utility/Stream.cpp')
-rw-r--r--lldb/source/Utility/Stream.cpp30
1 files changed, 30 insertions, 0 deletions
diff --git a/lldb/source/Utility/Stream.cpp b/lldb/source/Utility/Stream.cpp
index af28a49a1f0c..62e061e9d09c 100644
--- a/lldb/source/Utility/Stream.cpp
+++ b/lldb/source/Utility/Stream.cpp
@@ -8,11 +8,13 @@
#include "lldb/Utility/Stream.h"
+#include "lldb/Utility/AnsiTerminal.h"
#include "lldb/Utility/Endian.h"
#include "lldb/Utility/VASPrintf.h"
#include "llvm/ADT/SmallString.h"
#include "llvm/Support/Format.h"
#include "llvm/Support/LEB128.h"
+#include "llvm/Support/Regex.h"
#include <string>
@@ -70,6 +72,34 @@ size_t Stream::PutCString(llvm::StringRef str) {
return bytes_written;
}
+void Stream::PutCStringColorHighlighted(llvm::StringRef text,
+ llvm::StringRef pattern,
+ llvm::StringRef prefix,
+ llvm::StringRef suffix) {
+ // Only apply color formatting when a pattern is present and both prefix and
+ // suffix are specified. In the absence of these conditions, output the text
+ // without color formatting.
+ if (pattern.empty() || (prefix.empty() && suffix.empty())) {
+ PutCString(text);
+ return;
+ }
+
+ llvm::Regex reg_pattern(pattern);
+ llvm::SmallVector<llvm::StringRef, 1> matches;
+ llvm::StringRef remaining = text;
+ std::string format_str = lldb_private::ansi::FormatAnsiTerminalCodes(
+ prefix.str() + "%.*s" + suffix.str());
+ while (reg_pattern.match(remaining, &matches)) {
+ llvm::StringRef match = matches[0];
+ size_t match_start_pos = match.data() - remaining.data();
+ PutCString(remaining.take_front(match_start_pos));
+ Printf(format_str.c_str(), match.size(), match.data());
+ remaining = remaining.drop_front(match_start_pos + match.size());
+ }
+ if (remaining.size())
+ PutCString(remaining);
+}
+
// Print a double quoted NULL terminated C string to the stream using the
// printf format in "format".
void Stream::QuotedCString(const char *cstr, const char *format) {