1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
//! The part of a matched message a search shows: a window around its first
//! match, with where each matched word falls in it.
use std::ops::Range;
/// FTS5 `highlight()` markers around each matched token, as the index's
/// excerpt queries ask for them (`char(30)`, `char(31)`).
pub(super) const OPEN: char = '\u{1e}';
pub(super) const CLOSE: char = '\u{1f}';
/// How much of a message a hit shows, in characters, and how many of them
/// lead its first match. The corpus median message is 575 characters, so a
/// window is about half of one.
const WINDOW_CHARS: usize = 320;
const LEAD_CHARS: usize = 80;
/// A window of a message around its first match.
#[derive(Debug, PartialEq, Eq)]
pub(super) struct Window {
/// Its first and past-last characters in the message.
pub start: usize,
pub end: usize,
pub total_chars: usize,
pub text: String,
/// The matched words in `text`, as byte ranges on char boundaries, in
/// order.
pub highlights: Vec<Range<usize>>,
}
/// The window of `text` around its first match, which `highlighted`, the
/// same text with [`OPEN`] and [`CLOSE`] around each match, marks.
///
/// Markers are found by walking both strings: a character `highlighted`
/// has where `text` does not is a marker. Indexed text holds no marker
/// characters (`search_evidence` replaces them), so neither can a match.
/// Text that holds them anyway still reads right outside a match: the
/// first [`CLOSE`] after an open is the marker, and a literal one before
/// FTS5's [`OPEN`] reads as text.
pub(super) fn window(text: &str, highlighted: &str) -> Window {
let mut matches = Vec::new();
let mut source = text.char_indices().peekable();
let mut at = 0;
let mut open = None;
for marked in highlighted.chars() {
if marked == CLOSE {
if let Some(start) = open.take() {
matches.push(start..at);
continue;
}
}
if let Some(&(index, char)) = source.peek() {
if char == marked {
source.next();
at = index + char.len_utf8();
continue;
}
}
match marked {
OPEN => open = Some(at),
// Closes nothing: `open` was taken above.
CLOSE => {}
// `highlighted` is not `text` marked; show the start unmarked.
_ => {
matches.clear();
break;
}
}
}
let first = matches
.first()
.map_or(0, |first| text[..first.start].chars().count());
let start = first.saturating_sub(LEAD_CHARS);
let byte_at = |chars: usize| {
text.char_indices()
.nth(chars)
.map_or(text.len(), |(index, _)| index)
};
let (from, to) = (byte_at(start), byte_at(start + WINDOW_CHARS));
let shown = &text[from..to];
let highlights = matches
.into_iter()
.filter_map(|found| {
let (begin, end) = (found.start.max(from), found.end.min(to));
(begin < end).then(|| begin - from..end - from)
})
.collect();
Window {
start,
end: start + shown.chars().count(),
total_chars: text.chars().count(),
text: shown.to_owned(),
highlights,
}
}
#[cfg(test)]
#[path = "search_window_tests.rs"]
mod tests;