Skip to content

Commit f1e5093

Browse files
committed
uucore: split get_primitive_dec into different functions
1 parent c5528c0 commit f1e5093

File tree

1 file changed

+52
-19
lines changed

1 file changed

+52
-19
lines changed

src/uucore/src/lib/features/tokenize/num_format/formatters/float_common.rs

Lines changed: 52 additions & 19 deletions
Original file line numberDiff line numberDiff line change
@@ -265,33 +265,31 @@ fn round_terminal_digit(
265265
(before_dec, after_dec, false)
266266
}
267267

268-
#[allow(clippy::cognitive_complexity)]
269-
pub fn get_primitive_dec(
270-
initial_prefix: &InitialPrefix,
271-
str_in: &str,
272-
analysis: &FloatAnalysis,
273-
last_dec_place: usize,
274-
sci_mode: Option<bool>,
275-
) -> FormatPrimitive {
276-
let mut f = FormatPrimitive::default();
277-
268+
fn handle_sign(initial_prefix: &InitialPrefix) -> Option<String> {
278269
// add negative sign section
279270
if initial_prefix.sign == -1 {
280-
f.prefix = Some(String::from("-"));
271+
return Some(String::from("-"));
281272
}
273+
None
274+
}
282275

276+
fn split_string<'a>(analysis: &FloatAnalysis, str_in: &'a str) -> (&'a str, &'a str) {
283277
// assign the digits before and after the decimal points
284278
// to separate slices. If no digits after decimal point,
285279
// assign 0
286-
let (mut first_segment_raw, second_segment_raw) = match analysis.decimal_pos {
280+
match analysis.decimal_pos {
287281
Some(pos) => (&str_in[..pos], &str_in[pos + 1..]),
288282
None => (str_in, "0"),
289-
};
290-
if first_segment_raw.is_empty() {
291-
first_segment_raw = "0";
292283
}
284+
}
285+
286+
fn convert_to_base(
287+
initial_prefix: &InitialPrefix,
288+
first_segment_raw: &str,
289+
second_segment_raw: &str,
290+
) -> (String, String) {
293291
// convert to string, de_hexifying if input is in hex // spell-checker:disable-line
294-
let (first_segment, second_segment) = match initial_prefix.radix_in {
292+
match initial_prefix.radix_in {
295293
Base::Hex => (
296294
de_hex(first_segment_raw, true),
297295
de_hex(second_segment_raw, false),
@@ -300,8 +298,15 @@ pub fn get_primitive_dec(
300298
String::from(first_segment_raw),
301299
String::from(second_segment_raw),
302300
),
303-
};
304-
let (pre_dec_unrounded, post_dec_unrounded, mut mantissa) = if sci_mode.is_some() {
301+
}
302+
}
303+
304+
fn handle_sci_mode(
305+
first_segment: String,
306+
second_segment: String,
307+
sci_mode: Option<bool>,
308+
) -> (String, String, isize) {
309+
if sci_mode.is_some() {
305310
if first_segment.len() > 1 {
306311
let mut post_dec = String::from(&first_segment[1..]);
307312
post_dec.push_str(&second_segment);
@@ -339,10 +344,38 @@ pub fn get_primitive_dec(
339344
}
340345
} else {
341346
(first_segment, second_segment, 0)
342-
};
347+
}
348+
}
343349

350+
fn round_value(
351+
pre_dec_unrounded: String,
352+
post_dec_unrounded: String,
353+
last_dec_place: usize,
354+
) -> (String, String, bool) {
344355
let (pre_dec_draft, post_dec_draft, dec_place_chg) =
345356
round_terminal_digit(pre_dec_unrounded, post_dec_unrounded, last_dec_place - 1);
357+
(pre_dec_draft, post_dec_draft, dec_place_chg)
358+
}
359+
360+
pub fn get_primitive_dec(
361+
initial_prefix: &InitialPrefix,
362+
str_in: &str,
363+
analysis: &FloatAnalysis,
364+
last_dec_place: usize,
365+
sci_mode: Option<bool>,
366+
) -> FormatPrimitive {
367+
let mut f = FormatPrimitive::default();
368+
369+
f.prefix = handle_sign(initial_prefix);
370+
371+
let (first_segment_raw, second_segment_raw) = split_string(analysis, str_in);
372+
let (first_segment, second_segment) =
373+
convert_to_base(initial_prefix, first_segment_raw, second_segment_raw);
374+
let (pre_dec_unrounded, post_dec_unrounded, mut mantissa) =
375+
handle_sci_mode(first_segment, second_segment, sci_mode);
376+
let (pre_dec_draft, post_dec_draft, dec_place_chg) =
377+
round_value(pre_dec_unrounded, post_dec_unrounded, last_dec_place - 1);
378+
346379
f.post_decimal = Some(post_dec_draft);
347380
if let Some(capitalized) = sci_mode {
348381
let si_ind = if capitalized { 'E' } else { 'e' };

0 commit comments

Comments
 (0)