use super::{
error, finish_bits, BitStringValue, Produced, ProductionControl, ProductionVec, Result,
MAX_BIT_LENGTH,
};
pub fn from_integer_with_control(
value: i64,
modifier: i32,
control: &ProductionControl<'_>,
) -> Result<Produced<BitStringValue>> {
control.check()?;
let length = if modifier <= 0 || modifier as u32 > MAX_BIT_LENGTH {
1
} else {
modifier as u32
};
let mut bytes = ProductionVec::new(*control);
bytes.reserve(length.div_ceil(8) as usize)?;
for position in 0..length.div_ceil(8) {
let width = (length - position * 8).min(8);
let shift = length - position * 8 - width;
let chunk = if shift >= 64 {
if value < 0 {
u8::MAX
} else {
0
}
} else {
(value >> shift) as u8
};
bytes.push_copy(chunk << (8 - width))?;
}
finish_bits(bytes, length, control)
}
pub fn to_integer(value: &BitStringValue) -> Result<i32> {
if value.bit_len() > 32 {
return Err(error("22003", "integer out of range".into()));
}
Ok(to_unsigned(value) as i32)
}
pub fn to_bigint(value: &BitStringValue) -> Result<i64> {
if value.bit_len() > 64 {
return Err(error("22003", "bigint out of range".into()));
}
Ok(to_unsigned(value) as i64)
}
fn to_unsigned(value: &BitStringValue) -> u64 {
let mut output = 0_u64;
for byte in value.as_bytes() {
output = (output << 8) | u64::from(*byte);
}
output >> ((8 - value.bit_len() % 8) % 8)
}