/src/unicode-normalization/src/replace.rs
Line | Count | Source (jump to first uncovered line) |
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 | | impl<I: Iterator<Item = char>> Replacements<I> { |
26 | | /// Create a new iterator that replaces [CJK Compatibility Ideograph] codepoints with normal forms using [Standardized Variation Sequences]. |
27 | | /// |
28 | | /// Note that this iterator can also be obtained by directly calling [`.cjk_compat_variants()`] on the iterator. |
29 | | /// |
30 | | /// [CJK Compatibility Ideograph]: https://www.unicode.org/glossary/#compatibility_ideograph |
31 | | /// [Standardized Variation Sequences]: https://www.unicode.org/glossary/#standardized_variation_sequence |
32 | | /// [`.cjk_compat_variants()`]: crate::UnicodeNormalization::cjk_compat_variants |
33 | | #[inline] |
34 | 0 | pub fn new_cjk_compat_variants(iter: I) -> Replacements<I> { |
35 | 0 | Replacements { iter, buffer: None } |
36 | 0 | } |
37 | | } |
38 | | |
39 | | impl<I: Iterator<Item = char>> Iterator for Replacements<I> { |
40 | | type Item = char; |
41 | | |
42 | | #[inline] |
43 | 0 | fn next(&mut self) -> Option<char> { |
44 | 0 | if let Some(c) = self.buffer.take() { |
45 | 0 | return Some(c); |
46 | 0 | } |
47 | 0 |
|
48 | 0 | match self.iter.next() { |
49 | 0 | Some(ch) => { |
50 | 0 | // At this time, the longest replacement sequence has length 2. |
51 | 0 | let mut buffer = ArrayVec::<[char; 2]>::new(); |
52 | 0 | super::char::decompose_cjk_compat_variants(ch, |d| buffer.push(d)); |
53 | 0 | self.buffer = buffer.get(1).copied(); |
54 | 0 | Some(buffer[0]) |
55 | | } |
56 | 0 | None => None, |
57 | | } |
58 | 0 | } |
59 | | |
60 | 0 | fn size_hint(&self) -> (usize, Option<usize>) { |
61 | 0 | let (lower, _) = self.iter.size_hint(); |
62 | 0 | (lower, None) |
63 | 0 | } |
64 | | } |
65 | | |
66 | | impl<I: Iterator<Item = char> + FusedIterator> FusedIterator for Replacements<I> {} |
67 | | |
68 | | impl<I: Iterator<Item = char> + Clone> fmt::Display for Replacements<I> { |
69 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
70 | 0 | for c in self.clone() { |
71 | 0 | f.write_char(c)?; |
72 | | } |
73 | 0 | Ok(()) |
74 | 0 | } |
75 | | } |