Commit ff17894b authored by Andre Pinto's avatar Andre Pinto Committed by Facebook Github Bot

Add an overload to use backlashify with StringPiece

Summary: Allows calling backlashify on a StringPiece.

Reviewed By: yfeldblum

Differential Revision: D5930015

fbshipit-source-id: ca14c78d9a90c45781da7229eb5d2f437792b2d1
parent a95a6976
...@@ -491,8 +491,11 @@ void join(const Delim& delimiter, ...@@ -491,8 +491,11 @@ void join(const Delim& delimiter,
output); output);
} }
template <class String1, class String2> template <class OutputString>
void backslashify(const String1& input, String2& output, bool hex_style) { void backslashify(
folly::StringPiece input,
OutputString& output,
bool hex_style) {
static const char hexValues[] = "0123456789abcdef"; static const char hexValues[] = "0123456789abcdef";
output.clear(); output.clear();
output.reserve(3 * input.size()); output.reserve(3 * input.size());
......
...@@ -211,12 +211,15 @@ std::string& stringVAppendf(std::string* out, const char* format, va_list ap); ...@@ -211,12 +211,15 @@ std::string& stringVAppendf(std::string* out, const char* format, va_list ap);
* C++, use cEscape instead. This function is for display purposes * C++, use cEscape instead. This function is for display purposes
* only. * only.
*/ */
template <class String1, class String2> template <class OutputString>
void backslashify(const String1& input, String2& output, bool hex_style=false); void backslashify(
folly::StringPiece input,
OutputString& output,
bool hex_style = false);
template <class String> template <class OutputString = std::string>
String backslashify(const String& input, bool hex_style=false) { OutputString backslashify(StringPiece input, bool hex_style = false) {
String output; OutputString output;
backslashify(input, output, hex_style); backslashify(input, output, hex_style);
return output; return output;
} }
......
...@@ -1047,6 +1047,10 @@ TEST(String, backslashify) { ...@@ -1047,6 +1047,10 @@ TEST(String, backslashify) {
EXPECT_EQ("abc\\r", backslashify(string("abc\r"))); EXPECT_EQ("abc\\r", backslashify(string("abc\r")));
EXPECT_EQ("abc\\x0d", backslashify(string("abc\r"), true)); EXPECT_EQ("abc\\x0d", backslashify(string("abc\r"), true));
EXPECT_EQ("\\0\\0", backslashify(string(2, '\0'))); EXPECT_EQ("\\0\\0", backslashify(string(2, '\0')));
StringPiece input1 = "abc\r";
std::string output1 = backslashify(input1);
EXPECT_EQ("abc\\r", output1);
} }
TEST(String, humanify) { TEST(String, humanify) {
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment