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
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
pub use errors::FromStrError;
use error::{InvalidUtf8Slice,InvalidUtf8Array};
use Utf8Iterator;
use CharExt;
use U8UtfExt;
extern crate std;
use std::{hash,fmt, str};
use std::borrow::Borrow;
use std::ops::Deref;
use std::mem::transmute;
#[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, PartialOrd,Ord)]
#[derive(Clone,Copy)]
pub struct Utf8Char {
bytes: [u8; 4],
}
impl str::FromStr for Utf8Char {
type Err = FromStrError;
fn from_str(s: &str) -> Result<Self, FromStrError> {
let mut uc = Utf8Char::default();
for (src, dst) in s.as_bytes().iter().zip(uc.bytes.iter_mut()) {
*dst = *src;
}
if uc.len() == s.len() {Ok(uc)}
else if s.is_empty() {Err(FromStrError::Empty)}
else {Err(FromStrError::SeveralCodePoints)}
}
}
impl From<char> for Utf8Char {
fn from(c: char) -> Self {
Utf8Char{ bytes: c.to_utf8_array().0 }
}
}
impl From<Utf8Char> for char {
fn from(uc: Utf8Char) -> char {
unsafe{ char::from_utf8_exact_slice_unchecked(&uc.bytes[..uc.len()]) }
}
}
impl IntoIterator for Utf8Char {
type Item=u8;
type IntoIter=Utf8Iterator;
fn into_iter(self) -> Utf8Iterator {
Utf8Iterator::from(self)
}
}
impl AsRef<[u8]> for Utf8Char {
fn as_ref(&self) -> &[u8] {
&self.bytes[..self.len()]
}
}
impl AsRef<str> for Utf8Char {
fn as_ref(&self) -> &str {
unsafe{ std::str::from_utf8_unchecked( self.as_ref() ) }
}
}
impl Borrow<[u8]> for Utf8Char {
fn borrow(&self) -> &[u8] {
self.as_ref()
}
}
impl Borrow<str> for Utf8Char {
fn borrow(&self) -> &str {
self.as_ref()
}
}
impl Deref for Utf8Char {
type Target = str;
fn deref(&self) -> &Self::Target {
self.as_ref()
}
}
#[cfg(feature="ascii")]
impl AsciiExt for Utf8Char {
type Owned = Utf8Char;
fn is_ascii(&self) -> bool {
self.bytes[0].is_ascii()
}
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::Owned {
let ascii = self.bytes[0].to_ascii_uppercase();
if ascii == self.bytes[0] {*self}
else {Utf8Char{ bytes: [ascii,0,0,0] }}
}
fn to_ascii_lowercase(&self) -> Self::Owned {
let ascii = self.bytes[0].to_ascii_lowercase();
if ascii == self.bytes[0] {*self}
else {Utf8Char{ bytes: [ascii,0,0,0] }}
}
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 Utf8Char {
fn from(ac: Ascii) -> Self {
Utf8Char{ bytes: [ac.as_byte(),0,0,0] }
}
}
#[cfg(feature="ascii")]
impl<'a> AsciiCast<'a> for Utf8Char {
type Target = Ascii;
unsafe fn to_ascii_nocheck(&'a self) -> Ascii {
self.bytes[0].to_ascii_nocheck()
}
}
impl hash::Hash for Utf8Char {
fn hash<H : hash::Hasher>(&self, state: &mut H) {
self.to_char().hash(state);
}
}
impl fmt::Debug for Utf8Char {
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result {
fmt::Debug::fmt(&self.to_char(), fmtr)
}
}
impl Utf8Char {
pub fn from_slice_start(src: &[u8]) -> Result<(Self,usize),InvalidUtf8Slice> {
let len = try!(char::from_utf8_slice(src)).1;
let mut uc = Self::default();
for (dst, src) in uc.bytes.iter_mut().zip(src).take(len) {
*dst = *src;
}
Ok((uc,len))
}
pub fn from_array(utf8: [u8;4]) -> Result<Self,InvalidUtf8Array> {
try!(char::from_utf8_array(utf8));
let len = Utf8Char{ bytes: utf8 }.len() as u32;
let mask = u32::from_le(0xff_ff_ff_ff >> 8*(4-len));
let unused_zeroed = mask & unsafe{ transmute::<_,u32>(utf8) };
Ok(unsafe{ transmute(unused_zeroed) })
}
pub fn len(self) -> usize {
self.bytes[0].extra_utf8_bytes_unchecked() + 1
}
pub fn to_char(self) -> char {
self.into()
}
pub fn to_slice(self, dst: &mut[u8]) -> Option<usize> {
if self.len() <= dst.len() {
for (dst, src) in dst.iter_mut().zip(self.bytes.iter()) {
*dst = *src;
}
Some(self.len())
} else {
None
}
}
pub fn to_array(self) -> ([u8;4],usize) {
(self.bytes, self.len())
}
}