Skip to content

Commit

Permalink
Remove unnecessary cloning
Browse files Browse the repository at this point in the history
  • Loading branch information
Kuuuube committed Jul 27, 2023
1 parent 33f9ef3 commit 7eed39c
Show file tree
Hide file tree
Showing 3 changed files with 20 additions and 23 deletions.
24 changes: 12 additions & 12 deletions src/args_parser.rs
Original file line number Diff line number Diff line change
Expand Up @@ -14,19 +14,19 @@ pub fn parse_args() -> Option<Settings> {
match split[0].to_lowercase().as_str() {
"--help" => {help_message(); return None},
"-h" => {help_message(); return None},
"--mode" => {settings.mode = match safe_get_string(split, 1).as_str() {"generation" => Mode::Generation, "guessing" => Mode::Guessing, _ => Mode::Interactive}},
"--format" => {settings.format_string = safe_get_string(split, 1)},
"--prompt" => {settings.prompt = safe_get_string(split, 1)},
"--range" => {settings.range = parse_range(safe_get_string(split, 1))},
"--step" => {settings.raw_step = safe_get_string(split.clone(), 1); settings.step = safe_parse_f64(safe_get_string(split.clone(), 1)); if safe_get_string(split.clone(), 1).split(".").collect::<Vec<&str>>().len() == 2 {settings.step_decimal_len = safe_get_string(split, 1).len()}},
"--step-type" => {settings.step_type = match safe_get_string(split, 1).as_str() {"multiply" => StepType::Multiply, "exponent" => StepType::Exponent, _ => StepType::Add}},
"--mode" => {settings.mode = match safe_get_string(&split, 1).as_str() {"generation" => Mode::Generation, "guessing" => Mode::Guessing, _ => Mode::Interactive}},
"--format" => {settings.format_string = safe_get_string(&split, 1)},
"--prompt" => {settings.prompt = safe_get_string(&split, 1)},
"--range" => {settings.range = parse_range(safe_get_string(&split, 1))},
"--step" => {settings.raw_step = safe_get_string(&split, 1); settings.step = safe_parse_f64(safe_get_string(&split, 1)); if safe_get_string(&split, 1).split(".").collect::<Vec<&str>>().len() == 2 {settings.step_decimal_len = safe_get_string(&split, 1).len()}},
"--step-type" => {settings.step_type = match safe_get_string(&split, 1).as_str() {"multiply" => StepType::Multiply, "exponent" => StepType::Exponent, _ => StepType::Add}},
"--precise" => {settings.precise = true},
"--round" => {settings.round = safe_parse_i64(safe_get_string(split, 1))}
"--output" => {settings.output = safe_get_string(split, 1)},
"--correct" => {settings.correct = safe_get_string(split, 1)},
"--incorrect" => {settings.incorrect = safe_get_string(split, 1)},
"--round" => {settings.round = safe_parse_i64(safe_get_string(&split, 1))}
"--output" => {settings.output = safe_get_string(&split, 1)},
"--correct" => {settings.correct = safe_get_string(&split, 1)},
"--incorrect" => {settings.incorrect = safe_get_string(&split, 1)},
"--weight" => {settings.weight = true},
"--max-decimal" => {settings.max_decimal = safe_parse_usize(safe_get_string(split.clone(), 1))}
"--max-decimal" => {settings.max_decimal = safe_parse_usize(safe_get_string(&split, 1))}
_ => {unknown_command_message(split[0]); return None}
}

Expand All @@ -48,7 +48,7 @@ fn parse_range(input: String) -> (String, String) {
return (range_vec.get(0).unwrap_or(&"").to_string(), range_vec.get(1).unwrap_or(&"").to_string());
}

fn safe_get_string(input: Vec<&str>, index: usize) -> String {
fn safe_get_string(input: &Vec<&str>, index: usize) -> String {
return input.get(index).unwrap_or(&"").to_string();
}

Expand Down
12 changes: 5 additions & 7 deletions src/modes/guessing.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,12 +11,11 @@ pub fn guessing_mode(settings: Settings) {
let input_string;

if settings.weight {
let range = settings.range.clone();
let digit_start = range.0.len();
let digits_end = range.1.len();
let digit_start = settings.range.0.len();
let digits_end = settings.range.1.len();

let start_float = safe_parse_f64(range.0);
let end_float = safe_parse_f64(range.1);
let start_float = safe_parse_f64(&settings.range.0);
let end_float = safe_parse_f64(&settings.range.1);

let digit = rand::thread_rng().gen_range(digit_start..=digits_end);

Expand All @@ -38,8 +37,7 @@ pub fn guessing_mode(settings: Settings) {
let number = rand::thread_rng().gen_range(minimum_number..=maximum_number);
input_string = clean_f64_to_string(number, settings.max_decimal);
} else {
let range = settings.range.clone();
let number = rand::thread_rng().gen_range(safe_parse_f64(range.0)..=safe_parse_f64(range.1));
let number = rand::thread_rng().gen_range(safe_parse_f64(&settings.range.0)..=safe_parse_f64(&settings.range.1));
input_string = clean_f64_to_string(number, settings.max_decimal)
}

Expand Down
7 changes: 3 additions & 4 deletions src/utils.rs
Original file line number Diff line number Diff line change
Expand Up @@ -18,8 +18,7 @@ pub fn bigdecimal_powf(x: BigDecimal, e: &BigDecimal) -> BigDecimal {
let simplified_denominator = denominator / gcd;

let whole_result = bigdecimal_powi(&x.round(50), &BigDecimal::from_u32(simplified_numerator).unwrap()).round(50);
let result = bigdecimal_root(BigDecimal::from_u32(simplified_denominator).unwrap(), whole_result.clone());
return result;
return bigdecimal_root(BigDecimal::from_u32(simplified_denominator).unwrap(), whole_result);
} else {
return bigdecimal_powi(&x, &whole_value);
}
Expand All @@ -38,7 +37,7 @@ pub fn bigdecimal_powi(x: &BigDecimal, e: &BigDecimal) -> BigDecimal {
let mut r = BigDecimal::from_str("1").unwrap();
let mut i = BigDecimal::zero();
while i < *e {
r *= x.clone();
r *= x;
i += 1;
}
return r;
Expand Down Expand Up @@ -79,7 +78,7 @@ pub fn clean_f64_to_string(float: f64, rounding: usize) -> String {
return rounded;
}

pub fn safe_parse_f64(input: String) -> f64 {
pub fn safe_parse_f64(input: &str) -> f64 {
return input.parse::<f64>().unwrap_or_default();
}

Expand Down

0 comments on commit 7eed39c

Please sign in to comment.