pyphen_rs/pyphen/
iter.rs

1// This file is part of pyphen-rs
2//
3// Copyright 2008 - Wilbert Berendsen <info@wilbertberendsen.nl>
4// Copyright 2012-2013 - Guillaume Ayoub <guillaume.ayoub@kozea.fr>
5// Copyright 2019 - Naresh Ganduri <gandurinaresh@gmail.com>
6//
7// This library is free software.  It is released under the
8// GPL 2.0+/LGPL 2.1+/MPL 1.1 tri-license.  See COPYING.GPL, COPYING.LGPL and
9// COPYING.MPL for more details.
10//
11// This library is distributed in the hope that it will be useful, but WITHOUT
12// ANY WARRANTY; without even the implied warranty of MERCHANTABILITY or FITNESS
13// FOR A PARTICULAR PURPOSE.  See the GNU General Public License for more
14// details.
15
16use std::borrow::Cow;
17
18use crate::DataInt;
19
20/// Iterator over all hyphenation possibilities
21pub struct Iter<'a> {
22    pub(super) iter: std::iter::Rev<std::vec::IntoIter<DataInt>>,
23    pub(super) word: &'a str,
24    pub(super) is_upper: bool,
25}
26
27impl<'a> Iterator for Iter<'a> {
28    type Item = (Cow<'a, str>, Cow<'a, str>);
29
30    fn next(&mut self) -> Option<Self::Item> {
31        let position = self.iter.next()?;
32
33        if let Some(data) = position.data {
34            // get the nonstandard hyphenation data
35            let (change, mut index, cut) = data;
36            let change = if self.is_upper {
37                change.to_uppercase()
38            } else {
39                change.to_string()
40            };
41            index += position.value as isize;
42            let (c1, c2) = {
43                let mut x = change.split('=');
44                (x.next().unwrap(), x.next().unwrap())
45            };
46
47            let index = if index < 0 {
48                self.word.len() - index as usize
49            } else {
50                index as usize
51            };
52
53            let first = self.word[..index].to_string() + c1;
54            let second = c2.to_string() + &self.word[(index + cut)..];
55            Some((Cow::Owned(first), Cow::Owned(second)))
56        } else {
57            let first = &self.word[..position.value];
58            let second = &self.word[position.value..];
59            Some((Cow::Borrowed(first), Cow::Borrowed(second)))
60        }
61    }
62}