-
-
Notifications
You must be signed in to change notification settings - Fork 671
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Collapse overlapped highlighted ranges #1473
Conversation
src/snippet/mod.rs
Outdated
} | ||
|
||
let mut result = Vec::new(); | ||
let mut current = ranges[0].clone(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I am not sure but maybe it would be nicer to iterate only once, e.g.
let mut result = Vec::new();
let mut ranges = ranges.iter();
let mut current = match ranges.next() {
Some(range) => range.clone(),
None => return result,
};
for range in ranges {
...
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Sounds good. I changed so. Thanks.
@@ -186,6 +186,29 @@ fn select_best_fragment_combination(fragments: &[FragmentCandidate], text: &str) | |||
} | |||
} | |||
|
|||
/// Returns ranges that are collapsed into non-overlapped ranges. |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Please add your example in the comment.
src/snippet/mod.rs
Outdated
@@ -186,6 +186,29 @@ fn select_best_fragment_combination(fragments: &[FragmentCandidate], text: &str) | |||
} | |||
} | |||
|
|||
/// Returns ranges that are collapsed into non-overlapped ranges. | |||
fn collapse_overlapped_ranges(ranges: &Vec<Range<usize>>) -> Vec<Range<usize>> { |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
fn collapse_overlapped_ranges(ranges: &Vec<Range<usize>>) -> Vec<Range<usize>> { | |
fn collapse_overlapped_ranges(ranges: &[Range<usize>]) -> Vec<Range<usize>> { |
&Vec is almost never a good idea.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Another solution would be for you to pass an iterator directly.
impl Iterator<Item=Range<usize>>
src/snippet/mod.rs
Outdated
|
||
for range in ranges { | ||
if current.end > range.start { | ||
current = current.start..range.end; |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick... I feel the following is easier but that's your call.
current = current.start..range.end; | |
current.end = range.end; |
src/snippet/mod.rs
Outdated
/// Returns ranges that are collapsed into non-overlapped ranges. | ||
fn collapse_overlapped_ranges(ranges: &Vec<Range<usize>>) -> Vec<Range<usize>> { | ||
let mut result = Vec::new(); | ||
let mut ranges = ranges.iter(); |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
nitpick... I'm not fond of shadowing.
It is sometime useful to prevent the usage of a variable.
Here I would either call it ranges_it
, or pass an Iterator as argument.
@akr4 Awesome job spotting the job and fixing it! I had a bunch of rather minor comments. |
Codecov Report
@@ Coverage Diff @@
## main #1473 +/- ##
==========================================
+ Coverage 94.07% 94.09% +0.01%
==========================================
Files 239 239
Lines 44627 44833 +206
==========================================
+ Hits 41983 42185 +202
- Misses 2644 2648 +4
Help us with your feedback. Take ten seconds to tell us how you rate us. Have a feature suggestion? Share it here. |
NgramTokenizer with different min and max generates such ranges.
@fulmicoton Thank you very much for your review. I will make changes tomorrow. |
…ould be sorted, and debug_assert! that.
Committed some changes. The main ones are changing the argument from |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Approved! Thank you @akr4
@akr4 Can you run |
@fulmicoton Done. |
Hi. This fixes a problem that occurs when calling
Snippet.to_html()
and thesnippet.highlighted
are overlapped ranges like this:This type of ranges is generated by
NgramTokenizer
, for example.Error
In that case, there will be an error on calling
Snippet.to_html()
, which is caused by slicingString
with invalid indices.Fix
This PR collapses overlapped ranges into non-overlapped ones to fix the problem like this: