Refine names for shorter code

This commit is contained in:
ChJR
2020-11-10 01:32:54 +09:00
parent fdeb817e5d
commit e95469e1c5
3 changed files with 15 additions and 15 deletions

View File

@@ -84,24 +84,24 @@ pub fn is_integer(v: f64) -> bool {
}
#[derive(Debug)]
pub enum FloatFormatCase {
pub enum Case {
Lower,
Upper,
}
fn format_nan(case: FloatFormatCase) -> String {
fn format_nan(case: Case) -> String {
let nan = match case {
FloatFormatCase::Lower => "nan",
FloatFormatCase::Upper => "NAN",
Case::Lower => "nan",
Case::Upper => "NAN",
};
nan.to_string()
}
fn format_inf(case: FloatFormatCase) -> String {
fn format_inf(case: Case) -> String {
let inf = match case {
FloatFormatCase::Lower => "inf",
FloatFormatCase::Upper => "INF",
Case::Lower => "inf",
Case::Upper => "INF",
};
inf.to_string()
@@ -109,7 +109,7 @@ fn format_inf(case: FloatFormatCase) -> String {
// Formats floats into Python style exponent notation, by first formatting in Rust style
// exponent notation (`1.0000e0`), then convert to Python style (`1.0000e+00`).
pub fn format_float_as_exponent(precision: usize, magnitude: f64, case: FloatFormatCase) -> String {
pub fn format_exponent(precision: usize, magnitude: f64, case: Case) -> String {
match magnitude {
magnitude if magnitude.is_finite() => {
let r_exp = format!("{:.*e}", precision, magnitude);

View File

@@ -329,11 +329,11 @@ impl CFormatSpec {
}
CFormatType::Float(CFloatType::Exponent(case)) => {
let case = match case {
CFormatCase::Lowercase => float_ops::FloatFormatCase::Lower,
CFormatCase::Uppercase => float_ops::FloatFormatCase::Upper,
CFormatCase::Lowercase => float_ops::Case::Lower,
CFormatCase::Uppercase => float_ops::Case::Upper,
};
let magnitude = num.abs();
float_ops::format_float_as_exponent(precision, magnitude, case)
float_ops::format_exponent(precision, magnitude, case)
}
CFormatType::Float(CFloatType::General(case)) => {
let precision = if precision == 0 { 1 } else { precision };

View File

@@ -375,15 +375,15 @@ impl FormatSpec {
Some(FormatType::GeneralFormatLower) => {
Err("Format code 'g' for object of type 'float' not implemented yet")
}
Some(FormatType::ExponentUpper) => Ok(float_ops::format_float_as_exponent(
Some(FormatType::ExponentUpper) => Ok(float_ops::format_exponent(
precision,
magnitude,
float_ops::FloatFormatCase::Upper,
float_ops::Case::Upper,
)),
Some(FormatType::ExponentLower) => Ok(float_ops::format_float_as_exponent(
Some(FormatType::ExponentLower) => Ok(float_ops::format_exponent(
precision,
magnitude,
float_ops::FloatFormatCase::Lower,
float_ops::Case::Lower,
)),
Some(FormatType::Percentage) => match magnitude {
magnitude if magnitude.is_nan() => Ok("nan%".to_owned()),