use super::super::format_field::FormatField;
use super::super::formatter::{FormatPrimitive, Formatter, InPrefix};
use super::base_conv;
use super::base_conv::RadixDef;
use super::float_common::{primitive_to_str_common, FloatAnalysis};
pub struct CninetyNineHexFloatf {
as_num: f64,
}
impl CninetyNineHexFloatf {
pub fn new() -> CninetyNineHexFloatf {
CninetyNineHexFloatf { as_num: 0.0 }
}
}
impl Formatter for CninetyNineHexFloatf {
fn get_primitive(
&self,
field: &FormatField,
inprefix: &InPrefix,
str_in: &str,
) -> Option<FormatPrimitive> {
let second_field = field.second_field.unwrap_or(6) + 1;
let analysis =
FloatAnalysis::analyze(&str_in, inprefix, Some(second_field as usize), None, true);
let f = get_primitive_hex(
inprefix,
&str_in[inprefix.offset..],
&analysis,
second_field as usize,
*field.field_char == 'A',
);
Some(f)
}
fn primitive_to_str(&self, prim: &FormatPrimitive, field: FormatField) -> String {
primitive_to_str_common(prim, &field)
}
}
#[allow(unused_variables)]
#[allow(unused_assignments)]
fn get_primitive_hex(
inprefix: &InPrefix,
str_in: &str,
analysis: &FloatAnalysis,
last_dec_place: usize,
capitalized: bool,
) -> FormatPrimitive {
let prefix = Some(String::from(if inprefix.sign == -1 { "-0x" } else { "0x" }));
let (mut first_segment_raw, second_segment_raw) = match analysis.decimal_pos {
Some(pos) => (&str_in[..pos], &str_in[pos + 1..]),
None => (str_in, "0"),
};
if first_segment_raw.is_empty() {
first_segment_raw = "0";
}
let mantissa = 0;
let suffix = Some({
let ind = if capitalized { "P" } else { "p" };
if mantissa >= 0 {
format!("{}+{}", ind, mantissa)
} else {
format!("{}{}", ind, mantissa)
}
});
FormatPrimitive {
prefix,
suffix,
..Default::default()
}
}
fn to_hex(src: &str, before_decimal: bool) -> String {
let rten = base_conv::RadixTen;
let rhex = base_conv::RadixHex;
if before_decimal {
base_conv::base_conv_str(src, &rten, &rhex)
} else {
let as_arrnum_ten = base_conv::str_to_arrnum(src, &rten);
let s = format!(
"{}",
base_conv::base_conv_float(&as_arrnum_ten, rten.get_max(), rhex.get_max())
);
if s.len() > 2 {
String::from(&s[2..])
} else {
s
}
}
}