From 40a6da60a1a29ef72a2f89eb42ea9543b295bc81 Mon Sep 17 00:00:00 2001
From: Anna Zaks <ganna@apple.com>
Date: Thu, 29 Oct 2015 09:56:12 -0700
Subject: [PATCH 2007/2008] cherry-pick RemoveANSIEscapeSequencesFromString
 from master

Signed-off-by: Jeremy Huddleston Sequoia <jeremyhu@apple.com>
---
 lib/sanitizer_common/sanitizer_common.cc | 34 ++++++++++++++++++++++++++++++++
 lib/sanitizer_common/sanitizer_common.h  |  1 +
 2 files changed, 35 insertions(+)

diff --git llvm_release_37/projects/compiler-rt/lib/sanitizer_common/sanitizer_common.cc macports_release_37/projects/compiler-rt/lib/sanitizer_common/sanitizer_common.cc
index d14e988..bbc57e5 100644
--- llvm_release_37/projects/compiler-rt/lib/sanitizer_common/sanitizer_common.cc
+++ macports_release_37/projects/compiler-rt/lib/sanitizer_common/sanitizer_common.cc
@@ -255,6 +255,40 @@ void ReportErrorSummary(const char *error_type, const AddressInfo &info) {
 }
 #endif
 
+// Removes the ANSI escape sequences from the input string (in-place).
+void RemoveANSIEscapeSequencesFromString(char *str) {
+  if (!str)
+    return;
+
+  // We are going to remove the escape sequences in place.
+  char *s = str;
+  char *z = str;
+  while (*s != '\0') {
+    CHECK_GE(s, z);
+    // Skip over ANSI escape sequences with pointer 's'.
+    if (*s == '\033' && *(s + 1) == '[') {
+      s = internal_strchrnul(s, 'm');
+      if (*s == '\0') {
+        break;
+      }
+      s++;
+      continue;
+    }
+    // 's' now points at a character we want to keep. Copy over the buffer
+    // content if the escape sequence has been perviously skipped andadvance
+    // both pointers.
+    if (s != z)
+      *z = *s;
+
+    // If we have not seen an escape sequence, just advance both pointers.
+    z++;
+    s++;
+  }
+
+  // Null terminate the string.
+  *z = '\0';
+}
+
 void LoadedModule::set(const char *module_name, uptr base_address) {
   clear();
   full_name_ = internal_strdup(module_name);
diff --git llvm_release_37/projects/compiler-rt/lib/sanitizer_common/sanitizer_common.h macports_release_37/projects/compiler-rt/lib/sanitizer_common/sanitizer_common.h
index 2c5a8db..7e7a6dc 100644
--- llvm_release_37/projects/compiler-rt/lib/sanitizer_common/sanitizer_common.h
+++ macports_release_37/projects/compiler-rt/lib/sanitizer_common/sanitizer_common.h
@@ -160,6 +160,7 @@ void SetLowLevelAllocateCallback(LowLevelAllocateCallback callback);
 // IO
 void RawWrite(const char *buffer);
 bool ColorizeReports();
+void RemoveANSIEscapeSequencesFromString(char *buffer);
 void Printf(const char *format, ...);
 void Report(const char *format, ...);
 void SetPrintfAndReportCallback(void (*callback)(const char *));
-- 
2.6.2

