From 6d091c29cd160ffddf870929bfb3e601fa01031a Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 31 Mar 2020 19:56:02 +0100 Subject: Upgrade annotate-snippets version --- dhall/src/syntax/ast/span.rs | 10 ++-------- 1 file changed, 2 insertions(+), 8 deletions(-) (limited to 'dhall/src/syntax') diff --git a/dhall/src/syntax/ast/span.rs b/dhall/src/syntax/ast/span.rs index 153a624..79bcc52 100644 --- a/dhall/src/syntax/ast/span.rs +++ b/dhall/src/syntax/ast/span.rs @@ -76,18 +76,12 @@ impl Span { /// Convert a byte idx into a string into a char idx for consumption by annotate_snippets. fn char_idx_from_byte_idx(input: &str, idx: usize) -> usize { - let char_idx = input + input .char_indices() .enumerate() .find(|(_, (i, _))| *i == idx) .map(|(i, (_, _))| i) // We should be able to unwrap() here, but somehow it panics on an example from // serde_dhall/lib.rs... - .unwrap_or(0); - // Unix-style newlines are counted as two chars (see - // https://github.com/rust-lang/annotate-snippets-rs/issues/24). - let nbr_newlines = input[..idx].chars().filter(|c| *c == '\n').count(); - let nbr_carriage_returns = - input[..idx].chars().filter(|c| *c == '\r').count(); - char_idx + nbr_newlines - nbr_carriage_returns + .unwrap_or(0) } -- cgit v1.2.3 From e12fb2928307d240461e749e5d371cef17cf520c Mon Sep 17 00:00:00 2001 From: Nadrieril Date: Tue, 31 Mar 2020 20:10:49 +0100 Subject: Fix indexing for spans that go to the end of the input --- dhall/src/syntax/ast/span.rs | 12 +++++++----- 1 file changed, 7 insertions(+), 5 deletions(-) (limited to 'dhall/src/syntax') diff --git a/dhall/src/syntax/ast/span.rs b/dhall/src/syntax/ast/span.rs index 79bcc52..2e09863 100644 --- a/dhall/src/syntax/ast/span.rs +++ b/dhall/src/syntax/ast/span.rs @@ -75,13 +75,15 @@ impl Span { } /// Convert a byte idx into a string into a char idx for consumption by annotate_snippets. +/// The byte idx must be at a char boundary. fn char_idx_from_byte_idx(input: &str, idx: usize) -> usize { + use std::iter::once; input .char_indices() + .map(|(byte_i, _)| byte_i) // We don't care about the char + .chain(once(input.len())) // In case the idx points to the end of the string .enumerate() - .find(|(_, (i, _))| *i == idx) - .map(|(i, (_, _))| i) - // We should be able to unwrap() here, but somehow it panics on an example from - // serde_dhall/lib.rs... - .unwrap_or(0) + .find(|(_, byte_i)| *byte_i == idx) + .map(|(char_i, _)| char_i) + .unwrap() } -- cgit v1.2.3