[ctree] create a new method appendbinfromhex

Summary:
appendbinfromhex appends the binary representation of a 40-byte hex string onto a std::string.  This allows us to reuse a std::string rather than to allocate a new one every time.

Also:

1. converted binfromhex to use this method.
2. updated the docblocks to actually reflect reality.

Test Plan: `make local && cd ~/work/fbsource && PYTHONPATH=~/work/mercurial/facebook-hg-rpms/remotefilelog:~/work/mercurial/facebook-hg-rpms/fb-hgext/:~/work/mercurial/facebook-hg-rpms/remotenames/:~/work/mercurial/facebook-hg-rpms/lz4revlog/ /opt/local/bin/python2.7 ~/work/mercurial/facebook-hg-rpms/hg-crew/hg --config extensions.perftest=~/work/mercurial/facebook-hg-rpms/remotefilelog/tests/perftest.py testtree --kind flat,ctree,fast --test fulliter,diff,find --build "master~5::master"`

Reviewers: #fastmanifest, durham

Reviewed By: durham

Subscribers: mitrandir

Differential Revision: https://phabricator.intern.facebook.com/D3770048

Signature: t1:3770048:1472105520:eac79a42360ebfa258519346b68fc4541c2dbb7c
This commit is contained in:
Tony Tung 2016-08-26 13:49:17 -07:00
parent a6bc389217
commit 79f0ebb34a

View File

@ -34,16 +34,25 @@ static int8_t hextable[256] = {
}; };
/** /**
* Converts a given 20-byte node into a 40-byte hex string * Converts a given 40-byte hex string into a 20-byte node.
*/
inline void appendbinfromhex(const char *node, std::string &output) {
for (int i = 0; i < 40;) {
int8_t hi = hextable[(unsigned char)node[i++]];
int8_t lo = hextable[(unsigned char)node[i++]];
output.push_back((hi << 4) | lo);
}
}
/**
* Converts a given 40-byte hex string into a 20-byte node.
*/ */
inline std::string binfromhex(const char *node) { inline std::string binfromhex(const char *node) {
char binary[20]; std::string result;
for (int i = 0; i < 40;) {
int hi = hextable[(unsigned char)node[i++]]; result.reserve(20);
int lo = hextable[(unsigned char)node[i++]]; appendbinfromhex(node, result);
binary[(i - 2) / 2] = (hi << 4) | lo; return result;
}
return std::string(binary, 20);
} }
#endif //CTREEMANIFEST_CONVERT_H #endif //CTREEMANIFEST_CONVERT_H