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. |
10 | use core::{ |
11 | fmt::{self, Write}, |
12 | iter::FusedIterator, |
13 | }; |
14 | use tinyvec::ArrayVec; |
15 | |
16 | /// External iterator for replacements for a string's characters. |
17 | #[derive (Clone)] |
18 | pub struct Replacements<I> { |
19 | iter: I, |
20 | // At this time, the longest replacement sequence has length 2, so we just |
21 | // need buffer space for 1 codepoint. |
22 | buffer: Option<char>, |
23 | } |
24 | |
25 | #[inline ] |
26 | pub fn new_cjk_compat_variants<I: Iterator<Item = char>>(iter: I) -> Replacements<I> { |
27 | Replacements { iter, buffer: None } |
28 | } |
29 | |
30 | impl<I: Iterator<Item = char>> Iterator for Replacements<I> { |
31 | type Item = char; |
32 | |
33 | #[inline ] |
34 | fn next(&mut self) -> Option<char> { |
35 | if let Some(c) = self.buffer.take() { |
36 | return Some(c); |
37 | } |
38 | |
39 | match self.iter.next() { |
40 | Some(ch) => { |
41 | // At this time, the longest replacement sequence has length 2. |
42 | let mut buffer = ArrayVec::<[char; 2]>::new(); |
43 | super::char::decompose_cjk_compat_variants(ch, |d| buffer.push(d)); |
44 | self.buffer = buffer.get(1).copied(); |
45 | Some(buffer[0]) |
46 | } |
47 | None => None, |
48 | } |
49 | } |
50 | |
51 | fn size_hint(&self) -> (usize, Option<usize>) { |
52 | let (lower, _) = self.iter.size_hint(); |
53 | (lower, None) |
54 | } |
55 | } |
56 | |
57 | impl<I: Iterator<Item = char> + FusedIterator> FusedIterator for Replacements<I> {} |
58 | |
59 | impl<I: Iterator<Item = char> + Clone> fmt::Display for Replacements<I> { |
60 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
61 | for c: char in self.clone() { |
62 | f.write_char(c)?; |
63 | } |
64 | Ok(()) |
65 | } |
66 | } |
67 | |