-
Notifications
You must be signed in to change notification settings - Fork 0
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
Showing
4 changed files
with
128 additions
and
39 deletions.
There are no files selected for viewing
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
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
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,2 +1,3 @@ | ||
pub mod char_pos_finder; | ||
pub mod line_break_pos_finder; | ||
pub mod line_map; |
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
pub fn build_line_map(content: &str) -> Vec<usize> { | ||
content.char_indices().fold(vec![], |mut acc, (byte_pos, c)| { | ||
if c == '\n' { | ||
acc.push(byte_pos) | ||
} | ||
|
||
acc | ||
}) | ||
} | ||
|
||
pub fn find_line(line_map: &[usize], needle: usize) -> usize { | ||
let found = line_map.iter().position(|v| *v > needle); | ||
|
||
if let Some(found) = found { | ||
found + 1 | ||
} else { | ||
line_map.len() + 1 | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
mod tests { | ||
use super::*; | ||
|
||
const CONTENT: &str = "abc+def+efg+hijkl+mnopq"; | ||
|
||
#[test] | ||
fn test_build_line_map() { | ||
assert_eq!(build_line_map(&CONTENT.replace('+', "\n")), vec![3, 7, 11, 17]); | ||
} | ||
|
||
#[test] | ||
fn test_find_line() { | ||
let mapped = build_line_map(&CONTENT.replace('+', "\n")); | ||
assert_eq!(find_line(&mapped, 9), 3); | ||
} | ||
} |