pub struct ZalgoString { /* private fields */ }
Expand description

A thin wrapper around a String that has been encoded with zalgo_encode. This struct can be decoded in-place and also allows iteration over its characters and bytes, both in decoded and encoded form.

If the serde_support feature is enabled this struct derives the Serialize and Deserialize traits.

Implementations§

source§

impl ZalgoString

source

pub fn new(s: &str) -> Result<Self, Error>

Encodes the given string slice with zalgo_encode and stores the result in a new allocation. Returns an error if the input string contains bytes that don’t correspond to printable ASCII characters or newlines.

Examples
assert_eq!(ZalgoString::new("Zalgo").unwrap(), "É̺͇͌͏");

Can only encode printable ASCII and newlines:

assert!(ZalgoString::new("❤️").is_err());
assert!(ZalgoString::new("\r").is_err());
source

pub fn as_str(&self) -> &str

Returns the encoded contents of self as a string slice.

Example

Basic usage

let zs = ZalgoString::new("Oh boy!").unwrap();
assert_eq!(zs.as_str(), "È̯͈͂͏͙́");

Iterate through the encoded chars

let zs = ZalgoString::new("42").unwrap();
let mut chars = zs.as_str().chars();
// A `ZalgoString` always begins with an 'E'
assert_eq!(chars.next(), Some('E'));
// After that it gets weird
assert_eq!(chars.next(), Some('\u{314}'));

Combining characters lie deep in the dark depths of Unicode, and may not match with your intuition of what a character is.

let zs = ZalgoString::new("Zalgo").unwrap();
let mut ci = zs.as_str().char_indices();
assert_eq!(ci.next(), Some((0, 'E')));
assert_eq!(ci.next(), Some((1,'\u{33a}')));
// Note the 3 here, the combining characters can take up multiple bytes.
assert_eq!(ci.next(), Some((3, '\u{341}')));
// The final character begins at position 9
assert_eq!(ci.next_back(), Some((9, '\u{34f}')));
// even though the length in bytes is 11
assert_eq!(zs.len(), 11);
source

pub fn decoded_chars(&self) -> DecodedChars<'_>

Returns an iterator over the decoded characters of the ZalgoString. These characters are guaranteed to be valid ASCII.

Example
let zs = ZalgoString::new("Zlgoa").unwrap();
let mut decoded_chars = zs.decoded_chars();
assert_eq!(decoded_chars.next(), Some('Z'));
assert_eq!(decoded_chars.next_back(), Some('a'));
assert_eq!(decoded_chars.next(), Some('l'));
assert_eq!(decoded_chars.next(), Some('g'));
assert_eq!(decoded_chars.next_back(), Some('o'));
assert_eq!(decoded_chars.next(), None);
assert_eq!(decoded_chars.next_back(), None);
source

pub fn into_string(self) -> String

Converts self into a String. This simply returns the underlying String without any cloning or decoding.

Example

Basic usage

let zs = ZalgoString::new("Zalgo\n He comes!").unwrap();
let es = "É̺͇͌͏̨ͯ̀̀̓ͅ͏͍͓́ͅ";
assert_eq!(zs.to_string(), es);
assert_eq!(zs.into_string(), es);
// println!("{zs}"); // Error: value used after move
source

pub fn into_decoded_string(self) -> String

Decodes self into a String in-place. This method has no effect on the allocated capacity.

Example

Basic usage

let s = "Zalgo";
let zs = ZalgoString::new(s).unwrap();
assert_eq!(s, zs.into_decoded_string());
// println!("{zs}"); // Error: value used after move
source

pub fn as_bytes(&self) -> &[u8]

Returns the encoded contents of self as a byte slice. The first byte is always 69, after that the bytes no longer correspond to ASCII characters.

Example

Basic usage

let zs = ZalgoString::new("Zalgo").unwrap();
let bytes = zs.as_bytes();
assert_eq!(bytes[0], 69);
assert_eq!(&bytes[1..5], &[204, 186, 205, 129]);
source

pub fn decoded_bytes(&self) -> DecodedBytes<'_>

Returns an iterator over the decoded bytes of the ZalgoString. These bytes are guaranteed to represent valid ASCII.

Example
let zs = ZalgoString::new("Zalgo").unwrap();
let mut decoded_bytes = zs.decoded_bytes();
assert_eq!(decoded_bytes.next(), Some(90));
assert_eq!(decoded_bytes.next_back(), Some(111));
assert_eq!(decoded_bytes.collect::<Vec<u8>>(), vec![97, 108, 103]);
source

pub fn into_bytes(self) -> Vec<u8>

Converts self into a byte vector. This simply returns the underlying buffer without any cloning or decoding.

Example

Basic usage

let zs = ZalgoString::new("Zalgo").unwrap();
assert_eq!(zs.into_bytes(), vec![69, 204, 186, 205, 129, 205, 140, 205, 135, 205, 143]);
source

pub fn into_decoded_bytes(self) -> Vec<u8>

