use core::net::SocketAddr;
use std::borrow::{Borrow, BorrowMut};
use std::cmp;
use std::convert::TryFrom;
use std::fmt;
use std::hash::{Hash, Hasher};
use std::mem::MaybeUninit;
use std::ops::{Deref, DerefMut};
#[cfg(feature = "std")]
use std::path::Path;
use std::ptr;
use std::slice;
use std::str;
use std::str::FromStr;
use std::str::Utf8Error;
use crate::char::encode_utf8;
use crate::utils::MakeMaybeUninit;
use crate::CapacityError;
use crate::LenUint;
#[cfg(feature = "serde")]
use serde::{Deserialize, Deserializer, Serialize, Serializer};
/// A string with a fixed capacity.
///
/// The `ArrayString` is a string backed by a fixed size array. It keeps track
/// of its length, and is parameterized by `CAP` for the maximum capacity.
///
/// `CAP` is of type `usize` but is range limited to `u32::MAX` (or `u16` on 16-bit targets);
/// attempting to create larger arrayvecs with larger capacity will panic.
///
/// The string is a contiguous value that you can store directly on the stack
/// if needed.
#[derive(Copy)]
#[repr(C)]
pub struct ArrayString {
// the `len` first elements of the array are initialized
len: LenUint,
xs: [MaybeUninit; CAP],
}
impl Default for ArrayString {
/// Return an empty `ArrayString`
fn default() -> ArrayString {
ArrayString::new()
}
}
impl ArrayString {
/// Create a new empty `ArrayString`.
///
/// Capacity is inferred from the type parameter.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::::new();
/// string.push_str("foo");
/// assert_eq!(&string[..], "foo");
/// assert_eq!(string.capacity(), 16);
/// ```
pub fn new() -> ArrayString {
assert_capacity_limit!(CAP);
unsafe {
ArrayString {
xs: MaybeUninit::uninit().assume_init(),
len: 0,
}
}
}
/// Create a new empty `ArrayString` (const fn).
///
/// Capacity is inferred from the type parameter.
///
/// ```
/// use arrayvec::ArrayString;
///
/// static ARRAY: ArrayString = ArrayString::new_const();
/// ```
pub const fn new_const() -> ArrayString {
assert_capacity_limit_const!(CAP);
ArrayString {
xs: MakeMaybeUninit::ARRAY,
len: 0,
}
}
/// Return the length of the string.
#[inline]
pub const fn len(&self) -> usize {
self.len as usize
}
/// Returns whether the string is empty.
#[inline]
pub const fn is_empty(&self) -> bool {
self.len() == 0
}
/// Create a new `ArrayString` from a `str`.
///
/// Capacity is inferred from the type parameter.
///
/// **Errors** if the backing array is not large enough to fit the string.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::::from("foo").unwrap();
/// assert_eq!(&string[..], "foo");
/// assert_eq!(string.len(), 3);
/// assert_eq!(string.capacity(), 3);
/// ```
pub fn from(s: &str) -> Result {
let mut arraystr = Self::new();
arraystr.try_push_str(s)?;
Ok(arraystr)
}
/// Create a new `ArrayString` from a byte string literal.
///
/// **Errors** if the byte string literal is not valid UTF-8.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let string = ArrayString::from_byte_string(b"hello world").unwrap();
/// ```
pub fn from_byte_string(b: &[u8; CAP]) -> Result {
let len = str::from_utf8(b)?.len();
debug_assert_eq!(len, CAP);
let mut vec = Self::new();
unsafe {
(b as *const [u8; CAP] as *const [MaybeUninit; CAP])
.copy_to_nonoverlapping(&mut vec.xs as *mut [MaybeUninit; CAP], 1);
vec.set_len(CAP);
}
Ok(vec)
}
/// Create a new `ArrayString` value fully filled with ASCII NULL characters (`\0`). Useful
/// to be used as a buffer to collect external data or as a buffer for intermediate processing.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let string = ArrayString::::zero_filled();
/// assert_eq!(string.len(), 16);
/// ```
#[inline]
pub fn zero_filled() -> Self {
assert_capacity_limit!(CAP);
// SAFETY: `assert_capacity_limit` asserts that `len` won't overflow and
// `zeroed` fully fills the array with nulls.
unsafe {
ArrayString {
xs: MaybeUninit::zeroed().assume_init(),
len: CAP as _,
}
}
}
/// Return the capacity of the `ArrayString`.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let string = ArrayString::::new();
/// assert_eq!(string.capacity(), 3);
/// ```
#[inline(always)]
pub const fn capacity(&self) -> usize {
CAP
}
/// Return if the `ArrayString` is completely filled.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::::new();
/// assert!(!string.is_full());
/// string.push_str("A");
/// assert!(string.is_full());
/// ```
pub const fn is_full(&self) -> bool {
self.len() == self.capacity()
}
/// Returns the capacity left in the `ArrayString`.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::::from("abc").unwrap();
/// string.pop();
/// assert_eq!(string.remaining_capacity(), 1);
/// ```
pub const fn remaining_capacity(&self) -> usize {
self.capacity() - self.len()
}
/// Adds the given char to the end of the string.
///
/// ***Panics*** if the backing array is not large enough to fit the additional char.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::::new();
///
/// string.push('a');
/// string.push('b');
///
/// assert_eq!(&string[..], "ab");
/// ```
#[track_caller]
pub fn push(&mut self, c: char) {
self.try_push(c).unwrap();
}
/// Adds the given char to the end of the string.
///
/// Returns `Ok` if the push succeeds.
///
/// **Errors** if the backing array is not large enough to fit the additional char.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::::new();
///
/// string.try_push('a').unwrap();
/// string.try_push('b').unwrap();
/// let overflow = string.try_push('c');
///
/// assert_eq!(&string[..], "ab");
/// assert_eq!(overflow.unwrap_err().element(), 'c');
/// ```
pub fn try_push(&mut self, c: char) -> Result {
let len = self.len();
unsafe {
let ptr = self.as_mut_ptr().add(len);
let remaining_cap = self.capacity() - len;
match encode_utf8(c, ptr, remaining_cap) {
Ok(n) => {
self.set_len(len + n);
Ok(())
}
Err(_) => Err(CapacityError::new(c)),
}
}
}
/// Adds the given string slice to the end of the string.
///
/// ***Panics*** if the backing array is not large enough to fit the string.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::::new();
///
/// string.push_str("a");
/// string.push_str("d");
///
/// assert_eq!(&string[..], "ad");
/// ```
#[track_caller]
pub fn push_str(&mut self, s: &str) {
self.try_push_str(s).unwrap()
}
/// Adds the given string slice to the end of the string.
///
/// Returns `Ok` if the push succeeds.
///
/// **Errors** if the backing array is not large enough to fit the string.
///
/// ```
/// use arrayvec::ArrayString;
///
/// let mut string = ArrayString::::new();
///
/// string.try_push_str("a").unwrap();
/// let overflow1 = string.try_push_str("bc");
/// string.try_push_str("d").unwrap();
/// let overflow2 = string.try_push_str("ef");
///
/// assert_eq!(&string[..], "ad");
/// assert_eq!(overflow1.unwrap_err().element(), "bc");
/// assert_eq!(overflow2.unwrap_err().element(), "ef");
/// ```
pub fn try_push_str Result char {
let ch = match self[idx..].chars().next() {
Some(ch) => ch,
None => panic!("cannot remove a char from the end of a string"),
};
let next = idx + ch.len_utf8();
let len = self.len();
let ptr = self.as_mut_ptr();
unsafe {
ptr::copy(ptr.add(next), ptr.add(idx), len - next);
self.set_len(len - (next - idx));
}
ch
}
/// Make the string empty.
pub fn clear(&mut self) {
unsafe {
self.set_len(0);
}
}
/// Set the stringss length.
///
/// This function is `unsafe` because it changes the notion of the
/// number of valid bytes in the string. Use with care.
///
/// This method uses *debug assertions* to check the validity of `length`
/// and may use other debug assertions.
pub unsafe fn set_len(&mut self, length: usize) {
// type invariant that capacity always fits in LenUint
debug_assert!(length &str {
self
}
/// Return a mutable string slice of the whole `ArrayString`.
pub fn as_mut_str(&mut self) -> &mut str {
self
}
/// Return a raw pointer to the string's buffer.
pub fn as_ptr(&self) -> *const u8 {
self.xs.as_ptr() as *const u8
}
/// Return a raw mutable pointer to the string's buffer.
pub fn as_mut_ptr(&mut self) -> *mut u8 {
self.xs.as_mut_ptr() as *mut u8
}
}
impl Deref for ArrayString {
type Target = str;
#[inline]
fn deref(&self) -> &str {
unsafe {
let sl = slice::from_raw_parts(self.as_ptr(), self.len());
str::from_utf8_unchecked(sl)
}
}
}
impl DerefMut for ArrayString {
#[inline]
fn deref_mut(&mut self) -> &mut str {
unsafe {
let len = self.len();
let sl = slice::from_raw_parts_mut(self.as_mut_ptr(), len);
str::from_utf8_unchecked_mut(sl)
}
}
}
impl PartialEq for ArrayString {
fn eq(&self, rhs: &Self) -> bool {
**self == **rhs
}
}
impl PartialEq for ArrayString {
fn eq(&self, rhs: &str) -> bool {
&**self == rhs
}
}
impl PartialEq for str {
fn eq(&self, rhs: &ArrayString) -> bool {
self == &**rhs
}
}
impl Eq for ArrayString {}
impl Hash for ArrayString {
fn hash(&self, h: &mut H) {
(**self).hash(h)
}
}
impl Borrow for ArrayString {
fn borrow(&self) -> &str {
self
}
}
impl BorrowMut for ArrayString {
fn borrow_mut(&mut self) -> &mut str {
self
}
}
impl AsRef for ArrayString {
fn as_ref(&self) -> &str {
self
}
}
impl fmt::Debug for ArrayString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(**self).fmt(f)
}
}
#[cfg(feature = "std")]
impl AsRef for ArrayString {
fn as_ref(&self) -> &Path {
self.as_str().as_ref()
}
}
impl fmt::Display for ArrayString {
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
(**self).fmt(f)
}
}
/// `Write` appends written data to the end of the string.
impl fmt::Write for ArrayString {
fn write_char(&mut self, c: char) -> fmt::Result {
self.try_push(c).map_err(|_| fmt::Error)
}
fn write_str(&mut self, s: &str) -> fmt::Result {
self.try_push_str(s).map_err(|_| fmt::Error)
}
}
impl Clone for ArrayString {
fn clone(&self) -> ArrayString {
*self
}
fn clone_from(&mut self, rhs: &Self) {
// guaranteed to fit due to types matching.
self.clear();
self.try_push_str(rhs).ok();
}
}
impl PartialOrd for ArrayString {
fn partial_cmp(&self, rhs: &Self) -> Option {
(**self).partial_cmp(&**rhs)
}
fn lt(&self, rhs: &Self) -> bool {
**self < **rhs
}
fn le(&self, rhs: &Self) -> bool {
**self bool {
**self > **rhs
}
fn ge(&self, rhs: &Self) -> bool {
**self >= **rhs
}
}
impl PartialOrd for ArrayString {
fn partial_cmp(&self, rhs: &str) -> Option {
(**self).partial_cmp(rhs)
}
fn lt(&self, rhs: &str) -> bool {
&**self < rhs
}
fn le(&self, rhs: &str) -> bool {
&**self bool {
&**self > rhs
}
fn ge(&self, rhs: &str) -> bool {
&**self >= rhs
}
}
impl PartialOrd for str {
fn partial_cmp(&self, rhs: &ArrayString) -> Option {
self.partial_cmp(&**rhs)
}
fn lt(&self, rhs: &ArrayString) -> bool {
self < &**rhs
}
fn le(&self, rhs: &ArrayString) -> bool {
self bool {
self > &**rhs
}
fn ge(&self, rhs: &ArrayString) -> bool {
self >= &**rhs
}
}
impl Ord for ArrayString {
fn cmp(&self, rhs: &Self) -> cmp::Ordering {
(**self).cmp(&**rhs)
}
}
impl FromStr for ArrayString {
type Err = CapacityError;
fn from_str(s: &str) -> Result {
Self::from(s).map_err(CapacityError::simplify)
}
}
#[cfg(feature = "serde")]
/// Requires crate feature `"serde"`
impl Serialize for ArrayString {
fn serialize(&self, serializer: S) -> Result
where
S: Serializer,
{
serializer.serialize_str(&*self)
}
}
#[cfg(feature = "serde")]
/// Requires crate feature `"serde"`
impl for ArrayString {
fn deserialize(deserializer: D) -> Result
where
D: Deserializer Visitor TryFrom;
fn try_from(f: &'a str) -> Result {
let mut v = Self::new();
v.try_push_str(f)?;
Ok(v)
}
}
impl> for ArrayString {
type Error = CapacityError;
fn try_from(f: fmt::Arguments