/src/unicode-normalization/src/decompose.rs
Line | Count | Source |
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::fmt::{self, Write}; |
11 | | use core::iter::{Fuse, FusedIterator}; |
12 | | use core::ops::Range; |
13 | | use tinyvec::TinyVec; |
14 | | |
15 | | #[derive(Clone)] |
16 | | enum DecompositionType { |
17 | | Canonical, |
18 | | Compatible, |
19 | | } |
20 | | |
21 | | /// External iterator for a string decomposition's characters. |
22 | | #[derive(Clone)] |
23 | | pub struct Decompositions<I> { |
24 | | kind: DecompositionType, |
25 | | iter: Fuse<I>, |
26 | | |
27 | | // This buffer stores pairs of (canonical combining class, character), |
28 | | // pushed onto the end in text order. |
29 | | // |
30 | | // It's divided into up to three sections: |
31 | | // 1) A prefix that is free space; |
32 | | // 2) "Ready" characters which are sorted and ready to emit on demand; |
33 | | // 3) A "pending" block which stills needs more characters for us to be able |
34 | | // to sort in canonical order and is not safe to emit. |
35 | | buffer: TinyVec<[(u8, char); 4]>, |
36 | | ready: Range<usize>, |
37 | | } |
38 | | |
39 | | impl<I: Iterator<Item = char>> Decompositions<I> { |
40 | | /// Create a new decomposition iterator for canonical decompositions (NFD) |
41 | | /// |
42 | | /// Note that this iterator can also be obtained by directly calling [`.nfd()`](crate::UnicodeNormalization::nfd) |
43 | | /// on the iterator. |
44 | | #[inline] |
45 | 3.31k | pub fn new_canonical(iter: I) -> Decompositions<I> { |
46 | 3.31k | Decompositions { |
47 | 3.31k | kind: self::DecompositionType::Canonical, |
48 | 3.31k | iter: iter.fuse(), |
49 | 3.31k | buffer: TinyVec::new(), |
50 | 3.31k | ready: 0..0, |
51 | 3.31k | } |
52 | 3.31k | } Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>>>::new_canonical Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::new_canonical Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_>>::new_canonical <unicode_normalization::decompose::Decompositions<streaming::Counter>>::new_canonical Line | Count | Source | 45 | 2.32k | pub fn new_canonical(iter: I) -> Decompositions<I> { | 46 | 2.32k | Decompositions { | 47 | 2.32k | kind: self::DecompositionType::Canonical, | 48 | 2.32k | iter: iter.fuse(), | 49 | 2.32k | buffer: TinyVec::new(), | 50 | 2.32k | ready: 0..0, | 51 | 2.32k | } | 52 | 2.32k | } |
<unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::new_canonical Line | Count | Source | 45 | 992 | pub fn new_canonical(iter: I) -> Decompositions<I> { | 46 | 992 | Decompositions { | 47 | 992 | kind: self::DecompositionType::Canonical, | 48 | 992 | iter: iter.fuse(), | 49 | 992 | buffer: TinyVec::new(), | 50 | 992 | ready: 0..0, | 51 | 992 | } | 52 | 992 | } |
|
53 | | |
54 | | /// Create a new decomposition iterator for compatability decompositions (NFkD) |
55 | | /// |
56 | | /// Note that this iterator can also be obtained by directly calling [`.nfkd()`](crate::UnicodeNormalization::nfkd) |
57 | | /// on the iterator. |
58 | | #[inline] |
59 | 1.84k | pub fn new_compatible(iter: I) -> Decompositions<I> { |
60 | 1.84k | Decompositions { |
61 | 1.84k | kind: self::DecompositionType::Compatible, |
62 | 1.84k | iter: iter.fuse(), |
63 | 1.84k | buffer: TinyVec::new(), |
64 | 1.84k | ready: 0..0, |
65 | 1.84k | } |
66 | 1.84k | } Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::new_compatible Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_>>::new_compatible <unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::new_compatible Line | Count | Source | 59 | 1.84k | pub fn new_compatible(iter: I) -> Decompositions<I> { | 60 | 1.84k | Decompositions { | 61 | 1.84k | kind: self::DecompositionType::Compatible, | 62 | 1.84k | iter: iter.fuse(), | 63 | 1.84k | buffer: TinyVec::new(), | 64 | 1.84k | ready: 0..0, | 65 | 1.84k | } | 66 | 1.84k | } |
|
67 | | } |
68 | | |
69 | | impl<I> Decompositions<I> { |
70 | | #[inline] |
71 | 86.3M | fn push_back(&mut self, ch: char) { |
72 | 86.3M | let class = super::char::canonical_combining_class(ch); |
73 | | |
74 | 86.3M | if class == 0 { |
75 | 39.8M | self.sort_pending(); |
76 | 39.8M | self.buffer.push((class, ch)); |
77 | 39.8M | self.ready.end = self.buffer.len(); |
78 | 46.5M | } else { |
79 | 46.5M | self.buffer.push((class, ch)); |
80 | 46.5M | } |
81 | 86.3M | } Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>>>::push_back Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::push_back Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_>>::push_back <unicode_normalization::decompose::Decompositions<streaming::Counter>>::push_back Line | Count | Source | 71 | 15.9M | fn push_back(&mut self, ch: char) { | 72 | 15.9M | let class = super::char::canonical_combining_class(ch); | 73 | | | 74 | 15.9M | if class == 0 { | 75 | 7.71M | self.sort_pending(); | 76 | 7.71M | self.buffer.push((class, ch)); | 77 | 7.71M | self.ready.end = self.buffer.len(); | 78 | 8.20M | } else { | 79 | 8.20M | self.buffer.push((class, ch)); | 80 | 8.20M | } | 81 | 15.9M | } |
<unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::push_back Line | Count | Source | 71 | 70.4M | fn push_back(&mut self, ch: char) { | 72 | 70.4M | let class = super::char::canonical_combining_class(ch); | 73 | | | 74 | 70.4M | if class == 0 { | 75 | 32.1M | self.sort_pending(); | 76 | 32.1M | self.buffer.push((class, ch)); | 77 | 32.1M | self.ready.end = self.buffer.len(); | 78 | 38.3M | } else { | 79 | 38.3M | self.buffer.push((class, ch)); | 80 | 38.3M | } | 81 | 70.4M | } |
|
82 | | |
83 | | #[inline] |
84 | 39.8M | fn sort_pending(&mut self) { |
85 | | // NB: `sort_by_key` is stable, so it will preserve the original text's |
86 | | // order within a combining class. |
87 | 39.8M | self.buffer[self.ready.end..].sort_by_key(|k| k.0); |
88 | 39.8M | } Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>>>::sort_pending Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::sort_pending Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_>>::sort_pending <unicode_normalization::decompose::Decompositions<streaming::Counter>>::sort_pending Line | Count | Source | 84 | 7.71M | fn sort_pending(&mut self) { | 85 | | // NB: `sort_by_key` is stable, so it will preserve the original text's | 86 | | // order within a combining class. | 87 | 7.71M | self.buffer[self.ready.end..].sort_by_key(|k| k.0); | 88 | 7.71M | } |
<unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::sort_pending Line | Count | Source | 84 | 32.1M | fn sort_pending(&mut self) { | 85 | | // NB: `sort_by_key` is stable, so it will preserve the original text's | 86 | | // order within a combining class. | 87 | 32.1M | self.buffer[self.ready.end..].sort_by_key(|k| k.0); | 88 | 32.1M | } |
|
89 | | |
90 | | #[inline] |
91 | 17.0M | fn reset_buffer(&mut self) { |
92 | | // Equivalent to `self.buffer.drain(0..self.ready.end)` |
93 | | // but faster than drain() if the buffer is a SmallVec or TinyVec |
94 | 17.0M | let pending = self.buffer.len() - self.ready.end; |
95 | 17.0M | for i in 0..pending { |
96 | 60.8k | self.buffer[i] = self.buffer[i + self.ready.end]; |
97 | 60.8k | } |
98 | 17.0M | self.buffer.truncate(pending); |
99 | 17.0M | self.ready = 0..0; |
100 | 17.0M | } Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>>>::reset_buffer Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::reset_buffer Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_>>::reset_buffer <unicode_normalization::decompose::Decompositions<streaming::Counter>>::reset_buffer Line | Count | Source | 91 | 7.71M | fn reset_buffer(&mut self) { | 92 | | // Equivalent to `self.buffer.drain(0..self.ready.end)` | 93 | | // but faster than drain() if the buffer is a SmallVec or TinyVec | 94 | 7.71M | let pending = self.buffer.len() - self.ready.end; | 95 | 7.71M | for i in 0..pending { | 96 | 42.6k | self.buffer[i] = self.buffer[i + self.ready.end]; | 97 | 42.6k | } | 98 | 7.71M | self.buffer.truncate(pending); | 99 | 7.71M | self.ready = 0..0; | 100 | 7.71M | } |
<unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::reset_buffer Line | Count | Source | 91 | 9.38M | fn reset_buffer(&mut self) { | 92 | | // Equivalent to `self.buffer.drain(0..self.ready.end)` | 93 | | // but faster than drain() if the buffer is a SmallVec or TinyVec | 94 | 9.38M | let pending = self.buffer.len() - self.ready.end; | 95 | 9.38M | for i in 0..pending { | 96 | 18.2k | self.buffer[i] = self.buffer[i + self.ready.end]; | 97 | 18.2k | } | 98 | 9.38M | self.buffer.truncate(pending); | 99 | 9.38M | self.ready = 0..0; | 100 | 9.38M | } |
|
101 | | |
102 | | #[inline] |
103 | 86.3M | fn increment_next_ready(&mut self) { |
104 | 86.3M | let next = self.ready.start + 1; |
105 | 86.3M | if next == self.ready.end { |
106 | 17.0M | self.reset_buffer(); |
107 | 69.2M | } else { |
108 | 69.2M | self.ready.start = next; |
109 | 69.2M | } |
110 | 86.3M | } Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>>>::increment_next_ready Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::increment_next_ready Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_>>::increment_next_ready <unicode_normalization::decompose::Decompositions<streaming::Counter>>::increment_next_ready Line | Count | Source | 103 | 15.9M | fn increment_next_ready(&mut self) { | 104 | 15.9M | let next = self.ready.start + 1; | 105 | 15.9M | if next == self.ready.end { | 106 | 7.71M | self.reset_buffer(); | 107 | 8.21M | } else { | 108 | 8.21M | self.ready.start = next; | 109 | 8.21M | } | 110 | 15.9M | } |
<unicode_normalization::decompose::Decompositions<core::str::iter::Chars>>::increment_next_ready Line | Count | Source | 103 | 70.4M | fn increment_next_ready(&mut self) { | 104 | 70.4M | let next = self.ready.start + 1; | 105 | 70.4M | if next == self.ready.end { | 106 | 9.38M | self.reset_buffer(); | 107 | 61.0M | } else { | 108 | 61.0M | self.ready.start = next; | 109 | 61.0M | } | 110 | 70.4M | } |
|
111 | | } |
112 | | |
113 | | impl<I: Iterator<Item = char>> Iterator for Decompositions<I> { |
114 | | type Item = char; |
115 | | |
116 | | #[inline] |
117 | 86.3M | fn next(&mut self) -> Option<char> { |
118 | 127M | while self.ready.end == 0 { |
119 | 41.0M | match (self.iter.next(), &self.kind) { |
120 | 24.6M | (Some(ch), &DecompositionType::Canonical) => { |
121 | 37.2M | super::char::decompose_canonical(ch, |d| self.push_back(d)); Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::next::{closure#0}Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_> as core::iter::traits::iterator::Iterator>::next::{closure#0}<unicode_normalization::decompose::Decompositions<streaming::Counter> as core::iter::traits::iterator::Iterator>::next::{closure#0}Line | Count | Source | 121 | 15.9M | super::char::decompose_canonical(ch, |d| self.push_back(d)); |
<unicode_normalization::decompose::Decompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::next::{closure#0}Line | Count | Source | 121 | 21.3M | super::char::decompose_canonical(ch, |d| self.push_back(d)); |
|
122 | | } |
123 | 16.3M | (Some(ch), &DecompositionType::Compatible) => { |
124 | 49.0M | super::char::decompose_compatible(ch, |d| self.push_back(d)); Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>> as core::iter::traits::iterator::Iterator>::next::{closure#1}Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::next::{closure#1}Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_> as core::iter::traits::iterator::Iterator>::next::{closure#1}Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<streaming::Counter> as core::iter::traits::iterator::Iterator>::next::{closure#1}<unicode_normalization::decompose::Decompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::next::{closure#1}Line | Count | Source | 124 | 49.0M | super::char::decompose_compatible(ch, |d| self.push_back(d)); |
|
125 | | } |
126 | | (None, _) => { |
127 | 7.63k | if self.buffer.is_empty() { |
128 | 5.15k | return None; |
129 | | } else { |
130 | 2.48k | self.sort_pending(); |
131 | 2.48k | self.ready.end = self.buffer.len(); |
132 | | |
133 | | // This implementation means that we can call `next` |
134 | | // on an exhausted iterator; the last outer `next` call |
135 | | // will result in an inner `next` call. To make this |
136 | | // safe, we use `fuse`. |
137 | 2.48k | break; |
138 | | } |
139 | | } |
140 | | } |
141 | | } |
142 | | |
143 | | // We can assume here that, if `self.ready.end` is greater than zero, |
144 | | // it's also greater than `self.ready.start`. That's because we only |
145 | | // increment `self.ready.start` inside `increment_next_ready`, and |
146 | | // whenever it reaches equality with `self.ready.end`, we reset both |
147 | | // to zero, maintaining the invariant that: |
148 | | // self.ready.start < self.ready.end || self.ready.end == self.ready.start == 0 |
149 | | // |
150 | | // This less-than-obviously-safe implementation is chosen for performance, |
151 | | // minimizing the number & complexity of branches in `next` in the common |
152 | | // case of buffering then unbuffering a single character with each call. |
153 | 86.3M | let (_, ch) = self.buffer[self.ready.start]; |
154 | 86.3M | self.increment_next_ready(); |
155 | 86.3M | Some(ch) |
156 | 86.3M | } Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::next Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_> as core::iter::traits::iterator::Iterator>::next <unicode_normalization::decompose::Decompositions<streaming::Counter> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 117 | 15.9M | fn next(&mut self) -> Option<char> { | 118 | 27.7M | while self.ready.end == 0 { | 119 | 11.8M | match (self.iter.next(), &self.kind) { | 120 | 11.8M | (Some(ch), &DecompositionType::Canonical) => { | 121 | 11.8M | super::char::decompose_canonical(ch, |d| self.push_back(d)); | 122 | | } | 123 | 0 | (Some(ch), &DecompositionType::Compatible) => { | 124 | 0 | super::char::decompose_compatible(ch, |d| self.push_back(d)); | 125 | | } | 126 | | (None, _) => { | 127 | 3.27k | if self.buffer.is_empty() { | 128 | 2.32k | return None; | 129 | | } else { | 130 | 951 | self.sort_pending(); | 131 | 951 | self.ready.end = self.buffer.len(); | 132 | | | 133 | | // This implementation means that we can call `next` | 134 | | // on an exhausted iterator; the last outer `next` call | 135 | | // will result in an inner `next` call. To make this | 136 | | // safe, we use `fuse`. | 137 | 951 | break; | 138 | | } | 139 | | } | 140 | | } | 141 | | } | 142 | | | 143 | | // We can assume here that, if `self.ready.end` is greater than zero, | 144 | | // it's also greater than `self.ready.start`. That's because we only | 145 | | // increment `self.ready.start` inside `increment_next_ready`, and | 146 | | // whenever it reaches equality with `self.ready.end`, we reset both | 147 | | // to zero, maintaining the invariant that: | 148 | | // self.ready.start < self.ready.end || self.ready.end == self.ready.start == 0 | 149 | | // | 150 | | // This less-than-obviously-safe implementation is chosen for performance, | 151 | | // minimizing the number & complexity of branches in `next` in the common | 152 | | // case of buffering then unbuffering a single character with each call. | 153 | 15.9M | let (_, ch) = self.buffer[self.ready.start]; | 154 | 15.9M | self.increment_next_ready(); | 155 | 15.9M | Some(ch) | 156 | 15.9M | } |
<unicode_normalization::decompose::Decompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::next Line | Count | Source | 117 | 70.4M | fn next(&mut self) -> Option<char> { | 118 | 99.6M | while self.ready.end == 0 { | 119 | 29.1M | match (self.iter.next(), &self.kind) { | 120 | 12.7M | (Some(ch), &DecompositionType::Canonical) => { | 121 | 12.7M | super::char::decompose_canonical(ch, |d| self.push_back(d)); | 122 | | } | 123 | 16.3M | (Some(ch), &DecompositionType::Compatible) => { | 124 | 16.3M | super::char::decompose_compatible(ch, |d| self.push_back(d)); | 125 | | } | 126 | | (None, _) => { | 127 | 4.36k | if self.buffer.is_empty() { | 128 | 2.83k | return None; | 129 | | } else { | 130 | 1.52k | self.sort_pending(); | 131 | 1.52k | self.ready.end = self.buffer.len(); | 132 | | | 133 | | // This implementation means that we can call `next` | 134 | | // on an exhausted iterator; the last outer `next` call | 135 | | // will result in an inner `next` call. To make this | 136 | | // safe, we use `fuse`. | 137 | 1.52k | break; | 138 | | } | 139 | | } | 140 | | } | 141 | | } | 142 | | | 143 | | // We can assume here that, if `self.ready.end` is greater than zero, | 144 | | // it's also greater than `self.ready.start`. That's because we only | 145 | | // increment `self.ready.start` inside `increment_next_ready`, and | 146 | | // whenever it reaches equality with `self.ready.end`, we reset both | 147 | | // to zero, maintaining the invariant that: | 148 | | // self.ready.start < self.ready.end || self.ready.end == self.ready.start == 0 | 149 | | // | 150 | | // This less-than-obviously-safe implementation is chosen for performance, | 151 | | // minimizing the number & complexity of branches in `next` in the common | 152 | | // case of buffering then unbuffering a single character with each call. | 153 | 70.4M | let (_, ch) = self.buffer[self.ready.start]; | 154 | 70.4M | self.increment_next_ready(); | 155 | 70.4M | Some(ch) | 156 | 70.4M | } |
|
157 | | |
158 | 522 | fn size_hint(&self) -> (usize, Option<usize>) { |
159 | 522 | let (lower, _) = self.iter.size_hint(); |
160 | 522 | (lower, None) |
161 | 522 | } Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::size_hint Unexecuted instantiation: <unicode_normalization::decompose::Decompositions<_> as core::iter::traits::iterator::Iterator>::size_hint <unicode_normalization::decompose::Decompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::size_hint Line | Count | Source | 158 | 522 | fn size_hint(&self) -> (usize, Option<usize>) { | 159 | 522 | let (lower, _) = self.iter.size_hint(); | 160 | 522 | (lower, None) | 161 | 522 | } |
|
162 | | } |
163 | | |
164 | | impl<I: Iterator<Item = char> + FusedIterator> FusedIterator for Decompositions<I> {} |
165 | | |
166 | | impl<I: Iterator<Item = char> + Clone> fmt::Display for Decompositions<I> { |
167 | 0 | fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result { |
168 | 0 | for c in self.clone() { |
169 | 0 | f.write_char(c)?; |
170 | | } |
171 | 0 | Ok(()) |
172 | 0 | } |
173 | | } |