sapling/remotefilelog/ctreemanifest/convert.h
Tony Tung 79f0ebb34a [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
2016-08-26 13:49:17 -07:00

59 lines
2.0 KiB
C++

// convert.h - conversion utility methods
//
// Copyright 2016 Facebook, Inc.
//
// no-check-code
//
// This software may be used and distributed according to the terms of the
// GNU General Public License version 2 or any later version.
#ifndef CTREEMANIFEST_CONVERT_H
#define CTREEMANIFEST_CONVERT_H
#include <stdint.h>
#include <string>
static int8_t hextable[256] = {
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
0, 1, 2, 3, 4, 5, 6, 7, 8, 9, -1, -1, -1, -1, -1, -1, /* 0-9 */
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* A-F */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, 10, 11, 12, 13, 14, 15, -1, -1, -1, -1, -1, -1, -1, -1, -1, /* a-f */
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1,
-1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1, -1
};
/**
* 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) {
std::string result;
result.reserve(20);
appendbinfromhex(node, result);
return result;
}
#endif //CTREEMANIFEST_CONVERT_H