Commit 8bac351a authored by Adam Simpkins's avatar Adam Simpkins Committed by Facebook Github Bot 8

add hexlify() and unhexlify() convenience methods

Summary:
Add new versions of hexlify() and unhexlify() that directly return a
std::string, similar to the helper functions that already exist for humanify()
and backslashify().

The function signatures for these helpers are different than the humanify() and
backslashify() helpers--these functions always accept StringPiece or ByteRange
arguments.  This allows them to easily accept "const char*" arguments, without
having to convert the arguments to a std::string.  (I think we probably should
fix the humanify() and backslashify() helpers to be similar in a separate
diff.)

Reviewed By: yfeldblum

Differential Revision: D3403863

fbshipit-source-id: a2df49d5857e1d34caac3d78283dc293f4ef1ab6
parent bbac9a99
...@@ -259,6 +259,21 @@ template<class InputString, class OutputString> ...@@ -259,6 +259,21 @@ template<class InputString, class OutputString>
bool hexlify(const InputString& input, OutputString& output, bool hexlify(const InputString& input, OutputString& output,
bool append=false); bool append=false);
template <class OutputString = std::string>
OutputString hexlify(ByteRange input) {
OutputString output;
if (!hexlify(input, output)) {
// hexlify() currently always returns true, so this can't really happen
throw std::runtime_error("hexlify failed");
}
return output;
}
template <class OutputString = std::string>
OutputString hexlify(StringPiece input) {
return hexlify<OutputString>(ByteRange{input});
}
/** /**
* Same functionality as Python's binascii.unhexlify. Returns true * Same functionality as Python's binascii.unhexlify. Returns true
* on successful conversion. * on successful conversion.
...@@ -266,6 +281,17 @@ bool hexlify(const InputString& input, OutputString& output, ...@@ -266,6 +281,17 @@ bool hexlify(const InputString& input, OutputString& output,
template<class InputString, class OutputString> template<class InputString, class OutputString>
bool unhexlify(const InputString& input, OutputString& output); bool unhexlify(const InputString& input, OutputString& output);
template <class OutputString = std::string>
OutputString unhexlify(StringPiece input) {
OutputString output;
if (!unhexlify(input, output)) {
// unhexlify() fails if the input has non-hexidecimal characters,
// or if it doesn't consist of a whole number of bytes
throw std::domain_error("unhexlify() called with non-hex input");
}
return output;
}
/* /*
* A pretty-printer for numbers that appends suffixes of units of the * A pretty-printer for numbers that appends suffixes of units of the
* given type. It prints 4 sig-figs of value with the most * given type. It prints 4 sig-figs of value with the most
......
...@@ -19,6 +19,8 @@ ...@@ -19,6 +19,8 @@
#include <boost/regex.hpp> #include <boost/regex.hpp>
#include <gtest/gtest.h> #include <gtest/gtest.h>
#include <folly/Array.h>
using namespace folly; using namespace folly;
using namespace std; using namespace std;
...@@ -972,7 +974,7 @@ TEST(String, hexlify) { ...@@ -972,7 +974,7 @@ TEST(String, hexlify) {
string input1 = "0123"; string input1 = "0123";
string output1; string output1;
EXPECT_TRUE(hexlify(input1, output1)); EXPECT_TRUE(hexlify(input1, output1));
EXPECT_EQ(output1, "30313233"); EXPECT_EQ("30313233", output1);
fbstring input2 = "abcdefg"; fbstring input2 = "abcdefg";
input2[1] = 0; input2[1] = 0;
...@@ -980,7 +982,11 @@ TEST(String, hexlify) { ...@@ -980,7 +982,11 @@ TEST(String, hexlify) {
input2[5] = 0xb6; input2[5] = 0xb6;
fbstring output2; fbstring output2;
EXPECT_TRUE(hexlify(input2, output2)); EXPECT_TRUE(hexlify(input2, output2));
EXPECT_EQ(output2, "610063ff65b667"); EXPECT_EQ("610063ff65b667", output2);
EXPECT_EQ("666f6f626172", hexlify("foobar"));
auto bytes = folly::make_array<uint8_t>(1, 2, 3, 4);
EXPECT_EQ("01020304", hexlify(ByteRange{bytes.data(), bytes.size()}));
} }
TEST(String, unhexlify) { TEST(String, unhexlify) {
...@@ -1008,6 +1014,10 @@ TEST(String, unhexlify) { ...@@ -1008,6 +1014,10 @@ TEST(String, unhexlify) {
string input4 = "xy"; string input4 = "xy";
string output4; string output4;
EXPECT_FALSE(unhexlify(input4, output4)); EXPECT_FALSE(unhexlify(input4, output4));
EXPECT_EQ("foobar", unhexlify("666f6f626172"));
EXPECT_EQ(StringPiece("foo\0bar", 7), unhexlify("666f6f00626172"));
EXPECT_THROW(unhexlify("666f6fzz626172"), std::domain_error);
} }
TEST(String, backslashify) { TEST(String, backslashify) {
......
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