Decodes self into a byte vector in-place. This method has no effect on the allocated capacity.

Example

Basic usage

let zs = ZalgoString::new("Zalgo").unwrap();
assert_eq!(b"Zalgo".to_vec(), zs.into_decoded_bytes());
// println!("{zs}"); // Error: value used after move
source

pub fn len(&self) -> usize

Returns the length of self in bytes. The allocated capacity is the same. This length is twice the length of the original String plus one.

Example

Basic usage

let zs = ZalgoString::new("Z").unwrap();
assert_eq!(zs.len(), 3);
source

pub fn decoded_len(&self) -> usize

Returns the length of the ZalgoString in bytes if it were to be decoded.
This is computed without any decoding.

Example

Basic usage

let s = "Zalgo, He comes!";
let zs = ZalgoString::new(s).unwrap();
assert_eq!(s.len(), zs.decoded_len());
source

pub fn decoded_is_empty(&self) -> bool

Returns whether the string would be empty if decoded.

Example

Basic usage

let zs = ZalgoString::new("").unwrap();
assert!(zs.decoded_is_empty());
let zs = ZalgoString::new("Blargh").unwrap();
assert!(!zs.decoded_is_empty());

Trait Implementations§

source§

impl Clone for ZalgoString

source§

fn clone(&self) -> ZalgoString

Returns a copy of the value. Read more
1.0.0 · source§

fn clone_from(&mut self, source: &Self)

Performs copy-assignment from source. Read more
source§

impl Debug for ZalgoString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl<'de> Deserialize<'de> for ZalgoString

source§

fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where __D: Deserializer<'de>,

Deserialize this value from the given Serde deserializer. Read more
source§

impl Display for ZalgoString

source§

fn fmt(&self, f: &mut Formatter<'_>) -> Result

Formats the value using the given formatter. Read more
source§

impl Hash for ZalgoString

source§

fn hash<__H: Hasher>(&self, state: &mut __H)

Feeds this value into the given Hasher. Read more
1.3.0 · source§

fn hash_slice<H>(data: &[Self], state: &mut H)where H: Hasher, Self: Sized,

Feeds a slice of this type into the given Hasher. Read more
source§

impl<'a> PartialEq<&str> for ZalgoString

source§

fn eq(&self, other: &&str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<Cow<'a, str>> for ZalgoString

source§

fn eq(&self, other: &Cow<'a, str>) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<String> for ZalgoString

source§

fn eq(&self, other: &String) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl PartialEq<ZalgoString> for ZalgoString

source§

fn eq(&self, other: &ZalgoString) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl<'a> PartialEq<str> for ZalgoString

source§

fn eq(&self, other: &str) -> bool

This method tests for self and other values to be equal, and is used by ==.
1.0.0 · source§

fn ne(&self, other: &Rhs) -> bool

This method tests for !=. The default implementation is almost always sufficient, and should not be overridden without very good reason.
source§

impl Serialize for ZalgoString

source§

fn serialize<__S>(&self, __serializer: __S) -> Result<__S::Ok, __S::Error>where __S: Serializer,

Serialize this value into the given Serde serializer. Read more
source§

impl StructuralPartialEq for ZalgoString

Auto Trait Implementations§

Blanket Implementations§

source§

impl<T> Any for Twhere T: 'static + ?Sized,

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

impl<T> Borrow<T> for Twhere T: ?Sized,

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for Twhere T: ?Sized,

source§

fn borrow_mut(&mut self) -> &mut T

Mutably borrows from an owned value. Read more
source§

impl<T> From<T> for T

source§

fn from(t: T) -> T

Returns the argument unchanged.

source§

impl<T, U> Into<U> for Twhere U: From<T>,

source§

fn into(self) -> U

Calls U::from(self).

That is, this conversion is whatever the implementation of From<T> for U chooses to do.

source§

impl<T> ToOwned for Twhere T: Clone,

§

type Owned = T

The resulting type after obtaining ownership.
source§

fn to_owned(&self) -> T

Creates owned data from borrowed data, usually by cloning. Read more
source§

fn clone_into(&self, target: &mut T)

Uses borrowed data to replace owned data, usually by cloning. Read more
source§

impl<T> ToString for Twhere T: Display + ?Sized,

source§

default fn to_string(&self) -> String

Converts the given value to a String. Read more
source§

impl<T, U> TryFrom<U> for Twhere U: Into<T>,

§

type Error = Infallible

The type returned in the event of a conversion error.
source§

fn try_from(value: U) -> Result<T, <T as TryFrom<U>>::Error>

Performs the conversion.
source§

impl<T, U> TryInto<U> for Twhere U: TryFrom<T>,

§

type Error = <U as TryFrom<T>>::Error

The type returned in the event of a conversion error.
source§

fn try_into(self) -> Result<U, <U as TryFrom<T>>::Error>

Performs the conversion.
source§

impl<T> DeserializeOwned for Twhere T: for<'de> Deserialize<'de>,