summaryrefslogtreecommitdiff
path: root/absl/strings/escaping.cc
diff options
context:
space:
mode:
authorGravatar Abseil Team <absl-team@google.com>2017-12-20 12:34:46 -0800
committerGravatar Xiaoyi Zhang <zhangxy988@gmail.com>2017-12-20 17:03:18 -0500
commitff7045647330f5dc21725a42456091207f3eecd5 (patch)
treecae476d37e272b1a1d48172f07c4a7229a027fea /absl/strings/escaping.cc
parent4972c72c5cf2f27e2a0846ce9ff5d377d3f2b7af (diff)
Changes imported from Abseil "staging" branch:
- 20f4e7133d695e9a05e13ebdfd4d78da310b42b5 Remove the warning supressions -Wno-documentation and by Derek Mauro <dmauro@google.com> - e1bde85c0571673b1e7a88b9d45a393606ba7e6c Changed the optimized version of strings_internal::JoinAl... by Abseil Team <absl-team@google.com> - 746e6716b4c15be61547670d68d25a1c850d3954 Add missing absl:: qualification. by Alex Strelnikov <strel@google.com> - 4e5c18c488cbd49ca72b02911cf22d830d5a7f16 Internals change: Remove the ability to pass a custom met... by Greg Falcon <gfalcon@google.com> - 65d58107a5730d4b6468bbffc72bea2f980af826 Moved most common character case (ascii printable) out of... by Abseil Team <absl-team@google.com> - f031331cc55a3d57b9110e12c7cbe50ac3e2a04f Add missing copyright headers to a few source files. by Greg Falcon <gfalcon@google.com> - 6035a77af6fa951c536b42df4c710d16d1817aec Enable libstdc++'s memcmp optimization in absl::equal for... by Abseil Team <absl-team@google.com> - 73a665a4a10781e5d89f75a876ece7ad859f4116 Fix minor spelling error "hexidecimal". by Abseil Team <absl-team@google.com> GitOrigin-RevId: 20f4e7133d695e9a05e13ebdfd4d78da310b42b5 Change-Id: Id8c18ebd331d096935052a6ab259ebe0e2ef13ae
Diffstat (limited to 'absl/strings/escaping.cc')
-rw-r--r--absl/strings/escaping.cc110
1 files changed, 55 insertions, 55 deletions
diff --git a/absl/strings/escaping.cc b/absl/strings/escaping.cc
index abe9e0aa..fbc9f756 100644
--- a/absl/strings/escaping.cc
+++ b/absl/strings/escaping.cc
@@ -366,31 +366,31 @@ std::string CEscapeInternal(absl::string_view src, bool use_hex, bool utf8_safe)
return dest;
}
+/* clang-format off */
+constexpr char c_escaped_len[256] = {
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4, // \t, \n, \r
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // ", '
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // '0'..'9'
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'A'..'O'
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, // 'P'..'Z', '\'
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'a'..'o'
+ 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, // 'p'..'z', DEL
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+ 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
+};
+/* clang-format on */
+
// Calculates the length of the C-style escaped version of 'src'.
// Assumes that non-printable characters are escaped using octal sequences, and
// that UTF-8 bytes are not handled specially.
inline size_t CEscapedLength(absl::string_view src) {
- /* clang-format off */
- constexpr char c_escaped_len[256] = {
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 2, 2, 4, 4, 2, 4, 4, // \t, \n, \r
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 1, 1, 2, 1, 1, 1, 1, 2, 1, 1, 1, 1, 1, 1, 1, 1, // ", '
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // '0'..'9'
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'A'..'O'
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 2, 1, 1, 1, // 'P'..'Z', '\'
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, // 'a'..'o'
- 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 1, 4, // 'p'..'z', DEL
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4, 4,
- };
- /* clang-format on */
-
size_t escaped_len = 0;
for (unsigned char c : src) escaped_len += c_escaped_len[c];
return escaped_len;
@@ -409,41 +409,41 @@ void CEscapeAndAppendInternal(absl::string_view src, std::string* dest) {
char* append_ptr = &(*dest)[cur_dest_len];
for (unsigned char c : src) {
- switch (c) {
- case '\n':
- *append_ptr++ = '\\';
- *append_ptr++ = 'n';
- break;
- case '\r':
- *append_ptr++ = '\\';
- *append_ptr++ = 'r';
- break;
- case '\t':
- *append_ptr++ = '\\';
- *append_ptr++ = 't';
- break;
- case '\"':
- *append_ptr++ = '\\';
- *append_ptr++ = '\"';
- break;
- case '\'':
- *append_ptr++ = '\\';
- *append_ptr++ = '\'';
- break;
- case '\\':
- *append_ptr++ = '\\';
- *append_ptr++ = '\\';
- break;
- default:
- if (!absl::ascii_isprint(c)) {
+ int char_len = c_escaped_len[c];
+ if (char_len == 1) {
+ *append_ptr++ = c;
+ } else if (char_len == 2) {
+ switch (c) {
+ case '\n':
*append_ptr++ = '\\';
- *append_ptr++ = '0' + c / 64;
- *append_ptr++ = '0' + (c % 64) / 8;
- *append_ptr++ = '0' + c % 8;
- } else {
- *append_ptr++ = c;
- }
- break;
+ *append_ptr++ = 'n';
+ break;
+ case '\r':
+ *append_ptr++ = '\\';
+ *append_ptr++ = 'r';
+ break;
+ case '\t':
+ *append_ptr++ = '\\';
+ *append_ptr++ = 't';
+ break;
+ case '\"':
+ *append_ptr++ = '\\';
+ *append_ptr++ = '\"';
+ break;
+ case '\'':
+ *append_ptr++ = '\\';
+ *append_ptr++ = '\'';
+ break;
+ case '\\':
+ *append_ptr++ = '\\';
+ *append_ptr++ = '\\';
+ break;
+ }
+ } else {
+ *append_ptr++ = '\\';
+ *append_ptr++ = '0' + c / 64;
+ *append_ptr++ = '0' + (c % 64) / 8;
+ *append_ptr++ = '0' + c % 8;
}
}
}