refactor/printf ~ polish spelling (comments, names, and exceptions)

This commit is contained in:
Roy Ivy III 2021-05-30 18:34:39 -05:00
parent aa385cf23c
commit 324605c78c
13 changed files with 130 additions and 125 deletions

View file

@ -1,4 +1,4 @@
// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix hexifying glibc floatnum rten rhex arrnum fprim interp
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
//! Primitives used by Sub Tokenizer
//! and num_format modules

View file

@ -1,4 +1,3 @@
// spell-checker:ignore (ToDO) inprefix for conv
//! Primitives used by num_format and sub_modules.
//! never dealt with above (e.g. Sub Tokenizer never uses these)
@ -41,7 +40,7 @@ pub enum Base {
// information from the beginning of a numeric argument
// the precedes the beginning of a numeric value
pub struct InPrefix {
pub struct InitialPrefix {
pub radix_in: Base,
pub sign: i8,
pub offset: usize,
@ -54,7 +53,7 @@ pub trait Formatter {
fn get_primitive(
&self,
field: &FormatField,
inprefix: &InPrefix,
in_prefix: &InitialPrefix,
str_in: &str,
) -> Option<FormatPrimitive>;
// return a string from a FormatPrimitive,

View file

@ -1,4 +1,4 @@
// spell-checker:ignore (ToDO) arrnum mult basenum bufferval refd vals arrfloat conv intermed addl
// spell-checker:ignore (ToDO) arrnum arr_num mult basenum bufferval refd vals arrfloat conv intermed addl
pub fn arrnum_int_mult(arr_num: &[u8], basenum: u8, base_ten_int_fact: u8) -> Vec<u8> {
let mut carry: u16 = 0;

View file

@ -1,4 +1,4 @@
// spell-checker:ignore (ToDO) conv arrnum mult shortcircuit
// spell-checker:ignore (ToDO) arrnum mult
#[cfg(test)]
use super::*;
@ -29,7 +29,7 @@ fn test_arrnum_int_non_base_10() {
}
#[test]
fn test_arrnum_int_div_shortcircuit() {
fn test_arrnum_int_div_short_circuit() {
// (
let arrnum: Vec<u8> = vec![5, 5, 5, 5, 0];
let base_num = 10;

View file

@ -1,8 +1,9 @@
// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix hexifying glibc floatnum rten rhex arrnum
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
// spell-checker:ignore (ToDO) arrnum
//! formatter for %a %F C99 Hex-floating-point subs
use super::super::format_field::FormatField;
use super::super::formatter::{FormatPrimitive, Formatter, InPrefix};
use super::super::formatter::{FormatPrimitive, Formatter, InitialPrefix};
use super::base_conv;
use super::base_conv::RadixDef;
use super::float_common::{primitive_to_str_common, FloatAnalysis};
@ -20,15 +21,15 @@ impl Formatter for CninetyNineHexFloatf {
fn get_primitive(
&self,
field: &FormatField,
inprefix: &InPrefix,
initial_prefix: &InitialPrefix,
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);
FloatAnalysis::analyze(&str_in, initial_prefix, Some(second_field as usize), None, true);
let f = get_primitive_hex(
inprefix,
&str_in[inprefix.offset..],
initial_prefix,
&str_in[initial_prefix.offset..],
&analysis,
second_field as usize,
*field.field_char == 'A',
@ -44,13 +45,13 @@ impl Formatter for CninetyNineHexFloatf {
// on the todo list is to have a trait for get_primitive that is implemented by each float formatter and can override a default. when that happens we can take the parts of get_primitive_dec specific to dec and spin them out to their own functions that can be overridden.
fn get_primitive_hex(
inprefix: &InPrefix,
initial_prefix: &InitialPrefix,
_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 prefix = Some(String::from(if initial_prefix.sign == -1 { "-0x" } else { "0x" }));
// TODO actual conversion, make sure to get back mantissa.
// for hex to hex, it's really just a matter of moving the
@ -63,7 +64,7 @@ fn get_primitive_hex(
// the difficult part of this (arrnum_int_div_step) is already implemented.
// the hex float name may be a bit misleading in terms of how to go about the
// conversion. The best way to do it is to just convert the floatnum
// conversion. The best way to do it is to just convert the float number
// directly to base 2 and then at the end translate back to hex.
let mantissa = 0;
let suffix = Some({
@ -82,15 +83,15 @@ fn get_primitive_hex(
}
fn to_hex(src: &str, before_decimal: bool) -> String {
let rten = base_conv::RadixTen;
let rhex = base_conv::RadixHex;
let radix_ten = base_conv::RadixTen;
let radix_hex = base_conv::RadixHex;
if before_decimal {
base_conv::base_conv_str(src, &rten, &rhex)
base_conv::base_conv_str(src, &radix_ten, &radix_hex)
} else {
let as_arrnum_ten = base_conv::str_to_arrnum(src, &rten);
let as_arrnum_ten = base_conv::str_to_arrnum(src, &radix_ten);
let s = format!(
"{}",
base_conv::base_conv_float(&as_arrnum_ten, rten.get_max(), rhex.get_max())
base_conv::base_conv_float(&as_arrnum_ten, radix_ten.get_max(), radix_hex.get_max())
);
if s.len() > 2 {
String::from(&s[2..])

View file

@ -1,22 +1,22 @@
// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix hexifying glibc floatnum rten rhex arrnum fprim interp
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
//! formatter for %g %G decimal subs
use super::super::format_field::FormatField;
use super::super::formatter::{FormatPrimitive, Formatter, InPrefix};
use super::super::formatter::{FormatPrimitive, Formatter, InitialPrefix};
use super::float_common::{get_primitive_dec, primitive_to_str_common, FloatAnalysis};
fn get_len_fprim(fprim: &FormatPrimitive) -> usize {
fn get_len_fmt_primitive(fmt: &FormatPrimitive) -> usize {
let mut len = 0;
if let Some(ref s) = fprim.prefix {
if let Some(ref s) = fmt.prefix {
len += s.len();
}
if let Some(ref s) = fprim.pre_decimal {
if let Some(ref s) = fmt.pre_decimal {
len += s.len();
}
if let Some(ref s) = fprim.post_decimal {
if let Some(ref s) = fmt.post_decimal {
len += s.len();
}
if let Some(ref s) = fprim.suffix {
if let Some(ref s) = fmt.suffix {
len += s.len();
}
len
@ -33,22 +33,22 @@ impl Formatter for Decf {
fn get_primitive(
&self,
field: &FormatField,
inprefix: &InPrefix,
initial_prefix: &InitialPrefix,
str_in: &str,
) -> Option<FormatPrimitive> {
let second_field = field.second_field.unwrap_or(6) + 1;
// default to scif interp. so as to not truncate input vals
// default to scif interpretation so as to not truncate input vals
// (that would be displayed in scif) based on relation to decimal place
let analysis = FloatAnalysis::analyze(
str_in,
inprefix,
initial_prefix,
Some(second_field as usize + 1),
None,
false,
);
let mut f_sci = get_primitive_dec(
inprefix,
&str_in[inprefix.offset..],
initial_prefix,
&str_in[initial_prefix.offset..],
&analysis,
second_field as usize,
Some(*field.field_char == 'G'),
@ -70,13 +70,13 @@ impl Formatter for Decf {
}
}
let f_fl = get_primitive_dec(
inprefix,
&str_in[inprefix.offset..],
initial_prefix,
&str_in[initial_prefix.offset..],
&analysis,
second_field as usize,
None,
);
Some(if get_len_fprim(&f_fl) >= get_len_fprim(&f_sci) {
Some(if get_len_fmt_primitive(&f_fl) >= get_len_fmt_primitive(&f_sci) {
f_sci
} else {
f_fl

View file

@ -1,7 +1,8 @@
// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix hexifying glibc floatnum rten rhex arrnum
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
// spell-checker:ignore (ToDO) arrnum
use super::super::format_field::FormatField;
use super::super::formatter::{get_it_at, warn_incomplete_conv, Base, FormatPrimitive, InPrefix};
use super::super::formatter::{get_it_at, warn_incomplete_conv, Base, FormatPrimitive, InitialPrefix};
use super::base_conv;
use super::base_conv::RadixDef;
@ -39,7 +40,7 @@ fn has_enough_digits(
impl FloatAnalysis {
pub fn analyze(
str_in: &str,
inprefix: &InPrefix,
initial_prefix: &InitialPrefix,
max_sd_opt: Option<usize>,
max_after_dec_opt: Option<usize>,
hex_output: bool,
@ -47,13 +48,13 @@ impl FloatAnalysis {
// this fn assumes
// the input string
// has no leading spaces or 0s
let str_it = get_it_at(inprefix.offset, str_in);
let str_it = get_it_at(initial_prefix.offset, str_in);
let mut ret = FloatAnalysis {
len_important: 0,
decimal_pos: None,
follow: None,
};
let hex_input = match inprefix.radix_in {
let hex_input = match initial_prefix.radix_in {
Base::Hex => true,
Base::Ten => false,
Base::Octal => {
@ -126,15 +127,15 @@ impl FloatAnalysis {
}
fn de_hex(src: &str, before_decimal: bool) -> String {
let rten = base_conv::RadixTen;
let rhex = base_conv::RadixHex;
let radix_ten = base_conv::RadixTen;
let radix_hex = base_conv::RadixHex;
if before_decimal {
base_conv::base_conv_str(src, &rhex, &rten)
base_conv::base_conv_str(src, &radix_hex, &radix_ten)
} else {
let as_arrnum_hex = base_conv::str_to_arrnum(src, &rhex);
let as_arrnum_hex = base_conv::str_to_arrnum(src, &radix_hex);
let s = format!(
"{}",
base_conv::base_conv_float(&as_arrnum_hex, rhex.get_max(), rten.get_max())
base_conv::base_conv_float(&as_arrnum_hex, radix_hex.get_max(), radix_ten.get_max())
);
if s.len() > 2 {
String::from(&s[2..])
@ -200,7 +201,7 @@ fn round_terminal_digit(
}
pub fn get_primitive_dec(
inprefix: &InPrefix,
initial_prefix: &InitialPrefix,
str_in: &str,
analysis: &FloatAnalysis,
last_dec_place: usize,
@ -209,7 +210,7 @@ pub fn get_primitive_dec(
let mut f: FormatPrimitive = Default::default();
// add negative sign section
if inprefix.sign == -1 {
if initial_prefix.sign == -1 {
f.prefix = Some(String::from("-"));
}
@ -223,8 +224,8 @@ pub fn get_primitive_dec(
if first_segment_raw.is_empty() {
first_segment_raw = "0";
}
// convert to string, de_hexifying if input is in hex.
let (first_segment, second_segment) = match inprefix.radix_in {
// convert to string, de_hexifying if input is in hex // spell-checker:disable-line
let (first_segment, second_segment) = match initial_prefix.radix_in {
Base::Hex => (
de_hex(first_segment_raw, true),
de_hex(second_segment_raw, false),

View file

@ -1,8 +1,9 @@
// spell-checker:ignore (ToDO) floatf inprefix
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
// spell-checker:ignore (ToDO) arrnum
//! formatter for %f %F common-notation floating-point subs
use super::super::format_field::FormatField;
use super::super::formatter::{FormatPrimitive, Formatter, InPrefix};
use super::super::formatter::{FormatPrimitive, Formatter, InitialPrefix};
use super::float_common::{get_primitive_dec, primitive_to_str_common, FloatAnalysis};
pub struct Floatf;
@ -15,15 +16,15 @@ impl Formatter for Floatf {
fn get_primitive(
&self,
field: &FormatField,
inprefix: &InPrefix,
initial_prefix: &InitialPrefix,
str_in: &str,
) -> Option<FormatPrimitive> {
let second_field = field.second_field.unwrap_or(6) + 1;
let analysis =
FloatAnalysis::analyze(&str_in, inprefix, None, Some(second_field as usize), false);
FloatAnalysis::analyze(&str_in, initial_prefix, None, Some(second_field as usize), false);
let f = get_primitive_dec(
inprefix,
&str_in[inprefix.offset..],
initial_prefix,
&str_in[initial_prefix.offset..],
&analysis,
second_field as usize,
None,

View file

@ -1,11 +1,12 @@
// spell-checker:ignore (ToDO) fchar conv decr inprefix intf ints finalstr
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
// spell-checker:ignore (ToDO) arrnum
//! formatter for unsigned and signed int subs
//! unsigned ints: %X %x (hex u64) %o (octal u64) %u (base ten u64)
//! signed ints: %i %d (both base ten i64)
//! unsigned int: %X %x (hex u64) %o (octal u64) %u (base ten u64)
//! signed int: %i %d (both base ten i64)
use super::super::format_field::FormatField;
use super::super::formatter::{
get_it_at, warn_incomplete_conv, Base, FormatPrimitive, Formatter, InPrefix,
get_it_at, warn_incomplete_conv, Base, FormatPrimitive, Formatter, InitialPrefix,
};
use std::i64;
use std::u64;
@ -38,19 +39,19 @@ impl Intf {
// is_zero: true if number is zero, false otherwise
// len_digits: length of digits used to create the int
// important, for example, if we run into a non-valid character
fn analyze(str_in: &str, signed_out: bool, inprefix: &InPrefix) -> IntAnalysis {
fn analyze(str_in: &str, signed_out: bool, initial_prefix: &InitialPrefix) -> IntAnalysis {
// the maximum number of digits we could conceivably
// have before the decimal point without exceeding the
// max
let mut str_it = get_it_at(inprefix.offset, str_in);
let mut str_it = get_it_at(initial_prefix.offset, str_in);
let max_sd_in = if signed_out {
match inprefix.radix_in {
match initial_prefix.radix_in {
Base::Ten => 19,
Base::Octal => 21,
Base::Hex => 16,
}
} else {
match inprefix.radix_in {
match initial_prefix.radix_in {
Base::Ten => 20,
Base::Octal => 22,
Base::Hex => 16,
@ -118,13 +119,13 @@ impl Intf {
}
// get a FormatPrimitive of the maximum value for the field char
// and given sign
fn get_max(fchar: char, sign: i8) -> FormatPrimitive {
let mut fmt_prim: FormatPrimitive = Default::default();
fmt_prim.pre_decimal = Some(String::from(match fchar {
fn get_max(field_char: char, sign: i8) -> FormatPrimitive {
let mut fmt_primitive: FormatPrimitive = Default::default();
fmt_primitive.pre_decimal = Some(String::from(match field_char {
'd' | 'i' => match sign {
1 => "9223372036854775807",
_ => {
fmt_prim.prefix = Some(String::from("-"));
fmt_primitive.prefix = Some(String::from("-"));
"9223372036854775808"
}
},
@ -132,7 +133,7 @@ impl Intf {
'o' => "1777777777777777777777",
/* 'u' | */ _ => "18446744073709551615",
}));
fmt_prim
fmt_primitive
}
// conv_from_segment contract:
// 1. takes
@ -149,8 +150,8 @@ impl Intf {
// - if the string falls outside bounds:
// for i64 output, the int minimum or int max (depending on sign)
// for u64 output, the u64 max in the output radix
fn conv_from_segment(segment: &str, radix_in: Base, fchar: char, sign: i8) -> FormatPrimitive {
match fchar {
fn conv_from_segment(segment: &str, radix_in: Base, field_char: char, sign: i8) -> FormatPrimitive {
match field_char {
'i' | 'd' => match i64::from_str_radix(segment, radix_in as u32) {
Ok(i) => {
let mut fmt_prim: FormatPrimitive = Default::default();
@ -160,13 +161,13 @@ impl Intf {
fmt_prim.pre_decimal = Some(format!("{}", i));
fmt_prim
}
Err(_) => Intf::get_max(fchar, sign),
Err(_) => Intf::get_max(field_char, sign),
},
_ => match u64::from_str_radix(segment, radix_in as u32) {
Ok(u) => {
let mut fmt_prim: FormatPrimitive = Default::default();
let u_f = if sign == -1 { u64::MAX - (u - 1) } else { u };
fmt_prim.pre_decimal = Some(match fchar {
fmt_prim.pre_decimal = Some(match field_char {
'X' => format!("{:X}", u_f),
'x' => format!("{:x}", u_f),
'o' => format!("{:o}", u_f),
@ -174,7 +175,7 @@ impl Intf {
});
fmt_prim
}
Err(_) => Intf::get_max(fchar, sign),
Err(_) => Intf::get_max(field_char, sign),
},
}
}
@ -183,17 +184,17 @@ impl Formatter for Intf {
fn get_primitive(
&self,
field: &FormatField,
inprefix: &InPrefix,
initial_prefix: &InitialPrefix,
str_in: &str,
) -> Option<FormatPrimitive> {
let begin = inprefix.offset;
let begin = initial_prefix.offset;
// get information about the string. see Intf::Analyze
// def above.
let convert_hints = Intf::analyze(
str_in,
*field.field_char == 'i' || *field.field_char == 'd',
inprefix,
initial_prefix,
);
// We always will have a format primitive to return
Some(if convert_hints.len_digits == 0 || convert_hints.is_zero {
@ -209,22 +210,22 @@ impl Formatter for Intf {
'x' | 'X' => Base::Hex,
/* 'o' | */ _ => Base::Octal,
};
let radix_mismatch = !radix_out.eq(&inprefix.radix_in);
let decr_from_max: bool = inprefix.sign == -1 && *field.field_char != 'i';
let radix_mismatch = !radix_out.eq(&initial_prefix.radix_in);
let decrease_from_max: bool = initial_prefix.sign == -1 && *field.field_char != 'i';
let end = begin + convert_hints.len_digits as usize;
// convert to int if any one of these is true:
// - number of digits in int indicates it may be past max
// - we're subtracting from the max
// - we're converting the base
if convert_hints.check_past_max || decr_from_max || radix_mismatch {
if convert_hints.check_past_max || decrease_from_max || radix_mismatch {
// radix of in and out is the same.
let segment = String::from(&str_in[begin..end]);
Intf::conv_from_segment(
&segment,
inprefix.radix_in.clone(),
initial_prefix.radix_in.clone(),
*field.field_char,
inprefix.sign,
initial_prefix.sign,
)
} else {
// otherwise just do a straight string copy.
@ -233,20 +234,20 @@ impl Formatter for Intf {
// this is here and not earlier because
// zero doesn't get a sign, and conv_from_segment
// creates its format primitive separately
if inprefix.sign == -1 && *field.field_char == 'i' {
if initial_prefix.sign == -1 && *field.field_char == 'i' {
fmt_prim.prefix = Some(String::from("-"));
}
fmt_prim.pre_decimal = Some(String::from(&str_in[begin..end]));
fmt_prim
}
} else {
Intf::get_max(*field.field_char, inprefix.sign)
Intf::get_max(*field.field_char, initial_prefix.sign)
})
}
fn primitive_to_str(&self, prim: &FormatPrimitive, field: FormatField) -> String {
let mut finalstr: String = String::new();
let mut final_str: String = String::new();
if let Some(ref prefix) = prim.prefix {
finalstr.push_str(&prefix);
final_str.push_str(&prefix);
}
// integral second fields is zero-padded minimum-width
// which gets handled before general minimum-width
@ -256,11 +257,11 @@ impl Formatter for Intf {
let mut i = min;
let len = pre_decimal.len() as u32;
while i > len {
finalstr.push('0');
final_str.push('0');
i -= 1;
}
}
finalstr.push_str(&pre_decimal);
final_str.push_str(&pre_decimal);
}
None => {
panic!(
@ -269,6 +270,6 @@ impl Formatter for Intf {
);
}
}
finalstr
final_str
}
}

View file

@ -1,4 +1,4 @@
// spell-checker:ignore (ToDO) conv cninetyninehexfloatf floatf intf scif
// spell-checker:ignore (vars) charf cninetyninehexfloatf decf floatf intf scif strf Cninety
mod base_conv;
pub mod cninetyninehexfloatf;

View file

@ -1,8 +1,8 @@
// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety inprefix
// spell-checker:ignore (vars) charf cninetyninehexfloatf decf floatf intf scif strf Cninety
//! formatter for %e %E scientific notation subs
use super::super::format_field::FormatField;
use super::super::formatter::{FormatPrimitive, Formatter, InPrefix};
use super::super::formatter::{FormatPrimitive, Formatter, InitialPrefix};
use super::float_common::{get_primitive_dec, primitive_to_str_common, FloatAnalysis};
pub struct Scif;
@ -16,20 +16,20 @@ impl Formatter for Scif {
fn get_primitive(
&self,
field: &FormatField,
inprefix: &InPrefix,
initial_prefix: &InitialPrefix,
str_in: &str,
) -> Option<FormatPrimitive> {
let second_field = field.second_field.unwrap_or(6) + 1;
let analysis = FloatAnalysis::analyze(
str_in,
inprefix,
initial_prefix,
Some(second_field as usize + 1),
None,
false,
);
let f = get_primitive_dec(
inprefix,
&str_in[inprefix.offset..],
initial_prefix,
&str_in[initial_prefix.offset..],
&analysis,
second_field as usize,
Some(*field.field_char == 'E'),

View file

@ -1,12 +1,14 @@
// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety qchar topchar structs fmtr fchar inprefix devs octals cninetyninehexfloatf
// spell-checker:ignore (vars) charf cninetyninehexfloatf decf floatf intf scif strf Cninety
//! handles creating printed output for numeric substitutions
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
use std::env;
use std::vec::Vec;
use super::format_field::{FieldType, FormatField};
use super::formatter::{Base, FormatPrimitive, Formatter, InPrefix};
use super::formatter::{Base, FormatPrimitive, Formatter, InitialPrefix};
use super::formatters::cninetyninehexfloatf::CninetyNineHexFloatf;
use super::formatters::decf::Decf;
use super::formatters::floatf::Floatf;
@ -46,8 +48,8 @@ fn get_provided(str_in_opt: Option<&String>) -> Option<u8> {
match str_in_opt {
Some(str_in) => {
let mut byte_it = str_in.bytes();
if let Some(qchar) = byte_it.next() {
match qchar {
if let Some(ch) = byte_it.next() {
match ch {
C_S_QUOTE | C_D_QUOTE => {
Some(match byte_it.next() {
Some(second_byte) => {
@ -62,7 +64,7 @@ fn get_provided(str_in_opt: Option<&String>) -> Option<u8> {
}
// no byte after quote
None => {
let so_far = (qchar as u8 as char).to_string();
let so_far = (ch as u8 as char).to_string();
warn_expected_numeric(&so_far);
0_u8
}
@ -84,30 +86,30 @@ fn get_provided(str_in_opt: Option<&String>) -> Option<u8> {
// a base,
// and an offset for index after all
// initial spacing, sign, base prefix, and leading zeroes
fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix {
fn get_initial_prefix(str_in: &str, field_type: &FieldType) -> InitialPrefix {
let mut str_it = str_in.chars();
let mut ret = InPrefix {
let mut ret = InitialPrefix {
radix_in: Base::Ten,
sign: 1,
offset: 0,
};
let mut topchar = str_it.next();
// skip spaces and ensure topchar is the first non-space char
let mut top_char = str_it.next();
// skip spaces and ensure top_char is the first non-space char
// (or None if none exists)
while let Some(' ') = topchar {
while let Some(' ') = top_char {
ret.offset += 1;
topchar = str_it.next();
top_char = str_it.next();
}
// parse sign
match topchar {
match top_char {
Some('+') => {
ret.offset += 1;
topchar = str_it.next();
top_char = str_it.next();
}
Some('-') => {
ret.sign = -1;
ret.offset += 1;
topchar = str_it.next();
top_char = str_it.next();
}
_ => {}
}
@ -125,7 +127,7 @@ fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix {
// final offset. If the zero could be before
// a decimal point we don't move past the zero.
let mut is_hex = false;
if Some('0') == topchar {
if Some('0') == top_char {
if let Some(base) = str_it.next() {
// lead zeroes can only exist in
// octal and hex base
@ -152,7 +154,7 @@ fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix {
let mut first = true;
for ch_zero in str_it {
// see notes on offset above:
// this is why the offset for octals and decimals
// this is why the offset for octal and decimal numbers
// that reach this branch is 1 even though
// they have already eaten the characters '00'
// this is also why when hex encounters its
@ -194,21 +196,21 @@ fn get_inprefix(str_in: &str, field_type: &FieldType) -> InPrefix {
// if it is a numeric field, passing the field details
// and an iterator to the argument
pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option<String> {
let fchar = field.field_char;
let field_char = field.field_char;
// num format mainly operates by further delegating to one of
// several Formatter structs depending on the field
// see formatter.rs for more details
// to do switch to static dispatch
let fmtr: Box<dyn Formatter> = match *field.field_type {
let formatter: Box<dyn Formatter> = match *field.field_type {
FieldType::Intf => Box::new(Intf::new()),
FieldType::Floatf => Box::new(Floatf::new()),
FieldType::CninetyNineHexFloatf => Box::new(CninetyNineHexFloatf::new()),
FieldType::Scif => Box::new(Scif::new()),
FieldType::Decf => Box::new(Decf::new()),
_ => {
panic!("asked to do num format with non-num fieldtype");
panic!("asked to do num format with non-num field type");
}
};
let prim_opt=
@ -216,7 +218,7 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option<St
// few characters, use that value to create the FormatPrimitive
if let Some(provided_num) = get_provided(in_str_opt) {
let mut tmp : FormatPrimitive = Default::default();
match fchar {
match field_char {
'u' | 'i' | 'd' => {
tmp.pre_decimal = Some(
format!("{}", provided_num));
@ -231,11 +233,11 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option<St
},
'e' | 'E' | 'g' | 'G' => {
let as_str = format!("{}", provided_num);
let inprefix = get_inprefix(
let initial_prefix = get_initial_prefix(
&as_str,
&field.field_type
);
tmp=fmtr.get_primitive(field, &inprefix, &as_str)
tmp=formatter.get_primitive(field, &initial_prefix, &as_str)
.expect("err during default provided num");
},
_ => {
@ -254,14 +256,14 @@ pub fn num_format(field: &FormatField, in_str_opt: Option<&String>) -> Option<St
// first get information about the beginning of the
// numeric argument that would be useful for
// any formatter (int or float)
let inprefix = get_inprefix(
let initial_prefix = get_initial_prefix(
in_str,
&field.field_type
);
// then get the FormatPrimitive from the Formatter
fmtr.get_primitive(field, &inprefix, in_str)
formatter.get_primitive(field, &initial_prefix, in_str)
};
// if we have a formatPrimitive, print its results
// according to the field-char appropriate Formatter
prim_opt.map(|prim| fmtr.primitive_to_str(&prim, field.clone()))
prim_opt.map(|prim| formatter.primitive_to_str(&prim, field.clone()))
}

View file

@ -1,4 +1,4 @@
// spell-checker:ignore (ToDO) conv intf strf floatf scif charf fieldtype vals subparser unescaping submodule Cninety
// spell-checker:ignore (vars) charf decf floatf intf scif strf Cninety
//! Sub is a token that represents a
//! segment of the format string that is a substitution
@ -78,7 +78,7 @@ impl Sub {
'c' => FieldType::Charf,
_ => {
// should be unreachable.
println!("Invalid fieldtype");
println!("Invalid field type");
exit(cli::EXIT_ERR);
}
};
@ -130,7 +130,7 @@ impl SubParser {
}
}
fn build_token(parser: SubParser) -> Box<dyn token::Token> {
// not a self method so as to allow move of subparser vals.
// not a self method so as to allow move of sub-parser vals.
// return new Sub struct as token
let t: Box<dyn token::Token> = Box::new(Sub::new(
if parser.min_width_is_asterisk {
@ -354,7 +354,7 @@ impl token::Token for Sub {
// field char
let pre_min_width_opt: Option<String> = match *field.field_type {
// if %s just return arg
// if %b use UnescapedText module's unescaping-fn
// if %b use UnescapedText module's unescape-fn
// if %c return first char of arg
FieldType::Strf | FieldType::Charf => {
match pf_arg {