| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
|
| 6 |
|
| 7 |
|
| 8 |
|
| 9 |
use utf16_iterators::Utf16Iterator; |
| 10 |
use traits::{CharExt, U16UtfExt}; |
| 11 |
use utf8_char::Utf8Char; |
| 12 |
use errors::{InvalidUtf16Slice, InvalidUtf16Array, InvalidUtf16Tuple}; |
| 13 |
use errors::{NonBMPError, EmptyStrError, FromStrError}; |
| 14 |
extern crate core; |
| 15 |
use self::core::{hash,fmt}; |
| 16 |
use self::core::cmp::Ordering; |
| 17 |
use self::core::borrow::Borrow; |
| 18 |
use self::core::ops::Deref; |
| 19 |
use self::core::str::FromStr; |
| 20 |
#[cfg(feature="std")] |
| 21 |
use self::core::iter::FromIterator; |
| 22 |
#[cfg(feature="std")] |
| 23 |
#[allow(deprecated)] |
| 24 |
use std::ascii::AsciiExt; |
| 25 |
#[cfg(feature="ascii")] |
| 26 |
use self::core::char; |
| 27 |
#[cfg(feature="ascii")] |
| 28 |
extern crate ascii; |
| 29 |
#[cfg(feature="ascii")] |
| 30 |
use self::ascii::{AsciiChar,ToAsciiChar,ToAsciiCharError}; |
| 31 |
|
| 32 |
|
| 33 |
|
| 34 |
#[derive(Default)] |
| 35 |
|
| 36 |
|
| 37 |
|
| 38 |
#[derive(PartialEq,Eq)] |
| 39 |
#[derive(Clone,Copy)] |
| 40 |
|
| 41 |
|
| 42 |
|
| 43 |
|
| 44 |
|
| 45 |
pub struct Utf16Char { |
| 46 |
units: [u16; 2], |
| 47 |
} |
| 48 |
|
| 49 |
|
| 50 |
|
| 51 |
|
| 52 |
|
| 53 |
impl FromStr for Utf16Char { |
| 54 |
type Err = FromStrError; |
| 55 |
|
| 56 |
|
| 57 |
|
| 58 |
|
| 59 |
|
| 60 |
|
| 61 |
|
| 62 |
|
| 63 |
|
| 64 |
|
| 65 |
|
| 66 |
))); |
| 67 |
|
| 68 |
|
| 69 |
k |
| 70 |
|
| 71 |
fn from_str(s: &str) -> Result<Self, FromStrError> { |
| 72 |
match Utf16Char::from_str_start(s) { |
| 73 |
Ok((u16c,bytes)) if bytes == s.len() => Ok(u16c), |
| 74 |
Ok((_,_)) => Err(FromStrError::MultipleCodepoints), |
| 75 |
Err(EmptyStrError) => Err(FromStrError::Empty), |
| 76 |
} |
| 77 |
} |
| 78 |
} |
| 79 |
impl From<char> for Utf16Char { |
| 80 |
fn from(c: char) -> Self { |
| 81 |
let (first, second) = c.to_utf16_tuple(); |
| 82 |
Utf16Char{ units: [first, second.unwrap_or(0)] } |
| 83 |
} |
| 84 |
} |
| 85 |
impl From<Utf8Char> for Utf16Char { |
| 86 |
fn from(utf8: Utf8Char) -> Utf16Char { |
| 87 |
let (b, utf8_len) = utf8.to_array(); |
| 88 |
match utf8_len { |
| 89 |
1 => Utf16Char{ units: [b[0] as u16, 0] }, |
| 90 |
4 => { |
| 91 |
let mut first = 0xd800 - (0x01_00_00u32 >> 10) as u16; |
| 92 |
first += (b[0] as u16 & 0x07) << 8; |
| 93 |
first += (b[1] as u16 & 0x3f) << 2; |
| 94 |
first += (b[2] as u16 & 0x30) >> 4; |
| 95 |
let mut second = 0xdc00; |
| 96 |
second |= (b[2] as u16 & 0x0f) << 6; |
| 97 |
second |= b[3] as u16 & 0x3f; |
| 98 |
Utf16Char{ units: [first, second] } |
| 99 |
}, |
| 100 |
_ => { |
| 101 |
let mut unit = ((b[0] as u16 & 0x1f) << 6) | (b[1] as u16 & 0x3f); |
| 102 |
if utf8_len == 3 { |
| 103 |
unit = (unit << 6) | (b[2] as u16 & 0x3f); |
| 104 |
} |
| 105 |
Utf16Char{ units: [unit, 0] } |
| 106 |
}, |
| 107 |
} |
| 108 |
} |
| 109 |
} |
| 110 |
impl From<Utf16Char> for char { |
| 111 |
fn from(uc: Utf16Char) -> char { |
| 112 |
char::from_utf16_array_unchecked(uc.to_array()) |
| 113 |
} |
| 114 |
} |
| 115 |
impl IntoIterator for Utf16Char { |
| 116 |
type Item=u16; |
| 117 |
type IntoIter=Utf16Iterator; |
| 118 |
|
| 119 |
fn into_iter(self) -> Utf16Iterator { |
| 120 |
Utf16Iterator::from(self) |
| 121 |
} |
| 122 |
} |
| 123 |
|
| 124 |
#[cfg(feature="std")] |
| 125 |
impl Extend<Utf16Char> for Vec<u16> { |
| 126 |
fn extend<I:IntoIterator<Item=Utf16Char>>(&mut self, iter: I) { |
| 127 |
let iter = iter.into_iter(); |
| 128 |
self.reserve(iter.size_hint().0); |
| 129 |
for u16c in iter { |
| 130 |
self.push(u16c.units[0]); |
| 131 |
if u16c.units[1] != 0 { |
| 132 |
self.push(u16c.units[1]); |
| 133 |
} |
| 134 |
} |
| 135 |
} |
| 136 |
} |
| 137 |
#[cfg(feature="std")] |
| 138 |
impl<'a> Extend<&'a Utf16Char> for Vec<u16> { |
| 139 |
fn extend<I:IntoIterator<Item=&'a Utf16Char>>(&mut self, iter: I) { |
| 140 |
self.extend(iter.into_iter().cloned()) |
| 141 |
} |
| 142 |
} |
| 143 |
#[cfg(feature="std")] |
| 144 |
impl FromIterator<Utf16Char> for Vec<u16> { |
| 145 |
fn from_iter<I:IntoIterator<Item=Utf16Char>>(iter: I) -> Self { |
| 146 |
let mut vec = Vec::new(); |
| 147 |
vec.extend(iter); |
| 148 |
return vec; |
| 149 |
} |
| 150 |
} |
| 151 |
#[cfg(feature="std")] |
| 152 |
impl<'a> FromIterator<&'a Utf16Char> for Vec<u16> { |
| 153 |
fn from_iter<I:IntoIterator<Item=&'a Utf16Char>>(iter: I) -> Self { |
| 154 |
Self::from_iter(iter.into_iter().cloned()) |
| 155 |
} |
| 156 |
} |
| 157 |
|
| 158 |
#[cfg(feature="std")] |
| 159 |
impl Extend<Utf16Char> for String { |
| 160 |
fn extend<I:IntoIterator<Item=Utf16Char>>(&mut self, iter: I) { |
| 161 |
self.extend(iter.into_iter().map(|u16c| Utf8Char::from(u16c) )); |
| 162 |
} |
| 163 |
} |
| 164 |
#[cfg(feature="std")] |
| 165 |
impl<'a> Extend<&'a Utf16Char> for String { |
| 166 |
fn extend<I:IntoIterator<Item=&'a Utf16Char>>(&mut self, iter: I) { |
| 167 |
self.extend(iter.into_iter().cloned()); |
| 168 |
} |
| 169 |
} |
| 170 |
#[cfg(feature="std")] |
| 171 |
impl FromIterator<Utf16Char> for String { |
| 172 |
fn from_iter<I:IntoIterator<Item=Utf16Char>>(iter: I) -> Self { |
| 173 |
let mut s = String::new(); |
| 174 |
s.extend(iter); |
| 175 |
return s; |
| 176 |
} |
| 177 |
} |
| 178 |
#[cfg(feature="std")] |
| 179 |
impl<'a> FromIterator<&'a Utf16Char> for String { |
| 180 |
fn from_iter<I:IntoIterator<Item=&'a Utf16Char>>(iter: I) -> Self { |
| 181 |
Self::from_iter(iter.into_iter().cloned()) |
| 182 |
} |
| 183 |
} |
| 184 |
|
| 185 |
|
| 186 |
|
| 187 |
|
| 188 |
|
| 189 |
impl AsRef<[u16]> for Utf16Char { |
| 190 |
#[inline] |
| 191 |
fn as_ref(&self) -> &[u16] { |
| 192 |
&self.units[..self.len()] |
| 193 |
} |
| 194 |
} |
| 195 |
impl Borrow<[u16]> for Utf16Char { |
| 196 |
#[inline] |
| 197 |
fn borrow(&self) -> &[u16] { |
| 198 |
self.as_ref() |
| 199 |
} |
| 200 |
} |
| 201 |
impl Deref for Utf16Char { |
| 202 |
type Target = [u16]; |
| 203 |
#[inline] |
| 204 |
fn deref(&self) -> &[u16] { |
| 205 |
self.as_ref() |
| 206 |
} |
| 207 |
} |
| 208 |
|
| 209 |
|
| 210 |
|
| 211 |
|
| 212 |
|
| 213 |
#[cfg(feature="std")] |
| 214 |
#[allow(deprecated)] |
| 215 |
impl AsciiExt for Utf16Char { |
| 216 |
type Owned = Self; |
| 217 |
fn is_ascii(&self) -> bool { |
| 218 |
self.units[0] < 128 |
| 219 |
} |
| 220 |
fn eq_ignore_ascii_case(&self, other: &Self) -> bool { |
| 221 |
self.to_ascii_lowercase() == other.to_ascii_lowercase() |
| 222 |
} |
| 223 |
fn to_ascii_uppercase(&self) -> Self { |
| 224 |
let n = self.units[0].wrapping_sub(b'a' as u16); |
| 225 |
if n < 26 {Utf16Char{ units: [n+b'A' as u16, 0] }} |
| 226 |
else {*self} |
| 227 |
} |
| 228 |
fn to_ascii_lowercase(&self) -> Self { |
| 229 |
let n = self.units[0].wrapping_sub(b'A' as u16); |
| 230 |
if n < 26 {Utf16Char{ units: [n+b'a' as u16, 0] }} |
| 231 |
else {*self} |
| 232 |
} |
| 233 |
fn make_ascii_uppercase(&mut self) { |
| 234 |
*self = self.to_ascii_uppercase() |
| 235 |
} |
| 236 |
fn make_ascii_lowercase(&mut self) { |
| 237 |
*self = self.to_ascii_lowercase(); |
| 238 |
} |
| 239 |
} |
| 240 |
|
| 241 |
#[cfg(feature="ascii")] |
| 242 |
|
| 243 |
impl From<AsciiChar> for Utf16Char { |
| 244 |
#[inline] |
| 245 |
fn from(ac: AsciiChar) -> Self { |
| 246 |
Utf16Char{ units: [ac.as_byte() as u16, 0] } |
| 247 |
} |
| 248 |
} |
| 249 |
#[cfg(feature="ascii")] |
| 250 |
|
| 251 |
impl ToAsciiChar for Utf16Char { |
| 252 |
#[inline] |
| 253 |
fn to_ascii_char(self) -> Result<AsciiChar, ToAsciiCharError> { |
| 254 |
|
| 255 |
if self.is_ascii() {self.units[0] as u8} else {255}.to_ascii_char() |
| 256 |
} |
| 257 |
#[inline] |
| 258 |
unsafe fn to_ascii_char_unchecked(self) -> AsciiChar { |
| 259 |
(self.units[0] as u8).to_ascii_char_unchecked() |
| 260 |
} |
| 261 |
} |
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
impl hash::Hash for Utf16Char { |
| 268 |
fn hash<H : hash::Hasher>(&self, state: &mut H) { |
| 269 |
self.to_char().hash(state); |
| 270 |
} |
| 271 |
} |
| 272 |
impl fmt::Debug for Utf16Char { |
| 273 |
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result { |
| 274 |
fmt::Debug::fmt(&self.to_char(), fmtr) |
| 275 |
} |
| 276 |
} |
| 277 |
impl fmt::Display for Utf16Char { |
| 278 |
fn fmt(&self, fmtr: &mut fmt::Formatter) -> fmt::Result { |
| 279 |
fmt::Display::fmt(&Utf8Char::from(*self), fmtr) |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
|
| 284 |
impl PartialOrd for Utf16Char { |
| 285 |
#[inline] |
| 286 |
fn partial_cmp(&self, rhs: &Self) -> Option<Ordering> { |
| 287 |
Some(self.cmp(rhs)) |
| 288 |
} |
| 289 |
} |
| 290 |
impl Ord for Utf16Char { |
| 291 |
#[inline] |
| 292 |
fn cmp(&self, rhs: &Self) -> Ordering { |
| 293 |
|
| 294 |
|
| 295 |
|
| 296 |
|
| 297 |
|
| 298 |
|
| 299 |
let lhs = (self.units[0] as u32, self.units[1] as u32); |
| 300 |
let rhs = (rhs.units[0] as u32, rhs.units[1] as u32); |
| 301 |
let lhs = (lhs.0 << (lhs.1 >> 12)) + lhs.1; |
| 302 |
let rhs = (rhs.0 << (rhs.1 >> 12)) + rhs.1; |
| 303 |
lhs.cmp(&rhs) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
|
| 308 |
|
| 309 |
|
| 310 |
|
| 311 |
impl PartialEq<char> for Utf16Char { |
| 312 |
fn eq(&self, u32c: &char) -> bool { |
| 313 |
*self == Utf16Char::from(*u32c) |
| 314 |
} |
| 315 |
} |
| 316 |
impl PartialEq<Utf16Char> for char { |
| 317 |
fn eq(&self, u16c: &Utf16Char) -> bool { |
| 318 |
Utf16Char::from(*self) == *u16c |
| 319 |
} |
| 320 |
} |
| 321 |
impl PartialOrd<char> for Utf16Char { |
| 322 |
fn partial_cmp(&self, u32c: &char) -> Option<Ordering> { |
| 323 |
self.partial_cmp(&Utf16Char::from(*u32c)) |
| 324 |
} |
| 325 |
} |
| 326 |
impl PartialOrd<Utf16Char> for char { |
| 327 |
fn partial_cmp(&self, u16c: &Utf16Char) -> Option<Ordering> { |
| 328 |
Utf16Char::from(*self).partial_cmp(u16c) |
| 329 |
} |
| 330 |
} |
| 331 |
|
| 332 |
impl PartialEq<Utf8Char> for Utf16Char { |
| 333 |
fn eq(&self, u8c: &Utf8Char) -> bool { |
| 334 |
*self == Utf16Char::from(*u8c) |
| 335 |
} |
| 336 |
} |
| 337 |
impl PartialOrd<Utf8Char> for Utf16Char { |
| 338 |
fn partial_cmp(&self, u8c: &Utf8Char) -> Option<Ordering> { |
| 339 |
self.partial_cmp(&Utf16Char::from(*u8c)) |
| 340 |
} |
| 341 |
} |
| 342 |
|
| 343 |
|
| 344 |
|
| 345 |
|
| 346 |
|
| 347 |
|
| 348 |
|
| 349 |
|
| 350 |
|
| 351 |
|
| 352 |
|
| 353 |
|
| 354 |
|
| 355 |
|
| 356 |
|
| 357 |
|
| 358 |
impl PartialEq<u16> for Utf16Char { |
| 359 |
fn eq(&self, unit: &u16) -> bool { |
| 360 |
self.units[0] == *unit && self.units[1] == 0 |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
|
| 365 |
|
| 366 |
|
| 367 |
|
| 368 |
|
| 369 |
|
| 370 |
|
| 371 |
|
| 372 |
|
| 373 |
impl PartialEq<u8> for Utf16Char { |
| 374 |
fn eq(&self, byte: &u8) -> bool { |
| 375 |
self.units[0] == *byte as u16 |
| 376 |
} |
| 377 |
} |
| 378 |
#[cfg(feature = "ascii")] |
| 379 |
|
| 380 |
impl PartialEq<AsciiChar> for Utf16Char { |
| 381 |
#[inline] |
| 382 |
fn eq(&self, ascii: &AsciiChar) -> bool { |
| 383 |
self.units[0] == *ascii as u16 |
| 384 |
} |
| 385 |
} |
| 386 |
#[cfg(feature = "ascii")] |
| 387 |
|
| 388 |
impl PartialEq<Utf16Char> for AsciiChar { |
| 389 |
#[inline] |
| 390 |
fn eq(&self, u16c: &Utf16Char) -> bool { |
| 391 |
*self as u16 == u16c.units[0] |
| 392 |
} |
| 393 |
} |
| 394 |
#[cfg(feature = "ascii")] |
| 395 |
|
| 396 |
impl PartialOrd<AsciiChar> for Utf16Char { |
| 397 |
#[inline] |
| 398 |
fn partial_cmp(&self, ascii: &AsciiChar) -> Option<Ordering> { |
| 399 |
self.units[0].partial_cmp(&(*ascii as u16)) |
| 400 |
} |
| 401 |
} |
| 402 |
#[cfg(feature = "ascii")] |
| 403 |
|
| 404 |
impl PartialOrd<Utf16Char> for AsciiChar { |
| 405 |
#[inline] |
| 406 |
fn partial_cmp(&self, u16c: &Utf16Char) -> Option<Ordering> { |
| 407 |
(*self as u16).partial_cmp(&u16c.units[0]) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
|
| 412 |
|
| 413 |
|
| 414 |
|
| 415 |
impl Utf16Char { |
| 416 |
|
| 417 |
|
| 418 |
|
| 419 |
|
| 420 |
|
| 421 |
|
| 422 |
|
| 423 |
|
| 424 |
|
| 425 |
|
| 426 |
|
| 427 |
|
| 428 |
|
| 429 |
|
| 430 |
|
| 431 |
))); |
| 432 |
k |
| 433 |
|
| 434 |
|
| 435 |
pub fn from_str_start(s: &str) -> Result<(Self,usize), EmptyStrError> { |
| 436 |
if s.is_empty() { |
| 437 |
return Err(EmptyStrError); |
| 438 |
} |
| 439 |
let b = s.as_bytes(); |
| 440 |
|
| 441 |
match b[0] { |
| 442 |
0...127 => { |
| 443 |
let unit = b[0] as u16; |
| 444 |
Ok((Utf16Char{ units: [unit, 0] }, 1)) |
| 445 |
}, |
| 446 |
0b1000_0000...0b1101_1111 => { |
| 447 |
let unit = (((b[1] & 0x3f) as u16) << 0) |
| 448 |
| (((b[0] & 0x1f) as u16) << 6); |
| 449 |
Ok((Utf16Char{ units: [unit, 0] }, 2)) |
| 450 |
}, |
| 451 |
0b1110_0000...0b1110_1111 => { |
| 452 |
let unit = (((b[2] & 0x3f) as u16) << 0) |
| 453 |
| (((b[1] & 0x3f) as u16) << 6) |
| 454 |
| (((b[0] & 0x0f) as u16) << 12); |
| 455 |
Ok((Utf16Char{ units: [unit, 0] }, 3)) |
| 456 |
}, |
| 457 |
_ => { |
| 458 |
let second = 0xdc00 |
| 459 |
| (((b[3] & 0x3f) as u16) << 0) |
| 460 |
| (((b[2] & 0x0f) as u16) << 6); |
| 461 |
let first = 0xd800-(0x01_00_00u32>>10) as u16 |
| 462 |
+ (((b[2] & 0x30) as u16) >> 4) |
| 463 |
+ (((b[1] & 0x3f) as u16) << 2) |
| 464 |
+ (((b[0] & 0x07) as u16) << 8); |
| 465 |
Ok((Utf16Char{ units: [first, second] }, 4)) |
| 466 |
} |
| 467 |
} |
| 468 |
} |
| 469 |
|
| 470 |
|
| 471 |
pub fn from_slice_start(src: &[u16]) -> Result<(Self,usize), InvalidUtf16Slice> { |
| 472 |
char::from_utf16_slice_start(src).map(|(_,len)| { |
| 473 |
let second = if len==2 {src[1]} else {0}; |
| 474 |
(Utf16Char{ units: [src[0], second] }, len) |
| 475 |
}) |
| 476 |
} |
| 477 |
|
| 478 |
|
| 479 |
|
| 480 |
|
| 481 |
|
| 482 |
|
| 483 |
pub unsafe fn from_slice_start_unchecked(src: &[u16]) -> (Self,usize) { |
| 484 |
let first = *src.get_unchecked(0); |
| 485 |
if first.is_utf16_leading_surrogate() { |
| 486 |
(Utf16Char{ units: [first, *src.get_unchecked(1)] }, 2) |
| 487 |
} else { |
| 488 |
(Utf16Char{ units: [first, 0] }, 1) |
| 489 |
} |
| 490 |
} |
| 491 |
|
| 492 |
|
| 493 |
|
| 494 |
|
| 495 |
|
| 496 |
|
| 497 |
|
| 498 |
|
| 499 |
|
| 500 |
))); |
| 501 |
|
| 502 |
|
| 503 |
|
| 504 |
|
| 505 |
|
| 506 |
pub fn from_array(units: [u16; 2]) -> Result<Self,InvalidUtf16Array> { |
| 507 |
if (units[0] & 0xf8_00) != 0xd8_00 { |
| 508 |
Ok(Utf16Char { units: [units[0], 0] }) |
| 509 |
} else if units[0] < 0xdc_00 && (units[1] & 0xfc_00) == 0xdc_00 { |
| 510 |
Ok(Utf16Char { units: units }) |
| 511 |
} else if units[0] < 0xdc_00 { |
| 512 |
Err(InvalidUtf16Array::SecondIsNotTrailingSurrogate) |
| 513 |
} else { |
| 514 |
Err(InvalidUtf16Array::FirstIsTrailingSurrogate) |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
|
| 519 |
|
| 520 |
|
| 521 |
|
| 522 |
|
| 523 |
|
| 524 |
|
| 525 |
pub unsafe fn from_array_unchecked(units: [u16; 2]) -> Self { |
| 526 |
Utf16Char { units: units } |
| 527 |
} |
| 528 |
|
| 529 |
pub fn from_tuple(utf16: (u16,Option<u16>)) -> Result<Self,InvalidUtf16Tuple> { |
| 530 |
unsafe {char::from_utf16_tuple(utf16).map(|_| |
| 531 |
Self::from_tuple_unchecked(utf16) |
| 532 |
)} |
| 533 |
} |
| 534 |
|
| 535 |
|
| 536 |
|
| 537 |
|
| 538 |
|
| 539 |
|
| 540 |
|
| 541 |
pub unsafe fn from_tuple_unchecked(utf16: (u16,Option<u16>)) -> Self { |
| 542 |
Utf16Char { units: [utf16.0, utf16.1.unwrap_or(0)] } |
| 543 |
} |
| 544 |
|
| 545 |
|
| 546 |
|
| 547 |
|
| 548 |
|
| 549 |
|
| 550 |
|
| 551 |
|
| 552 |
|
| 553 |
|
| 554 |
|
| 555 |
|
| 556 |
|
| 557 |
|
| 558 |
|
| 559 |
); |
| 560 |
|
| 561 |
|
| 562 |
pub fn from_bmp(bmp_codepoint: u16) -> Result<Self,NonBMPError> { |
| 563 |
if bmp_codepoint & 0xf800 != 0xd800 { |
| 564 |
Ok(Utf16Char{ units: [bmp_codepoint, 0] }) |
| 565 |
} else { |
| 566 |
Err(NonBMPError) |
| 567 |
} |
| 568 |
} |
| 569 |
|
| 570 |
|
| 571 |
|
| 572 |
|
| 573 |
|
| 574 |
|
| 575 |
|
| 576 |
|
| 577 |
#[inline] |
| 578 |
pub unsafe fn from_bmp_unchecked(bmp_codepoint: u16) -> Self { |
| 579 |
Utf16Char{ units: [bmp_codepoint, 0] } |
| 580 |
} |
| 581 |
|
| 582 |
|
| 583 |
|
| 584 |
|
| 585 |
|
| 586 |
|
| 587 |
); |
| 588 |
); |
| 589 |
|
| 590 |
#[inline] |
| 591 |
pub fn is_bmp(&self) -> bool { |
| 592 |
self.units[1] == 0 |
| 593 |
} |
| 594 |
|
| 595 |
|
| 596 |
|
| 597 |
|
| 598 |
|
| 599 |
#[inline] |
| 600 |
pub fn len(self) -> usize { |
| 601 |
1 + (self.units[1] as usize >> 15) |
| 602 |
} |
| 603 |
|
| 604 |
|
| 605 |
|
| 606 |
#[inline] |
| 607 |
pub fn is_ascii(&self) -> bool { |
| 608 |
self.units[0] <= 127 |
| 609 |
} |
| 610 |
|
| 611 |
|
| 612 |
|
| 613 |
#[cfg(feature="std")] |
| 614 |
pub fn eq_ignore_ascii_case(&self, other: &Self) -> bool { |
| 615 |
self.to_ascii_lowercase() == other.to_ascii_lowercase() |
| 616 |
} |
| 617 |
|
| 618 |
|
| 619 |
|
| 620 |
|
| 621 |
#[cfg(feature="std")] |
| 622 |
pub fn to_ascii_uppercase(&self) -> Self { |
| 623 |
let n = self.units[0].wrapping_sub(b'a' as u16); |
| 624 |
if n < 26 {Utf16Char{ units: [n+b'A' as u16, 0] }} |
| 625 |
else {*self} |
| 626 |
} |
| 627 |
|
| 628 |
|
| 629 |
|
| 630 |
|
| 631 |
#[cfg(feature="std")] |
| 632 |
pub fn to_ascii_lowercase(&self) -> Self { |
| 633 |
let n = self.units[0].wrapping_sub(b'A' as u16); |
| 634 |
if n < 26 {Utf16Char{ units: [n+b'a' as u16, 0] }} |
| 635 |
else {*self} |
| 636 |
} |
| 637 |
|
| 638 |
|
| 639 |
|
| 640 |
|
| 641 |
#[cfg(feature="std")] |
| 642 |
pub fn make_ascii_uppercase(&mut self) { |
| 643 |
*self = self.to_ascii_uppercase() |
| 644 |
} |
| 645 |
|
| 646 |
|
| 647 |
|
| 648 |
|
| 649 |
#[cfg(feature="std")] |
| 650 |
pub fn make_ascii_lowercase(&mut self) { |
| 651 |
*self = self.to_ascii_lowercase(); |
| 652 |
} |
| 653 |
|
| 654 |
|
| 655 |
pub fn to_char(self) -> char { |
| 656 |
self.into() |
| 657 |
} |
| 658 |
|
| 659 |
|
| 660 |
|
| 661 |
|
| 662 |
|
| 663 |
|
| 664 |
|
| 665 |
pub fn to_slice(self, dst: &mut[u16]) -> usize { |
| 666 |
|
| 667 |
let extra = self.units[1] as usize >> 15; |
| 668 |
match dst.get_mut(extra) { |
| 669 |
Some(first) => *first = self.units[extra], |
| 670 |
None => panic!("The provided buffer is too small.") |
| 671 |
} |
| 672 |
if extra != 0 {dst[0] = self.units[0];} |
| 673 |
extra+1 |
| 674 |
} |
| 675 |
|
| 676 |
|
| 677 |
|
| 678 |
#[inline] |
| 679 |
pub fn to_array(self) -> [u16;2] { |
| 680 |
self.units |
| 681 |
} |
| 682 |
|
| 683 |
#[inline] |
| 684 |
pub fn to_tuple(self) -> (u16,Option<u16>) { |
| 685 |
(self.units[0], if self.units[1]==0 {None} else {Some(self.units[1])}) |
| 686 |
} |
| 687 |
} |