aboutsummaryrefslogtreecommitdiff
path: root/strutil.cc
diff options
context:
space:
mode:
authorShinichiro Hamaji <shinichiro.hamaji@gmail.com>2016-02-22 15:21:43 +0900
committerShinichiro Hamaji <shinichiro.hamaji@gmail.com>2016-02-22 15:35:48 +0900
commita67fba3bf217ece75b66cb9adb6539e9d098ce9c (patch)
tree7bffb806419e0353b822339da4e723713b9fe8ff /strutil.cc
parent3727d215444bdd9d2fe404bb4a98275b1b43f71e (diff)
[C++] EscapeShell in SSE4.2
Diffstat (limited to 'strutil.cc')
-rw-r--r--strutil.cc27
1 files changed, 27 insertions, 0 deletions
diff --git a/strutil.cc b/strutil.cc
index 80a4b5b..ded9500 100644
--- a/strutil.cc
+++ b/strutil.cc
@@ -523,6 +523,32 @@ string EchoEscape(const string str) {
}
void EscapeShell(string* s) {
+#ifdef __SSE4_2__
+ static const char ranges[] = "\0\0\n\n\"\"$$\\\\``";
+ size_t prev = 0;
+ size_t i = SkipUntilSSE42(s->c_str(), s->size(), ranges, 12);
+ if (i == s->size())
+ return;
+
+ string r;
+ for (; i < s->size();) {
+ StringPiece(*s).substr(prev, i - prev).AppendToString(&r);
+ char c = (*s)[i];
+ r += '\\';
+ if (c == '$') {
+ if ((*s)[i+1] == '$') {
+ r += '$';
+ i++;
+ }
+ }
+ r += c;
+ i++;
+ prev = i;
+ i += SkipUntilSSE42(s->c_str() + i, s->size() - i, ranges, 12);
+ }
+ StringPiece(*s).substr(prev).AppendToString(&r);
+ s->swap(r);
+#else
if (s->find_first_of("$`\\\"") == string::npos)
return;
string r;
@@ -550,4 +576,5 @@ void EscapeShell(string* s) {
}
}
s->swap(r);
+#endif
}