1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
use CharExt;
use Utf16Char;
extern crate std;
use std::fmt;
#[derive(Clone,Copy)]
pub struct Utf16Iterator {
first: Option<u16>,
second: Option<u16>,
}
impl From<char> for Utf16Iterator {
fn from(c: char) -> Self {
let (first, second) = c.to_utf16_tuple();
Utf16Iterator{ first: Some(first), second: second }
}
}
impl From<Utf16Char> for Utf16Iterator {
fn from(uc: Utf16Char) -> Self {
let (first, second) = uc.to_tuple();
Utf16Iterator{ first: Some(first), second: second }
}
}
impl Iterator for Utf16Iterator {
type Item=u16;
fn next(&mut self) -> Option<u16> {
match (self.first, self.second) {
(Some(first), _) => {self.first = None; Some(first) },
(None, Some(second)) => {self.second = None; Some(second)},
(None, None) => { None },
}
}
fn size_hint(&self) -> (usize, Option<usize>) {
(self.len(), Some(self.len()))
}
}
impl ExactSizeIterator for Utf16Iterator {
fn len(&self) -> usize {
match (self.first, self.second) {
(None , None ) => 0,
(Some(_), None ) => 1,
(None , Some(_)) => 1,
(Some(_), Some(_)) => 2,
}
}
}
impl fmt::Debug for Utf16Iterator {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
let v: Vec<u16> = self.collect();
write!(fmtr, "{:?}", v)
}
}