| 1 |
//! Owned, growable UTF strings. |
| 2 |
//! |
| 3 |
//! This module contains UTF strings and related types. |
| 4 |
|
| 5 |
use crate::{ |
| 6 |
decode_utf16_surrogate_pair, |
| 7 |
error::{Utf16Error, Utf32Error}, |
| 8 |
is_utf16_low_surrogate, is_utf16_surrogate, validate_utf16, validate_utf16_vec, validate_utf32, |
| 9 |
validate_utf32_vec, Utf16Str, Utf32Str, |
| 10 |
}; |
| 11 |
#[allow(unused_imports)] |
| 12 |
use alloc::{ |
| 13 |
borrow::{Cow, ToOwned}, |
| 14 |
boxed::Box, |
| 15 |
string::String, |
| 16 |
vec::Vec, |
| 17 |
}; |
| 18 |
#[allow(unused_imports)] |
| 19 |
use core::{ |
| 20 |
borrow::{Borrow, BorrowMut}, |
| 21 |
convert::{AsMut, AsRef, From, Infallible, TryFrom}, |
| 22 |
fmt::Write, |
| 23 |
iter::FromIterator, |
| 24 |
mem, |
| 25 |
ops::{Add, AddAssign, Deref, DerefMut, Index, IndexMut, RangeBounds}, |
| 26 |
ptr, |
| 27 |
slice::SliceIndex, |
| 28 |
str::FromStr, |
| 29 |
}; |
| 30 |
|
| 31 |
mod iter; |
| 32 |
pub use iter::*; |
| 33 |
|
| 34 |
macro_rules! utfstring_common_impl { |
| 35 |
{ |
| 36 |
$(#[$utfstring_meta:meta])* |
| 37 |
struct $utfstring:ident([$uchar:ty]); |
| 38 |
type UtfStr = $utfstr:ident; |
| 39 |
type UStr = $ustr:ident; |
| 40 |
type UCStr = $ucstr:ident; |
| 41 |
type UString = $ustring:ident; |
| 42 |
type UCString = $ucstring:ident; |
| 43 |
type UtfError = $utferror:ident; |
| 44 |
$(#[$from_vec_unchecked_meta:meta])* |
| 45 |
fn from_vec_unchecked() -> {} |
| 46 |
$(#[$from_str_meta:meta])* |
| 47 |
fn from_str() -> {} |
| 48 |
$(#[$push_utfstr_meta:meta])* |
| 49 |
fn push_utfstr() -> {} |
| 50 |
$(#[$as_mut_vec_meta:meta])* |
| 51 |
fn as_mut_vec() -> {} |
| 52 |
} => { |
| 53 |
$(#[$utfstring_meta])* |
| 54 |
#[derive(Clone, Default, PartialEq, Eq, PartialOrd, Ord, Hash)] |
| 55 |
#[cfg_attr(docsrs, doc(cfg(feature = "alloc")))] |
| 56 |
pub struct $utfstring { |
| 57 |
inner: Vec<$uchar>, |
| 58 |
} |
| 59 |
|
| 60 |
impl $utfstring { |
| 61 |
/// Creates a new empty string. |
| 62 |
/// |
| 63 |
/// Given that the string is empty, this will not allocate any initial buffer. While |
| 64 |
/// that means this initial operation is very inexpensive, it may cause excessive |
| 65 |
/// allocations later when you add data. If you have an idea of how much data the |
| 66 |
/// string will hold, consider [`with_capacity`][Self::with_capacity] instead to |
| 67 |
/// prevent excessive re-allocation. |
| 68 |
#[inline] |
| 69 |
#[must_use] |
| 70 |
pub const fn new() -> Self { |
| 71 |
Self { inner: Vec::new() } |
| 72 |
} |
| 73 |
|
| 74 |
/// Creates a new empty string with a particular capacity. |
| 75 |
/// |
| 76 |
/// This string has an internal buffer to hold its data. The capacity is the length of |
| 77 |
/// that buffer, and can be queried with the [`capacity`][Self::capacity] method. This |
| 78 |
/// method creates and empty string, but one with an initial buffer that can hold |
| 79 |
/// `capacity` elements. This is useful when you may be appending a bunch of data to |
| 80 |
/// the string, reducing the number of reallocations it needs to do. |
| 81 |
/// |
| 82 |
/// If the given capacity is `0`, no allocation will occur, and this method is identical |
| 83 |
/// to the [`new`][Self::new] method. |
| 84 |
#[inline] |
| 85 |
#[must_use] |
| 86 |
pub fn with_capacity(capacity: usize) -> Self { |
| 87 |
Self { |
| 88 |
inner: Vec::with_capacity(capacity), |
| 89 |
} |
| 90 |
} |
| 91 |
|
| 92 |
$(#[$from_vec_unchecked_meta])* |
| 93 |
#[inline] |
| 94 |
#[must_use] |
| 95 |
pub unsafe fn from_vec_unchecked(v: impl Into<Vec<$uchar>>) -> Self { |
| 96 |
Self { inner: v.into() } |
| 97 |
} |
| 98 |
|
| 99 |
$(#[$from_str_meta])* |
| 100 |
#[inline] |
| 101 |
#[allow(clippy::should_implement_trait)] |
| 102 |
#[must_use] |
| 103 |
pub fn from_str<S: AsRef<str> + ?Sized>(s: &S) -> Self { |
| 104 |
let s = s.as_ref(); |
| 105 |
let mut string = Self::new(); |
| 106 |
string.extend(s.chars()); |
| 107 |
string |
| 108 |
} |
| 109 |
|
| 110 |
/// Converts a string into a string slice. |
| 111 |
#[inline] |
| 112 |
#[must_use] |
| 113 |
pub fn as_utfstr(&self) -> &$utfstr { |
| 114 |
unsafe { $utfstr::from_slice_unchecked(self.inner.as_slice()) } |
| 115 |
} |
| 116 |
|
| 117 |
/// Converts a string into a mutable string slice. |
| 118 |
#[inline] |
| 119 |
#[must_use] |
| 120 |
pub fn as_mut_utfstr(&mut self) -> &mut $utfstr { |
| 121 |
unsafe { $utfstr::from_slice_unchecked_mut(&mut self.inner) } |
| 122 |
} |
| 123 |
|
| 124 |
/// Converts this string into a wide string of undefined encoding. |
| 125 |
#[inline] |
| 126 |
#[must_use] |
| 127 |
pub fn as_ustr(&self) -> &crate::$ustr { |
| 128 |
crate::$ustr::from_slice(self.as_slice()) |
| 129 |
} |
| 130 |
|
| 131 |
/// Converts a string into a vector of its elements. |
| 132 |
/// |
| 133 |
/// This consumes the string without copying its contents. |
| 134 |
#[inline] |
| 135 |
#[must_use] |
| 136 |
pub fn into_vec(self) -> Vec<$uchar> { |
| 137 |
self.inner |
| 138 |
} |
| 139 |
|
| 140 |
$(#[$push_utfstr_meta])* |
| 141 |
#[inline] |
| 142 |
pub fn push_utfstr<S: AsRef<$utfstr> + ?Sized>(&mut self, string: &S) { |
| 143 |
self.inner.extend_from_slice(string.as_ref().as_slice()) |
| 144 |
} |
| 145 |
|
| 146 |
/// Returns this string's capacity, in number of elements. |
| 147 |
#[inline] |
| 148 |
#[must_use] |
| 149 |
pub fn capacity(&self) -> usize { |
| 150 |
self.inner.capacity() |
| 151 |
} |
| 152 |
|
| 153 |
/// Ensures that this string's capacity is at least `additional` elements larger than |
| 154 |
/// its length. |
| 155 |
/// |
| 156 |
/// The capacity may be increased by more than `additional` elements if it chooses, to |
| 157 |
/// prevent frequent reallocations. |
| 158 |
/// |
| 159 |
/// If you do not want this "at least" behavior, see the |
| 160 |
/// [`reserve_exact`][Self::reserve_exact] method. |
| 161 |
/// |
| 162 |
/// # Panics |
| 163 |
/// |
| 164 |
/// Panics if the new capacity overflows [`usize`]. |
| 165 |
#[inline] |
| 166 |
pub fn reserve(&mut self, additional: usize) { |
| 167 |
self.inner.reserve(additional) |
| 168 |
} |
| 169 |
|
| 170 |
/// Ensures that this string's capacity is `additional` elements larger than its length. |
| 171 |
/// |
| 172 |
/// Consider using the [`reserve`][Self::reserve] method unless you absolutely know |
| 173 |
/// better than the allocator. |
| 174 |
/// |
| 175 |
/// # Panics |
| 176 |
/// |
| 177 |
/// Panics if the new capacity overflows [`usize`]. |
| 178 |
#[inline] |
| 179 |
pub fn reserve_exact(&mut self, additional: usize) { |
| 180 |
self.inner.reserve_exact(additional) |
| 181 |
} |
| 182 |
|
| 183 |
/// Shrinks the capacity of this string to match its length. |
| 184 |
#[inline] |
| 185 |
pub fn shrink_to_fit(&mut self) { |
| 186 |
self.inner.shrink_to_fit() |
| 187 |
} |
| 188 |
|
| 189 |
/// Shrinks the capacity of this string with a lower bound. |
| 190 |
/// |
| 191 |
/// The capacity will remain at least as large as both the length and the supplied |
| 192 |
/// value. |
| 193 |
/// |
| 194 |
/// If the current capacity is less than the lower limit, this is a no-op. |
| 195 |
#[inline] |
| 196 |
pub fn shrink_to(&mut self, min_capacity: usize) { |
| 197 |
self.inner.shrink_to(min_capacity) |
| 198 |
} |
| 199 |
|
| 200 |
/// Returns a slice of this string's contents. |
| 201 |
#[inline] |
| 202 |
#[must_use] |
| 203 |
pub fn as_slice(&self) -> &[$uchar] { |
| 204 |
self.inner.as_slice() |
| 205 |
} |
| 206 |
|
| 207 |
unsafe fn insert_slice(&mut self, idx: usize, slice: &[$uchar]) { |
| 208 |
let len = self.inner.len(); |
| 209 |
let amt = slice.len(); |
| 210 |
self.inner.reserve(amt); |
| 211 |
|
| 212 |
ptr::copy( |
| 213 |
self.inner.as_ptr().add(idx), |
| 214 |
self.inner.as_mut_ptr().add(idx + amt), |
| 215 |
len - idx, |
| 216 |
); |
| 217 |
ptr::copy_nonoverlapping(slice.as_ptr(), self.inner.as_mut_ptr().add(idx), amt); |
| 218 |
self.inner.set_len(len + amt); |
| 219 |
} |
| 220 |
|
| 221 |
$(#[$as_mut_vec_meta])* |
| 222 |
#[inline] |
| 223 |
#[must_use] |
| 224 |
pub unsafe fn as_mut_vec(&mut self) -> &mut Vec<$uchar> { |
| 225 |
&mut self.inner |
| 226 |
} |
| 227 |
|
| 228 |
/// Returns the length of this string in number of elements, not [`char`]s or |
| 229 |
/// graphemes. |
| 230 |
/// |
| 231 |
/// In other words, it might not be what a human considers the length of the string. |
| 232 |
#[inline] |
| 233 |
#[must_use] |
| 234 |
pub fn len(&self) -> usize { |
| 235 |
self.inner.len() |
| 236 |
} |
| 237 |
|
| 238 |
/// Returns `true` if this string has a length of zero, and `false` otherwise. |
| 239 |
#[inline] |
| 240 |
#[must_use] |
| 241 |
pub fn is_empty(&self) -> bool { |
| 242 |
self.inner.is_empty() |
| 243 |
} |
| 244 |
|
| 245 |
/// Truncates the string, removing all contents. |
| 246 |
/// |
| 247 |
/// While this means the string will have a length of zero, it does not touch its |
| 248 |
/// capacity. |
| 249 |
#[inline] |
| 250 |
pub fn clear(&mut self) { |
| 251 |
self.inner.clear() |
| 252 |
} |
| 253 |
|
| 254 |
/// Converts this string into a boxed string slice. |
| 255 |
/// |
| 256 |
/// This will drop excess capacity. |
| 257 |
#[inline] |
| 258 |
#[must_use] |
| 259 |
pub fn into_boxed_utfstr(self) -> Box<$utfstr> { |
| 260 |
let slice = self.inner.into_boxed_slice(); |
| 261 |
// SAFETY: Already valid UTF-16 |
| 262 |
unsafe { $utfstr::from_boxed_slice_unchecked(slice) } |
| 263 |
} |
| 264 |
|
| 265 |
/// Appends a given UTF-8 string slice onto the end of this string, converting it to |
| 266 |
/// UTF-16. |
| 267 |
#[inline] |
| 268 |
pub fn push_str<S: AsRef<str> + ?Sized>(&mut self, string: &S) { |
| 269 |
self.extend(string.as_ref().chars()) |
| 270 |
} |
| 271 |
} |
| 272 |
|
| 273 |
impl Add<&$utfstr> for $utfstring { |
| 274 |
type Output = $utfstring; |
| 275 |
|
| 276 |
#[inline] |
| 277 |
fn add(mut self, rhs: &$utfstr) -> Self::Output { |
| 278 |
self.push_utfstr(rhs); |
| 279 |
self |
| 280 |
} |
| 281 |
} |
| 282 |
|
| 283 |
impl Add<&str> for $utfstring { |
| 284 |
type Output = $utfstring; |
| 285 |
|
| 286 |
#[inline] |
| 287 |
fn add(mut self, rhs: &str) -> Self::Output { |
| 288 |
self.push_str(rhs); |
| 289 |
self |
| 290 |
} |
| 291 |
} |
| 292 |
|
| 293 |
impl AddAssign<&$utfstr> for $utfstring { |
| 294 |
#[inline] |
| 295 |
fn add_assign(&mut self, rhs: &$utfstr) { |
| 296 |
self.push_utfstr(rhs) |
| 297 |
} |
| 298 |
} |
| 299 |
|
| 300 |
impl AddAssign<&str> for $utfstring { |
| 301 |
#[inline] |
| 302 |
fn add_assign(&mut self, rhs: &str) { |
| 303 |
self.push_str(rhs) |
| 304 |
} |
| 305 |
} |
| 306 |
|
| 307 |
impl AsMut<$utfstr> for $utfstring { |
| 308 |
#[inline] |
| 309 |
fn as_mut(&mut self) -> &mut $utfstr { |
| 310 |
self.as_mut_utfstr() |
| 311 |
} |
| 312 |
} |
| 313 |
|
| 314 |
impl AsRef<$utfstr> for $utfstring { |
| 315 |
#[inline] |
| 316 |
fn as_ref(&self) -> &$utfstr { |
| 317 |
self.as_utfstr() |
| 318 |
} |
| 319 |
} |
| 320 |
|
| 321 |
impl AsRef<[$uchar]> for $utfstring { |
| 322 |
#[inline] |
| 323 |
fn as_ref(&self) -> &[$uchar] { |
| 324 |
&self.inner |
| 325 |
} |
| 326 |
} |
| 327 |
|
| 328 |
impl AsRef<crate::$ustr> for $utfstring { |
| 329 |
#[inline] |
| 330 |
fn as_ref(&self) -> &crate::$ustr { |
| 331 |
self.as_ustr() |
| 332 |
} |
| 333 |
} |
| 334 |
|
| 335 |
impl Borrow<$utfstr> for $utfstring { |
| 336 |
#[inline] |
| 337 |
fn borrow(&self) -> &$utfstr { |
| 338 |
self.as_utfstr() |
| 339 |
} |
| 340 |
} |
| 341 |
|
| 342 |
impl BorrowMut<$utfstr> for $utfstring { |
| 343 |
#[inline] |
| 344 |
fn borrow_mut(&mut self) -> &mut $utfstr { |
| 345 |
self.as_mut_utfstr() |
| 346 |
} |
| 347 |
} |
| 348 |
|
| 349 |
impl core::fmt::Debug for $utfstring { |
| 350 |
#[inline] |
| 351 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 352 |
core::fmt::Debug::fmt(self.as_utfstr(), f) |
| 353 |
} |
| 354 |
} |
| 355 |
|
| 356 |
impl Deref for $utfstring { |
| 357 |
type Target = $utfstr; |
| 358 |
|
| 359 |
#[inline] |
| 360 |
fn deref(&self) -> &Self::Target { |
| 361 |
self.as_utfstr() |
| 362 |
} |
| 363 |
} |
| 364 |
|
| 365 |
impl DerefMut for $utfstring { |
| 366 |
#[inline] |
| 367 |
fn deref_mut(&mut self) -> &mut Self::Target { |
| 368 |
self.as_mut_utfstr() |
| 369 |
} |
| 370 |
} |
| 371 |
|
| 372 |
impl core::fmt::Display for $utfstring { |
| 373 |
#[inline] |
| 374 |
fn fmt(&self, f: &mut core::fmt::Formatter<'_>) -> core::fmt::Result { |
| 375 |
core::fmt::Display::fmt(self.as_utfstr(), f) |
| 376 |
} |
| 377 |
} |
| 378 |
|
| 379 |
impl Extend<char> for $utfstring { |
| 380 |
#[inline] |
| 381 |
fn extend<T: IntoIterator<Item = char>>(&mut self, iter: T) { |
| 382 |
let iter = iter.into_iter(); |
| 383 |
let (lower_bound, _) = iter.size_hint(); |
| 384 |
self.reserve(lower_bound); |
| 385 |
iter.for_each(|c| self.push(c)); |
| 386 |
} |
| 387 |
} |
| 388 |
|
| 389 |
impl<'a> Extend<&'a char> for $utfstring { |
| 390 |
#[inline] |
| 391 |
fn extend<T: IntoIterator<Item = &'a char>>(&mut self, iter: T) { |
| 392 |
self.extend(iter.into_iter().copied()) |
| 393 |
} |
| 394 |
} |
| 395 |
|
| 396 |
impl<'a> Extend<&'a $utfstr> for $utfstring { |
| 397 |
#[inline] |
| 398 |
fn extend<T: IntoIterator<Item = &'a $utfstr>>(&mut self, iter: T) { |
| 399 |
iter.into_iter().for_each(|s| self.push_utfstr(s)) |
| 400 |
} |
| 401 |
} |
| 402 |
|
| 403 |
impl Extend<$utfstring> for $utfstring { |
| 404 |
#[inline] |
| 405 |
fn extend<T: IntoIterator<Item = $utfstring>>(&mut self, iter: T) { |
| 406 |
iter.into_iter() |
| 407 |
.for_each(|s| self.push_utfstr(&s)) |
| 408 |
} |
| 409 |
} |
| 410 |
|
| 411 |
impl<'a> Extend<Cow<'a, $utfstr>> for $utfstring { |
| 412 |
#[inline] |
| 413 |
fn extend<T: IntoIterator<Item = Cow<'a, $utfstr>>>(&mut self, iter: T) { |
| 414 |
iter.into_iter().for_each(|s| self.push_utfstr(&s)) |
| 415 |
} |
| 416 |
} |
| 417 |
|
| 418 |
impl Extend<Box<$utfstr>> for $utfstring { |
| 419 |
#[inline] |
| 420 |
fn extend<T: IntoIterator<Item = Box<$utfstr>>>(&mut self, iter: T) { |
| 421 |
iter.into_iter().for_each(|s| self.push_utfstr(&s)) |
| 422 |
} |
| 423 |
} |
| 424 |
|
| 425 |
impl<'a> Extend<&'a str> for $utfstring { |
| 426 |
#[inline] |
| 427 |
fn extend<T: IntoIterator<Item = &'a str>>(&mut self, iter: T) { |
| 428 |
iter.into_iter().for_each(|s| self.push_str(s)) |
| 429 |
} |
| 430 |
} |
| 431 |
|
| 432 |
impl Extend<String> for $utfstring { |
| 433 |
#[inline] |
| 434 |
fn extend<T: IntoIterator<Item = String>>(&mut self, iter: T) { |
| 435 |
iter.into_iter().for_each(|s| self.push_str(&s)) |
| 436 |
} |
| 437 |
} |
| 438 |
|
| 439 |
impl From<&mut $utfstr> for $utfstring { |
| 440 |
#[inline] |
| 441 |
fn from(value: &mut $utfstr) -> Self { |
| 442 |
value.to_owned() |
| 443 |
} |
| 444 |
} |
| 445 |
|
| 446 |
impl From<&$utfstr> for $utfstring { |
| 447 |
#[inline] |
| 448 |
fn from(value: &$utfstr) -> Self { |
| 449 |
value.to_owned() |
| 450 |
} |
| 451 |
} |
| 452 |
|
| 453 |
impl From<&$utfstring> for $utfstring { |
| 454 |
#[inline] |
| 455 |
fn from(value: &$utfstring) -> Self { |
| 456 |
value.clone() |
| 457 |
} |
| 458 |
} |
| 459 |
|
| 460 |
impl From<$utfstring> for Cow<'_, $utfstr> { |
| 461 |
#[inline] |
| 462 |
fn from(value: $utfstring) -> Self { |
| 463 |
Cow::Owned(value) |
| 464 |
} |
| 465 |
} |
| 466 |
|
| 467 |
impl<'a> From<&'a $utfstring> for Cow<'a, $utfstr> { |
| 468 |
#[inline] |
| 469 |
fn from(value: &'a $utfstring) -> Self { |
| 470 |
Cow::Borrowed(value) |
| 471 |
} |
| 472 |
} |
| 473 |
|
| 474 |
impl From<Cow<'_, $utfstr>> for $utfstring { |
| 475 |
#[inline] |
| 476 |
fn from(value: Cow<'_, $utfstr>) -> Self { |
| 477 |
value.into_owned() |
| 478 |
} |
| 479 |
} |
| 480 |
|
| 481 |
impl From<&str> for $utfstring { |
| 482 |
#[inline] |
| 483 |
fn from(value: &str) -> Self { |
| 484 |
Self::from_str(value) |
| 485 |
} |
| 486 |
} |
| 487 |
|
| 488 |
impl From<String> for $utfstring { |
| 489 |
#[inline] |
| 490 |
fn from(value: String) -> Self { |
| 491 |
Self::from_str(&value) |
| 492 |
} |
| 493 |
} |
| 494 |
|
| 495 |
impl From<$utfstring> for crate::$ustring { |
| 496 |
#[inline] |
| 497 |
fn from(value: $utfstring) -> Self { |
| 498 |
crate::$ustring::from_vec(value.into_vec()) |
| 499 |
} |
| 500 |
} |
| 501 |
|
| 502 |
impl From<&$utfstr> for String { |
| 503 |
#[inline] |
| 504 |
fn from(value: &$utfstr) -> Self { |
| 505 |
value.to_string() |
| 506 |
} |
| 507 |
} |
| 508 |
|
| 509 |
impl From<$utfstring> for String { |
| 510 |
#[inline] |
| 511 |
fn from(value: $utfstring) -> Self { |
| 512 |
value.to_string() |
| 513 |
} |
| 514 |
} |
| 515 |
|
| 516 |
#[cfg(feature = "std")] |
| 517 |
impl From<$utfstring> for std::ffi::OsString { |
| 518 |
#[inline] |
| 519 |
fn from(value: $utfstring) -> std::ffi::OsString { |
| 520 |
value.as_ustr().to_os_string() |
| 521 |
} |
| 522 |
} |
| 523 |
|
| 524 |
impl FromIterator<char> for $utfstring { |
| 525 |
#[inline] |
| 526 |
fn from_iter<T: IntoIterator<Item = char>>(iter: T) -> Self { |
| 527 |
let mut s = Self::new(); |
| 528 |
s.extend(iter); |
| 529 |
s |
| 530 |
} |
| 531 |
} |
| 532 |
|
| 533 |
impl<'a> FromIterator<&'a char> for $utfstring { |
| 534 |
#[inline] |
| 535 |
fn from_iter<T: IntoIterator<Item = &'a char>>(iter: T) -> Self { |
| 536 |
let mut s = Self::new(); |
| 537 |
s.extend(iter); |
| 538 |
s |
| 539 |
} |
| 540 |
} |
| 541 |
|
| 542 |
impl<'a> FromIterator<&'a $utfstr> for $utfstring { |
| 543 |
#[inline] |
| 544 |
fn from_iter<T: IntoIterator<Item = &'a $utfstr>>(iter: T) -> Self { |
| 545 |
let mut s = Self::new(); |
| 546 |
s.extend(iter); |
| 547 |
s |
| 548 |
} |
| 549 |
} |
| 550 |
|
| 551 |
impl FromIterator<$utfstring> for $utfstring { |
| 552 |
fn from_iter<T: IntoIterator<Item = $utfstring>>(iter: T) -> Self { |
| 553 |
let mut iterator = iter.into_iter(); |
| 554 |
|
| 555 |
// Because we're iterating over `String`s, we can avoid at least |
| 556 |
// one allocation by getting the first string from the iterator |
| 557 |
// and appending to it all the subsequent strings. |
| 558 |
match iterator.next() { |
| 559 |
None => Self::new(), |
| 560 |
Some(mut buf) => { |
| 561 |
buf.extend(iterator); |
| 562 |
buf |
| 563 |
} |
| 564 |
} |
| 565 |
} |
| 566 |
} |
| 567 |
|
| 568 |
impl FromIterator<Box<$utfstr>> for $utfstring { |
| 569 |
#[inline] |
| 570 |
fn from_iter<T: IntoIterator<Item = Box<$utfstr>>>(iter: T) -> Self { |
| 571 |
let mut s = Self::new(); |
| 572 |
s.extend(iter); |
| 573 |
s |
| 574 |
} |
| 575 |
} |
| 576 |
|
| 577 |
impl<'a> FromIterator<Cow<'a, $utfstr>> for $utfstring { |
| 578 |
#[inline] |
| 579 |
fn from_iter<T: IntoIterator<Item = Cow<'a, $utfstr>>>(iter: T) -> Self { |
| 580 |
let mut s = Self::new(); |
| 581 |
s.extend(iter); |
| 582 |
s |
| 583 |
} |
| 584 |
} |
| 585 |
|
| 586 |
impl<'a> FromIterator<&'a str> for $utfstring { |
| 587 |
#[inline] |
| 588 |
fn from_iter<T: IntoIterator<Item = &'a str>>(iter: T) -> Self { |
| 589 |
let mut s = Self::new(); |
| 590 |
s.extend(iter); |
| 591 |
s |
| 592 |
} |
| 593 |
} |
| 594 |
|
| 595 |
impl FromIterator<String> for $utfstring { |
| 596 |
#[inline] |
| 597 |
fn from_iter<T: IntoIterator<Item = String>>(iter: T) -> Self { |
| 598 |
let mut s = Self::new(); |
| 599 |
s.extend(iter); |
| 600 |
s |
| 601 |
} |
| 602 |
} |
| 603 |
|
| 604 |
impl FromStr for $utfstring { |
| 605 |
type Err = Infallible; |
| 606 |
|
| 607 |
#[inline] |
| 608 |
fn from_str(s: &str) -> Result<Self, Self::Err> { |
| 609 |
Ok($utfstring::from_str(s)) |
| 610 |
} |
| 611 |
} |
| 612 |
|
| 613 |
impl<I> Index<I> for $utfstring |
| 614 |
where |
| 615 |
I: RangeBounds<usize> + SliceIndex<[$uchar], Output = [$uchar]>, |
| 616 |
{ |
| 617 |
type Output = $utfstr; |
| 618 |
|
| 619 |
#[inline] |
| 620 |
fn index(&self, index: I) -> &Self::Output { |
| 621 |
&self.deref()[index] |
| 622 |
} |
| 623 |
} |
| 624 |
|
| 625 |
impl<I> IndexMut<I> for $utfstring |
| 626 |
where |
| 627 |
I: RangeBounds<usize> + SliceIndex<[$uchar], Output = [$uchar]>, |
| 628 |
{ |
| 629 |
#[inline] |
| 630 |
fn index_mut(&mut self, index: I) -> &mut Self::Output { |
| 631 |
&mut self.deref_mut()[index] |
| 632 |
} |
| 633 |
} |
| 634 |
|
| 635 |
impl PartialEq<$utfstr> for $utfstring { |
| 636 |
#[inline] |
| 637 |
fn eq(&self, other: &$utfstr) -> bool { |
| 638 |
self.as_slice() == other.as_slice() |
| 639 |
} |
| 640 |
} |
| 641 |
|
| 642 |
impl PartialEq<&$utfstr> for $utfstring { |
| 643 |
#[inline] |
| 644 |
fn eq(&self, other: &&$utfstr) -> bool { |
| 645 |
self.as_slice() == other.as_slice() |
| 646 |
} |
| 647 |
} |
| 648 |
|
| 649 |
impl PartialEq<Cow<'_, $utfstr>> for $utfstring { |
| 650 |
#[inline] |
| 651 |
fn eq(&self, other: &Cow<'_, $utfstr>) -> bool { |
| 652 |
self == other.as_ref() |
| 653 |
} |
| 654 |
} |
| 655 |
|
| 656 |
impl PartialEq<$utfstring> for Cow<'_, $utfstr> { |
| 657 |
#[inline] |
| 658 |
fn eq(&self, other: &$utfstring) -> bool { |
| 659 |
self.as_ref() == other |
| 660 |
} |
| 661 |
} |
| 662 |
|
| 663 |
impl PartialEq<$utfstring> for $utfstr { |
| 664 |
#[inline] |
| 665 |
fn eq(&self, other: &$utfstring) -> bool { |
| 666 |
self.as_slice() == other.as_slice() |
| 667 |
} |
| 668 |
} |
| 669 |
|
| 670 |
impl PartialEq<$utfstring> for &$utfstr { |
| 671 |
#[inline] |
| 672 |
fn eq(&self, other: &$utfstring) -> bool { |
| 673 |
self.as_slice() == other.as_slice() |
| 674 |
} |
| 675 |
} |
| 676 |
|
| 677 |
impl PartialEq<str> for $utfstring { |
| 678 |
#[inline] |
| 679 |
fn eq(&self, other: &str) -> bool { |
| 680 |
self.chars().eq(other.chars()) |
| 681 |
} |
| 682 |
} |
| 683 |
|
| 684 |
impl PartialEq<&str> for $utfstring { |
| 685 |
#[inline] |
| 686 |
fn eq(&self, other: &&str) -> bool { |
| 687 |
self.chars().eq(other.chars()) |
| 688 |
} |
| 689 |
} |
| 690 |
|
| 691 |
impl PartialEq<$utfstring> for str { |
| 692 |
#[inline] |
| 693 |
fn eq(&self, other: &$utfstring) -> bool { |
| 694 |
self.chars().eq(other.chars()) |
| 695 |
} |
| 696 |
} |
| 697 |
|
| 698 |
impl PartialEq<$utfstring> for &str { |
| 699 |
#[inline] |
| 700 |
fn eq(&self, other: &$utfstring) -> bool { |
| 701 |
self.chars().eq(other.chars()) |
| 702 |
} |
| 703 |
} |
| 704 |
|
| 705 |
impl PartialEq<String> for $utfstring { |
| 706 |
#[inline] |
| 707 |
fn eq(&self, other: &String) -> bool { |
| 708 |
self.chars().eq(other.chars()) |
| 709 |
} |
| 710 |
} |
| 711 |
|
| 712 |
impl PartialEq<$utfstring> for String { |
| 713 |
#[inline] |
| 714 |
fn eq(&self, other: &$utfstring) -> bool { |
| 715 |
self.chars().eq(other.chars()) |
| 716 |
} |
| 717 |
} |
| 718 |
|
| 719 |
impl PartialEq<String> for $utfstr { |
| 720 |
#[inline] |
| 721 |
fn eq(&self, other: &String) -> bool { |
| 722 |
self.chars().eq(other.chars()) |
| 723 |
} |
| 724 |
} |
| 725 |
|
| 726 |
impl PartialEq<$utfstr> for String { |
| 727 |
#[inline] |
| 728 |
fn eq(&self, other: &$utfstr) -> bool { |
| 729 |
self.chars().eq(other.chars()) |
| 730 |
} |
| 731 |
} |
| 732 |
|
| 733 |
impl PartialEq<Cow<'_, str>> for $utfstring { |
| 734 |
#[inline] |
| 735 |
fn eq(&self, other: &Cow<'_, str>) -> bool { |
| 736 |
self == other.as_ref() |
| 737 |
} |
| 738 |
} |
| 739 |
|
| 740 |
impl PartialEq<$utfstring> for Cow<'_, str> { |
| 741 |
#[inline] |
| 742 |
fn eq(&self, other: &$utfstring) -> bool { |
| 743 |
self.as_ref() == other |
| 744 |
} |
| 745 |
} |
| 746 |
|
| 747 |
impl PartialEq<crate::$ustr> for $utfstring { |
| 748 |
#[inline] |
| 749 |
fn eq(&self, other: &crate::$ustr) -> bool { |
| 750 |
self.as_slice() == other.as_slice() |
| 751 |
} |
| 752 |
} |
| 753 |
|
| 754 |
impl PartialEq<$utfstring> for crate::$ustr { |
| 755 |
#[inline] |
| 756 |
fn eq(&self, other: &$utfstring) -> bool { |
| 757 |
self.as_slice() == other.as_slice() |
| 758 |
} |
| 759 |
} |
| 760 |
|
| 761 |
impl PartialEq<crate::$ustring> for $utfstring { |
| 762 |
#[inline] |
| 763 |
fn eq(&self, other: &crate::$ustring) -> bool { |
| 764 |
self.as_slice() == other.as_slice() |
| 765 |
} |
| 766 |
} |
| 767 |
|
| 768 |
impl PartialEq<$utfstring> for crate::$ustring { |
| 769 |
#[inline] |
| 770 |
fn eq(&self, other: &$utfstring) -> bool { |
| 771 |
self.as_slice() == other.as_slice() |
| 772 |
} |
| 773 |
} |
| 774 |
|
| 775 |
impl PartialEq<crate::$ustring> for $utfstr { |
| 776 |
#[inline] |
| 777 |
fn eq(&self, other: &crate::$ustring) -> bool { |
| 778 |
self.as_slice() == other.as_slice() |
| 779 |
} |
| 780 |
} |
| 781 |
|
| 782 |
impl PartialEq<$utfstr> for crate::$ustring { |
| 783 |
#[inline] |
| 784 |
fn eq(&self, other: &$utfstr) -> bool { |
| 785 |
self.as_slice() == other.as_slice() |
| 786 |
} |
| 787 |
} |
| 788 |
|
| 789 |
impl PartialEq<crate::$ucstr> for $utfstring { |
| 790 |
#[inline] |
| 791 |
fn eq(&self, other: &crate::$ucstr) -> bool { |
| 792 |
self.as_slice() == other.as_slice() |
| 793 |
} |
| 794 |
} |
| 795 |
|
| 796 |
impl PartialEq<$utfstring> for crate::$ucstr { |
| 797 |
#[inline] |
| 798 |
fn eq(&self, other: &$utfstring) -> bool { |
| 799 |
self.as_slice() == other.as_slice() |
| 800 |
} |
| 801 |
} |
| 802 |
|
| 803 |
impl PartialEq<crate::$ucstring> for $utfstring { |
| 804 |
#[inline] |
| 805 |
fn eq(&self, other: &crate::$ucstring) -> bool { |
| 806 |
self.as_slice() == other.as_slice() |
| 807 |
} |
| 808 |
} |
| 809 |
|
| 810 |
impl PartialEq<$utfstring> for crate::$ucstring { |
| 811 |
#[inline] |
| 812 |
fn eq(&self, other: &$utfstring) -> bool { |
| 813 |
self.as_slice() == other.as_slice() |
| 814 |
} |
| 815 |
} |
| 816 |
|
| 817 |
impl PartialEq<crate::$ucstring> for $utfstr { |
| 818 |
#[inline] |
| 819 |
fn eq(&self, other: &crate::$ucstring) -> bool { |
| 820 |
self.as_slice() == other.as_slice() |
| 821 |
} |
| 822 |
} |
| 823 |
|
| 824 |
impl PartialEq<$utfstr> for crate::$ucstring { |
| 825 |
#[inline] |
| 826 |
fn eq(&self, other: &$utfstr) -> bool { |
| 827 |
self.as_slice() == other.as_slice() |
| 828 |
} |
| 829 |
} |
| 830 |
|
| 831 |
impl ToOwned for $utfstr { |
| 832 |
type Owned = $utfstring; |
| 833 |
|
| 834 |
#[inline] |
| 835 |
fn to_owned(&self) -> Self::Owned { |
| 836 |
unsafe { $utfstring::from_vec_unchecked(&self.inner) } |
| 837 |
} |
| 838 |
} |
| 839 |
|
| 840 |
impl TryFrom<crate::$ustring> for $utfstring { |
| 841 |
type Error = $utferror; |
| 842 |
|
| 843 |
#[inline] |
| 844 |
fn try_from(value: crate::$ustring) -> Result<Self, Self::Error> { |
| 845 |
$utfstring::from_ustring(value) |
| 846 |
} |
| 847 |
} |
| 848 |
|
| 849 |
impl TryFrom<crate::$ucstring> for $utfstring { |
| 850 |
type Error = $utferror; |
| 851 |
|
| 852 |
#[inline] |
| 853 |
fn try_from(value: crate::$ucstring) -> Result<Self, Self::Error> { |
| 854 |
$utfstring::from_ustring(value) |
| 855 |
} |
| 856 |
} |
| 857 |
|
| 858 |
impl TryFrom<&crate::$ustr> for $utfstring { |
| 859 |
type Error = $utferror; |
| 860 |
|
| 861 |
#[inline] |
| 862 |
fn try_from(value: &crate::$ustr) -> Result<Self, Self::Error> { |
| 863 |
$utfstring::from_ustring(value) |
| 864 |
} |
| 865 |
} |
| 866 |
|
| 867 |
impl TryFrom<&crate::$ucstr> for $utfstring { |
| 868 |
type Error = $utferror; |
| 869 |
|
| 870 |
#[inline] |
| 871 |
fn try_from(value: &crate::$ucstr) -> Result<Self, Self::Error> { |
| 872 |
$utfstring::from_ustring(value) |
| 873 |
} |
| 874 |
} |
| 875 |
|
| 876 |
impl Write for $utfstring { |
| 877 |
#[inline] |
| 878 |
fn write_str(&mut self, s: &str) -> core::fmt::Result { |
| 879 |
self.push_str(s); |
| 880 |
Ok(()) |
| 881 |
} |
| 882 |
|
| 883 |
#[inline] |
| 884 |
fn write_char(&mut self, c: char) -> core::fmt::Result { |
| 885 |
self.push(c); |
| 886 |
Ok(()) |
| 887 |
} |
| 888 |
} |
| 889 |
}; |
| 890 |
} |
| 891 |
|
| 892 |
utfstring_common_impl! { |
| 893 |
/// A UTF-16 encoded, growable owned string. |
| 894 |
/// |
| 895 |
/// [`Utf16String`] is a version of [`String`] that uses UTF-16 encoding instead of UTF-8 |
| 896 |
/// encoding. The equivalent of [`str`] for [`Utf16String`] is [`Utf16Str`]. |
| 897 |
/// |
| 898 |
/// Unlike [`U16String`][crate::U16String] which does not specify a coding, [`Utf16String`] is |
| 899 |
/// always valid UTF-16 encoding. Using unsafe methods to construct a [`Utf16String`] with |
| 900 |
/// invalid UTF-16 encoding results in undefined behavior. |
| 901 |
/// |
| 902 |
/// # UTF-16 |
| 903 |
/// |
| 904 |
/// [`Utf16String`] is always UTF-16. This means if you need non-UTF-16 wide strings, you should |
| 905 |
/// use [`U16String`][crate::U16String] instead. It is similar, but does not constrain the |
| 906 |
/// encoding. |
| 907 |
/// |
| 908 |
/// This also means you cannot directly index a single element of the string, as UTF-16 encoding |
| 909 |
/// may be a single `u16` value or a pair of `u16` surrogates. Instead, you can index subslices |
| 910 |
/// of the string, or use the [`chars`][Utf16Str::chars] iterator instead. |
| 911 |
/// |
| 912 |
/// # Examples |
| 913 |
/// |
| 914 |
/// The easiest way to use [`Utf16String`] is with the [`utf16str!`][crate::utf16str] macro to |
| 915 |
/// convert string literals into UTF-16 string slices at compile time: |
| 916 |
/// |
| 917 |
/// ``` |
| 918 |
/// use widestring::{Utf16String, utf16str}; |
| 919 |
/// let hello = Utf16String::from(utf16str!("Hello, world!")); |
| 920 |
/// ``` |
| 921 |
/// |
| 922 |
/// Because this string is always valid UTF-16, it is a non-fallible, lossless conversion to and |
| 923 |
/// from standard Rust strings: |
| 924 |
/// |
| 925 |
/// ``` |
| 926 |
/// use widestring::Utf16String; |
| 927 |
/// // Unlike the utf16str macro, this will do conversion at runtime instead of compile time |
| 928 |
/// let hello = Utf16String::from_str("Hello, world!"); |
| 929 |
/// let hello_string: String = hello.to_string(); |
| 930 |
/// assert_eq!(hello, hello_string); // Can easily compare between string types |
| 931 |
/// ``` |
| 932 |
struct Utf16String([u16]); |
| 933 |
|
| 934 |
type UtfStr = Utf16Str; |
| 935 |
type UStr = U16Str; |
| 936 |
type UCStr = U16CStr; |
| 937 |
type UString = U16String; |
| 938 |
type UCString = U16CString; |
| 939 |
type UtfError = Utf16Error; |
| 940 |
|
| 941 |
/// Converts a [`u16`] vector to a string without checking that the string contains valid |
| 942 |
/// UTF-16. |
| 943 |
/// |
| 944 |
/// See the safe version, [`from_vec`][Self::from_vec], for more information. |
| 945 |
/// |
| 946 |
/// # Safety |
| 947 |
/// |
| 948 |
/// This function is unsafe because it does not check that the vector passed to it is valid |
| 949 |
/// UTF-16. If this constraint is violated, undefined behavior results as it is assumed the |
| 950 |
/// [`Utf16String`] is always valid UTF-16. |
| 951 |
/// |
| 952 |
/// # Examples |
| 953 |
/// |
| 954 |
/// ``` |
| 955 |
/// use widestring::Utf16String; |
| 956 |
/// |
| 957 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair |
| 958 |
/// let sparkle_heart = unsafe { Utf16String::from_vec_unchecked(sparkle_heart) }; |
| 959 |
/// |
| 960 |
/// assert_eq!("💖", sparkle_heart); |
| 961 |
/// ``` |
| 962 |
fn from_vec_unchecked() -> {} |
| 963 |
|
| 964 |
/// Re-encodes a UTF-8--encoded string slice into a UTF-16--encoded string. |
| 965 |
/// |
| 966 |
/// This operation is lossless and infallible, but requires a memory allocation. |
| 967 |
/// |
| 968 |
/// # Examples |
| 969 |
/// |
| 970 |
/// ``` |
| 971 |
/// # use widestring::utf16str; |
| 972 |
/// use widestring::Utf16String; |
| 973 |
/// let music = Utf16String::from_str("𝄞music"); |
| 974 |
/// assert_eq!(utf16str!("𝄞music"), music); |
| 975 |
/// ``` |
| 976 |
fn from_str() -> {} |
| 977 |
|
| 978 |
/// Appends a given string slice onto the end of this string. |
| 979 |
/// |
| 980 |
/// # Examples |
| 981 |
/// |
| 982 |
/// ``` |
| 983 |
/// # use widestring::utf16str; |
| 984 |
/// use widestring::Utf16String; |
| 985 |
/// let mut s = Utf16String::from_str("foo"); |
| 986 |
/// s.push_utfstr(utf16str!("bar")); |
| 987 |
/// assert_eq!(utf16str!("foobar"), s); |
| 988 |
/// ``` |
| 989 |
fn push_utfstr() -> {} |
| 990 |
|
| 991 |
/// Returns a mutable reference to the contents of this string. |
| 992 |
/// |
| 993 |
/// # Safety |
| 994 |
/// |
| 995 |
/// This function is unsafe because it does not check that the values in the vector are valid |
| 996 |
/// UTF-16. If this constraint is violated, it may cause undefined beahvior with future |
| 997 |
/// users of the string, as it is assumed that this string is always valid UTF-16. |
| 998 |
fn as_mut_vec() -> {} |
| 999 |
} |
| 1000 |
|
| 1001 |
utfstring_common_impl! { |
| 1002 |
/// A UTF-32 encoded, growable owned string. |
| 1003 |
/// |
| 1004 |
/// [`Utf32String`] is a version of [`String`] that uses UTF-32 encoding instead of UTF-8 |
| 1005 |
/// encoding. The equivalent of [`str`] for [`Utf32String`] is [`Utf32Str`]. |
| 1006 |
/// |
| 1007 |
/// Unlike [`U32String`][crate::U32String] which does not specify a coding, [`Utf32String`] is |
| 1008 |
/// always valid UTF-32 encoding. Using unsafe methods to construct a [`Utf32String`] with |
| 1009 |
/// invalid UTF-32 encoding results in undefined behavior. |
| 1010 |
/// |
| 1011 |
/// # UTF-32 |
| 1012 |
/// |
| 1013 |
/// [`Utf32String`] is always UTF-32. This means if you need non-UTF-32 wide strings, you should |
| 1014 |
/// use [`U32String`][crate::U32String] instead. It is similar, but does not constrain the |
| 1015 |
/// encoding. |
| 1016 |
/// |
| 1017 |
/// Unlike UTF-16 or UTF-8 strings, you may index single elements of UTF-32 strings in addition |
| 1018 |
/// to subslicing. This is due to it being a fixed-length encoding for [`char`]s. This also |
| 1019 |
/// means that [`Utf32String`] is the same representation as a `Vec<char>`; indeed conversions |
| 1020 |
/// between the two exist and are simple typecasts. |
| 1021 |
/// |
| 1022 |
/// # Examples |
| 1023 |
/// |
| 1024 |
/// The easiest way to use [`Utf32String`] is with the [`utf32str!`][crate::utf32str] macro to |
| 1025 |
/// convert string literals into UTF-32 string slices at compile time: |
| 1026 |
/// |
| 1027 |
/// ``` |
| 1028 |
/// use widestring::{Utf32String, utf32str}; |
| 1029 |
/// let hello = Utf32String::from(utf32str!("Hello, world!")); |
| 1030 |
/// ``` |
| 1031 |
/// |
| 1032 |
/// Because this string is always valid UTF-32, it is a non-fallible, lossless conversion to and |
| 1033 |
/// from standard Rust strings: |
| 1034 |
/// |
| 1035 |
/// ``` |
| 1036 |
/// use widestring::Utf32String; |
| 1037 |
/// // Unlike the utf32str macro, this will do conversion at runtime instead of compile time |
| 1038 |
/// let hello = Utf32String::from_str("Hello, world!"); |
| 1039 |
/// let hello_string: String = hello.to_string(); |
| 1040 |
/// assert_eq!(hello, hello_string); // Can easily compare between string types |
| 1041 |
/// ``` |
| 1042 |
struct Utf32String([u32]); |
| 1043 |
|
| 1044 |
type UtfStr = Utf32Str; |
| 1045 |
type UStr = U32Str; |
| 1046 |
type UCStr = U32CStr; |
| 1047 |
type UString = U32String; |
| 1048 |
type UCString = U32CString; |
| 1049 |
type UtfError = Utf32Error; |
| 1050 |
|
| 1051 |
/// Converts a [`u32`] vector to a string without checking that the string contains valid |
| 1052 |
/// UTF-32. |
| 1053 |
/// |
| 1054 |
/// See the safe version, [`from_vec`][Self::from_vec], for more information. |
| 1055 |
/// |
| 1056 |
/// # Safety |
| 1057 |
/// |
| 1058 |
/// This function is unsafe because it does not check that the vector passed to it is valid |
| 1059 |
/// UTF-32. If this constraint is violated, undefined behavior results as it is assumed the |
| 1060 |
/// [`Utf32String`] is always valid UTF-32. |
| 1061 |
/// |
| 1062 |
/// # Examples |
| 1063 |
/// |
| 1064 |
/// ``` |
| 1065 |
/// use widestring::Utf32String; |
| 1066 |
/// |
| 1067 |
/// let sparkle_heart = vec![0x1f496]; |
| 1068 |
/// let sparkle_heart = unsafe { Utf32String::from_vec_unchecked(sparkle_heart) }; |
| 1069 |
/// |
| 1070 |
/// assert_eq!("💖", sparkle_heart); |
| 1071 |
/// ``` |
| 1072 |
fn from_vec_unchecked() -> {} |
| 1073 |
|
| 1074 |
/// Re-encodes a UTF-8--encoded string slice into a UTF-32--encoded string. |
| 1075 |
/// |
| 1076 |
/// This operation is lossless and infallible, but requires a memory allocation. |
| 1077 |
/// |
| 1078 |
/// # Examples |
| 1079 |
/// |
| 1080 |
/// ``` |
| 1081 |
/// # use widestring::utf32str; |
| 1082 |
/// use widestring::Utf32String; |
| 1083 |
/// let music = Utf32String::from_str("𝄞music"); |
| 1084 |
/// assert_eq!(utf32str!("𝄞music"), music); |
| 1085 |
/// ``` |
| 1086 |
fn from_str() -> {} |
| 1087 |
|
| 1088 |
/// Appends a given string slice onto the end of this string. |
| 1089 |
/// |
| 1090 |
/// # Examples |
| 1091 |
/// |
| 1092 |
/// ``` |
| 1093 |
/// # use widestring::utf32str; |
| 1094 |
/// use widestring::Utf32String; |
| 1095 |
/// let mut s = Utf32String::from_str("foo"); |
| 1096 |
/// s.push_utfstr(utf32str!("bar")); |
| 1097 |
/// assert_eq!(utf32str!("foobar"), s); |
| 1098 |
/// ``` |
| 1099 |
fn push_utfstr() -> {} |
| 1100 |
|
| 1101 |
/// Returns a mutable reference to the contents of this string. |
| 1102 |
/// |
| 1103 |
/// # Safety |
| 1104 |
/// |
| 1105 |
/// This function is unsafe because it does not check that the values in the vector are valid |
| 1106 |
/// UTF-16. If this constraint is violated, it may cause undefined beahvior with future |
| 1107 |
/// users of the string, as it is assumed that this string is always valid UTF-16. |
| 1108 |
fn as_mut_vec() -> {} |
| 1109 |
} |
| 1110 |
|
| 1111 |
impl Utf16String { |
| 1112 |
/// Converts a [`u16`] vector of UTF-16 data to a string. |
| 1113 |
/// |
| 1114 |
/// Not all slices of [`u16`] values are valid to convert, since [`Utf16String`] requires that |
| 1115 |
/// it is always valid UTF-16. This function checks to ensure that the values are valid UTF-16, |
| 1116 |
/// and then does the conversion. This does not do any copying. |
| 1117 |
/// |
| 1118 |
/// If you are sure that the slice is valid UTF-16, and you don't want to incur the overhead of |
| 1119 |
/// the validity check, there is an unsafe version of this function, |
| 1120 |
/// [`from_vec_unchecked`][Self::from_vec_unchecked], which has the same behavior but skips |
| 1121 |
/// the check. |
| 1122 |
/// |
| 1123 |
/// If you need a string slice, consider using [`Utf16Str::from_slice`] instead. |
| 1124 |
/// |
| 1125 |
/// The inverse of this method is [`into_vec`][Self::into_vec]. |
| 1126 |
/// |
| 1127 |
/// # Errors |
| 1128 |
/// |
| 1129 |
/// Returns an error if the vector is not UTF-16 with a description as to why the provided |
| 1130 |
/// vector is not UTF-16. The error will contain the original [`Vec`] that can be reclaimed with |
| 1131 |
/// [`into_vec`][Utf16Error::into_vec]. |
| 1132 |
/// |
| 1133 |
/// # Examples |
| 1134 |
/// |
| 1135 |
/// ``` |
| 1136 |
/// use widestring::Utf16String; |
| 1137 |
/// |
| 1138 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair |
| 1139 |
/// let sparkle_heart = Utf16String::from_vec(sparkle_heart).unwrap(); |
| 1140 |
/// |
| 1141 |
/// assert_eq!("💖", sparkle_heart); |
| 1142 |
/// ``` |
| 1143 |
/// |
| 1144 |
/// With incorrect values that return an error: |
| 1145 |
/// |
| 1146 |
/// ``` |
| 1147 |
/// use widestring::Utf16String; |
| 1148 |
/// |
| 1149 |
/// let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate |
| 1150 |
/// |
| 1151 |
/// assert!(Utf16String::from_vec(sparkle_heart).is_err()); |
| 1152 |
/// ``` |
| 1153 |
pub fn from_vec(v: impl Into<Vec<u16>>) -> Result<Self, Utf16Error> { |
| 1154 |
let v = validate_utf16_vec(v.into())?; |
| 1155 |
Ok(unsafe { Self::from_vec_unchecked(v) }) |
| 1156 |
} |
| 1157 |
|
| 1158 |
/// Converts a slice of [`u16`] data to a string, including invalid characters. |
| 1159 |
/// |
| 1160 |
/// Since the given [`u16`] slice may not be valid UTF-16, and [`Utf16String`] requires that |
| 1161 |
/// it is always valid UTF-16, during the conversion this function replaces any invalid UTF-16 |
| 1162 |
/// sequences with [`U+FFFD REPLACEMENT CHARACTER`][core::char::REPLACEMENT_CHARACTER], which |
| 1163 |
/// looks like this: � |
| 1164 |
/// |
| 1165 |
/// If you are sure that the slice is valid UTF-16, and you don't want to incur the overhead of |
| 1166 |
/// the conversion, there is an unsafe version of this function, |
| 1167 |
/// [`from_vec_unchecked`][Self::from_vec_unchecked], which has the same behavior but skips |
| 1168 |
/// the checks. |
| 1169 |
/// |
| 1170 |
/// This function returns a [`Cow<'_, Utf16Str>`][std::borrow::Cow]. If the given slice is |
| 1171 |
/// invalid UTF-16, then we need to insert our replacement characters which will change the size |
| 1172 |
/// of the string, and hence, require an owned [`Utf16String`]. But if it's already valid |
| 1173 |
/// UTF-16, we don't need a new allocation. This return type allows us to handle both cases. |
| 1174 |
/// |
| 1175 |
/// # Examples |
| 1176 |
/// |
| 1177 |
/// ``` |
| 1178 |
/// # use widestring::utf16str; |
| 1179 |
/// use widestring::Utf16String; |
| 1180 |
/// |
| 1181 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair |
| 1182 |
/// let sparkle_heart = Utf16String::from_slice_lossy(&sparkle_heart); |
| 1183 |
/// |
| 1184 |
/// assert_eq!(utf16str!("💖"), sparkle_heart); |
| 1185 |
/// ``` |
| 1186 |
/// |
| 1187 |
/// With incorrect values that return an error: |
| 1188 |
/// |
| 1189 |
/// ``` |
| 1190 |
/// # use widestring::utf16str; |
| 1191 |
/// use widestring::Utf16String; |
| 1192 |
/// |
| 1193 |
/// let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate |
| 1194 |
/// let sparkle_heart = Utf16String::from_slice_lossy(&sparkle_heart); |
| 1195 |
/// |
| 1196 |
/// assert_eq!(utf16str!("\u{fffd}\u{0000}"), sparkle_heart); |
| 1197 |
/// ``` |
| 1198 |
#[must_use] |
| 1199 |
pub fn from_slice_lossy(s: &[u16]) -> Cow<'_, Utf16Str> { |
| 1200 |
match validate_utf16(s) { |
| 1201 |
// SAFETY: validated as UTF-16 |
| 1202 |
Ok(()) => Cow::Borrowed(unsafe { Utf16Str::from_slice_unchecked(s) }), |
| 1203 |
Err(e) => { |
| 1204 |
let mut v = Vec::with_capacity(s.len()); |
| 1205 |
// Valid up until index |
| 1206 |
v.extend_from_slice(&s[..e.index()]); |
| 1207 |
let mut index = e.index(); |
| 1208 |
let mut replacement_char = [0; 2]; |
| 1209 |
let replacement_char = |
| 1210 |
char::REPLACEMENT_CHARACTER.encode_utf16(&mut replacement_char); |
| 1211 |
while index < s.len() { |
| 1212 |
let u = s[index]; |
| 1213 |
if is_utf16_surrogate(u) { |
| 1214 |
if is_utf16_low_surrogate(u) || index + 1 >= s.len() { |
| 1215 |
v.extend_from_slice(replacement_char); |
| 1216 |
} else { |
| 1217 |
let low = s[index + 1]; |
| 1218 |
if is_utf16_low_surrogate(low) { |
| 1219 |
// Valid surrogate pair |
| 1220 |
v.push(u); |
| 1221 |
v.push(low); |
| 1222 |
index += 1; |
| 1223 |
} else { |
| 1224 |
v.extend_from_slice(replacement_char); |
| 1225 |
} |
| 1226 |
} |
| 1227 |
} else { |
| 1228 |
v.push(u); |
| 1229 |
} |
| 1230 |
index += 1; |
| 1231 |
} |
| 1232 |
// SATEFY: Is now valid UTF-16 with replacement chars |
| 1233 |
Cow::Owned(unsafe { Self::from_vec_unchecked(v) }) |
| 1234 |
} |
| 1235 |
} |
| 1236 |
} |
| 1237 |
|
| 1238 |
/// Converts a wide string of undefined encoding to a UTF-16 string without checking that the |
| 1239 |
/// string contains valid UTF-16. |
| 1240 |
/// |
| 1241 |
/// See the safe version, [`from_ustring`][Self::from_ustring], for more information. |
| 1242 |
/// |
| 1243 |
/// # Safety |
| 1244 |
/// |
| 1245 |
/// This function is unsafe because it does not check that the string passed to it is valid |
| 1246 |
/// UTF-16. If this constraint is violated, undefined behavior results as it is assumed the |
| 1247 |
/// [`Utf16String`] is always valid UTF-16. |
| 1248 |
/// |
| 1249 |
/// # Examples |
| 1250 |
/// |
| 1251 |
/// ``` |
| 1252 |
/// use widestring::{U16String, Utf16String}; |
| 1253 |
/// |
| 1254 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair |
| 1255 |
/// let sparkle_heart = U16String::from_vec(sparkle_heart); |
| 1256 |
/// let sparkle_heart = unsafe { Utf16String::from_ustring_unchecked(sparkle_heart) }; |
| 1257 |
/// |
| 1258 |
/// assert_eq!("💖", sparkle_heart); |
| 1259 |
/// ``` |
| 1260 |
#[inline] |
| 1261 |
#[must_use] |
| 1262 |
pub unsafe fn from_ustring_unchecked(s: impl Into<crate::U16String>) -> Self { |
| 1263 |
Self::from_vec_unchecked(s.into().into_vec()) |
| 1264 |
} |
| 1265 |
|
| 1266 |
/// Converts a wide string of undefined encoding into a UTF-16 string. |
| 1267 |
/// |
| 1268 |
/// Not all strings with undefined encoding are valid to convert, since [`Utf16String`] requires |
| 1269 |
/// that it is always valid UTF-16. This function checks to ensure that the string is valid |
| 1270 |
/// UTF-16, and then does the conversion. This does not do any copying. |
| 1271 |
/// |
| 1272 |
/// If you are sure that the string is valid UTF-16, and you don't want to incur the overhead of |
| 1273 |
/// the validity check, there is an unsafe version of this function, |
| 1274 |
/// [`from_ustring_unchecked`][Self::from_ustring_unchecked], which has the same behavior but |
| 1275 |
/// skips the check. |
| 1276 |
/// |
| 1277 |
/// If you need a string slice, consider using [`Utf16Str::from_ustr`] instead. |
| 1278 |
/// |
| 1279 |
/// # Errors |
| 1280 |
/// |
| 1281 |
/// Returns an error if the string is not UTF-16 with a description as to why the provided |
| 1282 |
/// string is not UTF-16. |
| 1283 |
/// |
| 1284 |
/// # Examples |
| 1285 |
/// |
| 1286 |
/// ``` |
| 1287 |
/// use widestring::{U16String, Utf16String}; |
| 1288 |
/// |
| 1289 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair |
| 1290 |
/// let sparkle_heart = U16String::from_vec(sparkle_heart); |
| 1291 |
/// let sparkle_heart = Utf16String::from_ustring(sparkle_heart).unwrap(); |
| 1292 |
/// |
| 1293 |
/// assert_eq!("💖", sparkle_heart); |
| 1294 |
/// ``` |
| 1295 |
/// |
| 1296 |
/// With incorrect values that return an error: |
| 1297 |
/// |
| 1298 |
/// ``` |
| 1299 |
/// use widestring::{U16String, Utf16String}; |
| 1300 |
/// |
| 1301 |
/// let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate |
| 1302 |
/// let sparkle_heart = U16String::from_vec(sparkle_heart); // Valid for a U16String |
| 1303 |
/// |
| 1304 |
/// assert!(Utf16String::from_ustring(sparkle_heart).is_err()); // But not for a Utf16String |
| 1305 |
/// ``` |
| 1306 |
#[inline] |
| 1307 |
pub fn from_ustring(s: impl Into<crate::U16String>) -> Result<Self, Utf16Error> { |
| 1308 |
Self::from_vec(s.into().into_vec()) |
| 1309 |
} |
| 1310 |
|
| 1311 |
/// Converts a wide string slice of undefined encoding of to a UTF-16 string, including invalid |
| 1312 |
/// characters. |
| 1313 |
/// |
| 1314 |
/// Since the given string slice may not be valid UTF-16, and [`Utf16String`] requires that |
| 1315 |
/// it is always valid UTF-16, during the conversion this function replaces any invalid UTF-16 |
| 1316 |
/// sequences with [`U+FFFD REPLACEMENT CHARACTER`][core::char::REPLACEMENT_CHARACTER], which |
| 1317 |
/// looks like this: � |
| 1318 |
/// |
| 1319 |
/// If you are sure that the slice is valid UTF-16, and you don't want to incur the overhead of |
| 1320 |
/// the conversion, there is an unsafe version of this function, |
| 1321 |
/// [`from_ustring_unchecked`][Self::from_ustring_unchecked], which has the same behavior but |
| 1322 |
/// skips the checks. |
| 1323 |
/// |
| 1324 |
/// This function returns a [`Cow<'_, Utf16Str>`][std::borrow::Cow]. If the given slice is |
| 1325 |
/// invalid UTF-16, then we need to insert our replacement characters which will change the size |
| 1326 |
/// of the string, and hence, require an owned [`Utf16String`]. But if it's already valid |
| 1327 |
/// UTF-16, we don't need a new allocation. This return type allows us to handle both cases. |
| 1328 |
/// |
| 1329 |
/// # Examples |
| 1330 |
/// |
| 1331 |
/// ``` |
| 1332 |
/// # use widestring::utf16str; |
| 1333 |
/// use widestring::{U16Str, Utf16String}; |
| 1334 |
/// |
| 1335 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair |
| 1336 |
/// let sparkle_heart = U16Str::from_slice(&sparkle_heart); |
| 1337 |
/// let sparkle_heart = Utf16String::from_ustr_lossy(sparkle_heart); |
| 1338 |
/// |
| 1339 |
/// assert_eq!(utf16str!("💖"), sparkle_heart); |
| 1340 |
/// ``` |
| 1341 |
/// |
| 1342 |
/// With incorrect values that return an error: |
| 1343 |
/// |
| 1344 |
/// ``` |
| 1345 |
/// # use widestring::utf16str; |
| 1346 |
/// use widestring::{U16Str, Utf16String}; |
| 1347 |
/// |
| 1348 |
/// let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate |
| 1349 |
/// let sparkle_heart = U16Str::from_slice(&sparkle_heart); |
| 1350 |
/// let sparkle_heart = Utf16String::from_ustr_lossy(sparkle_heart); |
| 1351 |
/// |
| 1352 |
/// assert_eq!(utf16str!("\u{fffd}\u{0000}"), sparkle_heart); |
| 1353 |
/// ``` |
| 1354 |
#[inline] |
| 1355 |
#[must_use] |
| 1356 |
pub fn from_ustr_lossy(s: &crate::U16Str) -> Cow<'_, Utf16Str> { |
| 1357 |
Self::from_slice_lossy(s.as_slice()) |
| 1358 |
} |
| 1359 |
|
| 1360 |
/// Converts a wide C string to a UTF-16 string without checking that the string contains |
| 1361 |
/// valid UTF-16. |
| 1362 |
/// |
| 1363 |
/// The resulting string does *not* contain the nul terminator. |
| 1364 |
/// |
| 1365 |
/// See the safe version, [`from_ucstring`][Self::from_ucstring], for more information. |
| 1366 |
/// |
| 1367 |
/// # Safety |
| 1368 |
/// |
| 1369 |
/// This function is unsafe because it does not check that the string passed to it is valid |
| 1370 |
/// UTF-16. If this constraint is violated, undefined behavior results as it is assumed the |
| 1371 |
/// [`Utf16String`] is always valid UTF-16. |
| 1372 |
/// |
| 1373 |
/// # Examples |
| 1374 |
/// |
| 1375 |
/// ``` |
| 1376 |
/// use widestring::{U16CString, Utf16String}; |
| 1377 |
/// |
| 1378 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair |
| 1379 |
/// let sparkle_heart = U16CString::from_vec(sparkle_heart).unwrap(); |
| 1380 |
/// let sparkle_heart = unsafe { Utf16String::from_ucstring_unchecked(sparkle_heart) }; |
| 1381 |
/// |
| 1382 |
/// assert_eq!("💖", sparkle_heart); |
| 1383 |
/// ``` |
| 1384 |
#[inline] |
| 1385 |
#[must_use] |
| 1386 |
pub unsafe fn from_ucstring_unchecked(s: impl Into<crate::U16CString>) -> Self { |
| 1387 |
Self::from_vec_unchecked(s.into().into_vec()) |
| 1388 |
} |
| 1389 |
|
| 1390 |
/// Converts a wide C string into a UTF-16 string. |
| 1391 |
/// |
| 1392 |
/// The resulting string does *not* contain the nul terminator. |
| 1393 |
/// |
| 1394 |
/// Not all wide C strings are valid to convert, since [`Utf16String`] requires that |
| 1395 |
/// it is always valid UTF-16. This function checks to ensure that the string is valid UTF-16, |
| 1396 |
/// and then does the conversion. This does not do any copying. |
| 1397 |
/// |
| 1398 |
/// If you are sure that the string is valid UTF-16, and you don't want to incur the overhead of |
| 1399 |
/// the validity check, there is an unsafe version of this function, |
| 1400 |
/// [`from_ucstring_unchecked`][Self::from_ucstring_unchecked], which has the same behavior but |
| 1401 |
/// skips the check. |
| 1402 |
/// |
| 1403 |
/// If you need a string slice, consider using [`Utf16Str::from_ucstr`] instead. |
| 1404 |
/// |
| 1405 |
/// # Errors |
| 1406 |
/// |
| 1407 |
/// Returns an error if the string is not UTF-16 with a description as to why the provided |
| 1408 |
/// string is not UTF-16. |
| 1409 |
/// |
| 1410 |
/// # Examples |
| 1411 |
/// |
| 1412 |
/// ``` |
| 1413 |
/// use widestring::{U16CString, Utf16String}; |
| 1414 |
/// |
| 1415 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // Raw surrogate pair |
| 1416 |
/// let sparkle_heart = U16CString::from_vec(sparkle_heart).unwrap(); |
| 1417 |
/// let sparkle_heart = Utf16String::from_ucstring(sparkle_heart).unwrap(); |
| 1418 |
/// |
| 1419 |
/// assert_eq!("💖", sparkle_heart); |
| 1420 |
/// ``` |
| 1421 |
/// |
| 1422 |
/// With incorrect values that return an error: |
| 1423 |
/// |
| 1424 |
/// ``` |
| 1425 |
/// use widestring::{U16CString, Utf16String}; |
| 1426 |
/// |
| 1427 |
/// let sparkle_heart = vec![0xd83d]; // This is an invalid unpaired surrogate |
| 1428 |
/// let sparkle_heart = U16CString::from_vec(sparkle_heart).unwrap(); // Valid for a U16CString |
| 1429 |
/// |
| 1430 |
/// assert!(Utf16String::from_ucstring(sparkle_heart).is_err()); // But not for a Utf16String |
| 1431 |
/// ``` |
| 1432 |
#[inline] |
| 1433 |
pub fn from_ucstring(s: impl Into<crate::U16CString>) -> Result<Self, Utf16Error> { |
| 1434 |
Self::from_vec(s.into().into_vec()) |
| 1435 |
} |
| 1436 |
|
| 1437 |
/// Converts a wide C string slice of to a UTF-16 string, including invalid characters. |
| 1438 |
/// |
| 1439 |
/// The resulting string does *not* contain the nul terminator. |
| 1440 |
/// |
| 1441 |
/// Since the given string slice may not be valid UTF-16, and [`Utf16String`] requires that |
| 1442 |
/// it is always valid UTF-16, during the conversion this function replaces any invalid UTF-16 |
| 1443 |
/// sequences with [`U+FFFD REPLACEMENT CHARACTER`][core::char::REPLACEMENT_CHARACTER], which |
| 1444 |
/// looks like this: � |
| 1445 |
/// |
| 1446 |
/// If you are sure that the slice is valid UTF-16, and you don't want to incur the overhead of |
| 1447 |
/// the conversion, there is an unsafe version of this function, |
| 1448 |
/// [`from_ucstring_unchecked`][Self::from_ucstring_unchecked], which has the same behavior but |
| 1449 |
/// skips the checks. |
| 1450 |
/// |
| 1451 |
/// This function returns a [`Cow<'_, Utf16Str>`][std::borrow::Cow]. If the given slice is |
| 1452 |
/// invalid UTF-16, then we need to insert our replacement characters which will change the size |
| 1453 |
/// of the string, and hence, require an owned [`Utf16String`]. But if it's already valid |
| 1454 |
/// UTF-16, we don't need a new allocation. This return type allows us to handle both cases. |
| 1455 |
/// |
| 1456 |
/// # Examples |
| 1457 |
/// |
| 1458 |
/// ``` |
| 1459 |
/// # use widestring::utf16str; |
| 1460 |
/// use widestring::{U16CStr, Utf16String}; |
| 1461 |
/// |
| 1462 |
/// let sparkle_heart = vec![0xd83d, 0xdc96, 0x0]; // Raw surrogate pair |
| 1463 |
/// let sparkle_heart = U16CStr::from_slice(&sparkle_heart).unwrap(); |
| 1464 |
/// let sparkle_heart = Utf16String::from_ucstr_lossy(sparkle_heart); |
| 1465 |
/// |
| 1466 |
/// assert_eq!(utf16str!("💖"), sparkle_heart); |
| 1467 |
/// ``` |
| 1468 |
/// |
| 1469 |
/// With incorrect values that return an error: |
| 1470 |
/// |
| 1471 |
/// ``` |
| 1472 |
/// # use widestring::utf16str; |
| 1473 |
/// use widestring::{U16CStr, Utf16String}; |
| 1474 |
/// |
| 1475 |
/// let sparkle_heart = vec![0xd83d, 0x0]; // This is an invalid unpaired surrogate |
| 1476 |
/// let sparkle_heart = U16CStr::from_slice(&sparkle_heart).unwrap(); |
| 1477 |
/// let sparkle_heart = Utf16String::from_ucstr_lossy(sparkle_heart); |
| 1478 |
/// |
| 1479 |
/// assert_eq!(utf16str!("\u{fffd}"), sparkle_heart); |
| 1480 |
/// ``` |
| 1481 |
#[inline] |
| 1482 |
#[must_use] |
| 1483 |
pub fn from_ucstr_lossy(s: &crate::U16CStr) -> Cow<'_, Utf16Str> { |
| 1484 |
Self::from_slice_lossy(s.as_slice()) |
| 1485 |
} |
| 1486 |
|
| 1487 |
/// Appends the given [`char`] to the end of this string. |
| 1488 |
/// |
| 1489 |
/// # Examples |
| 1490 |
/// |
| 1491 |
/// ``` |
| 1492 |
/// use widestring::Utf16String; |
| 1493 |
/// let mut s = Utf16String::from_str("abc"); |
| 1494 |
/// |
| 1495 |
/// s.push('1'); |
| 1496 |
/// s.push('2'); |
| 1497 |
/// s.push('3'); |
| 1498 |
/// |
| 1499 |
/// assert_eq!("abc123", s); |
| 1500 |
/// ``` |
| 1501 |
#[inline] |
| 1502 |
pub fn push(&mut self, ch: char) { |
| 1503 |
let mut buf = [0; 2]; |
| 1504 |
self.inner.extend_from_slice(ch.encode_utf16(&mut buf)) |
| 1505 |
} |
| 1506 |
|
| 1507 |
/// Shortens this string to the specified length. |
| 1508 |
/// |
| 1509 |
/// If `new_len` is greater than the string's current length, this has no effect. |
| 1510 |
/// |
| 1511 |
/// Note that this method has no effect on the allocated capacity of the string. |
| 1512 |
/// |
| 1513 |
/// # Panics |
| 1514 |
/// |
| 1515 |
/// Panics if `new_len` does not lie on a [`char`] boundary. |
| 1516 |
/// |
| 1517 |
/// # Examples |
| 1518 |
/// |
| 1519 |
/// ``` |
| 1520 |
/// use widestring::Utf16String; |
| 1521 |
/// let mut s = Utf16String::from_str("hello"); |
| 1522 |
/// s.truncate(2); |
| 1523 |
/// assert_eq!("he", s); |
| 1524 |
/// ``` |
| 1525 |
#[inline] |
| 1526 |
pub fn truncate(&mut self, new_len: usize) { |
| 1527 |
assert!(self.is_char_boundary(new_len)); |
| 1528 |
self.inner.truncate(new_len) |
| 1529 |
} |
| 1530 |
|
| 1531 |
/// Removes the last character from the string buffer and returns it. |
| 1532 |
/// |
| 1533 |
/// Returns [`None`] if this string is empty. |
| 1534 |
/// |
| 1535 |
/// # Examples |
| 1536 |
/// |
| 1537 |
/// ``` |
| 1538 |
/// use widestring::Utf16String; |
| 1539 |
/// let mut s = Utf16String::from_str("foo𝄞"); |
| 1540 |
/// |
| 1541 |
/// assert_eq!(s.pop(), Some('𝄞')); |
| 1542 |
/// assert_eq!(s.pop(), Some('o')); |
| 1543 |
/// assert_eq!(s.pop(), Some('o')); |
| 1544 |
/// assert_eq!(s.pop(), Some('f')); |
| 1545 |
/// |
| 1546 |
/// assert_eq!(s.pop(), None); |
| 1547 |
/// ``` |
| 1548 |
pub fn pop(&mut self) -> Option<char> { |
| 1549 |
let c = self.inner.pop(); |
| 1550 |
if let Some(c) = c { |
| 1551 |
if is_utf16_low_surrogate(c) { |
| 1552 |
let high = self.inner.pop().unwrap(); |
| 1553 |
// SAFETY: string is always valid UTF-16, so pair is valid |
| 1554 |
Some(unsafe { decode_utf16_surrogate_pair(high, c) }) |
| 1555 |
} else { |
| 1556 |
// SAFETY: not a surrogate |
| 1557 |
Some(unsafe { char::from_u32_unchecked(c as u32) }) |
| 1558 |
} |
| 1559 |
} else { |
| 1560 |
None |
| 1561 |
} |
| 1562 |
} |
| 1563 |
|
| 1564 |
/// Removes a [`char`] from this string at an offset and returns it. |
| 1565 |
/// |
| 1566 |
/// This is an _O(n)_ operation, as it requires copying every element in the buffer. |
| 1567 |
/// |
| 1568 |
/// # Panics |
| 1569 |
/// |
| 1570 |
/// Panics if `idx` is larger than or equal to the string's length, or if it does not lie on a |
| 1571 |
/// [`char`] boundary. |
| 1572 |
/// |
| 1573 |
/// # Examples |
| 1574 |
/// |
| 1575 |
/// ``` |
| 1576 |
/// use widestring::Utf16String; |
| 1577 |
/// let mut s = Utf16String::from_str("𝄞foo"); |
| 1578 |
/// |
| 1579 |
/// assert_eq!(s.remove(0), '𝄞'); |
| 1580 |
/// assert_eq!(s.remove(1), 'o'); |
| 1581 |
/// assert_eq!(s.remove(0), 'f'); |
| 1582 |
/// assert_eq!(s.remove(0), 'o'); |
| 1583 |
/// ``` |
| 1584 |
#[inline] |
| 1585 |
pub fn remove(&mut self, idx: usize) -> char { |
| 1586 |
let c = self[idx..].chars().next().unwrap(); |
| 1587 |
let next = idx + c.len_utf16(); |
| 1588 |
let len = self.len(); |
| 1589 |
unsafe { |
| 1590 |
ptr::copy( |
| 1591 |
self.inner.as_ptr().add(next), |
| 1592 |
self.inner.as_mut_ptr().add(idx), |
| 1593 |
len - next, |
| 1594 |
); |
| 1595 |
self.inner.set_len(len - (next - idx)); |
| 1596 |
} |
| 1597 |
c |
| 1598 |
} |
| 1599 |
|
| 1600 |
/// Retains only the characters specified by the predicate. |
| 1601 |
/// |
| 1602 |
/// In other words, remove all characters `c` such that `f(c)` returns `false`. This method |
| 1603 |
/// operates in place, visiting each character exactly once in the original order, and preserves |
| 1604 |
/// the order of the retained characters. |
| 1605 |
/// |
| 1606 |
/// # Examples |
| 1607 |
/// |
| 1608 |
/// ``` |
| 1609 |
/// use widestring::Utf16String; |
| 1610 |
/// let mut s = Utf16String::from_str("f_o_ob_ar"); |
| 1611 |
/// |
| 1612 |
/// s.retain(|c| c != '_'); |
| 1613 |
/// |
| 1614 |
/// assert_eq!(s, "foobar"); |
| 1615 |
/// ``` |
| 1616 |
/// |
| 1617 |
/// Because the elements are visited exactly once in the original order, external state may be |
| 1618 |
/// used to decide which elements to keep. |
| 1619 |
/// |
| 1620 |
/// ``` |
| 1621 |
/// use widestring::Utf16String; |
| 1622 |
/// let mut s = Utf16String::from_str("abcde"); |
| 1623 |
/// let keep = [false, true, true, false, true]; |
| 1624 |
/// let mut iter = keep.iter(); |
| 1625 |
/// s.retain(|_| *iter.next().unwrap()); |
| 1626 |
/// assert_eq!(s, "bce"); |
| 1627 |
/// ``` |
| 1628 |
pub fn retain<F>(&mut self, mut f: F) |
| 1629 |
where |
| 1630 |
F: FnMut(char) -> bool, |
| 1631 |
{ |
| 1632 |
let mut index = 0; |
| 1633 |
while index < self.len() { |
| 1634 |
// SAFETY: always in bounds and incremented by len_utf16 only |
| 1635 |
let c = unsafe { self.get_unchecked(index..) } |
| 1636 |
.chars() |
| 1637 |
.next() |
| 1638 |
.unwrap(); |
| 1639 |
if !f(c) { |
| 1640 |
self.inner.drain(index..index + c.len_utf16()); |
| 1641 |
} else { |
| 1642 |
index += c.len_utf16(); |
| 1643 |
} |
| 1644 |
} |
| 1645 |
} |
| 1646 |
|
| 1647 |
/// Inserts a character into this string at an offset. |
| 1648 |
/// |
| 1649 |
/// This is an _O(n)_ operation as it requires copying every element in the buffer. |
| 1650 |
/// |
| 1651 |
/// # Panics |
| 1652 |
/// |
| 1653 |
/// Panics if `idx` is larger than the string's length, or if it does not lie on a [`char`] |
| 1654 |
/// boundary. |
| 1655 |
/// |
| 1656 |
/// # Examples |
| 1657 |
/// |
| 1658 |
/// ``` |
| 1659 |
/// use widestring::Utf16String; |
| 1660 |
/// let mut s = Utf16String::with_capacity(5); |
| 1661 |
/// |
| 1662 |
/// s.insert(0, '𝄞'); |
| 1663 |
/// s.insert(0, 'f'); |
| 1664 |
/// s.insert(1, 'o'); |
| 1665 |
/// s.insert(4, 'o'); |
| 1666 |
/// |
| 1667 |
/// assert_eq!("fo𝄞o", s); |
| 1668 |
/// ``` |
| 1669 |
#[inline] |
| 1670 |
pub fn insert(&mut self, idx: usize, ch: char) { |
| 1671 |
assert!(self.is_char_boundary(idx)); |
| 1672 |
let mut bits = [0; 2]; |
| 1673 |
let bits = ch.encode_utf16(&mut bits); |
| 1674 |
|
| 1675 |
unsafe { |
| 1676 |
self.insert_slice(idx, bits); |
| 1677 |
} |
| 1678 |
} |
| 1679 |
|
| 1680 |
/// Inserts a UTF-16 string slice into this string at an offset. |
| 1681 |
/// |
| 1682 |
/// This is an _O(n)_ operation as it requires copying every element in the buffer. |
| 1683 |
/// |
| 1684 |
/// # Panics |
| 1685 |
/// |
| 1686 |
/// Panics if `idx` is larger than the string's length, or if it does not lie on a [`char`] |
| 1687 |
/// boundary. |
| 1688 |
/// |
| 1689 |
/// # Examples |
| 1690 |
/// |
| 1691 |
/// ``` |
| 1692 |
/// # use widestring::utf16str; |
| 1693 |
/// use widestring::Utf16String; |
| 1694 |
/// let mut s = Utf16String::from_str("bar"); |
| 1695 |
/// |
| 1696 |
/// s.insert_utfstr(0, utf16str!("foo")); |
| 1697 |
/// |
| 1698 |
/// assert_eq!("foobar", s); |
| 1699 |
/// ``` |
| 1700 |
#[inline] |
| 1701 |
pub fn insert_utfstr(&mut self, idx: usize, string: &Utf16Str) { |
| 1702 |
assert!(self.is_char_boundary(idx)); |
| 1703 |
|
| 1704 |
unsafe { |
| 1705 |
self.insert_slice(idx, string.as_slice()); |
| 1706 |
} |
| 1707 |
} |
| 1708 |
|
| 1709 |
/// Splits the string into two at the given index. |
| 1710 |
/// |
| 1711 |
/// Returns a newly allocated string. `self` contains elements [0, at), and the returned string |
| 1712 |
/// contains elements [at, len). `at` must be on the boundary of a UTF-16 code point. |
| 1713 |
/// |
| 1714 |
/// Note that the capacity of `self` does not change. |
| 1715 |
/// |
| 1716 |
/// # Panics |
| 1717 |
/// |
| 1718 |
/// Panics if `at` is not on a UTF-16 code point boundary, or if it is beyond the last code |
| 1719 |
/// point of the string. |
| 1720 |
/// |
| 1721 |
/// # Examples |
| 1722 |
/// |
| 1723 |
/// ``` |
| 1724 |
/// use widestring::Utf16String; |
| 1725 |
/// let mut hello = Utf16String::from_str("Hello, World!"); |
| 1726 |
/// let world = hello.split_off(7); |
| 1727 |
/// assert_eq!(hello, "Hello, "); |
| 1728 |
/// assert_eq!(world, "World!"); |
| 1729 |
/// ``` |
| 1730 |
#[inline] |
| 1731 |
#[must_use] |
| 1732 |
pub fn split_off(&mut self, at: usize) -> Self { |
| 1733 |
assert!(self.is_char_boundary(at)); |
| 1734 |
unsafe { Self::from_vec_unchecked(self.inner.split_off(at)) } |
| 1735 |
} |
| 1736 |
|
| 1737 |
/// Creates a draining iterator that removes the specified range in the string and yields the |
| 1738 |
/// removed [`char`]s. |
| 1739 |
/// |
| 1740 |
/// Note: The element range is removed even if the iterator is not consumed until the end. |
| 1741 |
/// |
| 1742 |
/// # Panics |
| 1743 |
/// |
| 1744 |
/// Panics if the starting point or end point do not lie on a [`char`] boundary, or if they're |
| 1745 |
/// out of bounds. |
| 1746 |
/// |
| 1747 |
/// # Examples |
| 1748 |
/// |
| 1749 |
/// Basic usage: |
| 1750 |
/// |
| 1751 |
/// ``` |
| 1752 |
/// use widestring::Utf16String; |
| 1753 |
/// let mut s = Utf16String::from_str("α is alpha, β is beta"); |
| 1754 |
/// let beta_offset = 12; |
| 1755 |
/// |
| 1756 |
/// // Remove the range up until the β from the string |
| 1757 |
/// let t: Utf16String = s.drain(..beta_offset).collect(); |
| 1758 |
/// assert_eq!(t, "α is alpha, "); |
| 1759 |
/// assert_eq!(s, "β is beta"); |
| 1760 |
/// |
| 1761 |
/// // A full range clears the string |
| 1762 |
/// s.drain(..); |
| 1763 |
/// assert_eq!(s, ""); |
| 1764 |
/// ``` |
| 1765 |
pub fn drain<R>(&mut self, range: R) -> DrainUtf16<'_> |
| 1766 |
where |
| 1767 |
R: RangeBounds<usize>, |
| 1768 |
{ |
| 1769 |
// WARNING: Using range again would be unsound |
| 1770 |
// TODO: replace with core::slice::range when it is stabilized |
| 1771 |
let core::ops::Range { start, end } = crate::range(range, ..self.len()); |
| 1772 |
assert!(self.is_char_boundary(start)); |
| 1773 |
assert!(self.is_char_boundary(end)); |
| 1774 |
|
| 1775 |
// Take out two simultaneous borrows. The self_ptr won't be accessed |
| 1776 |
// until iteration is over, in Drop. |
| 1777 |
let self_ptr: *mut _ = self; |
| 1778 |
// SAFETY: `slice::range` and `is_char_boundary` do the appropriate bounds checks. |
| 1779 |
let chars_iter = unsafe { self.get_unchecked(start..end) }.chars(); |
| 1780 |
|
| 1781 |
DrainUtf16 { |
| 1782 |
start, |
| 1783 |
end, |
| 1784 |
iter: chars_iter, |
| 1785 |
string: self_ptr, |
| 1786 |
} |
| 1787 |
} |
| 1788 |
|
| 1789 |
/// Removes the specified range in the string, and replaces it with the given string. |
| 1790 |
/// |
| 1791 |
/// The given string doesn't need to be the same length as the range. |
| 1792 |
/// |
| 1793 |
/// # Panics |
| 1794 |
/// |
| 1795 |
/// Panics if the starting point or end point do not lie on a [`char`] boundary, or if they're |
| 1796 |
/// out of bounds. |
| 1797 |
/// |
| 1798 |
/// # Examples |
| 1799 |
/// |
| 1800 |
/// Basic usage: |
| 1801 |
/// |
| 1802 |
/// ``` |
| 1803 |
/// use widestring::{utf16str, Utf16String}; |
| 1804 |
/// let mut s = Utf16String::from_str("α is alpha, β is beta"); |
| 1805 |
/// let beta_offset = 12; |
| 1806 |
/// |
| 1807 |
/// // Replace the range up until the β from the string |
| 1808 |
/// s.replace_range(..beta_offset, utf16str!("Α is capital alpha; ")); |
| 1809 |
/// assert_eq!(s, "Α is capital alpha; β is beta"); |
| 1810 |
/// ``` |
| 1811 |
pub fn replace_range<R>(&mut self, range: R, replace_with: &Utf16Str) |
| 1812 |
where |
| 1813 |
R: RangeBounds<usize>, |
| 1814 |
{ |
| 1815 |
use core::ops::Bound::*; |
| 1816 |
|
| 1817 |
// WARNING: Using range again would be unsound |
| 1818 |
let start = range.start_bound(); |
| 1819 |
match start { |
| 1820 |
Included(&n) => assert!(self.is_char_boundary(n)), |
| 1821 |
Excluded(&n) => assert!(self.is_char_boundary(n + 1)), |
| 1822 |
Unbounded => {} |
| 1823 |
}; |
| 1824 |
// WARNING: Inlining this variable would be unsound |
| 1825 |
let end = range.end_bound(); |
| 1826 |
match end { |
| 1827 |
Included(&n) => assert!(self.is_char_boundary(n + 1)), |
| 1828 |
Excluded(&n) => assert!(self.is_char_boundary(n)), |
| 1829 |
Unbounded => {} |
| 1830 |
}; |
| 1831 |
|
| 1832 |
// Using `range` again would be unsound |
| 1833 |
// We assume the bounds reported by `range` remain the same, but |
| 1834 |
// an adversarial implementation could change between calls |
| 1835 |
self.inner |
| 1836 |
.splice((start, end), replace_with.as_slice().iter().copied()); |
| 1837 |
} |
| 1838 |
} |
| 1839 |
|
| 1840 |
impl Utf32String { |
| 1841 |
/// Converts a [`u32`] vector of UTF-32 data to a string. |
| 1842 |
/// |
| 1843 |
/// Not all slices of [`u32`] values are valid to convert, since [`Utf32String`] requires that |
| 1844 |
/// it is always valid UTF-32. This function checks to ensure that the values are valid UTF-32, |
| 1845 |
/// and then does the conversion. This does not do any copying. |
| 1846 |
/// |
| 1847 |
/// If you are sure that the slice is valid UTF-32, and you don't want to incur the overhead of |
| 1848 |
/// the validity check, there is an unsafe version of this function, |
| 1849 |
/// [`from_vec_unchecked`][Self::from_vec_unchecked], which has the same behavior but skips |
| 1850 |
/// the check. |
| 1851 |
/// |
| 1852 |
/// If you need a string slice, consider using [`Utf32Str::from_slice`] instead. |
| 1853 |
/// |
| 1854 |
/// The inverse of this method is [`into_vec`][Self::into_vec]. |
| 1855 |
/// |
| 1856 |
/// # Errors |
| 1857 |
/// |
| 1858 |
/// Returns an error if the vector is not UTF-32 with a description as to why the provided |
| 1859 |
/// vector is not UTF-32. The error will contain the original [`Vec`] that can be reclaimed with |
| 1860 |
/// [`into_vec`][Utf32Error::into_vec]. |
| 1861 |
/// |
| 1862 |
/// # Examples |
| 1863 |
/// |
| 1864 |
/// ``` |
| 1865 |
/// use widestring::Utf32String; |
| 1866 |
/// |
| 1867 |
/// let sparkle_heart = vec![0x1f496]; |
| 1868 |
/// let sparkle_heart = Utf32String::from_vec(sparkle_heart).unwrap(); |
| 1869 |
/// |
| 1870 |
/// assert_eq!("💖", sparkle_heart); |
| 1871 |
/// ``` |
| 1872 |
/// |
| 1873 |
/// With incorrect values that return an error: |
| 1874 |
/// |
| 1875 |
/// ``` |
| 1876 |
/// use widestring::Utf32String; |
| 1877 |
/// |
| 1878 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // UTF-16 surrogates are invalid |
| 1879 |
/// |
| 1880 |
/// assert!(Utf32String::from_vec(sparkle_heart).is_err()); |
| 1881 |
/// ``` |
| 1882 |
pub fn from_vec(v: impl Into<Vec<u32>>) -> Result<Self, Utf32Error> { |
| 1883 |
let v = validate_utf32_vec(v.into())?; |
| 1884 |
Ok(unsafe { Self::from_vec_unchecked(v) }) |
| 1885 |
} |
| 1886 |
|
| 1887 |
/// Converts a slice of [`u32`] data to a string, including invalid characters. |
| 1888 |
/// |
| 1889 |
/// Since the given [`u32`] slice may not be valid UTF-32, and [`Utf32String`] requires that |
| 1890 |
/// it is always valid UTF-32, during the conversion this function replaces any invalid UTF-32 |
| 1891 |
/// sequences with [`U+FFFD REPLACEMENT CHARACTER`][core::char::REPLACEMENT_CHARACTER], which |
| 1892 |
/// looks like this: � |
| 1893 |
/// |
| 1894 |
/// If you are sure that the slice is valid UTF-32, and you don't want to incur the overhead of |
| 1895 |
/// the conversion, there is an unsafe version of this function, |
| 1896 |
/// [`from_vec_unchecked`][Self::from_vec_unchecked], which has the same behavior but skips |
| 1897 |
/// the checks. |
| 1898 |
/// |
| 1899 |
/// This function returns a [`Cow<'_, Utf32Str>`][std::borrow::Cow]. If the given slice is |
| 1900 |
/// invalid UTF-32, then we need to insert our replacement characters which will change the size |
| 1901 |
/// of the string, and hence, require an owned [`Utf32String`]. But if it's already valid |
| 1902 |
/// UTF-32, we don't need a new allocation. This return type allows us to handle both cases. |
| 1903 |
/// |
| 1904 |
/// # Examples |
| 1905 |
/// |
| 1906 |
/// ``` |
| 1907 |
/// # use widestring::utf32str; |
| 1908 |
/// use widestring::Utf32String; |
| 1909 |
/// |
| 1910 |
/// let sparkle_heart = vec![0x1f496]; |
| 1911 |
/// let sparkle_heart = Utf32String::from_slice_lossy(&sparkle_heart); |
| 1912 |
/// |
| 1913 |
/// assert_eq!(utf32str!("💖"), sparkle_heart); |
| 1914 |
/// ``` |
| 1915 |
/// |
| 1916 |
/// With incorrect values that return an error: |
| 1917 |
/// |
| 1918 |
/// ``` |
| 1919 |
/// # use widestring::utf32str; |
| 1920 |
/// use widestring::Utf32String; |
| 1921 |
/// |
| 1922 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // UTF-16 surrogates are invalid |
| 1923 |
/// let sparkle_heart = Utf32String::from_slice_lossy(&sparkle_heart); |
| 1924 |
/// |
| 1925 |
/// assert_eq!(utf32str!("\u{fffd}\u{fffd}"), sparkle_heart); |
| 1926 |
/// ``` |
| 1927 |
#[must_use] |
| 1928 |
pub fn from_slice_lossy(s: &[u32]) -> Cow<'_, Utf32Str> { |
| 1929 |
match validate_utf32(s) { |
| 1930 |
// SAFETY: validated as UTF-32 |
| 1931 |
Ok(()) => Cow::Borrowed(unsafe { Utf32Str::from_slice_unchecked(s) }), |
| 1932 |
Err(e) => { |
| 1933 |
let mut v = Vec::with_capacity(s.len()); |
| 1934 |
// Valid up until index |
| 1935 |
v.extend_from_slice(&s[..e.index()]); |
| 1936 |
for u in s[e.index()..].iter().copied() { |
| 1937 |
if char::from_u32(u).is_some() { |
| 1938 |
v.push(u); |
| 1939 |
} else { |
| 1940 |
v.push(char::REPLACEMENT_CHARACTER as u32); |
| 1941 |
} |
| 1942 |
} |
| 1943 |
// SATEFY: Is now valid UTF-32 with replacement chars |
| 1944 |
Cow::Owned(unsafe { Self::from_vec_unchecked(v) }) |
| 1945 |
} |
| 1946 |
} |
| 1947 |
} |
| 1948 |
|
| 1949 |
/// Converts a wide string of undefined encoding to a UTF-32 string without checking that the |
| 1950 |
/// string contains valid UTF-32. |
| 1951 |
/// |
| 1952 |
/// See the safe version, [`from_ustring`][Self::from_ustring], for more information. |
| 1953 |
/// |
| 1954 |
/// # Safety |
| 1955 |
/// |
| 1956 |
/// This function is unsafe because it does not check that the string passed to it is valid |
| 1957 |
/// UTF-32. If this constraint is violated, undefined behavior results as it is assumed the |
| 1958 |
/// [`Utf32String`] is always valid UTF-32. |
| 1959 |
/// |
| 1960 |
/// # Examples |
| 1961 |
/// |
| 1962 |
/// ``` |
| 1963 |
/// use widestring::{U32String, Utf32String}; |
| 1964 |
/// |
| 1965 |
/// let sparkle_heart = vec![0x1f496]; |
| 1966 |
/// let sparkle_heart = U32String::from_vec(sparkle_heart); |
| 1967 |
/// let sparkle_heart = unsafe { Utf32String::from_ustring_unchecked(sparkle_heart) }; |
| 1968 |
/// |
| 1969 |
/// assert_eq!("💖", sparkle_heart); |
| 1970 |
/// ``` |
| 1971 |
#[inline] |
| 1972 |
#[must_use] |
| 1973 |
pub unsafe fn from_ustring_unchecked(s: impl Into<crate::U32String>) -> Self { |
| 1974 |
Self::from_vec_unchecked(s.into().into_vec()) |
| 1975 |
} |
| 1976 |
|
| 1977 |
/// Converts a wide string of undefined encoding string into a UTF-32 string. |
| 1978 |
/// |
| 1979 |
/// Not all strings of undefined encoding are valid to convert, since [`Utf32String`] requires |
| 1980 |
/// that it is always valid UTF-32. This function checks to ensure that the string is valid |
| 1981 |
/// UTF-32, and then does the conversion. This does not do any copying. |
| 1982 |
/// |
| 1983 |
/// If you are sure that the string is valid UTF-32, and you don't want to incur the overhead of |
| 1984 |
/// the validity check, there is an unsafe version of this function, |
| 1985 |
/// [`from_ustring_unchecked`][Self::from_ustring_unchecked], which has the same behavior but |
| 1986 |
/// skips the check. |
| 1987 |
/// |
| 1988 |
/// If you need a string slice, consider using [`Utf32Str::from_ustr`] instead. |
| 1989 |
/// |
| 1990 |
/// # Errors |
| 1991 |
/// |
| 1992 |
/// Returns an error if the string is not UTF-32 with a description as to why the provided |
| 1993 |
/// string is not UTF-32. |
| 1994 |
/// |
| 1995 |
/// # Examples |
| 1996 |
/// |
| 1997 |
/// ``` |
| 1998 |
/// use widestring::{U32String, Utf32String}; |
| 1999 |
/// |
| 2000 |
/// let sparkle_heart = vec![0x1f496]; |
| 2001 |
/// let sparkle_heart = U32String::from_vec(sparkle_heart); |
| 2002 |
/// let sparkle_heart = Utf32String::from_ustring(sparkle_heart).unwrap(); |
| 2003 |
/// |
| 2004 |
/// assert_eq!("💖", sparkle_heart); |
| 2005 |
/// ``` |
| 2006 |
/// |
| 2007 |
/// With incorrect values that return an error: |
| 2008 |
/// |
| 2009 |
/// ``` |
| 2010 |
/// use widestring::{U32String, Utf32String}; |
| 2011 |
/// |
| 2012 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // UTF-16 surrogates are invalid |
| 2013 |
/// let sparkle_heart = U32String::from_vec(sparkle_heart); // Valid for a U32String |
| 2014 |
/// |
| 2015 |
/// assert!(Utf32String::from_ustring(sparkle_heart).is_err()); // But not for a Utf32String |
| 2016 |
/// ``` |
| 2017 |
#[inline] |
| 2018 |
pub fn from_ustring(s: impl Into<crate::U32String>) -> Result<Self, Utf32Error> { |
| 2019 |
Self::from_vec(s.into().into_vec()) |
| 2020 |
} |
| 2021 |
|
| 2022 |
/// Converts a wide string slice of undefined encoding to a UTF-32 string, including invalid |
| 2023 |
/// characters. |
| 2024 |
/// |
| 2025 |
/// Since the given string slice may not be valid UTF-32, and [`Utf32String`] requires that |
| 2026 |
/// it is always valid UTF-32, during the conversion this function replaces any invalid UTF-32 |
| 2027 |
/// sequences with [`U+FFFD REPLACEMENT CHARACTER`][core::char::REPLACEMENT_CHARACTER], which |
| 2028 |
/// looks like this: � |
| 2029 |
/// |
| 2030 |
/// If you are sure that the slice is valid UTF-32, and you don't want to incur the overhead of |
| 2031 |
/// the conversion, there is an unsafe version of this function, |
| 2032 |
/// [`from_ustring_unchecked`][Self::from_ustring_unchecked], which has the same behavior but |
| 2033 |
/// skips the checks. |
| 2034 |
/// |
| 2035 |
/// This function returns a [`Cow<'_, Utf32Str>`][std::borrow::Cow]. If the given slice is |
| 2036 |
/// invalid UTF-32, then we need to insert our replacement characters which will change the size |
| 2037 |
/// of the string, and hence, require an owned [`Utf32String`]. But if it's already valid |
| 2038 |
/// UTF-32, we don't need a new allocation. This return type allows us to handle both cases. |
| 2039 |
/// |
| 2040 |
/// # Examples |
| 2041 |
/// |
| 2042 |
/// ``` |
| 2043 |
/// # use widestring::utf32str; |
| 2044 |
/// use widestring::{U32Str, Utf32String}; |
| 2045 |
/// |
| 2046 |
/// let sparkle_heart = vec![0x1f496]; |
| 2047 |
/// let sparkle_heart = U32Str::from_slice(&sparkle_heart); |
| 2048 |
/// let sparkle_heart = Utf32String::from_ustr_lossy(sparkle_heart); |
| 2049 |
/// |
| 2050 |
/// assert_eq!(utf32str!("💖"), sparkle_heart); |
| 2051 |
/// ``` |
| 2052 |
/// |
| 2053 |
/// With incorrect values that return an error: |
| 2054 |
/// |
| 2055 |
/// ``` |
| 2056 |
/// # use widestring::utf32str; |
| 2057 |
/// use widestring::{U32Str, Utf32String}; |
| 2058 |
/// |
| 2059 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // UTF-16 surrogates are invalid |
| 2060 |
/// let sparkle_heart = U32Str::from_slice(&sparkle_heart); |
| 2061 |
/// let sparkle_heart = Utf32String::from_ustr_lossy(sparkle_heart); |
| 2062 |
/// |
| 2063 |
/// assert_eq!(utf32str!("\u{fffd}\u{fffd}"), sparkle_heart); |
| 2064 |
/// ``` |
| 2065 |
#[inline] |
| 2066 |
#[must_use] |
| 2067 |
pub fn from_ustr_lossy(s: &crate::U32Str) -> Cow<'_, Utf32Str> { |
| 2068 |
Self::from_slice_lossy(s.as_slice()) |
| 2069 |
} |
| 2070 |
|
| 2071 |
/// Converts a wide C string to a UTF-32 string without checking that the string contains |
| 2072 |
/// valid UTF-32. |
| 2073 |
/// |
| 2074 |
/// The resulting string does *not* contain the nul terminator. |
| 2075 |
/// |
| 2076 |
/// See the safe version, [`from_ucstring`][Self::from_ucstring], for more information. |
| 2077 |
/// |
| 2078 |
/// # Safety |
| 2079 |
/// |
| 2080 |
/// This function is unsafe because it does not check that the string passed to it is valid |
| 2081 |
/// UTF-32. If this constraint is violated, undefined behavior results as it is assumed the |
| 2082 |
/// [`Utf32String`] is always valid UTF-32. |
| 2083 |
/// |
| 2084 |
/// # Examples |
| 2085 |
/// |
| 2086 |
/// ``` |
| 2087 |
/// use widestring::{U32CString, Utf32String}; |
| 2088 |
/// |
| 2089 |
/// let sparkle_heart = vec![0x1f496]; |
| 2090 |
/// let sparkle_heart = U32CString::from_vec(sparkle_heart).unwrap(); |
| 2091 |
/// let sparkle_heart = unsafe { Utf32String::from_ucstring_unchecked(sparkle_heart) }; |
| 2092 |
/// |
| 2093 |
/// assert_eq!("💖", sparkle_heart); |
| 2094 |
/// ``` |
| 2095 |
#[inline] |
| 2096 |
#[must_use] |
| 2097 |
pub unsafe fn from_ucstring_unchecked(s: impl Into<crate::U32CString>) -> Self { |
| 2098 |
Self::from_vec_unchecked(s.into().into_vec()) |
| 2099 |
} |
| 2100 |
|
| 2101 |
/// Converts a wide C string into a UTF-32 string. |
| 2102 |
/// |
| 2103 |
/// The resulting string does *not* contain the nul terminator. |
| 2104 |
/// |
| 2105 |
/// Not all wide C strings are valid to convert, since [`Utf32String`] requires that |
| 2106 |
/// it is always valid UTF-32. This function checks to ensure that the string is valid UTF-32, |
| 2107 |
/// and then does the conversion. This does not do any copying. |
| 2108 |
/// |
| 2109 |
/// If you are sure that the string is valid UTF-32, and you don't want to incur the overhead of |
| 2110 |
/// the validity check, there is an unsafe version of this function, |
| 2111 |
/// [`from_ucstring_unchecked`][Self::from_ucstring_unchecked], which has the same behavior but |
| 2112 |
/// skips the check. |
| 2113 |
/// |
| 2114 |
/// If you need a string slice, consider using [`Utf32Str::from_ucstr`] instead. |
| 2115 |
/// |
| 2116 |
/// # Errors |
| 2117 |
/// |
| 2118 |
/// Returns an error if the string is not UTF-32 with a description as to why the provided |
| 2119 |
/// string is not UTF-32. |
| 2120 |
/// |
| 2121 |
/// # Examples |
| 2122 |
/// |
| 2123 |
/// ``` |
| 2124 |
/// use widestring::{U32CString, Utf32String}; |
| 2125 |
/// |
| 2126 |
/// let sparkle_heart = vec![0x1f496]; |
| 2127 |
/// let sparkle_heart = U32CString::from_vec(sparkle_heart).unwrap(); |
| 2128 |
/// let sparkle_heart = Utf32String::from_ucstring(sparkle_heart).unwrap(); |
| 2129 |
/// |
| 2130 |
/// assert_eq!("💖", sparkle_heart); |
| 2131 |
/// ``` |
| 2132 |
/// |
| 2133 |
/// With incorrect values that return an error: |
| 2134 |
/// |
| 2135 |
/// ``` |
| 2136 |
/// use widestring::{U32CString, Utf32String}; |
| 2137 |
/// |
| 2138 |
/// let sparkle_heart = vec![0xd83d, 0xdc96]; // UTF-16 surrogates are invalid |
| 2139 |
/// let sparkle_heart = U32CString::from_vec(sparkle_heart).unwrap(); // Valid for a U32CString |
| 2140 |
/// |
| 2141 |
/// assert!(Utf32String::from_ucstring(sparkle_heart).is_err()); // But not for a Utf32String |
| 2142 |
/// ``` |
| 2143 |
#[inline] |
| 2144 |
pub fn from_ucstring(s: impl Into<crate::U32CString>) -> Result<Self, Utf32Error> { |
| 2145 |
Self::from_vec(s.into().into_vec()) |
| 2146 |
} |
| 2147 |
|
| 2148 |
/// Converts a wide C string slice of to a UTF-32 string, including invalid characters. |
| 2149 |
/// |
| 2150 |
/// The resulting string does *not* contain the nul terminator. |
| 2151 |
/// |
| 2152 |
/// Since the given string slice may not be valid UTF-32, and [`Utf32String`] requires that |
| 2153 |
/// it is always valid UTF-32, during the conversion this function replaces any invalid UTF-32 |
| 2154 |
/// sequences with [`U+FFFD REPLACEMENT CHARACTER`][core::char::REPLACEMENT_CHARACTER], which |
| 2155 |
/// looks like this: � |
| 2156 |
/// |
| 2157 |
/// If you are sure that the slice is valid UTF-32, and you don't want to incur the overhead of |
| 2158 |
/// the conversion, there is an unsafe version of this function, |
| 2159 |
/// [`from_ucstring_unchecked`][Self::from_ucstring_unchecked], which has the same behavior but |
| 2160 |
/// skips the checks. |
| 2161 |
/// |
| 2162 |
/// This function returns a [`Cow<'_, Utf32Str>`][std::borrow::Cow]. If the given slice is |
| 2163 |
/// invalid UTF-32, then we need to insert our replacement characters which will change the size |
| 2164 |
/// of the string, and hence, require an owned [`Utf32String`]. But if it's already valid |
| 2165 |
/// UTF-32, we don't need a new allocation. This return type allows us to handle both cases. |
| 2166 |
/// |
| 2167 |
/// # Examples |
| 2168 |
/// |
| 2169 |
/// ``` |
| 2170 |
/// # use widestring::utf32str; |
| 2171 |
/// use widestring::{U32CStr, Utf32String}; |
| 2172 |
/// |
| 2173 |
/// let sparkle_heart = vec![0x1f496, 0x0]; |
| 2174 |
/// let sparkle_heart = U32CStr::from_slice(&sparkle_heart).unwrap(); |
| 2175 |
/// let sparkle_heart = Utf32String::from_ucstr_lossy(sparkle_heart); |
| 2176 |
/// |
| 2177 |
/// assert_eq!(utf32str!("💖"), sparkle_heart); |
| 2178 |
/// ``` |
| 2179 |
/// |
| 2180 |
/// With incorrect values that return an error: |
| 2181 |
/// |
| 2182 |
/// ``` |
| 2183 |
/// # use widestring::utf32str; |
| 2184 |
/// use widestring::{U32CStr, Utf32String}; |
| 2185 |
/// |
| 2186 |
/// let sparkle_heart = vec![0xd83d, 0xdc96, 0x0]; // UTF-16 surrogates are invalid |
| 2187 |
/// let sparkle_heart = U32CStr::from_slice(&sparkle_heart).unwrap(); |
| 2188 |
/// let sparkle_heart = Utf32String::from_ucstr_lossy(sparkle_heart); |
| 2189 |
/// |
| 2190 |
/// assert_eq!(utf32str!("\u{fffd}\u{fffd}"), sparkle_heart); |
| 2191 |
/// ``` |
| 2192 |
#[inline] |
| 2193 |
#[must_use] |
| 2194 |
pub fn from_ucstr_lossy(s: &crate::U32CStr) -> Cow<'_, Utf32Str> { |
| 2195 |
Self::from_slice_lossy(s.as_slice()) |
| 2196 |
} |
| 2197 |
|
| 2198 |
/// Converts a vector of [`char`]s into a UTF-32 string. |
| 2199 |
/// |
| 2200 |
/// Since [`char`]s are always valid UTF-32, this is infallible and efficient. |
| 2201 |
/// |
| 2202 |
/// If you need a string slice, consider using [`Utf32Str::from_char_slice`] instead. |
| 2203 |
/// |
| 2204 |
/// # Examples |
| 2205 |
/// |
| 2206 |
/// ``` |
| 2207 |
/// use widestring::{U32CString, Utf32String}; |
| 2208 |
/// |
| 2209 |
/// let sparkle_heart = vec!['💖']; |
| 2210 |
/// let sparkle_heart = Utf32String::from_chars(sparkle_heart); |
| 2211 |
/// |
| 2212 |
/// assert_eq!("💖", sparkle_heart); |
| 2213 |
/// ``` |
| 2214 |
#[inline] |
| 2215 |
#[must_use] |
| 2216 |
pub fn from_chars(s: impl Into<Vec<char>>) -> Self { |
| 2217 |
// SAFETY: Char slices are always valid UTF-32 |
| 2218 |
// TODO: replace mem:transmute when Vec::into_raw_parts is stabilized |
| 2219 |
// Clippy reports this is unsound due to different sized types; but the sizes are the same |
| 2220 |
// size. Still best to swap to Vec::into_raw_parts asap. |
| 2221 |
#[allow(clippy::unsound_collection_transmute)] |
| 2222 |
unsafe { |
| 2223 |
let vec: Vec<u32> = mem::transmute(s.into()); |
| 2224 |
Self::from_vec_unchecked(vec) |
| 2225 |
} |
| 2226 |
} |
| 2227 |
|
| 2228 |
/// Appends the given [`char`] to the end of this string. |
| 2229 |
/// |
| 2230 |
/// # Examples |
| 2231 |
/// |
| 2232 |
/// ``` |
| 2233 |
/// use widestring::Utf32String; |
| 2234 |
/// let mut s = Utf32String::from_str("abc"); |
| 2235 |
/// |
| 2236 |
/// s.push('1'); |
| 2237 |
/// s.push('2'); |
| 2238 |
/// s.push('3'); |
| 2239 |
/// |
| 2240 |
/// assert_eq!("abc123", s); |
| 2241 |
/// ``` |
| 2242 |
#[inline] |
| 2243 |
pub fn push(&mut self, ch: char) { |
| 2244 |
self.inner.push(ch.into()) |
| 2245 |
} |
| 2246 |
|
| 2247 |
/// Shortens this string to the specified length. |
| 2248 |
/// |
| 2249 |
/// If `new_len` is greater than the string's current length, this has no effect. |
| 2250 |
/// |
| 2251 |
/// Note that this method has no effect on the allocated capacity of the string. |
| 2252 |
/// |
| 2253 |
/// # Examples |
| 2254 |
/// |
| 2255 |
/// ``` |
| 2256 |
/// use widestring::Utf32String; |
| 2257 |
/// let mut s = Utf32String::from_str("hello"); |
| 2258 |
/// s.truncate(2); |
| 2259 |
/// assert_eq!("he", s); |
| 2260 |
/// ``` |
| 2261 |
#[inline] |
| 2262 |
pub fn truncate(&mut self, new_len: usize) { |
| 2263 |
self.inner.truncate(new_len) |
| 2264 |
} |
| 2265 |
|
| 2266 |
/// Removes the last character from the string buffer and returns it. |
| 2267 |
/// |
| 2268 |
/// Returns [`None`] if this string is empty. |
| 2269 |
/// |
| 2270 |
/// # Examples |
| 2271 |
/// |
| 2272 |
/// ``` |
| 2273 |
/// use widestring::Utf32String; |
| 2274 |
/// let mut s = Utf32String::from_str("foo"); |
| 2275 |
/// |
| 2276 |
/// assert_eq!(s.pop(), Some('o')); |
| 2277 |
/// assert_eq!(s.pop(), Some('o')); |
| 2278 |
/// assert_eq!(s.pop(), Some('f')); |
| 2279 |
/// |
| 2280 |
/// assert_eq!(s.pop(), None); |
| 2281 |
/// ``` |
| 2282 |
#[inline] |
| 2283 |
pub fn pop(&mut self) -> Option<char> { |
| 2284 |
// SAFETY: String is already valid UTF-32 |
| 2285 |
self.inner |
| 2286 |
.pop() |
| 2287 |
.map(|c| unsafe { core::char::from_u32_unchecked(c) }) |
| 2288 |
} |
| 2289 |
|
| 2290 |
/// Removes a [`char`] from this string at an offset and returns it. |
| 2291 |
/// |
| 2292 |
/// This is an _O(n)_ operation, as it requires copying every element in the buffer. |
| 2293 |
/// |
| 2294 |
/// # Panics |
| 2295 |
/// |
| 2296 |
/// Panics if `idx` is larger than or equal to the string's length. |
| 2297 |
/// |
| 2298 |
/// # Examples |
| 2299 |
/// |
| 2300 |
/// ``` |
| 2301 |
/// use widestring::Utf32String; |
| 2302 |
/// let mut s = Utf32String::from_str("foo"); |
| 2303 |
/// |
| 2304 |
/// assert_eq!(s.remove(1), 'o'); |
| 2305 |
/// assert_eq!(s.remove(0), 'f'); |
| 2306 |
/// assert_eq!(s.remove(0), 'o'); |
| 2307 |
/// ``` |
| 2308 |
#[inline] |
| 2309 |
pub fn remove(&mut self, idx: usize) -> char { |
| 2310 |
let next = idx + 1; |
| 2311 |
let len = self.len(); |
| 2312 |
unsafe { |
| 2313 |
let c = core::char::from_u32_unchecked(self.inner[idx]); |
| 2314 |
ptr::copy( |
| 2315 |
self.inner.as_ptr().add(next), |
| 2316 |
self.inner.as_mut_ptr().add(idx), |
| 2317 |
len - next, |
| 2318 |
); |
| 2319 |
self.inner.set_len(len - (next - idx)); |
| 2320 |
c |
| 2321 |
} |
| 2322 |
} |
| 2323 |
|
| 2324 |
/// Retains only the characters specified by the predicate. |
| 2325 |
/// |
| 2326 |
/// In other words, remove all characters `c` such that `f(c)` returns `false`. This method |
| 2327 |
/// operates in place, visiting each character exactly once in the original order, and preserves |
| 2328 |
/// the order of the retained characters. |
| 2329 |
/// |
| 2330 |
/// # Examples |
| 2331 |
/// |
| 2332 |
/// ``` |
| 2333 |
/// use widestring::Utf32String; |
| 2334 |
/// let mut s = Utf32String::from_str("f_o_ob_ar"); |
| 2335 |
/// |
| 2336 |
/// s.retain(|c| c != '_'); |
| 2337 |
/// |
| 2338 |
/// assert_eq!(s, "foobar"); |
| 2339 |
/// ``` |
| 2340 |
/// |
| 2341 |
/// Because the elements are visited exactly once in the original order, external state may be |
| 2342 |
/// used to decide which elements to keep. |
| 2343 |
/// |
| 2344 |
/// ``` |
| 2345 |
/// use widestring::Utf32String; |
| 2346 |
/// let mut s = Utf32String::from_str("abcde"); |
| 2347 |
/// let keep = [false, true, true, false, true]; |
| 2348 |
/// let mut iter = keep.iter(); |
| 2349 |
/// s.retain(|_| *iter.next().unwrap()); |
| 2350 |
/// assert_eq!(s, "bce"); |
| 2351 |
/// ``` |
| 2352 |
pub fn retain<F>(&mut self, mut f: F) |
| 2353 |
where |
| 2354 |
F: FnMut(char) -> bool, |
| 2355 |
{ |
| 2356 |
let mut index = 0; |
| 2357 |
while index < self.len() { |
| 2358 |
// SAFETY: always in bounds |
| 2359 |
let c = unsafe { self.get_unchecked(index..) } |
| 2360 |
.chars() |
| 2361 |
.next() |
| 2362 |
.unwrap(); |
| 2363 |
if !f(c) { |
| 2364 |
self.inner.remove(index); |
| 2365 |
} else { |
| 2366 |
index += 1; |
| 2367 |
} |
| 2368 |
} |
| 2369 |
} |
| 2370 |
|
| 2371 |
/// Inserts a character into this string at an offset. |
| 2372 |
/// |
| 2373 |
/// This is an _O(n)_ operation as it requires copying every element in the buffer. |
| 2374 |
/// |
| 2375 |
/// # Panics |
| 2376 |
/// |
| 2377 |
/// Panics if `idx` is larger than the string's length. |
| 2378 |
/// |
| 2379 |
/// # Examples |
| 2380 |
/// |
| 2381 |
/// ``` |
| 2382 |
/// use widestring::Utf32String; |
| 2383 |
/// let mut s = Utf32String::with_capacity(3); |
| 2384 |
/// |
| 2385 |
/// s.insert(0, 'f'); |
| 2386 |
/// s.insert(1, 'o'); |
| 2387 |
/// s.insert(1, 'o'); |
| 2388 |
/// |
| 2389 |
/// assert_eq!("foo", s); |
| 2390 |
/// ``` |
| 2391 |
#[inline] |
| 2392 |
pub fn insert(&mut self, idx: usize, ch: char) { |
| 2393 |
unsafe { |
| 2394 |
self.insert_slice(idx, &[ch as u32]); |
| 2395 |
} |
| 2396 |
} |
| 2397 |
|
| 2398 |
/// Inserts a UTF-32 string slice into this string at an offset. |
| 2399 |
/// |
| 2400 |
/// This is an _O(n)_ operation as it requires copying every element in the buffer. |
| 2401 |
/// |
| 2402 |
/// # Panics |
| 2403 |
/// |
| 2404 |
/// Panics if `idx` is larger than the string's length. |
| 2405 |
/// |
| 2406 |
/// # Examples |
| 2407 |
/// |
| 2408 |
/// ``` |
| 2409 |
/// # use widestring::utf32str; |
| 2410 |
/// use widestring::Utf32String; |
| 2411 |
/// let mut s = Utf32String::from_str("bar"); |
| 2412 |
/// |
| 2413 |
/// s.insert_utfstr(0, utf32str!("foo")); |
| 2414 |
/// |
| 2415 |
/// assert_eq!("foobar", s); |
| 2416 |
/// ``` |
| 2417 |
#[inline] |
| 2418 |
pub fn insert_utfstr(&mut self, idx: usize, string: &Utf32Str) { |
| 2419 |
unsafe { |
| 2420 |
self.insert_slice(idx, string.as_slice()); |
| 2421 |
} |
| 2422 |
} |
| 2423 |
|
| 2424 |
/// Splits the string into two at the given index. |
| 2425 |
/// |
| 2426 |
/// Returns a newly allocated string. `self` contains elements [0, at), and the returned string |
| 2427 |
/// contains elements [at, len). |
| 2428 |
/// |
| 2429 |
/// Note that the capacity of `self` does not change. |
| 2430 |
/// |
| 2431 |
/// # Panics |
| 2432 |
/// |
| 2433 |
/// Panics if `at`it is beyond the last code point of the string. |
| 2434 |
/// |
| 2435 |
/// # Examples |
| 2436 |
/// |
| 2437 |
/// ``` |
| 2438 |
/// use widestring::Utf32String; |
| 2439 |
/// let mut hello = Utf32String::from_str("Hello, World!"); |
| 2440 |
/// let world = hello.split_off(7); |
| 2441 |
/// assert_eq!(hello, "Hello, "); |
| 2442 |
/// assert_eq!(world, "World!"); |
| 2443 |
/// ``` |
| 2444 |
#[inline] |
| 2445 |
#[must_use] |
| 2446 |
pub fn split_off(&mut self, at: usize) -> Self { |
| 2447 |
unsafe { Self::from_vec_unchecked(self.inner.split_off(at)) } |
| 2448 |
} |
| 2449 |
|
| 2450 |
/// Creates a draining iterator that removes the specified range in the string and yields the |
| 2451 |
/// removed [`char`]s. |
| 2452 |
/// |
| 2453 |
/// Note: The element range is removed even if the iterator is not consumed until the end. |
| 2454 |
/// |
| 2455 |
/// # Panics |
| 2456 |
/// |
| 2457 |
/// Panics if the starting point or end point are out of bounds. |
| 2458 |
/// |
| 2459 |
/// # Examples |
| 2460 |
/// |
| 2461 |
/// Basic usage: |
| 2462 |
/// |
| 2463 |
/// ``` |
| 2464 |
/// use widestring::Utf32String; |
| 2465 |
/// let mut s = Utf32String::from_str("α is alpha, β is beta"); |
| 2466 |
/// let beta_offset = 12; |
| 2467 |
/// |
| 2468 |
/// // Remove the range up until the β from the string |
| 2469 |
/// let t: Utf32String = s.drain(..beta_offset).collect(); |
| 2470 |
/// assert_eq!(t, "α is alpha, "); |
| 2471 |
/// assert_eq!(s, "β is beta"); |
| 2472 |
/// |
| 2473 |
/// // A full range clears the string |
| 2474 |
/// s.drain(..); |
| 2475 |
/// assert_eq!(s, ""); |
| 2476 |
/// ``` |
| 2477 |
pub fn drain<R>(&mut self, range: R) -> DrainUtf32<'_> |
| 2478 |
where |
| 2479 |
R: RangeBounds<usize>, |
| 2480 |
{ |
| 2481 |
// WARNING: Using range again would be unsound |
| 2482 |
// TODO: replace with core::slice::range when it is stabilized |
| 2483 |
let core::ops::Range { start, end } = crate::range(range, ..self.len()); |
| 2484 |
|
| 2485 |
// Take out two simultaneous borrows. The self_ptr won't be accessed |
| 2486 |
// until iteration is over, in Drop. |
| 2487 |
let self_ptr: *mut _ = self; |
| 2488 |
// SAFETY: `slice::range` and `is_char_boundary` do the appropriate bounds checks. |
| 2489 |
let chars_iter = unsafe { self.get_unchecked(start..end) }.chars(); |
| 2490 |
|
| 2491 |
DrainUtf32 { |
| 2492 |
start, |
| 2493 |
end, |
| 2494 |
iter: chars_iter, |
| 2495 |
string: self_ptr, |
| 2496 |
} |
| 2497 |
} |
| 2498 |
|
| 2499 |
/// Removes the specified range in the string, and replaces it with the given string. |
| 2500 |
/// |
| 2501 |
/// The given string doesn't need to be the same length as the range. |
| 2502 |
/// |
| 2503 |
/// # Panics |
| 2504 |
/// |
| 2505 |
/// Panics if the starting point or end point are out of bounds. |
| 2506 |
/// |
| 2507 |
/// # Examples |
| 2508 |
/// |
| 2509 |
/// Basic usage: |
| 2510 |
/// |
| 2511 |
/// ``` |
| 2512 |
/// use widestring::{utf32str, Utf32String}; |
| 2513 |
/// let mut s = Utf32String::from_str("α is alpha, β is beta"); |
| 2514 |
/// let beta_offset = 12; |
| 2515 |
/// |
| 2516 |
/// // Replace the range up until the β from the string |
| 2517 |
/// s.replace_range(..beta_offset, utf32str!("Α is capital alpha; ")); |
| 2518 |
/// assert_eq!(s, "Α is capital alpha; β is beta"); |
| 2519 |
/// ``` |
| 2520 |
#[inline] |
| 2521 |
pub fn replace_range<R>(&mut self, range: R, replace_with: &Utf32Str) |
| 2522 |
where |
| 2523 |
R: RangeBounds<usize>, |
| 2524 |
{ |
| 2525 |
self.inner |
| 2526 |
.splice(range, replace_with.as_slice().iter().copied()); |
| 2527 |
} |
| 2528 |
|
| 2529 |
/// Converts string into a [`Vec`] of [`char`]s. |
| 2530 |
/// |
| 2531 |
/// This consumes the string without copying its contents. |
| 2532 |
#[allow(trivial_casts)] |
| 2533 |
#[inline] |
| 2534 |
#[must_use] |
| 2535 |
pub fn into_char_vec(self) -> Vec<char> { |
| 2536 |
let mut v = mem::ManuallyDrop::new(self.into_vec()); |
| 2537 |
let (ptr, len, cap) = (v.as_mut_ptr(), v.len(), v.capacity()); |
| 2538 |
// SAFETY: Self should be valid UTF-32 so chars will be in range |
| 2539 |
unsafe { Vec::from_raw_parts(ptr as *mut char, len, cap) } |
| 2540 |
} |
| 2541 |
} |
| 2542 |
|
| 2543 |
impl AsMut<[char]> for Utf32String { |
| 2544 |
#[inline] |
| 2545 |
fn as_mut(&mut self) -> &mut [char] { |
| 2546 |
self.as_char_slice_mut() |
| 2547 |
} |
| 2548 |
} |
| 2549 |
|
| 2550 |
impl AsRef<[char]> for Utf32String { |
| 2551 |
#[inline] |
| 2552 |
fn as_ref(&self) -> &[char] { |
| 2553 |
self.as_char_slice() |
| 2554 |
} |
| 2555 |
} |
| 2556 |
|
| 2557 |
impl From<Vec<char>> for Utf32String { |
| 2558 |
#[inline] |
| 2559 |
fn from(value: Vec<char>) -> Self { |
| 2560 |
Utf32String::from_chars(value) |
| 2561 |
} |
| 2562 |
} |
| 2563 |
|
| 2564 |
impl From<&[char]> for Utf32String { |
| 2565 |
#[inline] |
| 2566 |
fn from(value: &[char]) -> Self { |
| 2567 |
Utf32String::from_chars(value) |
| 2568 |
} |
| 2569 |
} |
| 2570 |
|
| 2571 |
impl From<Utf32String> for Vec<char> { |
| 2572 |
#[inline] |
| 2573 |
fn from(value: Utf32String) -> Self { |
| 2574 |
value.into_char_vec() |
| 2575 |
} |
| 2576 |
} |
| 2577 |
|
| 2578 |
impl PartialEq<[char]> for Utf32String { |
| 2579 |
#[inline] |
| 2580 |
fn eq(&self, other: &[char]) -> bool { |
| 2581 |
self.as_char_slice() == other |
| 2582 |
} |
| 2583 |
} |
| 2584 |
|
| 2585 |
impl PartialEq<Utf16String> for Utf32String { |
| 2586 |
#[inline] |
| 2587 |
fn eq(&self, other: &Utf16String) -> bool { |
| 2588 |
self.chars().eq(other.chars()) |
| 2589 |
} |
| 2590 |
} |
| 2591 |
|
| 2592 |
impl PartialEq<Utf32String> for Utf16String { |
| 2593 |
#[inline] |
| 2594 |
fn eq(&self, other: &Utf32String) -> bool { |
| 2595 |
self.chars().eq(other.chars()) |
| 2596 |
} |
| 2597 |
} |
| 2598 |
|
| 2599 |
impl PartialEq<&Utf16Str> for Utf32String { |
| 2600 |
#[inline] |
| 2601 |
fn eq(&self, other: &&Utf16Str) -> bool { |
| 2602 |
self.chars().eq(other.chars()) |
| 2603 |
} |
| 2604 |
} |
| 2605 |
|
| 2606 |
impl PartialEq<&Utf32Str> for Utf16String { |
| 2607 |
#[inline] |
| 2608 |
fn eq(&self, other: &&Utf32Str) -> bool { |
| 2609 |
self.chars().eq(other.chars()) |
| 2610 |
} |
| 2611 |
} |
| 2612 |
|
| 2613 |
impl PartialEq<Utf32String> for &Utf16Str { |
| 2614 |
#[inline] |
| 2615 |
fn eq(&self, other: &Utf32String) -> bool { |
| 2616 |
self.chars().eq(other.chars()) |
| 2617 |
} |
| 2618 |
} |
| 2619 |
|
| 2620 |
impl PartialEq<Utf16String> for &Utf32Str { |
| 2621 |
#[inline] |
| 2622 |
fn eq(&self, other: &Utf16String) -> bool { |
| 2623 |
self.chars().eq(other.chars()) |
| 2624 |
} |
| 2625 |
} |
| 2626 |
|
| 2627 |
impl TryFrom<Vec<u16>> for Utf16String { |
| 2628 |
type Error = Utf16Error; |
| 2629 |
|
| 2630 |
#[inline] |
| 2631 |
fn try_from(value: Vec<u16>) -> Result<Self, Self::Error> { |
| 2632 |
Utf16String::from_vec(value) |
| 2633 |
} |
| 2634 |
} |
| 2635 |
|
| 2636 |
impl TryFrom<Vec<u32>> for Utf32String { |
| 2637 |
type Error = Utf32Error; |
| 2638 |
|
| 2639 |
#[inline] |
| 2640 |
fn try_from(value: Vec<u32>) -> Result<Self, Self::Error> { |
| 2641 |
Utf32String::from_vec(value) |
| 2642 |
} |
| 2643 |
} |
| 2644 |
|
| 2645 |
impl TryFrom<&[u16]> for Utf16String { |
| 2646 |
type Error = Utf16Error; |
| 2647 |
|
| 2648 |
#[inline] |
| 2649 |
fn try_from(value: &[u16]) -> Result<Self, Self::Error> { |
| 2650 |
Utf16String::from_vec(value) |
| 2651 |
} |
| 2652 |
} |
| 2653 |
|
| 2654 |
impl TryFrom<&[u32]> for Utf32String { |
| 2655 |
type Error = Utf32Error; |
| 2656 |
|
| 2657 |
#[inline] |
| 2658 |
fn try_from(value: &[u32]) -> Result<Self, Self::Error> { |
| 2659 |
Utf32String::from_vec(value) |
| 2660 |
} |
| 2661 |
} |
| 2662 |
|
| 2663 |
/// Alias for [`Utf16String`] or [`Utf32String`] depending on platform. Intended to match typical C |
| 2664 |
/// `wchar_t` size on platform. |
| 2665 |
#[cfg(not(windows))] |
| 2666 |
pub type WideUtfString = Utf32String; |
| 2667 |
|
| 2668 |
/// Alias for [`Utf16String`] or [`Utf32String`] depending on platform. Intended to match typical C |
| 2669 |
/// `wchar_t` size on platform. |
| 2670 |
#[cfg(windows)] |
| 2671 |
pub type WideUtfString = Utf16String; |