]>
git.proxmox.com Git - cargo.git/blob - vendor/unicode-normalization/src/recompose.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.
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.
11 use decompose
::Decompositions
;
12 use smallvec
::SmallVec
;
13 use std
::fmt
::{self, Write}
;
16 enum RecompositionState
{
22 /// External iterator for a string recomposition's characters.
24 pub struct Recompositions
<I
> {
25 iter
: Decompositions
<I
>,
26 state
: RecompositionState
,
27 buffer
: SmallVec
<[char; 4]>,
28 composee
: Option
<char>,
33 pub fn new_canonical
<I
: Iterator
<Item
=char>>(iter
: I
) -> Recompositions
<I
> {
35 iter
: super::decompose
::new_canonical(iter
),
36 state
: self::RecompositionState
::Composing
,
37 buffer
: SmallVec
::new(),
44 pub fn new_compatible
<I
: Iterator
<Item
=char>>(iter
: I
) -> Recompositions
<I
> {
46 iter
: super::decompose
::new_compatible(iter
),
47 state
: self::RecompositionState
::Composing
,
48 buffer
: SmallVec
::new(),
54 impl<I
: Iterator
<Item
=char>> Iterator
for Recompositions
<I
> {
58 fn next(&mut self) -> Option
<char> {
59 use self::RecompositionState
::*;
64 for ch
in self.iter
.by_ref() {
65 let ch_class
= super::char::canonical_combining_class(ch
);
66 let k
= match self.composee
{
71 self.composee
= Some(ch
);
78 match super::char::compose(k
, ch
) {
80 self.composee
= Some(r
);
85 self.composee
= Some(ch
);
89 self.last_ccc
= Some(ch_class
);
94 if l_class
>= ch_class
{
95 // `ch` is blocked from `composee`
97 self.composee
= Some(ch
);
99 self.state
= Purging(0);
102 self.buffer
.push(ch
);
103 self.last_ccc
= Some(ch_class
);
106 match super::char::compose(k
, ch
) {
108 self.composee
= Some(r
);
112 self.buffer
.push(ch
);
113 self.last_ccc
= Some(ch_class
);
119 self.state
= Finished(0);
120 if self.composee
.is_some() {
121 return self.composee
.take();
125 match self.buffer
.get(next
).cloned() {
128 self.state
= Composing
;
131 self.state
= Purging(next
+ 1);
137 match self.buffer
.get(next
).cloned() {
140 return self.composee
.take()
143 self.state
= Finished(next
+ 1);
153 impl<I
: Iterator
<Item
=char> + Clone
> fmt
::Display
for Recompositions
<I
> {
154 fn fmt(&self, f
: &mut fmt
::Formatter
) -> fmt
::Result
{
155 for c
in self.clone() {