Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

End of year refactoring #75

Merged
merged 15 commits into from
Dec 7, 2022
Next Next commit
Refactor fn perform_addition
  • Loading branch information
Urhengulas committed Nov 28, 2022
commit b7f556848a4f1c02b4f1987eb7fa5dbf95b995f6
45 changes: 25 additions & 20 deletions src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -332,31 +332,36 @@ fn find_ram_in_linker_script(linker_script: &str) -> Option<MemoryEntry> {
/// Perform addition when ORIGN or LENGTH variables contain an addition.
/// If there is no addition to be performed, it will return the `u64` value.
fn perform_addition(line: &str) -> u64 {
let segments: Vec<&str> = line.split('+').map(|s| s.trim().trim_end()).collect();
let segments = line.split('+').map(|s| s.trim()).collect::<Vec<_>>();

let mut total_length = 0;
for segment in segments {
let boundary_pos = segment
.find(|c| c == 'K' || c == 'M')
.unwrap_or(segment.len());
const HEX: &str = "0x";
// Special case parsing for hex numbers.
let length: u64 = if segment.starts_with(HEX) {
tryc!(u64::from_str_radix(&segment[HEX.len()..boundary_pos], 16))
} else {
tryc!(segment[..boundary_pos].parse())
// Split number and the optional unit
let (number, unit) = match segment.find(['K', 'M']) {
Some(unit_pos) => {
let (number, unit) = segment.split_at(unit_pos);
(number, Some(unit))
}
None => (segment, None),
};

let raw = &segment[boundary_pos..];
let mut chars = raw.chars();
let unit = chars.next();
if unit == Some('K') {
total_length += length * 1024;
} else if unit == Some('M') {
total_length += length * 1024 * 1024;
} else if unit.is_none() {
total_length += length;
}
// Parse number
let (number, radix) = match number.strip_prefix("0x") {
Some(s) => (s, 16),
None => (number, 10),
};
let length = tryc!(u64::from_str_radix(number, radix));

// Handle unit
let multiplier = match unit {
Some("K") => 1024,
Some("M") => 1024 * 1024,
None => 1,
_ => unreachable!(),
};

// Add length
total_length += length * multiplier;
}
total_length
}
Expand Down