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
@@ impl ChainRuleSet {
pub fn apply(&self, c: &mut ApplyContext) -> bool {
- for rule in &self.rules {
- if rule.apply(c) {
- return true;
- }
- }
- false
+ let count = self.rules.len();
+
+ if cfg!(feature = "optimize_size") || count <= 4 {
+ return self.apply_slow(c);
+ }
+
+ let mut skippy = c.iter_input();
+ skippy.reset(c.buffer.idx);
+ skippy.set_match_func(Some(match_always), None);
+
+ let mut unsafe_to = 0;
+ let matched = skippy.next(&mut unsafe_to);
+ if !matched {
+ return self.apply_slow(c);
+ }
+
+ let first = c.buffer.info[skippy.idx].codepoint;
+ let may_skip = skippy.may_skip(&c.buffer.info[skippy.idx]);
+ if may_skip {
+ return self.apply_slow(c);
+ }
+
+ let mut unsafe_to_concat = false;
+ for rule in &self.rules {
+ if rule.input_len() <= 1 || rule.first_input() == Some(first) {
+ if rule.apply(c) {
+ if unsafe_to_concat {
+ c.buffer.unsafe_to_concat(c.buffer.idx, unsafe_to);
+ }
+ return true;
+ }
+ } else if rule.input_len() > 1 {
+ unsafe_to_concat = true;
+ }
+ }
+ if unsafe_to_concat {
+ c.buffer.unsafe_to_concat(c.buffer.idx, unsafe_to);
+ }
+ false
+ }
+
+ fn apply_slow(&self, c: &mut ApplyContext) -> bool {
+ for rule in &self.rules {
+ if rule.apply(c) {
+ return true;
+ }
+ }
+ false
}
}