summaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorMatthias Beyer <matthias.beyer@ifm.com>2022-01-27 14:38:04 +0100
committerMatthias Beyer <matthias.beyer@ifm.com>2022-02-24 09:38:50 +0100
commitff4ddaab9cecc23b774b02795104ebef8ff4a63c (patch)
treea6f2264ac6a01cd4f6802065cc4287ef7a347884
parent5b918722498871637163d32aa4d4aaa2808394e8 (diff)
Simplify implementation of thin_edge_json::utils::excerpt
Signed-off-by: Matthias Beyer <matthias.beyer@ifm.com>
-rw-r--r--crates/core/thin_edge_json/src/utils.rs47
1 files changed, 9 insertions, 38 deletions
diff --git a/crates/core/thin_edge_json/src/utils.rs b/crates/core/thin_edge_json/src/utils.rs
index c4fff151..76fe6fe4 100644
--- a/crates/core/thin_edge_json/src/utils.rs
+++ b/crates/core/thin_edge_json/src/utils.rs
@@ -1,43 +1,14 @@
/// Returns a substring of `s` starting at `line` and `column`. At most `max_chars` are returned.
pub(crate) fn excerpt(s: &str, line: usize, column: usize, max_chars: usize) -> String {
- let mut current_line = 1;
- let mut chars = s.chars();
-
- while current_line < line {
- match chars.next() {
- Some(ch) => {
- if ch == '\n' {
- current_line += 1;
- }
- }
- None => {
- break;
- }
- }
- }
-
- // Seek forward. We cannot use `skip`, as we then can no longer call `as_str`.
- let mut current_column = 1;
- while current_column < column {
- match chars.next() {
- Some(_) => {
- current_column += 1;
- }
- None => break,
- }
- }
-
- // Don't use byte slicing for UTF-8 strings, e.g. `[..80]`, as this might panic in case of a
- // wide-character at this position.
- let mut excerpt = String::with_capacity(max_chars);
- for _i in 1..=max_chars {
- match chars.next() {
- Some(ch) => excerpt.push(ch),
- None => break,
- }
- }
-
- excerpt
+ s.lines() // omits the newlines
+ .skip(line - 1)
+ .map(|line| {
+ line.chars().chain(std::iter::once('\n')) // adds the newlines again
+ })
+ .flatten()
+ .skip(column - 1)
+ .take(max_chars)
+ .collect()
}
#[test]