Skip to content

Instantly share code, notes, and snippets.

@ebresafegaga
Created October 17, 2022 13:51
Show Gist options
  • Save ebresafegaga/0b129eed379ae3e3924fa88ce882ac62 to your computer and use it in GitHub Desktop.
Save ebresafegaga/0b129eed379ae3e3924fa88ce882ac62 to your computer and use it in GitHub Desktop.
Offset
fn calculate_offset(c: u32, l: u32, t: &String) -> usize {
let (c, l) = (c as usize, l as usize);
// Line is 0 indexed so we don't need to skip the
// line that the cursor is currently on.
// It will be done automatically during the `take`, because
// of the 0 index.
let count = t
.split('\n')
.take(l)
.collect::<Vec<_>>()
.iter()
.rev()
.map(|s| s.len()) // len() includes the new line character for us
.sum::<usize>();
let c = c; //- 1; // adjust because of 0 index
count + c
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment