From: Andre Pinto Date: Thu, 5 Oct 2017 15:34:49 +0000 (-0700) Subject: Add an overload to use backlashify with StringPiece X-Git-Tag: v2017.10.09.00~9 X-Git-Url: http://demsky.eecs.uci.edu/git/?a=commitdiff_plain;ds=sidebyside;h=ff17894be919173b4bbaf5d9f535b62f92806ebf;hp=a95a6976020356513a4c0dc8a3c8557c0b2b4496;p=folly.git 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 --- diff --git a/folly/String-inl.h b/folly/String-inl.h index 02af558f..cfcbd00a 100644 --- a/folly/String-inl.h +++ b/folly/String-inl.h @@ -491,8 +491,11 @@ void join(const Delim& delimiter, output); } -template -void backslashify(const String1& input, String2& output, bool hex_style) { +template +void backslashify( + folly::StringPiece input, + OutputString& output, + bool hex_style) { static const char hexValues[] = "0123456789abcdef"; output.clear(); output.reserve(3 * input.size()); diff --git a/folly/String.h b/folly/String.h index 67faa9ec..6889476b 100644 --- a/folly/String.h +++ b/folly/String.h @@ -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 * only. */ -template -void backslashify(const String1& input, String2& output, bool hex_style=false); +template +void backslashify( + folly::StringPiece input, + OutputString& output, + bool hex_style = false); -template -String backslashify(const String& input, bool hex_style=false) { - String output; +template +OutputString backslashify(StringPiece input, bool hex_style = false) { + OutputString output; backslashify(input, output, hex_style); return output; } diff --git a/folly/test/StringTest.cpp b/folly/test/StringTest.cpp index e9d56047..7bd2c301 100644 --- a/folly/test/StringTest.cpp +++ b/folly/test/StringTest.cpp @@ -1047,6 +1047,10 @@ TEST(String, backslashify) { EXPECT_EQ("abc\\r", backslashify(string("abc\r"))); EXPECT_EQ("abc\\x0d", backslashify(string("abc\r"), true)); 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) {