Add an overload to use backlashify with StringPiece
authorAndre Pinto <aap@fb.com>
Thu, 5 Oct 2017 15:34:49 +0000 (08:34 -0700)
committerFacebook Github Bot <facebook-github-bot@users.noreply.github.com>
Thu, 5 Oct 2017 15:59:51 +0000 (08:59 -0700)
Summary: Allows calling backlashify on a StringPiece.

Reviewed By: yfeldblum

Differential Revision: D5930015

fbshipit-source-id: ca14c78d9a90c45781da7229eb5d2f437792b2d1

folly/String-inl.h
folly/String.h
folly/test/StringTest.cpp

index 02af558f59b6371ea5eeecc0598222be00df9b9c..cfcbd00a5486ef84fedcbcdb83cc8344556b0607 100644 (file)
@@ -491,8 +491,11 @@ void join(const Delim& delimiter,
     output);
 }
 
-template <class String1, class String2>
-void backslashify(const String1& input, String2& output, bool hex_style) {
+template <class OutputString>
+void backslashify(
+    folly::StringPiece input,
+    OutputString& output,
+    bool hex_style) {
   static const char hexValues[] = "0123456789abcdef";
   output.clear();
   output.reserve(3 * input.size());
index 67faa9ec6da2b983487e3ceaa9a00be4598e8a1b..6889476bd87069aff07f95072cc3ab1150f761b1 100644 (file)
@@ -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 <class String1, class String2>
-void backslashify(const String1& input, String2& output, bool hex_style=false);
+template <class OutputString>
+void backslashify(
+    folly::StringPiece input,
+    OutputString& output,
+    bool hex_style = false);
 
-template <class String>
-String backslashify(const String& input, bool hex_style=false) {
-  String output;
+template <class OutputString = std::string>
+OutputString backslashify(StringPiece input, bool hex_style = false) {
+  OutputString output;
   backslashify(input, output, hex_style);
   return output;
 }
index e9d560475f0a9e4a3698eaaac770d206b6733452..7bd2c301b6aed203e53c5dacd637a1dd15611958 100644 (file)
@@ -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) {