unicode_normalization/
replace.rs1use core::fmt::{self, Write};
11use tinyvec::ArrayVec;
12
13#[derive(Clone)]
15pub struct Replacements<I> {
16 iter: I,
17 buffer: Option<char>,
20}
21
22#[inline]
23pub fn new_cjk_compat_variants<I: Iterator<Item = char>>(iter: I) -> Replacements<I> {
24 Replacements { iter, buffer: None }
25}
26
27impl<I: Iterator<Item = char>> Iterator for Replacements<I> {
28 type Item = char;
29
30 #[inline]
31 fn next(&mut self) -> Option<char> {
32 if let Some(c) = self.buffer.take() {
33 return Some(c);
34 }
35
36 match self.iter.next() {
37 Some(ch) => {
38 let mut buffer = ArrayVec::<[char; 2]>::new();
40 super::char::decompose_cjk_compat_variants(ch, |d| buffer.push(d));
41 self.buffer = buffer.get(1).copied();
42 Some(buffer[0])
43 }
44 None => None,
45 }
46 }
47
48 fn size_hint(&self) -> (usize, Option<usize>) {
49 let (lower, _) = self.iter.size_hint();
50 (lower, None)
51 }
52}
53
54impl<I: Iterator<Item = char> + Clone> fmt::Display for Replacements<I> {
55 fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
56 for c in self.clone() {
57 f.write_char(c)?;
58 }
59 Ok(())
60 }
61}