unicode_normalization/
replace.rs

1// Copyright 2012-2015 The Rust Project Developers. See the COPYRIGHT
2// file at the top-level directory of this distribution and at
3// http://rust-lang.org/COPYRIGHT.
4//
5// Licensed under the Apache License, Version 2.0 <LICENSE-APACHE or
6// http://www.apache.org/licenses/LICENSE-2.0> or the MIT license
7// <LICENSE-MIT or http://opensource.org/licenses/MIT>, at your
8// option. This file may not be copied, modified, or distributed
9// except according to those terms.
10use core::fmt::{self, Write};
11use tinyvec::ArrayVec;
12
13/// External iterator for replacements for a string's characters.
14#[derive(Clone)]
15pub struct Replacements<I> {
16    iter: I,
17    // At this time, the longest replacement sequence has length 2, so we just
18    // need buffer space for 1 codepoint.
19    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                // At this time, the longest replacement sequence has length 2.
39                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}