Coverage Report

Created: 2026-12-01 06:50

next uncovered line (L), next uncovered region (R), next uncovered branch (B)
/src/unicode-normalization/src/recompose.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
11
use crate::decompose::Decompositions;
12
use core::{
13
    fmt::{self, Write},
14
    iter::FusedIterator,
15
};
16
use tinyvec::TinyVec;
17
18
#[derive(Clone)]
19
enum RecompositionState {
20
    Composing,
21
    Purging(usize),
22
    Finished(usize),
23
}
24
25
/// External iterator for a string recomposition's characters.
26
#[derive(Clone)]
27
pub struct Recompositions<I> {
28
    iter: Decompositions<I>,
29
    state: RecompositionState,
30
    buffer: TinyVec<[char; 4]>,
31
    composee: Option<char>,
32
    last_ccc: Option<u8>,
33
}
34
35
impl<I: Iterator<Item = char>> Recompositions<I> {
36
    /// Create a new recomposition iterator for canonical compositions (NFC)
37
    ///
38
    /// Note that this iterator can also be obtained by directly calling [`.nfc()`](crate::UnicodeNormalization::nfc)
39
    /// on the iterator.
40
    #[inline]
41
24.7k
    pub fn new_canonical(iter: I) -> Self {
42
24.7k
        Recompositions {
43
24.7k
            iter: Decompositions::new_canonical(iter),
44
24.7k
            state: self::RecompositionState::Composing,
45
24.7k
            buffer: TinyVec::new(),
46
24.7k
            composee: None,
47
24.7k
            last_ccc: None,
48
24.7k
        }
49
24.7k
    }
<unicode_normalization::recompose::Recompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>>>::new_canonical
Line
Count
Source
41
11.8k
    pub fn new_canonical(iter: I) -> Self {
42
11.8k
        Recompositions {
43
11.8k
            iter: Decompositions::new_canonical(iter),
44
11.8k
            state: self::RecompositionState::Composing,
45
11.8k
            buffer: TinyVec::new(),
46
11.8k
            composee: None,
47
11.8k
            last_ccc: None,
48
11.8k
        }
49
11.8k
    }
<unicode_normalization::recompose::Recompositions<core::str::iter::Chars>>::new_canonical
Line
Count
Source
41
9.58k
    pub fn new_canonical(iter: I) -> Self {
42
9.58k
        Recompositions {
43
9.58k
            iter: Decompositions::new_canonical(iter),
44
9.58k
            state: self::RecompositionState::Composing,
45
9.58k
            buffer: TinyVec::new(),
46
9.58k
            composee: None,
47
9.58k
            last_ccc: None,
48
9.58k
        }
49
9.58k
    }
Unexecuted instantiation: <unicode_normalization::recompose::Recompositions<_>>::new_canonical
<unicode_normalization::recompose::Recompositions<streaming::Counter>>::new_canonical
Line
Count
Source
41
2.37k
    pub fn new_canonical(iter: I) -> Self {
42
2.37k
        Recompositions {
43
2.37k
            iter: Decompositions::new_canonical(iter),
44
2.37k
            state: self::RecompositionState::Composing,
45
2.37k
            buffer: TinyVec::new(),
46
2.37k
            composee: None,
47
2.37k
            last_ccc: None,
48
2.37k
        }
49
2.37k
    }
<unicode_normalization::recompose::Recompositions<core::str::iter::Chars>>::new_canonical
Line
Count
Source
41
845
    pub fn new_canonical(iter: I) -> Self {
42
845
        Recompositions {
43
845
            iter: Decompositions::new_canonical(iter),
44
845
            state: self::RecompositionState::Composing,
45
845
            buffer: TinyVec::new(),
46
845
            composee: None,
47
845
            last_ccc: None,
48
845
        }
49
845
    }
50
51
    /// Create a new recomposition iterator for compatability compositions (NFkC)
52
    ///
53
    /// Note that this iterator can also be obtained by directly calling [`.nfkc()`](crate::UnicodeNormalization::nfkc)
54
    /// on the iterator.
55
    #[inline]
56
10.9k
    pub fn new_compatible(iter: I) -> Self {
57
10.9k
        Recompositions {
58
10.9k
            iter: Decompositions::new_compatible(iter),
59
10.9k
            state: self::RecompositionState::Composing,
60
10.9k
            buffer: TinyVec::new(),
61
10.9k
            composee: None,
62
10.9k
            last_ccc: None,
63
10.9k
        }
64
10.9k
    }
<unicode_normalization::recompose::Recompositions<core::str::iter::Chars>>::new_compatible
Line
Count
Source
56
9.38k
    pub fn new_compatible(iter: I) -> Self {
57
9.38k
        Recompositions {
58
9.38k
            iter: Decompositions::new_compatible(iter),
59
9.38k
            state: self::RecompositionState::Composing,
60
9.38k
            buffer: TinyVec::new(),
61
9.38k
            composee: None,
62
9.38k
            last_ccc: None,
63
9.38k
        }
64
9.38k
    }
Unexecuted instantiation: <unicode_normalization::recompose::Recompositions<_>>::new_compatible
<unicode_normalization::recompose::Recompositions<core::str::iter::Chars>>::new_compatible
Line
Count
Source
56
1.54k
    pub fn new_compatible(iter: I) -> Self {
57
1.54k
        Recompositions {
58
1.54k
            iter: Decompositions::new_compatible(iter),
59
1.54k
            state: self::RecompositionState::Composing,
60
1.54k
            buffer: TinyVec::new(),
61
1.54k
            composee: None,
62
1.54k
            last_ccc: None,
63
1.54k
        }
64
1.54k
    }
65
}
66
67
impl<I: Iterator<Item = char>> Iterator for Recompositions<I> {
68
    type Item = char;
69
70
    #[inline]
71
309M
    fn next(&mut self) -> Option<char> {
72
        use self::RecompositionState::*;
73
74
        loop {
75
312M
            match self.state {
76
                Composing => {
77
319M
                    for ch in self.iter.by_ref() {
78
319M
                        let ch_class = super::char::canonical_combining_class(ch);
79
319M
                        let k = match self.composee {
80
                            None => {
81
2.54M
                                if ch_class != 0 {
82
2.51M
                                    return Some(ch);
83
33.3k
                                }
84
33.3k
                                self.composee = Some(ch);
85
33.3k
                                continue;
86
                            }
87
317M
                            Some(k) => k,
88
                        };
89
317M
                        match self.last_ccc {
90
136M
                            None => match super::char::compose(k, ch) {
91
9.83M
                                Some(r) => {
92
9.83M
                                    self.composee = Some(r);
93
9.83M
                                    continue;
94
                                }
95
                                None => {
96
126M
                                    if ch_class == 0 {
97
123M
                                        self.composee = Some(ch);
98
123M
                                        return Some(k);
99
2.83M
                                    }
100
2.83M
                                    self.buffer.push(ch);
101
2.83M
                                    self.last_ccc = Some(ch_class);
102
                                }
103
                            },
104
181M
                            Some(l_class) => {
105
181M
                                if l_class >= ch_class {
106
                                    // `ch` is blocked from `composee`
107
180M
                                    if ch_class == 0 {
108
2.82M
                                        self.composee = Some(ch);
109
2.82M
                                        self.last_ccc = None;
110
2.82M
                                        self.state = Purging(0);
111
2.82M
                                        return Some(k);
112
177M
                                    }
113
177M
                                    self.buffer.push(ch);
114
177M
                                    self.last_ccc = Some(ch_class);
115
177M
                                    continue;
116
467k
                                }
117
467k
                                match super::char::compose(k, ch) {
118
24.9k
                                    Some(r) => {
119
24.9k
                                        self.composee = Some(r);
120
24.9k
                                        continue;
121
                                    }
122
442k
                                    None => {
123
442k
                                        self.buffer.push(ch);
124
442k
                                        self.last_ccc = Some(ch_class);
125
442k
                                    }
126
                                }
127
                            }
128
                        }
129
                    }
130
35.2k
                    self.state = Finished(0);
131
35.2k
                    if self.composee.is_some() {
132
32.9k
                        return self.composee.take();
133
2.25k
                    }
134
                }
135
141M
                Purging(next) => match self.buffer.get(next).cloned() {
136
2.82M
                    None => {
137
2.82M
                        self.buffer.clear();
138
2.82M
                        self.state = Composing;
139
2.82M
                    }
140
138M
                    s => {
141
138M
                        self.state = Purging(next + 1);
142
138M
                        return s;
143
                    }
144
                },
145
42.4M
                Finished(next) => match self.buffer.get(next).cloned() {
146
                    None => {
147
35.0k
                        self.buffer.clear();
148
35.0k
                        return self.composee.take();
149
                    }
150
42.3M
                    s => {
151
42.3M
                        self.state = Finished(next + 1);
152
42.3M
                        return s;
153
                    }
154
                },
155
            }
156
        }
157
309M
    }
<unicode_normalization::recompose::Recompositions<unicode_normalization::stream_safe::StreamSafe<core::str::iter::Chars>> as core::iter::traits::iterator::Iterator>::next
Line
Count
Source
71
85.8M
    fn next(&mut self) -> Option<char> {
72
        use self::RecompositionState::*;
73
74
        loop {
75
87.9M
            match self.state {
76
                Composing => {
77
88.1M
                    for ch in self.iter.by_ref() {
78
88.1M
                        let ch_class = super::char::canonical_combining_class(ch);
79
88.1M
                        let k = match self.composee {
80
                            None => {
81
37.8k
                                if ch_class != 0 {
82
26.5k
                                    return Some(ch);
83
11.3k
                                }
84
11.3k
                                self.composee = Some(ch);
85
11.3k
                                continue;
86
                            }
87
88.1M
                            Some(k) => k,
88
                        };
89
88.1M
                        match self.last_ccc {
90
29.5M
                            None => match super::char::compose(k, ch) {
91
2.35M
                                Some(r) => {
92
2.35M
                                    self.composee = Some(r);
93
2.35M
                                    continue;
94
                                }
95
                                None => {
96
27.2M
                                    if ch_class == 0 {
97
25.0M
                                        self.composee = Some(ch);
98
25.0M
                                        return Some(k);
99
2.16M
                                    }
100
2.16M
                                    self.buffer.push(ch);
101
2.16M
                                    self.last_ccc = Some(ch_class);
102
                                }
103
                            },
104
58.5M
                            Some(l_class) => {
105
58.5M
                                if l_class >= ch_class {
106
                                    // `ch` is blocked from `composee`
107
58.3M
                                    if ch_class == 0 {
108
2.15M
                                        self.composee = Some(ch);
109
2.15M
                                        self.last_ccc = None;
110
2.15M
                                        self.state = Purging(0);
111
2.15M
                                        return Some(k);
112
56.1M
                                    }
113
56.1M
                                    self.buffer.push(ch);
114
56.1M
                                    self.last_ccc = Some(ch_class);
115
56.1M
                                    continue;
116
231k
                                }
117
231k
                                match super::char::compose(k, ch) {
118
7.47k
                                    Some(r) => {
119
7.47k
                                        self.composee = Some(r);
120
7.47k
                                        continue;
121
                                    }
122
224k
                                    None => {
123
224k
                                        self.buffer.push(ch);
124
224k
                                        self.last_ccc = Some(ch_class);
125
224k
                                    }
126
                                }
127
                            }
128
                        }
129
                    }
130
11.7k
                    self.state = Finished(0);
131
11.7k
                    if self.composee.is_some() {
132
11.2k
                        return self.composee.take();
133
522
                    }
134
                }
135
60.6M
                Purging(next) => match self.buffer.get(next).cloned() {
136
2.15M
                    None => {
137
2.15M
                        self.buffer.clear();
138
2.15M
                        self.state = Composing;
139
2.15M
                    }
140
58.5M
                    s => {
141
58.5M
                        self.state = Purging(next + 1);
142
58.5M
                        return s;
143
                    }
144
                },
145
43.4k
                Finished(next) => match self.buffer.get(next).cloned() {
146
                    None => {
147
11.7k
                        self.buffer.clear();
148
11.7k
                        return self.composee.take();
149
                    }
150
31.7k
                    s => {
151
31.7k
                        self.state = Finished(next + 1);
152
31.7k
                        return s;
153
                    }
154
                },
155
            }
156
        }
157
85.8M
    }
<unicode_normalization::recompose::Recompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::next
Line
Count
Source
71
147M
    fn next(&mut self) -> Option<char> {
72
        use self::RecompositionState::*;
73
74
        loop {
75
148M
            match self.state {
76
                Composing => {
77
155M
                    for ch in self.iter.by_ref() {
78
155M
                        let ch_class = super::char::canonical_combining_class(ch);
79
155M
                        let k = match self.composee {
80
                            None => {
81
111k
                                if ch_class != 0 {
82
93.5k
                                    return Some(ch);
83
17.7k
                                }
84
17.7k
                                self.composee = Some(ch);
85
17.7k
                                continue;
86
                            }
87
155M
                            Some(k) => k,
88
                        };
89
155M
                        match self.last_ccc {
90
77.1M
                            None => match super::char::compose(k, ch) {
91
7.41M
                                Some(r) => {
92
7.41M
                                    self.composee = Some(r);
93
7.41M
                                    continue;
94
                                }
95
                                None => {
96
69.7M
                                    if ch_class == 0 {
97
69.4M
                                        self.composee = Some(ch);
98
69.4M
                                        return Some(k);
99
302k
                                    }
100
302k
                                    self.buffer.push(ch);
101
302k
                                    self.last_ccc = Some(ch_class);
102
                                }
103
                            },
104
78.0M
                            Some(l_class) => {
105
78.0M
                                if l_class >= ch_class {
106
                                    // `ch` is blocked from `composee`
107
77.9M
                                    if ch_class == 0 {
108
296k
                                        self.composee = Some(ch);
109
296k
                                        self.last_ccc = None;
110
296k
                                        self.state = Purging(0);
111
296k
                                        return Some(k);
112
77.6M
                                    }
113
77.6M
                                    self.buffer.push(ch);
114
77.6M
                                    self.last_ccc = Some(ch_class);
115
77.6M
                                    continue;
116
68.5k
                                }
117
68.5k
                                match super::char::compose(k, ch) {
118
10.9k
                                    Some(r) => {
119
10.9k
                                        self.composee = Some(r);
120
10.9k
                                        continue;
121
                                    }
122
57.5k
                                    None => {
123
57.5k
                                        self.buffer.push(ch);
124
57.5k
                                        self.last_ccc = Some(ch_class);
125
57.5k
                                    }
126
                                }
127
                            }
128
                        }
129
                    }
130
18.7k
                    self.state = Finished(0);
131
18.7k
                    if self.composee.is_some() {
132
17.5k
                        return self.composee.take();
133
1.18k
                    }
134
                }
135
59.7M
                Purging(next) => match self.buffer.get(next).cloned() {
136
296k
                    None => {
137
296k
                        self.buffer.clear();
138
296k
                        self.state = Composing;
139
296k
                    }
140
59.4M
                    s => {
141
59.4M
                        self.state = Purging(next + 1);
142
59.4M
                        return s;
143
                    }
144
                },
145
18.5M
                Finished(next) => match self.buffer.get(next).cloned() {
146
                    None => {
147
18.5k
                        self.buffer.clear();
148
18.5k
                        return self.composee.take();
149
                    }
150
18.5M
                    s => {
151
18.5M
                        self.state = Finished(next + 1);
152
18.5M
                        return s;
153
                    }
154
                },
155
            }
156
        }
157
147M
    }
Unexecuted instantiation: <unicode_normalization::recompose::Recompositions<_> as core::iter::traits::iterator::Iterator>::next
<unicode_normalization::recompose::Recompositions<streaming::Counter> as core::iter::traits::iterator::Iterator>::next
Line
Count
Source
71
17.0M
    fn next(&mut self) -> Option<char> {
72
        use self::RecompositionState::*;
73
74
        loop {
75
17.3M
            match self.state {
76
                Composing => {
77
17.0M
                    for ch in self.iter.by_ref() {
78
17.0M
                        let ch_class = super::char::canonical_combining_class(ch);
79
17.0M
                        let k = match self.composee {
80
                            None => {
81
10.0k
                                if ch_class != 0 {
82
7.88k
                                    return Some(ch);
83
2.16k
                                }
84
2.16k
                                self.composee = Some(ch);
85
2.16k
                                continue;
86
                            }
87
17.0M
                            Some(k) => k,
88
                        };
89
17.0M
                        match self.last_ccc {
90
7.26M
                            None => match super::char::compose(k, ch) {
91
65.9k
                                Some(r) => {
92
65.9k
                                    self.composee = Some(r);
93
65.9k
                                    continue;
94
                                }
95
                                None => {
96
7.20M
                                    if ch_class == 0 {
97
6.83M
                                        self.composee = Some(ch);
98
6.83M
                                        return Some(k);
99
363k
                                    }
100
363k
                                    self.buffer.push(ch);
101
363k
                                    self.last_ccc = Some(ch_class);
102
                                }
103
                            },
104
9.79M
                            Some(l_class) => {
105
9.79M
                                if l_class >= ch_class {
106
                                    // `ch` is blocked from `composee`
107
9.65M
                                    if ch_class == 0 {
108
362k
                                        self.composee = Some(ch);
109
362k
                                        self.last_ccc = None;
110
362k
                                        self.state = Purging(0);
111
362k
                                        return Some(k);
112
9.28M
                                    }
113
9.28M
                                    self.buffer.push(ch);
114
9.28M
                                    self.last_ccc = Some(ch_class);
115
9.28M
                                    continue;
116
145k
                                }
117
145k
                                match super::char::compose(k, ch) {
118
4.46k
                                    Some(r) => {
119
4.46k
                                        self.composee = Some(r);
120
4.46k
                                        continue;
121
                                    }
122
140k
                                    None => {
123
140k
                                        self.buffer.push(ch);
124
140k
                                        self.last_ccc = Some(ch_class);
125
140k
                                    }
126
                                }
127
                            }
128
                        }
129
                    }
130
2.37k
                    self.state = Finished(0);
131
2.37k
                    if self.composee.is_some() {
132
2.16k
                        return self.composee.take();
133
212
                    }
134
                }
135
10.1M
                Purging(next) => match self.buffer.get(next).cloned() {
136
362k
                    None => {
137
362k
                        self.buffer.clear();
138
362k
                        self.state = Composing;
139
362k
                    }
140
9.78M
                    s => {
141
9.78M
                        self.state = Purging(next + 1);
142
9.78M
                        return s;
143
                    }
144
                },
145
9.93k
                Finished(next) => match self.buffer.get(next).cloned() {
146
                    None => {
147
2.37k
                        self.buffer.clear();
148
2.37k
                        return self.composee.take();
149
                    }
150
7.55k
                    s => {
151
7.55k
                        self.state = Finished(next + 1);
152
7.55k
                        return s;
153
                    }
154
                },
155
            }
156
        }
157
17.0M
    }
<unicode_normalization::recompose::Recompositions<core::str::iter::Chars> as core::iter::traits::iterator::Iterator>::next
Line
Count
Source
71
59.2M
    fn next(&mut self) -> Option<char> {
72
        use self::RecompositionState::*;
73
74
        loop {
75
59.2M
            match self.state {
76
                Composing => {
77
59.2M
                    for ch in self.iter.by_ref() {
78
59.2M
                        let ch_class = super::char::canonical_combining_class(ch);
79
59.2M
                        let k = match self.composee {
80
                            None => {
81
2.38M
                                if ch_class != 0 {
82
2.38M
                                    return Some(ch);
83
2.05k
                                }
84
2.05k
                                self.composee = Some(ch);
85
2.05k
                                continue;
86
                            }
87
56.8M
                            Some(k) => k,
88
                        };
89
56.8M
                        match self.last_ccc {
90
22.2M
                            None => match super::char::compose(k, ch) {
91
10.3k
                                Some(r) => {
92
10.3k
                                    self.composee = Some(r);
93
10.3k
                                    continue;
94
                                }
95
                                None => {
96
22.2M
                                    if ch_class == 0 {
97
22.2M
                                        self.composee = Some(ch);
98
22.2M
                                        return Some(k);
99
11.2k
                                    }
100
11.2k
                                    self.buffer.push(ch);
101
11.2k
                                    self.last_ccc = Some(ch_class);
102
                                }
103
                            },
104
34.5M
                            Some(l_class) => {
105
34.5M
                                if l_class >= ch_class {
106
                                    // `ch` is blocked from `composee`
107
34.5M
                                    if ch_class == 0 {
108
10.4k
                                        self.composee = Some(ch);
109
10.4k
                                        self.last_ccc = None;
110
10.4k
                                        self.state = Purging(0);
111
10.4k
                                        return Some(k);
112
34.5M
                                    }
113
34.5M
                                    self.buffer.push(ch);
114
34.5M
                                    self.last_ccc = Some(ch_class);
115
34.5M
                                    continue;
116
21.6k
                                }
117
21.6k
                                match super::char::compose(k, ch) {
118
2.01k
                                    Some(r) => {
119
2.01k
                                        self.composee = Some(r);
120
2.01k
                                        continue;
121
                                    }
122
19.6k
                                    None => {
123
19.6k
                                        self.buffer.push(ch);
124
19.6k
                                        self.last_ccc = Some(ch_class);
125
19.6k
                                    }
126
                                }
127
                            }
128
                        }
129
                    }
130
2.38k
                    self.state = Finished(0);
131
2.38k
                    if self.composee.is_some() {
132
2.05k
                        return self.composee.take();
133
334
                    }
134
                }
135
10.8M
                Purging(next) => match self.buffer.get(next).cloned() {
136
10.4k
                    None => {
137
10.4k
                        self.buffer.clear();
138
10.4k
                        self.state = Composing;
139
10.4k
                    }
140
10.8M
                    s => {
141
10.8M
                        self.state = Purging(next + 1);
142
10.8M
                        return s;
143
                    }
144
                },
145
23.7M
                Finished(next) => match self.buffer.get(next).cloned() {
146
                    None => {
147
2.38k
                        self.buffer.clear();
148
2.38k
                        return self.composee.take();
149
                    }
150
23.7M
                    s => {
151
23.7M
                        self.state = Finished(next + 1);
152
23.7M
                        return s;
153
                    }
154
                },
155
            }
156
        }
157
59.2M
    }
158
}
159
160
impl<I: Iterator<Item = char> + FusedIterator> FusedIterator for Recompositions<I> {}
161
162
impl<I: Iterator<Item = char> + Clone> fmt::Display for Recompositions<I> {
163
0
    fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
164
0
        for c in self.clone() {
165
0
            f.write_char(c)?;
166
        }
167
0
        Ok(())
168
0
    }
169
}