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
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
use Utf16Iterator;
use CharExt;
use errors::{InvalidUtf16Slice,InvalidUtf16Tuple};
extern crate std;
use std::{hash,fmt,cmp};
use std::borrow::Borrow;
use std::ops::Deref;
#[cfg(feature="ascii")]
use std::ascii::AsciiExt;
#[cfg(feature="ascii")]
extern crate ascii;
#[cfg(feature="ascii")]
use self::ascii::{Ascii,AsciiCast};
#[derive(Default)]
#[derive(PartialEq,Eq)]
#[derive(Clone,Copy)]
pub struct Utf16Char {
units: [u16; 2],
}
impl From<char> for Utf16Char {
fn from(c: char) -> Self {
let (first, second) = c.to_utf16_tuple();
Utf16Char{ units: [first, second.unwrap_or(0)] }
}
}
impl From<Utf16Char> for char {
fn from(uc: Utf16Char) -> char {
unsafe{ char::from_utf16_tuple_unchecked(uc.to_tuple()) }
}
}
impl IntoIterator for Utf16Char {
type Item=u16;
type IntoIter=Utf16Iterator;
fn into_iter(self) -> Utf16Iterator {
Utf16Iterator::from(self)
}
}
impl AsRef<[u16]> for Utf16Char {
fn as_ref(&self) -> &[u16] {
&self.units[..self.len()]
}
}
impl Borrow<[u16]> for Utf16Char {
fn borrow(&self) -> &[u16] {
self.as_ref()
}
}
impl Deref for Utf16Char {
type Target = [u16];
fn deref(&self) -> &[u16] {
self.as_ref()
}
}
#[cfg(feature="ascii")]
impl AsciiExt for Utf16Char {
type Owned = Self;
fn is_ascii(&self) -> bool {
self.units[0] < 0x80
}
fn eq_ignore_ascii_case(&self, other: &Self) -> bool {
self.to_char().eq_ignore_ascii_case(&other.to_char())
}
fn to_ascii_uppercase(&self) -> Self {
self.to_char().to_ascii_uppercase().to_utf16()
}
fn to_ascii_lowercase(&self) -> Self {
self.to_char().to_ascii_lowercase().to_utf16()
}
fn make_ascii_uppercase(&mut self) {
*self = self.to_ascii_uppercase()
}
fn make_ascii_lowercase(&mut self) {
*self = self.to_ascii_lowercase();
}
}
#[cfg(feature="ascii")]
impl From<Ascii> for Utf16Char {
fn from(ac: Ascii) -> Self {
Utf16Char{ units: [ac.as_byte() as u16,0] }
}
}
#[cfg(feature="ascii")]
impl<'a> AsciiCast<'a> for Utf16Char {
type Target = Ascii;
unsafe fn to_ascii_nocheck(&'a self) -> Ascii {
(self.units[0] as u8).to_ascii_nocheck()
}
}
impl hash::Hash for Utf16Char {
fn hash<H : hash::Hasher>(&self, state: &mut H) {
self.to_char().hash(state);
}
}
impl fmt::Debug for Utf16Char {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.to_char(), fmtr)
}
}
impl cmp::PartialOrd for Utf16Char {
fn partial_cmp(&self, rhs: &Self) -> Option<cmp::Ordering> {
self.to_char().partial_cmp(&rhs.to_char())
}
}
impl cmp::Ord for Utf16Char {
fn cmp(&self, rhs: &Self) -> cmp::Ordering {
self.to_char().cmp(&rhs.to_char())
}
}
impl Utf16Char {
pub fn from_slice(src: &[u16]) -> Result<(Self,usize),InvalidUtf16Slice> {
char::from_utf16_slice(src).map(
|(_,len)| (Utf16Char{ units: [src[0], if len==2 {src[1]} else {0}] }, len)
)
}
pub fn from_tuple(utf16: (u16,Option<u16>)) -> Result<Self,InvalidUtf16Tuple> {
char::from_utf16_tuple(utf16).map(
|_| Utf16Char{ units: [utf16.0, utf16.1.unwrap_or(0)] }
)
}
pub fn len(self) -> usize {
if self.units[1] == 0 {1} else {2}
}
pub fn to_char(self) -> char {
self.into()
}
pub fn to_slice(self, dst: &mut[u16]) -> Option<usize> {
if self.len() <= dst.len() {
dst[0] = self.units[0];
if self.len() == 2 {
dst[1] = self.units[1];
}
Some(self.len())
} else {
None
}
}
pub fn to_tuple(self) -> (u16,Option<u16>) {
(self.units[0], if self.len()==2 {Some(self.units[1])} else {None})
}
}