| 1 | use std::borrow::Cow; |
| 2 | use snowball::Among; |
| 3 | |
| 4 | #[derive(Debug, Clone)] |
| 5 | pub struct SnowballEnv<'a> { |
| 6 | pub current: Cow<'a, str>, |
| 7 | pub cursor: usize, |
| 8 | pub limit: usize, |
| 9 | pub limit_backward: usize, |
| 10 | pub bra: usize, |
| 11 | pub ket: usize, |
| 12 | } |
| 13 | |
| 14 | |
| 15 | impl<'a> SnowballEnv<'a> { |
| 16 | pub fn create(value: &'a str) -> Self { |
| 17 | let len = value.len(); |
| 18 | SnowballEnv { |
| 19 | current: Cow::from(value), |
| 20 | cursor: 0, |
| 21 | limit: len, |
| 22 | limit_backward: 0, |
| 23 | bra: 0, |
| 24 | ket: len, |
| 25 | } |
| 26 | } |
| 27 | |
| 28 | pub fn get_current(self) -> Cow<'a, str> { |
| 29 | self.current |
| 30 | } |
| 31 | |
| 32 | fn replace_s(&mut self, bra: usize, ket: usize, s: &str) -> i32 { |
| 33 | let adjustment = s.len() as i32 - (ket as i32 - bra as i32); |
| 34 | let mut result = String::with_capacity(self.current.len()); |
| 35 | { |
| 36 | let (lhs, _) = self.current.split_at(bra); |
| 37 | let (_, rhs) = self.current.split_at(ket); |
| 38 | result.push_str(lhs); |
| 39 | result.push_str(s); |
| 40 | result.push_str(rhs); |
| 41 | } |
| 42 | // ... not very nice... |
| 43 | let new_lim = self.limit as i32 + adjustment; |
| 44 | self.limit = new_lim as usize; |
| 45 | if self.cursor >= ket { |
| 46 | let new_cur = self.cursor as i32 + adjustment; |
| 47 | self.cursor = new_cur as usize; |
| 48 | } else if self.cursor > bra { |
| 49 | self.cursor = bra |
| 50 | } |
| 51 | self.current = Cow::from(result); |
| 52 | adjustment |
| 53 | } |
| 54 | |
| 55 | /// Check if s is after cursor. |
| 56 | /// If so, move cursor to the end of s |
| 57 | pub fn eq_s(&mut self, s: &str) -> bool { |
| 58 | if self.cursor >= self.limit { |
| 59 | return false; |
| 60 | } |
| 61 | if self.current[self.cursor..].starts_with(s) { |
| 62 | self.cursor += s.len(); |
| 63 | while !self.current.is_char_boundary(self.cursor) { |
| 64 | self.cursor += 1; |
| 65 | } |
| 66 | true |
| 67 | } else { |
| 68 | false |
| 69 | } |
| 70 | } |
| 71 | |
| 72 | /// Check if 's' is before cursor |
| 73 | /// If so, move cursor to the beginning of s |
| 74 | pub fn eq_s_b(&mut self, s: &str) -> bool { |
| 75 | if (self.cursor as i32 - self.limit_backward as i32) < s.len() as i32 { |
| 76 | false |
| 77 | // Check if cursor -s.len is a char boundry. if not well... return false obv |
| 78 | } else if !self.current.is_char_boundary(self.cursor - s.len()) || |
| 79 | !self.current[self.cursor - s.len()..].starts_with(s) { |
| 80 | false |
| 81 | } else { |
| 82 | self.cursor -= s.len(); |
| 83 | true |
| 84 | } |
| 85 | } |
| 86 | |
| 87 | /// Replace string between `bra` and `ket` with s |
| 88 | pub fn slice_from(&mut self, s: &str) -> bool { |
| 89 | let (bra, ket) = (self.bra, self.ket); |
| 90 | self.replace_s(bra, ket, s); |
| 91 | true |
| 92 | } |
| 93 | |
| 94 | /// Move cursor to next charater |
| 95 | pub fn next_char(&mut self) { |
| 96 | self.cursor += 1; |
| 97 | while !self.current.is_char_boundary(self.cursor) { |
| 98 | self.cursor += 1; |
| 99 | } |
| 100 | } |
| 101 | |
| 102 | /// Move cursor to previous character |
| 103 | pub fn previous_char(&mut self) { |
| 104 | self.cursor -= 1; |
| 105 | while !self.current.is_char_boundary(self.cursor) { |
| 106 | self.cursor -= 1; |
| 107 | } |
| 108 | } |
| 109 | |
| 110 | pub fn byte_index_for_hop(&self, mut delta: i32) -> i32 { |
| 111 | if delta > 0 { |
| 112 | let mut res = self.cursor; |
| 113 | while delta > 0 { |
| 114 | res += 1; |
| 115 | delta -= 1; |
| 116 | while res <= self.current.len() && !self.current.is_char_boundary(res) { |
| 117 | res += 1; |
| 118 | } |
| 119 | } |
| 120 | return res as i32; |
| 121 | } else if delta < 0 { |
| 122 | let mut res: i32 = self.cursor as i32; |
| 123 | while delta < 0 { |
| 124 | res -= 1; |
| 125 | delta += 1; |
| 126 | while res >= 0 && !self.current.is_char_boundary(res as usize) { |
| 127 | res -= 1; |
| 128 | } |
| 129 | } |
| 130 | return res as i32; |
| 131 | } else { |
| 132 | return self.cursor as i32; |
| 133 | } |
| 134 | } |
| 135 | |
| 136 | // A grouping is represented by a minimum code point, a maximum code point, |
| 137 | // and a bitfield of which code points in that range are in the grouping. |
| 138 | // For example, in english.sbl, valid_LI is 'cdeghkmnrt'. |
| 139 | // The minimum and maximum code points are 99 and 116, |
| 140 | // so every time one of these grouping functions is called for g_valid_LI, |
| 141 | // min must be 99 and max must be 116. There are 18 code points within that |
| 142 | // range (inclusive) so the grouping is represented with 18 bits, plus 6 bits of padding: |
| 143 | // |
| 144 | // cdefghij klmnopqr st |
| 145 | // 11101100 10110001 01000000 |
| 146 | // |
| 147 | // The first bit is the least significant. |
| 148 | // Those three bytes become &[0b00110111, 0b10001101, 0b00000010], |
| 149 | // which is &[55, 141, 2], which is how g_valid_LI is defined in english.rs. |
| 150 | /// Check if the char the cursor points to is in the grouping |
| 151 | pub fn in_grouping(&mut self, chars: &[u8], min: u32, max: u32) -> bool { |
| 152 | if self.cursor >= self.limit { |
| 153 | return false; |
| 154 | } |
| 155 | if let Some(chr) = self.current[self.cursor..].chars().next() { |
| 156 | let mut ch = chr as u32; //codepoint as integer |
| 157 | if ch > max || ch < min { |
| 158 | return false; |
| 159 | } |
| 160 | ch -= min; |
| 161 | if (chars[(ch >> 3) as usize] & (0x1 << (ch & 0x7))) == 0 { |
| 162 | return false; |
| 163 | } |
| 164 | self.next_char(); |
| 165 | return true; |
| 166 | } |
| 167 | return false; |
| 168 | } |
| 169 | |
| 170 | pub fn in_grouping_b(&mut self, chars: &[u8], min: u32, max: u32) -> bool { |
| 171 | if self.cursor <= self.limit_backward { |
| 172 | return false; |
| 173 | } |
| 174 | self.previous_char(); |
| 175 | if let Some(chr) = self.current[self.cursor..].chars().next() { |
| 176 | let mut ch = chr as u32; //codepoint as integer |
| 177 | self.next_char(); |
| 178 | if ch > max || ch < min { |
| 179 | return false; |
| 180 | } |
| 181 | ch -= min; |
| 182 | if (chars[(ch >> 3) as usize] & (0x1 << (ch & 0x7))) == 0 { |
| 183 | return false; |
| 184 | } |
| 185 | self.previous_char(); |
| 186 | return true; |
| 187 | } |
| 188 | return false; |
| 189 | } |
| 190 | |
| 191 | pub fn out_grouping(&mut self, chars: &[u8], min: u32, max: u32) -> bool { |
| 192 | if self.cursor >= self.limit { |
| 193 | return false; |
| 194 | } |
| 195 | if let Some(chr) = self.current[self.cursor..].chars().next() { |
| 196 | let mut ch = chr as u32; //codepoint as integer |
| 197 | if ch > max || ch < min { |
| 198 | self.next_char(); |
| 199 | return true; |
| 200 | } |
| 201 | ch -= min; |
| 202 | if (chars[(ch >> 3) as usize] & (0x1 << (ch & 0x7))) == 0 { |
| 203 | self.next_char(); |
| 204 | return true; |
| 205 | } |
| 206 | } |
| 207 | return false; |
| 208 | } |
| 209 | |
| 210 | pub fn out_grouping_b(&mut self, chars: &[u8], min: u32, max: u32) -> bool { |
| 211 | if self.cursor <= self.limit_backward { |
| 212 | return false; |
| 213 | } |
| 214 | self.previous_char(); |
| 215 | if let Some(chr) = self.current[self.cursor..].chars().next() { |
| 216 | let mut ch = chr as u32; //codepoint as integer |
| 217 | self.next_char(); |
| 218 | if ch > max || ch < min { |
| 219 | self.previous_char(); |
| 220 | return true; |
| 221 | } |
| 222 | ch -= min; |
| 223 | if (chars[(ch >> 3) as usize] & (0x1 << (ch & 0x7))) == 0 { |
| 224 | self.previous_char(); |
| 225 | return true; |
| 226 | } |
| 227 | } |
| 228 | return false; |
| 229 | |
| 230 | } |
| 231 | |
| 232 | |
| 233 | /// Helper function that removes the string slice between `bra` and `ket` |
| 234 | pub fn slice_del(&mut self) -> bool { |
| 235 | self.slice_from("") |
| 236 | } |
| 237 | |
| 238 | pub fn insert(&mut self, bra: usize, ket: usize, s: &str) { |
| 239 | let adjustment = self.replace_s(bra, ket, s); |
| 240 | if bra <= self.bra { |
| 241 | self.bra = (self.bra as i32 + adjustment) as usize; |
| 242 | } |
| 243 | if bra <= self.ket { |
| 244 | self.ket = (self.ket as i32 + adjustment) as usize; |
| 245 | } |
| 246 | } |
| 247 | |
| 248 | pub fn slice_to(&mut self) -> String { |
| 249 | self.current[self.bra..self.ket].to_string() |
| 250 | } |
| 251 | |
| 252 | pub fn find_among<T>(&mut self, amongs: &[Among<T>], context: &mut T) -> i32 { |
| 253 | use std::cmp::min; |
| 254 | let mut i: i32 = 0; |
| 255 | let mut j: i32 = amongs.len() as i32; |
| 256 | |
| 257 | let c = self.cursor; |
| 258 | let l = self.limit; |
| 259 | |
| 260 | let mut common_i = 0; |
| 261 | let mut common_j = 0; |
| 262 | |
| 263 | let mut first_key_inspected = false; |
| 264 | loop { |
| 265 | let k = i + ((j - i) >> 1); |
| 266 | let mut diff: i32 = 0; |
| 267 | let mut common = min(common_i, common_j); |
| 268 | let w = &amongs[k as usize]; |
| 269 | for lvar in common..w.0.len() { |
| 270 | if c + common == l { |
| 271 | diff = -1; |
| 272 | break; |
| 273 | } |
| 274 | diff = self.current.as_bytes()[c + common] as i32 - w.0.as_bytes()[lvar] as i32; |
| 275 | if diff != 0 { |
| 276 | break; |
| 277 | } |
| 278 | common += 1; |
| 279 | } |
| 280 | if diff < 0 { |
| 281 | j = k; |
| 282 | common_j = common; |
| 283 | } else { |
| 284 | i = k; |
| 285 | common_i = common; |
| 286 | } |
| 287 | if j - i <= 1 { |
| 288 | if i > 0 { |
| 289 | break; |
| 290 | } |
| 291 | if j == i { |
| 292 | break; |
| 293 | } |
| 294 | if first_key_inspected { |
| 295 | break; |
| 296 | } |
| 297 | first_key_inspected = true; |
| 298 | } |
| 299 | } |
| 300 | |
| 301 | loop { |
| 302 | let w = &amongs[i as usize]; |
| 303 | if common_i >= w.0.len() { |
| 304 | self.cursor = c + w.0.len(); |
| 305 | if let Some(ref method) = w.3 { |
| 306 | let res = method(self, context); |
| 307 | self.cursor = c + w.0.len(); |
| 308 | if res { |
| 309 | return w.2; |
| 310 | } |
| 311 | } else { |
| 312 | return w.2; |
| 313 | } |
| 314 | } |
| 315 | i = w.1; |
| 316 | if i < 0 { |
| 317 | return 0; |
| 318 | } |
| 319 | } |
| 320 | } |
| 321 | |
| 322 | pub fn find_among_b<T>(&mut self, amongs: &[Among<T>], context: &mut T) -> i32 { |
| 323 | let mut i: i32 = 0; |
| 324 | let mut j: i32 = amongs.len() as i32; |
| 325 | |
| 326 | let c = self.cursor; |
| 327 | let lb = self.limit_backward; |
| 328 | |
| 329 | let mut common_i = 0; |
| 330 | let mut common_j = 0; |
| 331 | |
| 332 | let mut first_key_inspected = false; |
| 333 | |
| 334 | loop { |
| 335 | let k = i + ((j - i) >> 1); |
| 336 | let mut diff: i32 = 0; |
| 337 | let mut common = if common_i < common_j { |
| 338 | common_i |
| 339 | } else { |
| 340 | common_j |
| 341 | }; |
| 342 | let w = &amongs[k as usize]; |
| 343 | for lvar in (0..w.0.len() - common).rev() { |
| 344 | if c - common == lb { |
| 345 | diff = -1; |
| 346 | break; |
| 347 | } |
| 348 | diff = self.current.as_bytes()[c - common - 1] as i32 - w.0.as_bytes()[lvar] as i32; |
| 349 | if diff != 0 { |
| 350 | break; |
| 351 | } |
| 352 | common += 1; |
| 353 | } |
| 354 | if diff < 0 { |
| 355 | j = k; |
| 356 | common_j = common; |
| 357 | } else { |
| 358 | i = k; |
| 359 | common_i = common; |
| 360 | } |
| 361 | if j - i <= 1 { |
| 362 | if i > 0 { |
| 363 | break; |
| 364 | } |
| 365 | if j == i { |
| 366 | break; |
| 367 | } |
| 368 | if first_key_inspected { |
| 369 | break; |
| 370 | } |
| 371 | first_key_inspected = true; |
| 372 | } |
| 373 | } |
| 374 | loop { |
| 375 | let w = &amongs[i as usize]; |
| 376 | if common_i >= w.0.len() { |
| 377 | self.cursor = c - w.0.len(); |
| 378 | if let Some(ref method) = w.3 { |
| 379 | let res = method(self, context); |
| 380 | self.cursor = c - w.0.len(); |
| 381 | if res { |
| 382 | return w.2; |
| 383 | } |
| 384 | } else { |
| 385 | return w.2; |
| 386 | } |
| 387 | } |
| 388 | i = w.1; |
| 389 | if i < 0 { |
| 390 | return 0; |
| 391 | } |
| 392 | } |
| 393 | } |
| 394 | } |