pub fn crop_bits(buffer: Vec<u8>, bit_length: isize) -> Vec<u8> {
    if bit_length == 0 {
        return buffer;
    }

    let is_positive = bit_length > 0;
    let u_bit_length = bit_length as usize;

    let (byte_length, clear_bits) = match is_positive {
        true => {
            let length = byte_length(bit_length);
            if length > buffer.len() as isize {
                return buffer[0..length as usize].to_vec();
            }
            (length as usize, BYTE - (u_bit_length % BYTE))
        }
        false => {
            let length: isize = buffer.len() as isize + (bit_length / (BYTE as isize));
            if length < 1 {
                return vec![0];
            }
            (length as usize, u_bit_length)
        }
    };

    let mut cropped_buffer = buffer[0..byte_length].to_vec();

    /* Clear unused bits in last byte */
    if clear_bits < BYTE {
        cropped_buffer[byte_length - 1] &= 0xFF << clear_bits;
    }

    cropped_buffer
}
/**
 * Shortens the byte array to fit the length of the used bits
 */
Shortens the byte array to fit the length of the used bits
Shortens the byte array to fit the length of the used bits
pub fn crop_bits(buffer: Vec<u8>, bit_length: isize) -> Vec<u8> {
    if bit_length == 0 {
        return buffer;
    }
    let is_positive = bit_length > 0;
    let u_bit_length = bit_length as usize;
    let (byte_length, clear_bits) = match is_positive {
        true => {
            let length = byte_length(bit_length);
            if length > buffer.len() as isize {
                return buffer[0..length as usize].to_vec();
            }
            (length as usize, BYTE - (u_bit_length % BYTE))
        }
        false => {
            let length: isize = buffer.len() as isize + (bit_length / (BYTE as isize));
            if length < 1 {
                return vec![0];
            }
            (length as usize, u_bit_length)
        }
    };
    let mut cropped_buffer = buffer[0..byte_length].to_vec();
    /* Clear unused bits in last byte */
    if clear_bits < BYTE {
        cropped_buffer[byte_length - 1] &= 0xFF << clear_bits;
    }
    cropped_buffer
}
pub fn crop_bits ( buffer : Vec < u8 > , bit_length : isize ) -> Vec < u8 > { if bit_length == 0 { return buffer ; } let is_positive = bit_length > 0 ; let u_bit_length = bit_length as usize ; let ( byte_length , clear_bits ) = match is_positive { true => { let length = byte_length ( bit_length ) ; if length > buffer . len ( ) as isize { return buffer [ 0 .. length as usize ] . to_vec ( ) ; } ( length as usize , BYTE - ( u_bit_length % BYTE ) ) } false => { let length : isize = buffer . len ( ) as isize + ( bit_length / ( BYTE as isize ) ) ; if length < 1 { return vec ! [ 0 ] ; } ( length as usize , u_bit_length ) } } ; let mut cropped_buffer = buffer [ 0 .. byte_length ] . to_vec ( ) ; /* Clear unused bits in last byte */ if clear_bits < BYTE { cropped_buffer [ byte_length - 1 ] &= 0xFF << clear_bits ; } cropped_buffer }
Shortens the byte array to fit the length of the used bits
Shortens the byte array to fit the length of the used bits
buffer
Vec<u8>
bit_length
isize
buffer
Vec<u8>
bit_length
isize