|
||||
File indexing completed on 2024-11-15 09:01:16
0001 // 0002 // Copyright 2017 The Abseil Authors. 0003 // 0004 // Licensed under the Apache License, Version 2.0 (the "License"); 0005 // you may not use this file except in compliance with the License. 0006 // You may obtain a copy of the License at 0007 // 0008 // https://www.apache.org/licenses/LICENSE-2.0 0009 // 0010 // Unless required by applicable law or agreed to in writing, software 0011 // distributed under the License is distributed on an "AS IS" BASIS, 0012 // WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 0013 // See the License for the specific language governing permissions and 0014 // limitations under the License. 0015 // 0016 // ----------------------------------------------------------------------------- 0017 // File: escaping.h 0018 // ----------------------------------------------------------------------------- 0019 // 0020 // This header file contains string utilities involved in escaping and 0021 // unescaping strings in various ways. 0022 0023 #ifndef ABSL_STRINGS_ESCAPING_H_ 0024 #define ABSL_STRINGS_ESCAPING_H_ 0025 0026 #include <cstddef> 0027 #include <string> 0028 #include <vector> 0029 0030 #include "absl/base/macros.h" 0031 #include "absl/strings/ascii.h" 0032 #include "absl/strings/str_join.h" 0033 #include "absl/strings/string_view.h" 0034 0035 namespace absl { 0036 ABSL_NAMESPACE_BEGIN 0037 0038 // CUnescape() 0039 // 0040 // Unescapes a `source` string and copies it into `dest`, rewriting C-style 0041 // escape sequences (https://en.cppreference.com/w/cpp/language/escape) into 0042 // their proper code point equivalents, returning `true` if successful. 0043 // 0044 // The following unescape sequences can be handled: 0045 // 0046 // * ASCII escape sequences ('\n','\r','\\', etc.) to their ASCII equivalents 0047 // * Octal escape sequences ('\nnn') to byte nnn. The unescaped value must 0048 // resolve to a single byte or an error will occur. E.g. values greater than 0049 // 0xff will produce an error. 0050 // * Hexadecimal escape sequences ('\xnn') to byte nn. While an arbitrary 0051 // number of following digits are allowed, the unescaped value must resolve 0052 // to a single byte or an error will occur. E.g. '\x0045' is equivalent to 0053 // '\x45', but '\x1234' will produce an error. 0054 // * Unicode escape sequences ('\unnnn' for exactly four hex digits or 0055 // '\Unnnnnnnn' for exactly eight hex digits, which will be encoded in 0056 // UTF-8. (E.g., `\u2019` unescapes to the three bytes 0xE2, 0x80, and 0057 // 0x99). 0058 // 0059 // If any errors are encountered, this function returns `false`, leaving the 0060 // `dest` output parameter in an unspecified state, and stores the first 0061 // encountered error in `error`. To disable error reporting, set `error` to 0062 // `nullptr` or use the overload with no error reporting below. 0063 // 0064 // Example: 0065 // 0066 // std::string s = "foo\\rbar\\nbaz\\t"; 0067 // std::string unescaped_s; 0068 // if (!absl::CUnescape(s, &unescaped_s) { 0069 // ... 0070 // } 0071 // EXPECT_EQ(unescaped_s, "foo\rbar\nbaz\t"); 0072 bool CUnescape(absl::string_view source, std::string* dest, std::string* error); 0073 0074 // Overload of `CUnescape()` with no error reporting. 0075 inline bool CUnescape(absl::string_view source, std::string* dest) { 0076 return CUnescape(source, dest, nullptr); 0077 } 0078 0079 // CEscape() 0080 // 0081 // Escapes a 'src' string using C-style escapes sequences 0082 // (https://en.cppreference.com/w/cpp/language/escape), escaping other 0083 // non-printable/non-whitespace bytes as octal sequences (e.g. "\377"). 0084 // 0085 // Example: 0086 // 0087 // std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n"; 0088 // std::string escaped_s = absl::CEscape(s); 0089 // EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\010\\t\\n\\013\\014\\r\\n"); 0090 std::string CEscape(absl::string_view src); 0091 0092 // CHexEscape() 0093 // 0094 // Escapes a 'src' string using C-style escape sequences, escaping 0095 // other non-printable/non-whitespace bytes as hexadecimal sequences (e.g. 0096 // "\xFF"). 0097 // 0098 // Example: 0099 // 0100 // std::string s = "foo\rbar\tbaz\010\011\012\013\014\x0d\n"; 0101 // std::string escaped_s = absl::CHexEscape(s); 0102 // EXPECT_EQ(escaped_s, "foo\\rbar\\tbaz\\x08\\t\\n\\x0b\\x0c\\r\\n"); 0103 std::string CHexEscape(absl::string_view src); 0104 0105 // Utf8SafeCEscape() 0106 // 0107 // Escapes a 'src' string using C-style escape sequences, escaping bytes as 0108 // octal sequences, and passing through UTF-8 characters without conversion. 0109 // I.e., when encountering any bytes with their high bit set, this function 0110 // will not escape those values, whether or not they are valid UTF-8. 0111 std::string Utf8SafeCEscape(absl::string_view src); 0112 0113 // Utf8SafeCHexEscape() 0114 // 0115 // Escapes a 'src' string using C-style escape sequences, escaping bytes as 0116 // hexadecimal sequences, and passing through UTF-8 characters without 0117 // conversion. 0118 std::string Utf8SafeCHexEscape(absl::string_view src); 0119 0120 // Base64Escape() 0121 // 0122 // Encodes a `src` string into a base64-encoded 'dest' string with padding 0123 // characters. This function conforms with RFC 4648 section 4 (base64) and RFC 0124 // 2045. 0125 void Base64Escape(absl::string_view src, std::string* dest); 0126 std::string Base64Escape(absl::string_view src); 0127 0128 // WebSafeBase64Escape() 0129 // 0130 // Encodes a `src` string into a base64 string, like Base64Escape() does, but 0131 // outputs '-' instead of '+' and '_' instead of '/', and does not pad 'dest'. 0132 // This function conforms with RFC 4648 section 5 (base64url). 0133 void WebSafeBase64Escape(absl::string_view src, std::string* dest); 0134 std::string WebSafeBase64Escape(absl::string_view src); 0135 0136 // Base64Unescape() 0137 // 0138 // Converts a `src` string encoded in Base64 (RFC 4648 section 4) to its binary 0139 // equivalent, writing it to a `dest` buffer, returning `true` on success. If 0140 // `src` contains invalid characters, `dest` is cleared and returns `false`. 0141 // If padding is included (note that `Base64Escape()` does produce it), it must 0142 // be correct. In the padding, '=' and '.' are treated identically. 0143 bool Base64Unescape(absl::string_view src, std::string* dest); 0144 0145 // WebSafeBase64Unescape() 0146 // 0147 // Converts a `src` string encoded in "web safe" Base64 (RFC 4648 section 5) to 0148 // its binary equivalent, writing it to a `dest` buffer. If `src` contains 0149 // invalid characters, `dest` is cleared and returns `false`. If padding is 0150 // included (note that `WebSafeBase64Escape()` does not produce it), it must be 0151 // correct. In the padding, '=' and '.' are treated identically. 0152 bool WebSafeBase64Unescape(absl::string_view src, std::string* dest); 0153 0154 // HexStringToBytes() 0155 // 0156 // Converts an ASCII hex string into bytes, returning binary data of length 0157 // `from.size()/2`. 0158 std::string HexStringToBytes(absl::string_view from); 0159 0160 // BytesToHexString() 0161 // 0162 // Converts binary data into an ASCII text string, returning a string of size 0163 // `2*from.size()`. 0164 std::string BytesToHexString(absl::string_view from); 0165 0166 ABSL_NAMESPACE_END 0167 } // namespace absl 0168 0169 #endif // ABSL_STRINGS_ESCAPING_H_
[ Source navigation ] | [ Diff markup ] | [ Identifier search ] | [ general search ] |
This page was automatically generated by the 2.3.7 LXR engine. The LXR team |