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
use crate::buffer_proxy_iterator::BufferProxyIterator;
use crate::radix::Node;
use crate::{weights_to_type, Type};
use std::hash::{Hash, Hasher};
#[derive(Clone)]
pub(crate) struct Match {
/// The word being matched.
pub(crate) node: &'static Node,
/// Stores the index in the string when this match was created.
pub(crate) start: usize,
// Stores the index in the string when this match was completed.
pub(crate) end: usize,
/// Stores the last matched character.
pub(crate) last: char,
/// Whether the match was preceded by a separator.
pub(crate) space_before: bool,
/// Whether the match was followed by a separator.
pub(crate) space_after: bool,
/// Stores how many spaces appeared within the match, excluding spaces that directly correspond to the pattern.
pub(crate) spaces: u8,
}
impl Match {
/// Combines in a way that the order of matches doesn't matter.
pub(crate) fn combine(&self, other: &Self) -> Self {
Self {
start: self.start.min(other.start),
spaces: self.spaces.min(other.spaces),
last: self.last.min(other.last),
..*self
}
}
pub(crate) fn commit<I: Iterator<Item = char>>(
&self,
weights: &mut [i8; 4],
spy: &BufferProxyIterator<I>,
censor_threshold: Type,
censor_first_character_threshold: Type,
censor_replacement: char,
) {
/*
#[cfg(debug_assertions)]
{
println!(
"matching \"{}\" b={} m={} a={}",
self.node.phrase, self.space_before, self.spaces, self.space_after
);
}
*/
//let length = m.end - m.start;
if !(self.space_before && self.space_after)
&& self.spaces as usize + 4 > self.node.depth as usize
{
// Match isn't strong enough.
return;
}
// Apply weights.
for (i, weight) in self.node.weights.iter().enumerate() {
weights[i] = weights[i].saturating_add(*weight);
}
let typ = weights_to_type(&self.node.weights);
if typ.isnt(censor_threshold) {
// Match isn't severe enough to censor.
return;
}
// Censor.
let offset = if typ.is(censor_first_character_threshold) {
0
} else {
1
};
spy.censor(self.start + offset..=self.end, censor_replacement);
}
}
impl PartialEq for Match {
fn eq(&self, other: &Self) -> bool {
std::ptr::eq(self.node, other.node) && self.space_before == other.space_before
}
}
impl Eq for Match {}
impl Hash for Match {
fn hash<H: Hasher>(&self, state: &mut H) {
state.write_usize(self.node as *const _ as usize);
state.write_u8(self.space_before as u8);
}
}