as_nibbles

This commit is contained in:
debris 2015-11-29 12:39:37 +01:00
parent 96e1580ddf
commit 1f6ae56468

View File

@ -4,7 +4,6 @@
//use hash::*; //use hash::*;
//use rlp; //use rlp;
///
/// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1. /// Hex-prefix Notation. First nibble has flags: oddness = 2^0 & termination = 2^1.
/// ///
/// The "termination marker" and "leaf-node" specifier are completely equivalent. /// The "termination marker" and "leaf-node" specifier are completely equivalent.
@ -57,8 +56,8 @@
/// } /// }
/// ``` /// ```
/// ///
pub fn hex_prefix_encode(hex: &[u8], leaf: bool) -> Vec<u8> { pub fn hex_prefix_encode(nibbles: &[u8], leaf: bool) -> Vec<u8> {
let inlen = hex.len(); let inlen = nibbles.len();
let oddness_factor = inlen % 2; let oddness_factor = inlen % 2;
// next even number divided by two // next even number divided by two
let reslen = (inlen + 2) >> 1; let reslen = (inlen + 2) >> 1;
@ -68,7 +67,7 @@ pub fn hex_prefix_encode(hex: &[u8], leaf: bool) -> Vec<u8> {
let first_byte = { let first_byte = {
let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4; let mut bits = ((inlen as u8 & 1) + (2 * leaf as u8)) << 4;
if oddness_factor == 1 { if oddness_factor == 1 {
bits += hex[0]; bits += nibbles[0];
} }
bits bits
}; };
@ -77,7 +76,7 @@ pub fn hex_prefix_encode(hex: &[u8], leaf: bool) -> Vec<u8> {
let mut offset = oddness_factor; let mut offset = oddness_factor;
while offset < inlen { while offset < inlen {
let byte = (hex[offset] << 4) + hex[offset + 1]; let byte = (nibbles[offset] << 4) + nibbles[offset + 1];
res.push(byte); res.push(byte);
offset += 2; offset += 2;
} }
@ -85,6 +84,28 @@ pub fn hex_prefix_encode(hex: &[u8], leaf: bool) -> Vec<u8> {
res res
} }
/// Converts slice of bytes to nibbles.
///
/// ```rust
/// extern crate ethcore_util as util;
/// use util::triehash::*;
///
/// fn main () {
/// let v = vec![0x31, 0x23, 0x45];
/// let e = vec![3, 1, 2, 3, 4, 5];
/// assert_eq!(as_nibbles(&v), e);
/// }
/// ```
pub fn as_nibbles(bytes: &[u8]) -> Vec<u8> {
let mut res = vec![];
res.reserve(bytes.len() * 2);
for i in 0..bytes.len() {
res.push(bytes[i] >> 4);
res.push((bytes[i] << 4) >> 4);
}
res
}
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
} }