summaryrefslogtreecommitdiffstats
path: root/src/util.cc
diff options
context:
space:
mode:
Diffstat (limited to 'src/util.cc')
-rw-r--r--src/util.cc28
1 files changed, 28 insertions, 0 deletions
diff --git a/src/util.cc b/src/util.cc
index dbf7566..5f5d8dc 100644
--- a/src/util.cc
+++ b/src/util.cc
@@ -255,3 +255,31 @@ string GetLastErrorString() {
return msg;
}
#endif
+
+static bool islatinalpha(int c) {
+ // isalpha() is locale-dependent.
+ return (c >= 'a' && c <= 'z') || (c >= 'A' && c <= 'Z');
+}
+
+string StripAnsiEscapeCodes(const string& in) {
+ string stripped;
+ stripped.reserve(in.size());
+
+ for (size_t i = 0; i < in.size(); ++i) {
+ if (in[i] != '\33') {
+ // Not an escape code.
+ stripped.push_back(in[i]);
+ continue;
+ }
+
+ // Only strip CSIs for now.
+ if (i + 1 >= in.size()) break;
+ if (in[i + 1] != '[') continue; // Not a CSI.
+ i += 2;
+
+ // Skip everything up to and including the next [a-zA-Z].
+ while (i < in.size() && !islatinalpha(in[i]))
+ ++i;
+ }
+ return stripped;
+}