Documented varint
This commit is contained in:
parent
c085e9294f
commit
a70bf86037
|
@ -99,7 +99,7 @@ namespace tools
|
||||||
static decoded_block_sizes instance;
|
static decoded_block_sizes instance;
|
||||||
|
|
||||||
private:
|
private:
|
||||||
std::vector<int> m_data;
|
std::vector<int> m_data;p
|
||||||
};
|
};
|
||||||
|
|
||||||
decoded_block_sizes decoded_block_sizes::instance;
|
decoded_block_sizes decoded_block_sizes::instance;
|
||||||
|
|
|
@ -136,8 +136,8 @@ namespace tools
|
||||||
/*! \breif calles m_handler */
|
/*! \breif calles m_handler */
|
||||||
static void handle_signal()
|
static void handle_signal()
|
||||||
{
|
{
|
||||||
/* static std::mutex m_mutex; */
|
static std::mutex m_mutex;
|
||||||
/* std::unique_lock<std::mutex> lock(m_mutex); */
|
std::unique_lock<std::mutex> lock(m_mutex);
|
||||||
m_handler();
|
m_handler();
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
|
@ -35,45 +35,82 @@
|
||||||
#include <utility>
|
#include <utility>
|
||||||
#include <sstream>
|
#include <sstream>
|
||||||
#include <string>
|
#include <string>
|
||||||
|
/*! \file varint.h
|
||||||
|
* \breif provides the implementation of varint's
|
||||||
|
*
|
||||||
|
* The representation of varints is rather odd. The first bit of each
|
||||||
|
* octet is significant, it represents wheter there is another part
|
||||||
|
* waiting to be read. For example 0x8002 would return 0x200, even
|
||||||
|
* though 0x02 does not have its msb set. The actual way they are read
|
||||||
|
* is as follows: Strip the msb of each byte, then from left to right,
|
||||||
|
* read in what remains, placing it in reverse, into the buffer. Thus,
|
||||||
|
* the following bit stream: 0xff02 would return 0x027f. 0xff turns
|
||||||
|
* into 0x7f, is placed on the beggining of the buffer, then 0x02 is
|
||||||
|
* unchanged, since its msb is not set, and placed at the end of the
|
||||||
|
* buffer.
|
||||||
|
*/
|
||||||
|
|
||||||
namespace tools {
|
namespace tools {
|
||||||
|
|
||||||
|
/*! \breif Error codes for varint
|
||||||
|
*/
|
||||||
|
enum {
|
||||||
|
/* \breif Represents the overflow error */
|
||||||
|
EVARINT_OVERFLOW = -1,
|
||||||
|
/* \breif Represents a non conical represnetation */
|
||||||
|
EVARINT_REPRESENT = -2,
|
||||||
|
};
|
||||||
|
|
||||||
|
/*! \breif writes a varint to a stream.
|
||||||
|
*/
|
||||||
template<typename OutputIt, typename T>
|
template<typename OutputIt, typename T>
|
||||||
|
/* Requires T to be both an integral type and unsigned, should be a compile error if it is not */
|
||||||
typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, void>::type
|
typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value, void>::type
|
||||||
write_varint(OutputIt &&dest, T i) {
|
write_varint(OutputIt &&dest, T i) {
|
||||||
|
/* Make sure that there is one after this */
|
||||||
while (i >= 0x80) {
|
while (i >= 0x80) {
|
||||||
*dest++ = (static_cast<char>(i) & 0x7f) | 0x80;
|
*dest = (static_cast<char>(i) & 0x7f) | 0x80;
|
||||||
i >>= 7;
|
++dest;
|
||||||
|
i >>= 7; /* I should be in multiples of 7, this should just get the next part */
|
||||||
}
|
}
|
||||||
*dest++ = static_cast<char>(i);
|
/* writes the last one to dest */
|
||||||
|
*dest = static_cast<char>(i);
|
||||||
|
dest++; /* Seems kinda pointless... */
|
||||||
}
|
}
|
||||||
|
|
||||||
template<typename t_type>
|
/*! \breif Returns the string that represents the varint
|
||||||
std::string get_varint_data(const t_type& v)
|
*/
|
||||||
|
template<typename T>
|
||||||
|
std::string get_varint_data(const T& v)
|
||||||
{
|
{
|
||||||
std::stringstream ss;
|
std::stringstream ss;
|
||||||
write_varint(std::ostreambuf_iterator<char>(ss), v);
|
write_varint(std::ostreambuf_iterator<char>(ss), v);
|
||||||
return ss.str();
|
return ss.str();
|
||||||
}
|
}
|
||||||
|
/*! \breif reads in the varint as pointer to by InputIt into i
|
||||||
|
*/
|
||||||
template<int bits, typename InputIt, typename T>
|
template<int bits, typename InputIt, typename T>
|
||||||
typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value && 0 <= bits && bits <= std::numeric_limits<T>::digits, int>::type
|
typename std::enable_if<std::is_integral<T>::value && std::is_unsigned<T>::value && 0 <= bits && bits <= std::numeric_limits<T>::digits, int>::type
|
||||||
read_varint(InputIt &&first, InputIt &&last, T &i) {
|
read_varint(InputIt &&first, InputIt &&last, T &write) {
|
||||||
int read = 0;
|
int read = 0;
|
||||||
i = 0;
|
write = 0;
|
||||||
for (int shift = 0;; shift += 7) {
|
for (int shift = 0;; shift += 7) {
|
||||||
if (first == last) {
|
if (first == last) {
|
||||||
return read; // End of input.
|
return read;
|
||||||
}
|
}
|
||||||
unsigned char byte = *first++;
|
unsigned char byte = *first;
|
||||||
|
++first;
|
||||||
++read;
|
++read;
|
||||||
if (shift + 7 >= bits && byte >= 1 << (bits - shift)) {
|
if (shift + 7 >= bits && byte >= 1 << (bits - shift)) {
|
||||||
return -1; // Overflow.
|
return EVARINT_OVERFLOW;
|
||||||
}
|
}
|
||||||
if (byte == 0 && shift != 0) {
|
if (byte == 0 && shift != 0) {
|
||||||
return -2; // Non-canonical representation.
|
return EVARINT_REPRESENT;
|
||||||
}
|
}
|
||||||
i |= static_cast<T>(byte & 0x7f) << shift;
|
|
||||||
|
write |= static_cast<T>(byte & 0x7f) << shift; /* Does the actualy placing into write, stripping the first bit */
|
||||||
|
|
||||||
|
/* If there is no next */
|
||||||
if ((byte & 0x80) == 0) {
|
if ((byte & 0x80) == 0) {
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
|
@ -81,6 +118,9 @@ namespace tools {
|
||||||
return read;
|
return read;
|
||||||
}
|
}
|
||||||
|
|
||||||
|
/*! \breif Wrapper around the other read_varint,
|
||||||
|
* Sets template parameters for you.
|
||||||
|
*/
|
||||||
template<typename InputIt, typename T>
|
template<typename InputIt, typename T>
|
||||||
int read_varint(InputIt &&first, InputIt &&last, T &i) {
|
int read_varint(InputIt &&first, InputIt &&last, T &i) {
|
||||||
return read_varint<std::numeric_limits<T>::digits, InputIt, T>(std::move(first), std::move(last), i);
|
return read_varint<std::numeric_limits<T>::digits, InputIt, T>(std::move(first), std::move(last), i);
|
||||||
|
|
Loading…
Reference in New Issue