Created
October 17, 2022 13:51
-
-
Save ebresafegaga/0b129eed379ae3e3924fa88ce882ac62 to your computer and use it in GitHub Desktop.
Offset
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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