| 1 |
|
| 2 |
|
| 3 |
|
| 4 |
|
| 5 |
use crate::{U16CStr, U16CString, U16Str, U32CStr, U32CString, U32Str}; |
| 6 |
#[allow(unused_imports)] |
| 7 |
use alloc::{ |
| 8 |
borrow::{Cow, ToOwned}, |
| 9 |
boxed::Box, |
| 10 |
string::String, |
| 11 |
vec::Vec, |
| 12 |
}; |
| 13 |
#[allow(unused_imports)] |
| 14 |
use core::{ |
| 15 |
borrow::{Borrow, BorrowMut}, |
| 16 |
char, cmp, |
| 17 |
convert::Infallible, |
| 18 |
fmt::Write, |
| 19 |
iter::FromIterator, |
| 20 |
mem, |
| 21 |
ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut, RangeBounds}, |
| 22 |
slice::{self, SliceIndex}, |
| 23 |
str::FromStr, |
| 24 |
}; |
| 25 |
|
| 26 |
mod iter; |
| 27 |
|
| 28 |
pub use iter::*; |
| 29 |
|
| 30 |
macro_rules! ustring_common_impl { |
| 31 |
{ |
| 32 |
$(#[$ustring_meta:meta])* |
| 33 |
struct $ustring:ident([$uchar:ty]); |
| 34 |
type UStr = $ustr:ident; |
| 35 |
type UCString = $ucstring:ident; |
| 36 |
type UCStr = $ucstr:ident; |
| 37 |
type UtfStr = $utfstr:ident; |
| 38 |
type UtfString = $utfstring:ident; |
| 39 |
$(#[$push_meta:meta])* |
| 40 |
fn push() -> {} |
| 41 |
$(#[$push_slice_meta:meta])* |
| 42 |
fn push_slice() -> {} |
| 43 |
$(#[$into_boxed_ustr_meta:meta])* |
| 44 |
fn into_boxed_ustr() -> {} |
| 45 |
} => { |
| 46 |
$(#[$ustring_meta])* |
| 47 |
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] |
| 48 |
#[derive(Default, Clone, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 49 |
pub struct $ustring { |
| 50 |
pub(crate) inner: Vec<$uchar>, |
| 51 |
} |
| 52 |
|
| 53 |
impl $ustring { |
| 54 |
|
| 55 |
#[inline] |
| 56 |
#[must_use] |
| 57 |
pub const fn new() -> Self { |
| 58 |
Self { inner: Vec::new() } |
| 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 |
#[inline] |
| 86 |
#[must_use] |
| 87 |
pub fn from_vec(raw: impl Into<Vec<$uchar>>) -> Self { |
| 88 |
Self { inner: raw.into() } |
| 89 |
} |
| 90 |
|
| 91 |
|
| 92 |
|
| 93 |
|
| 94 |
|
| 95 |
|
| 96 |
|
| 97 |
|
| 98 |
|
| 99 |
|
| 100 |
|
| 101 |
|
| 102 |
|
| 103 |
|
| 104 |
|
| 105 |
|
| 106 |
#[must_use] |
| 107 |
pub unsafe fn from_ptr(p: *const $uchar, len: usize) -> Self { |
| 108 |
if len == 0 { |
| 109 |
return Self::new(); |
| 110 |
} |
| 111 |
assert!(!p.is_null()); |
| 112 |
let slice = slice::from_raw_parts(p, len); |
| 113 |
Self::from_vec(slice) |
| 114 |
} |
| 115 |
|
| 116 |
|
| 117 |
|
| 118 |
|
| 119 |
|
| 120 |
#[inline] |
| 121 |
#[must_use] |
| 122 |
pub fn with_capacity(capacity: usize) -> Self { |
| 123 |
Self { |
| 124 |
inner: Vec::with_capacity(capacity), |
| 125 |
} |
| 126 |
} |
| 127 |
|
| 128 |
|
| 129 |
#[inline] |
| 130 |
#[must_use] |
| 131 |
pub fn capacity(&self) -> usize { |
| 132 |
self.inner.capacity() |
| 133 |
} |
| 134 |
|
| 135 |
|
| 136 |
#[inline] |
| 137 |
pub fn clear(&mut self) { |
| 138 |
self.inner.clear() |
| 139 |
} |
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
|
| 144 |
|
| 145 |
#[inline] |
| 146 |
pub fn reserve(&mut self, additional: usize) { |
| 147 |
self.inner.reserve(additional) |
| 148 |
} |
| 149 |
|
| 150 |
|
| 151 |
|
| 152 |
|
| 153 |
|
| 154 |
|
| 155 |
|
| 156 |
#[inline] |
| 157 |
pub fn reserve_exact(&mut self, additional: usize) { |
| 158 |
self.inner.reserve_exact(additional) |
| 159 |
} |
| 160 |
|
| 161 |
|
| 162 |
#[inline] |
| 163 |
#[must_use] |
| 164 |
pub fn into_vec(self) -> Vec<$uchar> { |
| 165 |
self.inner |
| 166 |
} |
| 167 |
|
| 168 |
|
| 169 |
#[inline] |
| 170 |
#[must_use] |
| 171 |
pub fn as_ustr(&self) -> &$ustr { |
| 172 |
$ustr::from_slice(&self.inner) |
| 173 |
} |
| 174 |
|
| 175 |
|
| 176 |
#[inline] |
| 177 |
#[must_use] |
| 178 |
pub fn as_mut_ustr(&mut self) -> &mut $ustr { |
| 179 |
$ustr::from_slice_mut(&mut self.inner) |
| 180 |
} |
| 181 |
|
| 182 |
|
| 183 |
#[inline] |
| 184 |
#[must_use] |
| 185 |
pub fn as_vec(&self) -> &Vec<$uchar> { |
| 186 |
&self.inner |
| 187 |
} |
| 188 |
|
| 189 |
|
| 190 |
#[inline] |
| 191 |
#[must_use] |
| 192 |
pub fn as_mut_vec(&mut self) -> &mut Vec<$uchar> { |
| 193 |
&mut self.inner |
| 194 |
} |
| 195 |
|
| 196 |
$(#[$push_meta])* |
| 197 |
#[inline] |
| 198 |
pub fn push(&mut self, s: impl AsRef<$ustr>) { |
| 199 |
self.inner.extend_from_slice(&s.as_ref().as_slice()) |
| 200 |
} |
| 201 |
|
| 202 |
$(#[$push_slice_meta])* |
| 203 |
#[inline] |
| 204 |
pub fn push_slice(&mut self, s: impl AsRef<[$uchar]>) { |
| 205 |
self.inner.extend_from_slice(s.as_ref()) |
| 206 |
} |
| 207 |
|
| 208 |
|
| 209 |
#[inline] |
| 210 |
pub fn shrink_to_fit(&mut self) { |
| 211 |
self.inner.shrink_to_fit(); |
| 212 |
} |
| 213 |
|
| 214 |
|
| 215 |
|
| 216 |
|
| 217 |
|
| 218 |
|
| 219 |
|
| 220 |
#[inline] |
| 221 |
pub fn shrink_to(&mut self, min_capacity: usize) { |
| 222 |
self.inner.shrink_to(min_capacity) |
| 223 |
} |
| 224 |
|
| 225 |
$(#[$into_boxed_ustr_meta])* |
| 226 |
#[must_use] |
| 227 |
pub fn into_boxed_ustr(self) -> Box<$ustr> { |
| 228 |
let rw = Box::into_raw(self.inner.into_boxed_slice()) as *mut $ustr; |
| 229 |
unsafe { Box::from_raw(rw) } |
| 230 |
} |
| 231 |
|
| 232 |
|
| 233 |
|
| 234 |
|
| 235 |
|
| 236 |
|
| 237 |
#[inline] |
| 238 |
pub fn truncate(&mut self, new_len: usize) { |
| 239 |
self.inner.truncate(new_len) |
| 240 |
} |
| 241 |
|
| 242 |
|
| 243 |
|
| 244 |
|
| 245 |
|
| 246 |
|
| 247 |
|
| 248 |
|
| 249 |
pub fn insert_ustr(&mut self, idx: usize, string: &$ustr) { |
| 250 |
assert!(idx <= self.len()); |
| 251 |
self.inner |
| 252 |
.resize_with(self.len() + string.len(), Default::default); |
| 253 |
self.inner.copy_within(idx.., idx + string.len()); |
| 254 |
self.inner[idx..].copy_from_slice(string.as_slice()); |
| 255 |
} |
| 256 |
|
| 257 |
|
| 258 |
|
| 259 |
|
| 260 |
|
| 261 |
|
| 262 |
|
| 263 |
|
| 264 |
|
| 265 |
|
| 266 |
|
| 267 |
#[inline] |
| 268 |
#[must_use] |
| 269 |
pub fn split_off(&mut self, at: usize) -> $ustring { |
| 270 |
Self::from_vec(self.inner.split_off(at)) |
| 271 |
} |
| 272 |
|
| 273 |
|
| 274 |
|
| 275 |
|
| 276 |
|
| 277 |
|
| 278 |
pub fn retain<F>(&mut self, mut f: F) |
| 279 |
where |
| 280 |
F: FnMut($uchar) -> bool, |
| 281 |
{ |
| 282 |
self.inner.retain(|e| f(*e)) |
| 283 |
} |
| 284 |
|
| 285 |
|
| 286 |
|
| 287 |
|
| 288 |
|
| 289 |
|
| 290 |
|
| 291 |
|
| 292 |
|
| 293 |
|
| 294 |
pub fn drain<R>(&mut self, range: R) -> Drain<'_, $uchar> |
| 295 |
where |
| 296 |
R: RangeBounds<usize>, |
| 297 |
{ |
| 298 |
Drain { inner: self.inner.drain(range) } |
| 299 |
} |
| 300 |
|
| 301 |
|
| 302 |
|
| 303 |
|
| 304 |
|
| 305 |
|
| 306 |
|
| 307 |
|
| 308 |
pub fn replace_range<R>(&mut self, range: R, replace_with: impl AsRef<$ustr>) |
| 309 |
where |
| 310 |
R: RangeBounds<usize>, |
| 311 |
{ |
| 312 |
self.inner |
| 313 |
.splice(range, replace_with.as_ref().as_slice().iter().copied()); |
| 314 |
} |
| 315 |
} |
| 316 |
|
| 317 |
impl Add<&$ustr> for $ustring { |
| 318 |
type Output = $ustring; |
| 319 |
|
| 320 |
#[inline] |
| 321 |
fn add(mut self, rhs: &$ustr) -> Self::Output { |
| 322 |
self.push(rhs); |
| 323 |
self |
| 324 |
} |
| 325 |
} |
| 326 |
|
| 327 |
impl Add<&$ucstr> for $ustring { |
| 328 |
type Output = $ustring; |
| 329 |
|
| 330 |
#[inline] |
| 331 |
fn add(mut self, rhs: &$ucstr) -> Self::Output { |
| 332 |
self.push(rhs); |
| 333 |
self |
| 334 |
} |
| 335 |
} |
| 336 |
|
| 337 |
impl Add<&crate::$utfstr> for $ustring { |
| 338 |
type Output = $ustring; |
| 339 |
|
| 340 |
#[inline] |
| 341 |
fn add(mut self, rhs: &crate::$utfstr) -> Self::Output { |
| 342 |
self.push(rhs); |
| 343 |
self |
| 344 |
} |
| 345 |
} |
| 346 |
|
| 347 |
impl Add<&str> for $ustring { |
| 348 |
type Output = $ustring; |
| 349 |
|
| 350 |
#[inline] |
| 351 |
fn add(mut self, rhs: &str) -> Self::Output { |
| 352 |
self.push_str(rhs); |
| 353 |
self |
| 354 |
} |
| 355 |
} |
| 356 |
|
| 357 |
impl AddAssign<&$ustr> for $ustring { |
| 358 |
#[inline] |
| 359 |
fn add_assign(&mut self, rhs: &$ustr) { |
| 360 |
self.push(rhs) |
| 361 |
} |
| 362 |
} |
| 363 |
|
| 364 |
impl AddAssign<&$ucstr> for $ustring { |
| 365 |
#[inline] |
| 366 |
fn add_assign(&mut self, rhs: &$ucstr) { |
| 367 |
self.push(rhs) |
| 368 |
} |
| 369 |
} |
| 370 |
|
| 371 |
impl AddAssign<&crate::$utfstr> for $ustring { |
| 372 |
#[inline] |
| 373 |
fn add_assign(&mut self, rhs: &crate::$utfstr) { |
| 374 |
self.push(rhs) |
| 375 |
} |
| 376 |
} |
| 377 |
|
| 378 |
impl AddAssign<&str> for $ustring { |
| 379 |
#[inline] |
| 380 |
fn add_assign(&mut self, rhs: &str) { |
| 381 |
self.push_str(rhs); |
| 382 |
} |
| 383 |
} |
| 384 |
|
| 385 |
impl AsMut<$ustr> for $ustring { |
| 386 |
#[inline] |
| 387 |
fn as_mut(&mut self) -> &mut $ustr { |
| 388 |
self.as_mut_ustr() |
| 389 |
} |
| 390 |
} |
| 391 |
|
| 392 |
impl AsMut<[$uchar]> for $ustring { |
| 393 |
#[inline] |
| 394 |
fn as_mut(&mut self) -> &mut [$uchar] { |
| 395 |
self.as_mut_slice() |
| 396 |
} |
| 397 |
} |
| 398 |
|
| 399 |
impl AsRef<$ustr> for $ustring { |
| 400 |
#[inline] |
| 401 |
fn as_ref(&self) -> &$ustr { |
| 402 |
self.as_ustr() |
| 403 |
} |
| 404 |
} |
| 405 |
|
| 406 |
impl AsRef<[$uchar]> for $ustring { |
| 407 |
#[inline] |
| 408 |
fn as_ref(&self) -> &[$uchar] { |
| 409 |
self.as_slice() |
| 410 |
} |
| 411 |
} |
| 412 |
|
| 413 |
impl Borrow<$ustr> for $ustring { |
| 414 |
#[inline] |
| 415 |
fn borrow(&self) -> &$ustr { |
| 416 |
self.as_ustr() |
| 417 |
} |
| 418 |
} |
| 419 |
|
| 420 |
impl BorrowMut<$ustr> for $ustring { |
| 421 |
#[inline] |
| 422 |
fn borrow_mut(&mut self) -> &mut $ustr { |
| 423 |
self.as_mut_ustr() |
| 424 |
} |
| 425 |
} |
| 426 |
|
| 427 |
impl Default for Box<$ustr> { |
| 428 |
#[inline] |
| 429 |
fn default() -> Self { |
| 430 |
let boxed: Box<[$uchar]> = Box::from([]); |
| 431 |
let rw = Box::into_raw(boxed) as *mut $ustr; |
| 432 |
unsafe { Box::from_raw(rw) } |
| 433 |
} |
| 434 |
} |
| 435 |
|
| 436 |
impl Deref for $ustring { |
| 437 |
type Target = $ustr; |
| 438 |
|
| 439 |
#[inline] |
| 440 |
fn deref(&self) -> &$ustr { |
| 441 |
self.as_ustr() |
| 442 |
} |
| 443 |
} |
| 444 |
|
| 445 |
impl DerefMut for $ustring { |
| 446 |
#[inline] |
| 447 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 448 |
self.as_mut_ustr() |
| 449 |
} |
| 450 |
} |
| 451 |
|
| 452 |
impl<'a> Extend<&'a $ustr> for $ustring { |
| 453 |
#[inline] |
| 454 |
fn extend<T: IntoIterator<Item = &'a $ustr>>(&mut self, iter: T) { |
| 455 |
iter.into_iter().for_each(|s| self.push(s)) |
| 456 |
} |
| 457 |
} |
| 458 |
|
| 459 |
impl<'a> Extend<&'a $ucstr> for $ustring { |
| 460 |
#[inline] |
| 461 |
fn extend<T: IntoIterator<Item = &'a $ucstr>>(&mut self, iter: T) { |
| 462 |
iter.into_iter().for_each(|s| self.push(s)) |
| 463 |
} |
| 464 |
} |
| 465 |
|
| 466 |
impl<'a> Extend<&'a crate::$utfstr> for $ustring { |
| 467 |
#[inline] |
| 468 |
fn extend<T: IntoIterator<Item = &'a crate::$utfstr>>(&mut self, iter: T) { |
| 469 |
iter.into_iter().for_each(|s| self.push(s)) |
| 470 |
} |
| 471 |
} |
| 472 |
|
| 473 |
impl<'a> Extend<&'a str> for $ustring { |
| 474 |
#[inline] |
| 475 |
fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) { |
| 476 |
iter.into_iter().for_each(|s| self.push_str(s)) |
| 477 |
} |
| 478 |
} |
| 479 |
|
| 480 |
impl Extend<$ustring> for $ustring { |
| 481 |
#[inline] |
| 482 |
fn extend<T: IntoIterator<Item = $ustring>>(&mut self, iter: T) { |
| 483 |
iter.into_iter().for_each(|s| self.push(s)) |
| 484 |
} |
| 485 |
} |
| 486 |
|
| 487 |
impl Extend<$ucstring> for $ustring { |
| 488 |
#[inline] |
| 489 |
fn extend<T: IntoIterator<Item = $ucstring>>(&mut self, iter: T) { |
| 490 |
iter.into_iter().for_each(|s| self.push(s.as_ucstr())) |
| 491 |
} |
| 492 |
} |
| 493 |
|
| 494 |
impl Extend<crate::$utfstring> for $ustring { |
| 495 |
#[inline] |
| 496 |
fn extend<T: IntoIterator<Item = crate::$utfstring>>(&mut self, iter: T) { |
| 497 |
iter.into_iter().for_each(|s| self.push(s.as_ustr())) |
| 498 |
} |
| 499 |
} |
| 500 |
|
| 501 |
impl Extend<String> for $ustring { |
| 502 |
#[inline] |
| 503 |
fn extend<T: IntoIterator<Item = String>>(&mut self, iter: T) { |
| 504 |
iter.into_iter().for_each(|s| self.push_str(s)) |
| 505 |
} |
| 506 |
} |
| 507 |
|
| 508 |
impl Extend<char> for $ustring { |
| 509 |
#[inline] |
| 510 |
fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T) { |
| 511 |
let iter = iter.into_iter(); |
| 512 |
let (lower_bound, _) = iter.size_hint(); |
| 513 |
self.reserve(lower_bound); |
| 514 |
iter.for_each(|c| self.push_char(c)); |
| 515 |
} |
| 516 |
} |
| 517 |
|
| 518 |
impl<'a> Extend<&'a char> for $ustring { |
| 519 |
#[inline] |
| 520 |
fn extend<T: IntoIterator<Item = &'a char>>(&mut self, iter: T) { |
| 521 |
self.extend(iter.into_iter().copied()) |
| 522 |
} |
| 523 |
} |
| 524 |
|
| 525 |
impl Extend<Box<$ustr>> for $ustring { |
| 526 |
#[inline] |
| 527 |
fn extend<T: IntoIterator<Item = Box<$ustr>>>(&mut self, iter: T) { |
| 528 |
iter.into_iter().for_each(|s| self.push(s)) |
| 529 |
} |
| 530 |
} |
| 531 |
|
| 532 |
impl<'a> Extend<Cow<'a, $ustr>> for $ustring { |
| 533 |
#[inline] |
| 534 |
fn extend<T: IntoIterator<Item = Cow<'a, $ustr>>>(&mut self, iter: T) { |
| 535 |
iter.into_iter().for_each(|s| self.push(s)) |
| 536 |
} |
| 537 |
} |
| 538 |
|
| 539 |
impl From<$ustring> for Vec<$uchar> { |
| 540 |
#[inline] |
| 541 |
fn from(value: $ustring) -> Self { |
| 542 |
value.into_vec() |
| 543 |
} |
| 544 |
} |
| 545 |
|
| 546 |
impl<'a> From<$ustring> for Cow<'a, $ustr> { |
| 547 |
#[inline] |
| 548 |
fn from(s: $ustring) -> Self { |
| 549 |
Cow::Owned(s) |
| 550 |
} |
| 551 |
} |
| 552 |
|
| 553 |
impl From<Vec<$uchar>> for $ustring { |
| 554 |
#[inline] |
| 555 |
fn from(value: Vec<$uchar>) -> Self { |
| 556 |
Self::from_vec(value) |
| 557 |
} |
| 558 |
} |
| 559 |
|
| 560 |
impl From<String> for $ustring { |
| 561 |
#[inline] |
| 562 |
fn from(s: String) -> Self { |
| 563 |
Self::from_str(&s) |
| 564 |
} |
| 565 |
} |
| 566 |
|
| 567 |
impl From<&str> for $ustring { |
| 568 |
#[inline] |
| 569 |
fn from(s: &str) -> Self { |
| 570 |
Self::from_str(s) |
| 571 |
} |
| 572 |
} |
| 573 |
|
| 574 |
#[cfg(feature = "std")] |
| 575 |
impl From<std::ffi::OsString> for $ustring { |
| 576 |
#[inline] |
| 577 |
fn from(s: std::ffi::OsString) -> Self { |
| 578 |
Self::from_os_str(&s) |
| 579 |
} |
| 580 |
} |
| 581 |
|
| 582 |
#[cfg(feature = "std")] |
| 583 |
impl From<$ustring> for std::ffi::OsString { |
| 584 |
#[inline] |
| 585 |
fn from(s: $ustring) -> Self { |
| 586 |
s.to_os_string() |
| 587 |
} |
| 588 |
} |
| 589 |
|
| 590 |
impl<'a, T: ?Sized + AsRef<$ustr>> From<&'a T> for $ustring { |
| 591 |
#[inline] |
| 592 |
fn from(s: &'a T) -> Self { |
| 593 |
s.as_ref().to_ustring() |
| 594 |
} |
| 595 |
} |
| 596 |
|
| 597 |
impl<'a> From<&'a $ustr> for Cow<'a, $ustr> { |
| 598 |
#[inline] |
| 599 |
fn from(s: &'a $ustr) -> Self { |
| 600 |
Cow::Borrowed(s) |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
impl<'a> From<&'a $ustr> for Box<$ustr> { |
| 605 |
fn from(s: &'a $ustr) -> Self { |
| 606 |
let boxed: Box<[$uchar]> = Box::from(&s.inner); |
| 607 |
let rw = Box::into_raw(boxed) as *mut $ustr; |
| 608 |
unsafe { Box::from_raw(rw) } |
| 609 |
} |
| 610 |
} |
| 611 |
|
| 612 |
impl From<Box<$ustr>> for $ustring { |
| 613 |
#[inline] |
| 614 |
fn from(boxed: Box<$ustr>) -> Self { |
| 615 |
boxed.into_ustring() |
| 616 |
} |
| 617 |
} |
| 618 |
|
| 619 |
impl From<$ustring> for Box<$ustr> { |
| 620 |
#[inline] |
| 621 |
fn from(s: $ustring) -> Self { |
| 622 |
s.into_boxed_ustr() |
| 623 |
} |
| 624 |
} |
| 625 |
|
| 626 |
impl<'a> FromIterator<&'a $ustr> for $ustring { |
| 627 |
#[inline] |
| 628 |
fn from_iter<T: IntoIterator<Item = &'a $ustr>>(iter: T) -> Self { |
| 629 |
let mut string = Self::new(); |
| 630 |
string.extend(iter); |
| 631 |
string |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
impl<'a> FromIterator<&'a $ucstr> for $ustring { |
| 636 |
#[inline] |
| 637 |
fn from_iter<T: IntoIterator<Item = &'a $ucstr>>(iter: T) -> Self { |
| 638 |
let mut string = Self::new(); |
| 639 |
string.extend(iter); |
| 640 |
string |
| 641 |
} |
| 642 |
} |
| 643 |
|
| 644 |
impl<'a> FromIterator<&'a crate::$utfstr> for $ustring { |
| 645 |
#[inline] |
| 646 |
fn from_iter<T: IntoIterator<Item = &'a crate::$utfstr>>(iter: T) -> Self { |
| 647 |
let mut string = Self::new(); |
| 648 |
string.extend(iter); |
| 649 |
string |
| 650 |
} |
| 651 |
} |
| 652 |
|
| 653 |
impl<'a> FromIterator<&'a str> for $ustring { |
| 654 |
#[inline] |
| 655 |
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self { |
| 656 |
let mut string = Self::new(); |
| 657 |
string.extend(iter); |
| 658 |
string |
| 659 |
} |
| 660 |
} |
| 661 |
|
| 662 |
impl FromIterator<$ustring> for $ustring { |
| 663 |
#[inline] |
| 664 |
fn from_iter<T: IntoIterator<Item = $ustring>>(iter: T) -> Self { |
| 665 |
let mut string = Self::new(); |
| 666 |
string.extend(iter); |
| 667 |
string |
| 668 |
} |
| 669 |
} |
| 670 |
|
| 671 |
impl FromIterator<$ucstring> for $ustring { |
| 672 |
#[inline] |
| 673 |
fn from_iter<T: IntoIterator<Item = $ucstring>>(iter: T) -> Self { |
| 674 |
let mut string = Self::new(); |
| 675 |
string.extend(iter); |
| 676 |
string |
| 677 |
} |
| 678 |
} |
| 679 |
|
| 680 |
impl FromIterator<crate::$utfstring> for $ustring { |
| 681 |
#[inline] |
| 682 |
fn from_iter<T: IntoIterator<Item = crate::$utfstring>>(iter: T) -> Self { |
| 683 |
let mut string = Self::new(); |
| 684 |
string.extend(iter); |
| 685 |
string |
| 686 |
} |
| 687 |
} |
| 688 |
|
| 689 |
impl FromIterator<String> for $ustring { |
| 690 |
#[inline] |
| 691 |
fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self { |
| 692 |
let mut string = Self::new(); |
| 693 |
string.extend(iter); |
| 694 |
string |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
impl FromIterator<char> for $ustring { |
| 699 |
#[inline] |
| 700 |
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self { |
| 701 |
let mut string = Self::new(); |
| 702 |
string.extend(iter); |
| 703 |
string |
| 704 |
} |
| 705 |
} |
| 706 |
|
| 707 |
impl<'a> FromIterator<&'a char> for $ustring { |
| 708 |
#[inline] |
| 709 |
fn from_iter<T: IntoIterator<Item = &'a char>>(iter: T) -> Self { |
| 710 |
let mut string = Self::new(); |
| 711 |
string.extend(iter); |
| 712 |
string |
| 713 |
} |
| 714 |
} |
| 715 |
|
| 716 |
impl FromIterator<Box<$ustr>> for $ustring { |
| 717 |
#[inline] |
| 718 |
fn from_iter<T: IntoIterator<Item = Box<$ustr>>>(iter: T) -> Self { |
| 719 |
let mut string = Self::new(); |
| 720 |
string.extend(iter); |
| 721 |
string |
| 722 |
} |
| 723 |
} |
| 724 |
|
| 725 |
impl<'a> FromIterator<Cow<'a, $ustr>> for $ustring { |
| 726 |
#[inline] |
| 727 |
fn from_iter<T: IntoIterator<Item = Cow<'a, $ustr>>>(iter: T) -> Self { |
| 728 |
let mut string = Self::new(); |
| 729 |
string.extend(iter); |
| 730 |
string |
| 731 |
} |
| 732 |
} |
| 733 |
|
| 734 |
impl FromStr for $ustring { |
| 735 |
type Err = Infallible; |
| 736 |
|
| 737 |
#[inline] |
| 738 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 739 |
Ok(Self::from_str(s)) |
| 740 |
} |
| 741 |
} |
| 742 |
|
| 743 |
impl<I> Index<I> for $ustring |
| 744 |
where |
| 745 |
I: SliceIndex<[$uchar], Output = [$uchar]>, |
| 746 |
{ |
| 747 |
type Output = $ustr; |
| 748 |
|
| 749 |
#[inline] |
| 750 |
fn index(&self, index: I) -> &$ustr { |
| 751 |
&self.as_ustr()[index] |
| 752 |
} |
| 753 |
} |
| 754 |
|
| 755 |
impl<I> IndexMut<I> for $ustring |
| 756 |
where |
| 757 |
I: SliceIndex<[$uchar], Output = [$uchar]>, |
| 758 |
{ |
| 759 |
fn index_mut(&mut self, index: I) -> &mut Self::Output { |
| 760 |
&mut self.as_mut_ustr()[index] |
| 761 |
} |
| 762 |
} |
| 763 |
|
| 764 |
impl PartialEq<$ustr> for $ustring { |
| 765 |
#[inline] |
| 766 |
fn eq(&self, other: &$ustr) -> bool { |
| 767 |
self.as_ustr() == other |
| 768 |
} |
| 769 |
} |
| 770 |
|
| 771 |
impl PartialEq<$ucstr> for $ustring { |
| 772 |
#[inline] |
| 773 |
fn eq(&self, other: &$ucstr) -> bool { |
| 774 |
self.as_ustr() == other |
| 775 |
} |
| 776 |
} |
| 777 |
|
| 778 |
impl PartialEq<$ucstring> for $ustring { |
| 779 |
#[inline] |
| 780 |
fn eq(&self, other: &$ucstring) -> bool { |
| 781 |
self.as_ustr() == other.as_ucstr() |
| 782 |
} |
| 783 |
} |
| 784 |
|
| 785 |
impl<'a> PartialEq<&'a $ustr> for $ustring { |
| 786 |
#[inline] |
| 787 |
fn eq(&self, other: &&'a $ustr) -> bool { |
| 788 |
self.as_ustr() == *other |
| 789 |
} |
| 790 |
} |
| 791 |
|
| 792 |
impl<'a> PartialEq<&'a $ucstr> for $ustring { |
| 793 |
#[inline] |
| 794 |
fn eq(&self, other: &&'a $ucstr) -> bool { |
| 795 |
self.as_ustr() == *other |
| 796 |
} |
| 797 |
} |
| 798 |
|
| 799 |
impl<'a> PartialEq<Cow<'a, $ustr>> for $ustring { |
| 800 |
#[inline] |
| 801 |
fn eq(&self, other: &Cow<'a, $ustr>) -> bool { |
| 802 |
self.as_ustr() == other.as_ref() |
| 803 |
} |
| 804 |
} |
| 805 |
|
| 806 |
impl<'a> PartialEq<Cow<'a, $ucstr>> for $ustring { |
| 807 |
#[inline] |
| 808 |
fn eq(&self, other: &Cow<'a, $ucstr>) -> bool { |
| 809 |
self.as_ustr() == other.as_ref() |
| 810 |
} |
| 811 |
} |
| 812 |
|
| 813 |
impl PartialEq<$ustring> for $ustr { |
| 814 |
#[inline] |
| 815 |
fn eq(&self, other: &$ustring) -> bool { |
| 816 |
self == other.as_ustr() |
| 817 |
} |
| 818 |
} |
| 819 |
|
| 820 |
impl PartialEq<$ustring> for $ucstr { |
| 821 |
#[inline] |
| 822 |
fn eq(&self, other: &$ustring) -> bool { |
| 823 |
self.as_ustr() == other.as_ustr() |
| 824 |
} |
| 825 |
} |
| 826 |
|
| 827 |
impl PartialEq<$ustring> for &$ustr { |
| 828 |
#[inline] |
| 829 |
fn eq(&self, other: &$ustring) -> bool { |
| 830 |
self == other.as_ustr() |
| 831 |
} |
| 832 |
} |
| 833 |
|
| 834 |
impl PartialEq<$ustring> for &$ucstr { |
| 835 |
#[inline] |
| 836 |
fn eq(&self, other: &$ustring) -> bool { |
| 837 |
self.as_ustr() == other.as_ustr() |
| 838 |
} |
| 839 |
} |
| 840 |
|
| 841 |
impl PartialOrd<$ustr> for $ustring { |
| 842 |
#[inline] |
| 843 |
fn partial_cmp(&self, other: &$ustr) -> Option<cmp::Ordering> { |
| 844 |
self.as_ustr().partial_cmp(other) |
| 845 |
} |
| 846 |
} |
| 847 |
|
| 848 |
impl PartialOrd<$ucstr> for $ustring { |
| 849 |
#[inline] |
| 850 |
fn partial_cmp(&self, other: &$ucstr) -> Option<cmp::Ordering> { |
| 851 |
self.as_ustr().partial_cmp(other) |
| 852 |
} |
| 853 |
} |
| 854 |
|
| 855 |
impl<'a> PartialOrd<&'a $ustr> for $ustring { |
| 856 |
#[inline] |
| 857 |
fn partial_cmp(&self, other: &&'a $ustr) -> Option<cmp::Ordering> { |
| 858 |
self.as_ustr().partial_cmp(*other) |
| 859 |
} |
| 860 |
} |
| 861 |
|
| 862 |
impl<'a> PartialOrd<&'a $ucstr> for $ustring { |
| 863 |
#[inline] |
| 864 |
fn partial_cmp(&self, other: &&'a $ucstr) -> Option<cmp::Ordering> { |
| 865 |
self.as_ustr().partial_cmp(*other) |
| 866 |
} |
| 867 |
} |
| 868 |
|
| 869 |
impl<'a> PartialOrd<Cow<'a, $ustr>> for $ustring { |
| 870 |
#[inline] |
| 871 |
fn partial_cmp(&self, other: &Cow<'a, $ustr>) -> Option<cmp::Ordering> { |
| 872 |
self.as_ustr().partial_cmp(other.as_ref()) |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
impl<'a> PartialOrd<Cow<'a, $ucstr>> for $ustring { |
| 877 |
#[inline] |
| 878 |
fn partial_cmp(&self, other: &Cow<'a, $ucstr>) -> Option<cmp::Ordering> { |
| 879 |
self.as_ustr().partial_cmp(other.as_ref()) |
| 880 |
} |
| 881 |
} |
| 882 |
|
| 883 |
impl PartialOrd<$ucstring> for $ustring { |
| 884 |
#[inline] |
| 885 |
fn partial_cmp(&self, other: &$ucstring) -> Option<cmp::Ordering> { |
| 886 |
self.as_ustr().partial_cmp(other.as_ucstr()) |
| 887 |
} |
| 888 |
} |
| 889 |
|
| 890 |
impl ToOwned for $ustr { |
| 891 |
type Owned = $ustring; |
| 892 |
|
| 893 |
#[inline] |
| 894 |
fn to_owned(&self) -> $ustring { |
| 895 |
self.to_ustring() |
| 896 |
} |
| 897 |
} |
| 898 |
|
| 899 |
impl Write for $ustring { |
| 900 |
#[inline] |
| 901 |
fn write_str(&mut self, s: &str) -> core::fmt::Result { |
| 902 |
self.push_str(s); |
| 903 |
Ok(()) |
| 904 |
} |
| 905 |
|
| 906 |
#[inline] |
| 907 |
fn write_char(&mut self, c: char) -> core::fmt::Result { |
| 908 |
self.push_char(c); |
| 909 |
Ok(()) |
| 910 |
} |
| 911 |
} |
| 912 |
}; |
| 913 |
} |
| 914 |
|
| 915 |
ustring_common_impl! { |
| 916 |
|
| 917 |
|
| 918 |
|
| 919 |
|
| 920 |
|
| 921 |
|
| 922 |
|
| 923 |
|
| 924 |
|
| 925 |
|
| 926 |
|
| 927 |
|
| 928 |
|
| 929 |
|
| 930 |
|
| 931 |
|
| 932 |
|
| 933 |
|
| 934 |
|
| 935 |
|
| 936 |
|
| 937 |
|
| 938 |
|
| 939 |
|
| 940 |
|
| 941 |
|
| 942 |
|
| 943 |
|
| 944 |
|
| 945 |
|
| 946 |
|
| 947 |
|
| 948 |
|
| 949 |
|
| 950 |
|
| 951 |
|
| 952 |
|
| 953 |
|
| 954 |
|
| 955 |
|
| 956 |
|
| 957 |
|
| 958 |
|
| 959 |
; |
| 960 |
|
| 961 |
|
| 962 |
|
| 963 |
|
| 964 |
|
| 965 |
|
| 966 |
|
| 967 |
|
| 968 |
|
| 969 |
|
| 970 |
|
| 971 |
|
| 972 |
|
| 973 |
|
| 974 |
|
| 975 |
|
| 976 |
|
| 977 |
|
| 978 |
|
| 979 |
|
| 980 |
struct U16String([u16]); |
| 981 |
|
| 982 |
type UStr = U16Str; |
| 983 |
type UCString = U16CString; |
| 984 |
type UCStr = U16CStr; |
| 985 |
type UtfStr = Utf16Str; |
| 986 |
type UtfString = Utf16String; |
| 987 |
|
| 988 |
|
| 989 |
|
| 990 |
|
| 991 |
|
| 992 |
|
| 993 |
|
| 994 |
|
| 995 |
|
| 996 |
|
| 997 |
|
| 998 |
|
| 999 |
|
| 1000 |
|
| 1001 |
|
| 1002 |
|
| 1003 |
|
| 1004 |
|
| 1005 |
|
| 1006 |
fn push() -> {} |
| 1007 |
|
| 1008 |
|
| 1009 |
|
| 1010 |
|
| 1011 |
|
| 1012 |
|
| 1013 |
|
| 1014 |
|
| 1015 |
|
| 1016 |
|
| 1017 |
|
| 1018 |
|
| 1019 |
|
| 1020 |
|
| 1021 |
|
| 1022 |
|
| 1023 |
|
| 1024 |
|
| 1025 |
|
| 1026 |
fn push_slice() -> {} |
| 1027 |
|
| 1028 |
|
| 1029 |
|
| 1030 |
|
| 1031 |
|
| 1032 |
|
| 1033 |
|
| 1034 |
|
| 1035 |
|
| 1036 |
|
| 1037 |
|
| 1038 |
|
| 1039 |
fn into_boxed_ustr() -> {} |
| 1040 |
} |
| 1041 |
|
| 1042 |
ustring_common_impl! { |
| 1043 |
|
| 1044 |
|
| 1045 |
|
| 1046 |
|
| 1047 |
|
| 1048 |
|
| 1049 |
|
| 1050 |
|
| 1051 |
|
| 1052 |
|
| 1053 |
|
| 1054 |
|
| 1055 |
|
| 1056 |
|
| 1057 |
|
| 1058 |
|
| 1059 |
|
| 1060 |
|
| 1061 |
|
| 1062 |
|
| 1063 |
|
| 1064 |
|
| 1065 |
|
| 1066 |
|
| 1067 |
|
| 1068 |
|
| 1069 |
|
| 1070 |
|
| 1071 |
|
| 1072 |
|
| 1073 |
|
| 1074 |
|
| 1075 |
|
| 1076 |
|
| 1077 |
|
| 1078 |
|
| 1079 |
|
| 1080 |
|
| 1081 |
|
| 1082 |
|
| 1083 |
|
| 1084 |
|
| 1085 |
|
| 1086 |
; |
| 1087 |
|
| 1088 |
|
| 1089 |
|
| 1090 |
|
| 1091 |
|
| 1092 |
|
| 1093 |
|
| 1094 |
|
| 1095 |
|
| 1096 |
|
| 1097 |
|
| 1098 |
|
| 1099 |
|
| 1100 |
|
| 1101 |
|
| 1102 |
|
| 1103 |
|
| 1104 |
|
| 1105 |
|
| 1106 |
|
| 1107 |
struct U32String([u32]); |
| 1108 |
|
| 1109 |
type UStr = U32Str; |
| 1110 |
type UCString = U32CString; |
| 1111 |
type UCStr = U32CStr; |
| 1112 |
type UtfStr = Utf32Str; |
| 1113 |
type UtfString = Utf32String; |
| 1114 |
|
| 1115 |
|
| 1116 |
|
| 1117 |
|
| 1118 |
|
| 1119 |
|
| 1120 |
|
| 1121 |
|
| 1122 |
|
| 1123 |
|
| 1124 |
|
| 1125 |
|
| 1126 |
|
| 1127 |
|
| 1128 |
|
| 1129 |
|
| 1130 |
|
| 1131 |
|
| 1132 |
|
| 1133 |
fn push() -> {} |
| 1134 |
|
| 1135 |
|
| 1136 |
|
| 1137 |
|
| 1138 |
|
| 1139 |
|
| 1140 |
|
| 1141 |
|
| 1142 |
|
| 1143 |
|
| 1144 |
|
| 1145 |
|
| 1146 |
|
| 1147 |
|
| 1148 |
|
| 1149 |
|
| 1150 |
|
| 1151 |
|
| 1152 |
|
| 1153 |
fn push_slice() -> {} |
| 1154 |
|
| 1155 |
|
| 1156 |
|
| 1157 |
|
| 1158 |
|
| 1159 |
|
| 1160 |
|
| 1161 |
|
| 1162 |
|
| 1163 |
|
| 1164 |
|
| 1165 |
|
| 1166 |
fn into_boxed_ustr() -> {} |
| 1167 |
} |
| 1168 |
|
| 1169 |
impl U16String { |
| 1170 |
|
| 1171 |
|
| 1172 |
|
| 1173 |
|
| 1174 |
|
| 1175 |
|
| 1176 |
|
| 1177 |
|
| 1178 |
|
| 1179 |
|
| 1180 |
|
| 1181 |
|
| 1182 |
|
| 1183 |
|
| 1184 |
|
| 1185 |
#[allow(clippy::should_implement_trait)] |
| 1186 |
#[inline] |
| 1187 |
#[must_use] |
| 1188 |
pub fn from_str<S: AsRef<str> + ?Sized>(s: &S) -> Self { |
| 1189 |
Self { |
| 1190 |
inner: s.as_ref().encode_utf16().collect(), |
| 1191 |
} |
| 1192 |
} |
| 1193 |
|
| 1194 |
|
| 1195 |
|
| 1196 |
|
| 1197 |
|
| 1198 |
|
| 1199 |
|
| 1200 |
|
| 1201 |
|
| 1202 |
|
| 1203 |
|
| 1204 |
|
| 1205 |
|
| 1206 |
|
| 1207 |
|
| 1208 |
|
| 1209 |
|
| 1210 |
|
| 1211 |
|
| 1212 |
|
| 1213 |
|
| 1214 |
#[cfg(feature = "std")] |
| 1215 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 1216 |
#[inline] |
| 1217 |
#[must_use] |
| 1218 |
pub fn from_os_str<S: AsRef<std::ffi::OsStr> + ?Sized>(s: &S) -> Self { |
| 1219 |
Self { |
| 1220 |
inner: crate::platform::os_to_wide(s.as_ref()), |
| 1221 |
} |
| 1222 |
} |
| 1223 |
|
| 1224 |
|
| 1225 |
|
| 1226 |
|
| 1227 |
|
| 1228 |
|
| 1229 |
|
| 1230 |
|
| 1231 |
|
| 1232 |
|
| 1233 |
|
| 1234 |
|
| 1235 |
|
| 1236 |
|
| 1237 |
|
| 1238 |
|
| 1239 |
|
| 1240 |
#[inline] |
| 1241 |
pub fn push_str(&mut self, s: impl AsRef<str>) { |
| 1242 |
self.inner.extend(s.as_ref().encode_utf16()) |
| 1243 |
} |
| 1244 |
|
| 1245 |
|
| 1246 |
|
| 1247 |
|
| 1248 |
|
| 1249 |
|
| 1250 |
|
| 1251 |
|
| 1252 |
|
| 1253 |
|
| 1254 |
|
| 1255 |
|
| 1256 |
|
| 1257 |
|
| 1258 |
|
| 1259 |
|
| 1260 |
|
| 1261 |
#[cfg(feature = "std")] |
| 1262 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 1263 |
#[inline] |
| 1264 |
pub fn push_os_str(&mut self, s: impl AsRef<std::ffi::OsStr>) { |
| 1265 |
self.inner.extend(crate::platform::os_to_wide(s.as_ref())) |
| 1266 |
} |
| 1267 |
|
| 1268 |
|
| 1269 |
#[inline] |
| 1270 |
pub fn push_char(&mut self, c: char) { |
| 1271 |
let mut buf = [0; 2]; |
| 1272 |
self.inner.extend_from_slice(c.encode_utf16(&mut buf)) |
| 1273 |
} |
| 1274 |
|
| 1275 |
|
| 1276 |
|
| 1277 |
|
| 1278 |
|
| 1279 |
|
| 1280 |
|
| 1281 |
|
| 1282 |
pub fn pop_char(&mut self) -> Option<u32> { |
| 1283 |
match self.inner.pop() { |
| 1284 |
Some(low) if crate::is_utf16_surrogate(low) => { |
| 1285 |
if !crate::is_utf16_low_surrogate(low) || self.inner.is_empty() { |
| 1286 |
Some(low as u32) |
| 1287 |
} else { |
| 1288 |
let high = self.inner[self.len() - 1]; |
| 1289 |
if crate::is_utf16_high_surrogate(high) { |
| 1290 |
self.inner.pop(); |
| 1291 |
let buf = [high, low]; |
| 1292 |
Some( |
| 1293 |
char::decode_utf16(buf.iter().copied()) |
| 1294 |
.next() |
| 1295 |
.unwrap() |
| 1296 |
.unwrap() as u32, |
| 1297 |
) |
| 1298 |
} else { |
| 1299 |
Some(low as u32) |
| 1300 |
} |
| 1301 |
} |
| 1302 |
} |
| 1303 |
Some(u) => Some(u as u32), |
| 1304 |
None => None, |
| 1305 |
} |
| 1306 |
} |
| 1307 |
|
| 1308 |
|
| 1309 |
|
| 1310 |
|
| 1311 |
|
| 1312 |
|
| 1313 |
|
| 1314 |
|
| 1315 |
|
| 1316 |
|
| 1317 |
|
| 1318 |
|
| 1319 |
pub fn remove_char(&mut self, idx: usize) -> u32 { |
| 1320 |
let slice = &self.inner[idx..]; |
| 1321 |
let c = char::decode_utf16(slice.iter().copied()).next().unwrap(); |
| 1322 |
let clen = c.as_ref().map(|c| c.len_utf16()).unwrap_or(1); |
| 1323 |
let c = c |
| 1324 |
.map(|c| c as u32) |
| 1325 |
.unwrap_or_else(|_| self.inner[idx] as u32); |
| 1326 |
self.inner.drain(idx..idx + clen); |
| 1327 |
c |
| 1328 |
} |
| 1329 |
|
| 1330 |
|
| 1331 |
|
| 1332 |
|
| 1333 |
|
| 1334 |
|
| 1335 |
|
| 1336 |
|
| 1337 |
pub fn insert_char(&mut self, idx: usize, c: char) { |
| 1338 |
assert!(idx <= self.len()); |
| 1339 |
let mut buf = [0; 2]; |
| 1340 |
let slice = c.encode_utf16(&mut buf); |
| 1341 |
let old_size = self.len(); |
| 1342 |
self.inner.resize(self.len() + slice.len(), 0); |
| 1343 |
self.inner.copy_within(idx..old_size, idx + slice.len()); |
| 1344 |
self.inner[idx..idx + slice.len()].copy_from_slice(slice); |
| 1345 |
} |
| 1346 |
} |
| 1347 |
|
| 1348 |
impl U32String { |
| 1349 |
|
| 1350 |
|
| 1351 |
|
| 1352 |
|
| 1353 |
|
| 1354 |
|
| 1355 |
|
| 1356 |
|
| 1357 |
|
| 1358 |
|
| 1359 |
|
| 1360 |
|
| 1361 |
|
| 1362 |
|
| 1363 |
#[must_use] |
| 1364 |
pub fn from_chars(raw: impl Into<Vec<char>>) -> Self { |
| 1365 |
let mut chars = raw.into(); |
| 1366 |
Self { |
| 1367 |
inner: unsafe { |
| 1368 |
let ptr = chars.as_mut_ptr() as *mut u32; |
| 1369 |
let len = chars.len(); |
| 1370 |
let cap = chars.capacity(); |
| 1371 |
mem::forget(chars); |
| 1372 |
Vec::from_raw_parts(ptr, len, cap) |
| 1373 |
}, |
| 1374 |
} |
| 1375 |
} |
| 1376 |
|
| 1377 |
|
| 1378 |
|
| 1379 |
|
| 1380 |
|
| 1381 |
|
| 1382 |
|
| 1383 |
|
| 1384 |
|
| 1385 |
|
| 1386 |
|
| 1387 |
|
| 1388 |
|
| 1389 |
|
| 1390 |
|
| 1391 |
|
| 1392 |
#[allow(clippy::should_implement_trait)] |
| 1393 |
#[inline] |
| 1394 |
#[must_use] |
| 1395 |
pub fn from_str<S: AsRef<str> + ?Sized>(s: &S) -> Self { |
| 1396 |
let v: Vec<char> = s.as_ref().chars().collect(); |
| 1397 |
Self::from_chars(v) |
| 1398 |
} |
| 1399 |
|
| 1400 |
|
| 1401 |
|
| 1402 |
|
| 1403 |
|
| 1404 |
|
| 1405 |
|
| 1406 |
|
| 1407 |
|
| 1408 |
|
| 1409 |
|
| 1410 |
|
| 1411 |
|
| 1412 |
|
| 1413 |
|
| 1414 |
|
| 1415 |
|
| 1416 |
|
| 1417 |
|
| 1418 |
|
| 1419 |
|
| 1420 |
#[cfg(feature = "std")] |
| 1421 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 1422 |
#[must_use] |
| 1423 |
pub fn from_os_str<S: AsRef<std::ffi::OsStr> + ?Sized>(s: &S) -> Self { |
| 1424 |
let v: Vec<char> = s.as_ref().to_string_lossy().chars().collect(); |
| 1425 |
Self::from_chars(v) |
| 1426 |
} |
| 1427 |
|
| 1428 |
|
| 1429 |
|
| 1430 |
|
| 1431 |
|
| 1432 |
|
| 1433 |
|
| 1434 |
|
| 1435 |
|
| 1436 |
|
| 1437 |
|
| 1438 |
|
| 1439 |
|
| 1440 |
|
| 1441 |
|
| 1442 |
#[inline] |
| 1443 |
#[must_use] |
| 1444 |
pub unsafe fn from_char_ptr(p: *const char, len: usize) -> Self { |
| 1445 |
Self::from_ptr(p as *const u32, len) |
| 1446 |
} |
| 1447 |
|
| 1448 |
|
| 1449 |
|
| 1450 |
|
| 1451 |
|
| 1452 |
|
| 1453 |
|
| 1454 |
|
| 1455 |
|
| 1456 |
|
| 1457 |
|
| 1458 |
|
| 1459 |
|
| 1460 |
|
| 1461 |
|
| 1462 |
|
| 1463 |
|
| 1464 |
#[inline] |
| 1465 |
pub fn push_str(&mut self, s: impl AsRef<str>) { |
| 1466 |
self.inner.extend(s.as_ref().chars().map(|c| c as u32)) |
| 1467 |
} |
| 1468 |
|
| 1469 |
|
| 1470 |
|
| 1471 |
|
| 1472 |
|
| 1473 |
|
| 1474 |
|
| 1475 |
|
| 1476 |
|
| 1477 |
|
| 1478 |
|
| 1479 |
|
| 1480 |
|
| 1481 |
|
| 1482 |
|
| 1483 |
|
| 1484 |
|
| 1485 |
#[cfg(feature = "std")] |
| 1486 |
#[cfg_attr(docsrs, doc(cfg(feature = "std")))] |
| 1487 |
#[inline] |
| 1488 |
pub fn push_os_str(&mut self, s: impl AsRef<std::ffi::OsStr>) { |
| 1489 |
self.inner |
| 1490 |
.extend(s.as_ref().to_string_lossy().chars().map(|c| c as u32)) |
| 1491 |
} |
| 1492 |
|
| 1493 |
|
| 1494 |
#[inline] |
| 1495 |
pub fn push_char(&mut self, c: char) { |
| 1496 |
self.inner.push(c as u32); |
| 1497 |
} |
| 1498 |
|
| 1499 |
|
| 1500 |
|
| 1501 |
|
| 1502 |
|
| 1503 |
|
| 1504 |
#[inline] |
| 1505 |
pub fn pop_char(&mut self) -> Option<u32> { |
| 1506 |
self.inner.pop() |
| 1507 |
} |
| 1508 |
|
| 1509 |
|
| 1510 |
|
| 1511 |
|
| 1512 |
|
| 1513 |
|
| 1514 |
|
| 1515 |
|
| 1516 |
|
| 1517 |
|
| 1518 |
#[inline] |
| 1519 |
pub fn remove_char(&mut self, idx: usize) -> u32 { |
| 1520 |
self.inner.remove(idx) |
| 1521 |
} |
| 1522 |
|
| 1523 |
|
| 1524 |
|
| 1525 |
|
| 1526 |
|
| 1527 |
|
| 1528 |
|
| 1529 |
|
| 1530 |
#[inline] |
| 1531 |
pub fn insert_char(&mut self, idx: usize, c: char) { |
| 1532 |
self.inner.insert(idx, c as u32) |
| 1533 |
} |
| 1534 |
} |
| 1535 |
|
| 1536 |
impl core::fmt::Debug for U16String { |
| 1537 |
#[inline] |
| 1538 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 1539 |
crate::debug_fmt_u16(self.as_slice(), f) |
| 1540 |
} |
| 1541 |
} |
| 1542 |
|
| 1543 |
impl core::fmt::Debug for U32String { |
| 1544 |
#[inline] |
| 1545 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 1546 |
crate::debug_fmt_u32(self.as_slice(), f) |
| 1547 |
} |
| 1548 |
} |
| 1549 |
|
| 1550 |
impl From<Vec<char>> for U32String { |
| 1551 |
#[inline] |
| 1552 |
fn from(value: Vec<char>) -> Self { |
| 1553 |
Self::from_chars(value) |
| 1554 |
} |
| 1555 |
} |
| 1556 |
|
| 1557 |
impl From<&[char]> for U32String { |
| 1558 |
#[inline] |
| 1559 |
fn from(value: &[char]) -> Self { |
| 1560 |
U32String::from_chars(value) |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
|
| 1565 |
|
| 1566 |
#[cfg(not(windows))] |
| 1567 |
pub type WideString = U32String; |
| 1568 |
|
| 1569 |
|
| 1570 |
|
| 1571 |
#[cfg(windows)] |
| 1572 |
pub type WideString = U16String; |
| 1573 |
|
| 1574 |
#[cfg(test)] |
| 1575 |
mod test { |
| 1576 |
use super::*; |
| 1577 |
|
| 1578 |
#[test] |
| 1579 |
#[allow(clippy::write_literal)] |
| 1580 |
fn number_to_string() { |
| 1581 |
let mut s = U16String::new(); |
| 1582 |
write!(s, "{}", 1234).unwrap(); |
| 1583 |
assert_eq!(s, U16String::from_str("1234")); |
| 1584 |
} |
| 1585 |
|
| 1586 |
#[test] |
| 1587 |
fn truncated_with_surrogate() { |
| 1588 |
|
| 1589 |
let buf = "𤭢"; |
| 1590 |
let mut s = U16String::from_str(buf); |
| 1591 |
assert_eq!(s.pop_char(), Some('𤭢' as u32)); |
| 1592 |
} |
| 1593 |
|
| 1594 |
#[test] |
| 1595 |
fn insert_char() { |
| 1596 |
|
| 1597 |
let mut s = U16String::from_str("Hllo, world!"); |
| 1598 |
s.insert_char(1, 'e'); |
| 1599 |
assert_eq!(s.to_string_lossy(), "Hello, world!"); |
| 1600 |
} |
| 1601 |
} |