/rust/registry/src/index.crates.io-6f17d22bba15001f/unicode-bidi-0.3.8/src/explicit.rs
Line | Count | Source (jump to first uncovered line) |
1 | | // Copyright 2015 The Servo Project Developers. See the |
2 | | // COPYRIGHT file at the top-level directory of this distribution. |
3 | | // |
4 | | // Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or |
5 | | // http://www.apache.org/licenses/LICENSE-2.0> or the MIT license |
6 | | // <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your |
7 | | // option. This file may not be copied, modified, or distributed |
8 | | // except according to those terms. |
9 | | |
10 | | //! 3.3.2 Explicit Levels and Directions |
11 | | //! |
12 | | //! <http://www.unicode.org/reports/tr9/#Explicit_Levels_and_Directions> |
13 | | |
14 | | use alloc::vec::Vec; |
15 | | |
16 | | use super::char_data::{ |
17 | | is_rtl, |
18 | | BidiClass::{self, *}, |
19 | | }; |
20 | | use super::level::Level; |
21 | | |
22 | | /// Compute explicit embedding levels for one paragraph of text (X1-X8). |
23 | | /// |
24 | | /// `processing_classes[i]` must contain the `BidiClass` of the char at byte index `i`, |
25 | | /// for each char in `text`. |
26 | | #[cfg_attr(feature = "flame_it", flamer::flame)] |
27 | 0 | pub fn compute( |
28 | 0 | text: &str, |
29 | 0 | para_level: Level, |
30 | 0 | original_classes: &[BidiClass], |
31 | 0 | levels: &mut [Level], |
32 | 0 | processing_classes: &mut [BidiClass], |
33 | 0 | ) { |
34 | 0 | assert_eq!(text.len(), original_classes.len()); |
35 | | |
36 | | // <http://www.unicode.org/reports/tr9/#X1> |
37 | 0 | let mut stack = DirectionalStatusStack::new(); |
38 | 0 | stack.push(para_level, OverrideStatus::Neutral); |
39 | 0 |
|
40 | 0 | let mut overflow_isolate_count = 0u32; |
41 | 0 | let mut overflow_embedding_count = 0u32; |
42 | 0 | let mut valid_isolate_count = 0u32; |
43 | | |
44 | 0 | for (i, c) in text.char_indices() { |
45 | 0 | match original_classes[i] { |
46 | | // Rules X2-X5c |
47 | | RLE | LRE | RLO | LRO | RLI | LRI | FSI => { |
48 | 0 | let last_level = stack.last().level; |
49 | | |
50 | | // X5a-X5c: Isolate initiators get the level of the last entry on the stack. |
51 | 0 | let is_isolate = match original_classes[i] { |
52 | 0 | RLI | LRI | FSI => true, |
53 | 0 | _ => false, |
54 | | }; |
55 | 0 | if is_isolate { |
56 | 0 | levels[i] = last_level; |
57 | 0 | match stack.last().status { |
58 | 0 | OverrideStatus::RTL => processing_classes[i] = R, |
59 | 0 | OverrideStatus::LTR => processing_classes[i] = L, |
60 | 0 | _ => {} |
61 | | } |
62 | 0 | } |
63 | | |
64 | 0 | let new_level = if is_rtl(original_classes[i]) { |
65 | 0 | last_level.new_explicit_next_rtl() |
66 | | } else { |
67 | 0 | last_level.new_explicit_next_ltr() |
68 | | }; |
69 | 0 | if new_level.is_ok() && overflow_isolate_count == 0 && overflow_embedding_count == 0 |
70 | | { |
71 | 0 | let new_level = new_level.unwrap(); |
72 | 0 | stack.push( |
73 | 0 | new_level, |
74 | 0 | match original_classes[i] { |
75 | 0 | RLO => OverrideStatus::RTL, |
76 | 0 | LRO => OverrideStatus::LTR, |
77 | 0 | RLI | LRI | FSI => OverrideStatus::Isolate, |
78 | 0 | _ => OverrideStatus::Neutral, |
79 | | }, |
80 | | ); |
81 | 0 | if is_isolate { |
82 | 0 | valid_isolate_count += 1; |
83 | 0 | } else { |
84 | 0 | // The spec doesn't explicitly mention this step, but it is necessary. |
85 | 0 | // See the reference implementations for comparison. |
86 | 0 | levels[i] = new_level; |
87 | 0 | } |
88 | 0 | } else if is_isolate { |
89 | 0 | overflow_isolate_count += 1; |
90 | 0 | } else if overflow_isolate_count == 0 { |
91 | 0 | overflow_embedding_count += 1; |
92 | 0 | } |
93 | | } |
94 | | |
95 | | // <http://www.unicode.org/reports/tr9/#X6a> |
96 | | PDI => { |
97 | 0 | if overflow_isolate_count > 0 { |
98 | 0 | overflow_isolate_count -= 1; |
99 | 0 | } else if valid_isolate_count > 0 { |
100 | 0 | overflow_embedding_count = 0; |
101 | | loop { |
102 | | // Pop everything up to and including the last Isolate status. |
103 | 0 | match stack.vec.pop() { |
104 | | None |
105 | | | Some(Status { |
106 | | status: OverrideStatus::Isolate, |
107 | | .. |
108 | 0 | }) => break, |
109 | 0 | _ => continue, |
110 | | } |
111 | | } |
112 | 0 | valid_isolate_count -= 1; |
113 | 0 | } |
114 | 0 | let last = stack.last(); |
115 | 0 | levels[i] = last.level; |
116 | 0 | match last.status { |
117 | 0 | OverrideStatus::RTL => processing_classes[i] = R, |
118 | 0 | OverrideStatus::LTR => processing_classes[i] = L, |
119 | 0 | _ => {} |
120 | | } |
121 | | } |
122 | | |
123 | | // <http://www.unicode.org/reports/tr9/#X7> |
124 | | PDF => { |
125 | 0 | if overflow_isolate_count > 0 { |
126 | 0 | continue; |
127 | 0 | } |
128 | 0 | if overflow_embedding_count > 0 { |
129 | 0 | overflow_embedding_count -= 1; |
130 | 0 | continue; |
131 | 0 | } |
132 | 0 | if stack.last().status != OverrideStatus::Isolate && stack.vec.len() >= 2 { |
133 | 0 | stack.vec.pop(); |
134 | 0 | } |
135 | | // The spec doesn't explicitly mention this step, but it is necessary. |
136 | | // See the reference implementations for comparison. |
137 | 0 | levels[i] = stack.last().level; |
138 | | } |
139 | | |
140 | | // Nothing |
141 | 0 | B | BN => {} |
142 | | |
143 | | // <http://www.unicode.org/reports/tr9/#X6> |
144 | | _ => { |
145 | 0 | let last = stack.last(); |
146 | 0 | levels[i] = last.level; |
147 | 0 | match last.status { |
148 | 0 | OverrideStatus::RTL => processing_classes[i] = R, |
149 | 0 | OverrideStatus::LTR => processing_classes[i] = L, |
150 | 0 | _ => {} |
151 | | } |
152 | | } |
153 | | } |
154 | | |
155 | | // Handle multi-byte characters. |
156 | 0 | for j in 1..c.len_utf8() { |
157 | 0 | levels[i + j] = levels[i]; |
158 | 0 | processing_classes[i + j] = processing_classes[i]; |
159 | 0 | } |
160 | | } |
161 | 0 | } |
162 | | |
163 | | /// Entries in the directional status stack: |
164 | | struct Status { |
165 | | level: Level, |
166 | | status: OverrideStatus, |
167 | | } |
168 | | |
169 | 0 | #[derive(PartialEq)] |
170 | | enum OverrideStatus { |
171 | | Neutral, |
172 | | RTL, |
173 | | LTR, |
174 | | Isolate, |
175 | | } |
176 | | |
177 | | struct DirectionalStatusStack { |
178 | | vec: Vec<Status>, |
179 | | } |
180 | | |
181 | | impl DirectionalStatusStack { |
182 | 0 | fn new() -> Self { |
183 | 0 | DirectionalStatusStack { |
184 | 0 | vec: Vec::with_capacity(Level::max_explicit_depth() as usize + 2), |
185 | 0 | } |
186 | 0 | } |
187 | | |
188 | 0 | fn push(&mut self, level: Level, status: OverrideStatus) { |
189 | 0 | self.vec.push(Status { level, status }); |
190 | 0 | } |
191 | | |
192 | 0 | fn last(&self) -> &Status { |
193 | 0 | self.vec.last().unwrap() |
194 | 0 | } |
195 | | } |