1
  2
  3
  4
  5
  6
  7
  8
  9
 10
 11
 12
 13
 14
 15
 16
 17
 18
 19
 20
 21
 22
 23
 24
 25
 26
 27
 28
 29
 30
 31
 32
 33
 34
 35
 36
 37
 38
 39
 40
 41
 42
 43
 44
 45
 46
 47
 48
 49
 50
 51
 52
 53
 54
 55
 56
 57
 58
 59
 60
 61
 62
 63
 64
 65
 66
 67
 68
 69
 70
 71
 72
 73
 74
 75
 76
 77
 78
 79
 80
 81
 82
 83
 84
 85
 86
 87
 88
 89
 90
 91
 92
 93
 94
 95
 96
 97
 98
 99
100
101
102
//! Unicode string slices.
//!
//! *[See also the `str` primitive type][str].*
//!
//! The `&str` type is one of the two main string types, the other being `String`.
//! Unlike its `String` counterpart, its contents are borrowed.
//!
//! # Basic Usage
//!
//! A basic string declaration of `&str` type:
//!
//! ```
//! # #![allow(unused_variables)]
//! let hello_world = "Hello, World!";
//! ```
//!
//! Here we have declared a string literal, also known as a string slice.
//! String literals have a static lifetime, which means the string `hello_world`
//! is guaranteed to be valid for the duration of the entire program.
//! We can explicitly specify `hello_world`'s lifetime as well:
//!
//! ```
//! # #![allow(unused_variables)]
//! let hello_world: &'static str = "Hello, world!";
//! ```
//!
//! [str]: https://doc.rust-lang.org/nightly/std/primitive.str.html

// pub use core::str::pattern;
use crate::{
    alloc::AllocRef,
    borrow::{Borrow, BorrowMut},
    boxed::Box,
    string::String,
};
#[allow(deprecated)]
pub use liballoc::str::LinesAny;
pub use liballoc::str::{
    from_utf8,
    from_utf8_mut,
    from_utf8_unchecked,
    from_utf8_unchecked_mut,
    Bytes,
    CharIndices,
    Chars,
    EncodeUtf16,
    EscapeDebug,
    EscapeDefault,
    EscapeUnicode,
    FromStr,
    Lines,
    MatchIndices,
    Matches,
    ParseBoolError,
    RMatchIndices,
    RMatches,
    RSplit,
    RSplitN,
    RSplitTerminator,
    Split,
    SplitAsciiWhitespace,
    SplitN,
    SplitTerminator,
    SplitWhitespace,
    Utf8Error,
};

impl<D: AllocRef> Borrow<str> for String<D> {
    #[inline]
    fn borrow(&self) -> &str {
        &self[..]
    }
}

impl<D: AllocRef> BorrowMut<str> for String<D> {
    #[inline]
    fn borrow_mut(&mut self) -> &mut str {
        &mut self[..]
    }
}

/// Converts a boxed slice of bytes to a boxed string slice without checking
/// that the string contains valid UTF-8.
///
/// # Examples
///
/// Basic usage:
///
/// ```
/// use alloc_wg::boxed::Box;
///
/// let smile_utf8 = Box::new([226, 152, 186]);
/// let smile = unsafe { alloc_wg::str::from_boxed_utf8_unchecked(smile_utf8) };
///
/// assert_eq!("☺", &*smile);
/// ```
#[allow(clippy::missing_safety_doc)]
#[inline]
pub unsafe fn from_boxed_utf8_unchecked<A: AllocRef>(v: Box<[u8], A>) -> Box<str, A> {
    let a = core::ptr::read(v.build_alloc());
    Box::from_raw_in(Box::into_raw(v) as *mut str, a)
}