pub struct ZalgoString(/* private fields */);
Expand description

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 feature is enabled this struct implements 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.

Errors

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")?, "É̺͇͌͏");

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!")?;
assert_eq!(zs.as_str(), "È̯͈͂͏͙́");

Note that ZalgoString implements PartialEq with common string types, so the comparison in the above example could also be done directly

assert_eq!(zs, "È̯͈͂͏͙́");
source

pub fn chars(&self) -> Chars<'_>

Returns an iterator over the encoded characters of the ZalgoString.

The first character is an “E”, the others are unicode combining characters.

Example

Iterate through the encoded chars:

let zs = ZalgoString::new("42")?;
let mut chars = zs.chars();
assert_eq!(chars.next(), Some('E'));
assert_eq!(chars.next(), Some('\u{314}'));
source

pub fn char_indices(&self) -> CharIndices<'_>

Returns an iterator over the encoded characters of the ZalgoString and their positions.

Example

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")?;
let mut ci = zs.char_indices();
assert_eq!(ci.next(), Some((0, 'E')));
assert_eq!(ci.next(), Some((1,'\u{33a}')));
// Note the 3 here, the combining characters take up two 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")?;
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!")?;
assert_eq!(zs.into_string(), "É̺͇͌͏̨ͯ̀̀̓ͅ͏͍͓́ͅ");
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)?;
assert_eq!(s, zs.into_decoded_string());
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")?;
let bytes = zs.as_bytes();
assert_eq!(bytes[0], 69);
assert_eq!(&bytes[1..5], &[204, 186, 205, 129]);
source

pub fn bytes(&self) -> Bytes<'_>

Returns an iterator over the encoded bytes of the ZalgoString.

Since a ZalgoString always begins with an “E”, the first byte is always 69. After that the bytes no longer correspond to ASCII values.

Example

Basic usage

let zs = ZalgoString::new("Bytes")?;
let mut bytes = zs.bytes();
assert_eq!(bytes.next(), Some(69));
assert_eq!(bytes.nth(5), Some(148));
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")?;
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")?;
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")?;
assert_eq!(b"Zalgo".to_vec(), zs.into_decoded_bytes());
source

pub fn len(&self) -> usize

Returns the length of self in bytes.

This length is twice the length of the original String plus one.

Example

Basic usage

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

pub fn capacity(&self) -> usize

Returns the capacity of the underlying encoded string in bytes.

The ZalgoString is preallocated to the needed capacity of twice the length of the original unencoded String plus one. However, this size is not guaranteed since the allocator can choose to allocate more space.

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)?;
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("")?;
assert!(zs.decoded_is_empty());
let zs = ZalgoString::new("Blargh")?;
assert!(!zs.decoded_is_empty());
source

pub fn push_zalgo_str(&mut self, zalgo_string: &ZalgoString)

Appends the combining characters of a different ZalgoString to the end of self.

Example
let (s1, s2) = ("Zalgo", ", He comes!");

let mut zs1 = ZalgoString::new(s1)?;
let zs2 = ZalgoString::new(s2)?;

zs1.push_zalgo_str(&zs2);

assert_eq!(zs1.into_decoded_string(), format!("{s1}{s2}"));
source

pub fn as_combining_chars(&self) -> &str

Returns a string slice of just the combining characters of the ZalgoString without the inital ‘E’.

Note that zalgo_decode assumes that the initial ‘E’ is present, and can not decode the result of this method.

Example
let zs = ZalgoString::new("Hi")?;
assert_eq!(zs.as_combining_chars(), "\u{328}\u{349}");

Trait Implementations§

source§

impl Add<&ZalgoString> for ZalgoString

Implements the + operator for concaternating two ZalgoStrings. Memorywise it works the same as the Add implementation for the normal String type: it consumes the lefthand side, extends its buffer, and copies the combining characters of the right hand side into it.

§

type Output = ZalgoString

The resulting type after applying the + operator.
source§

fn add(self, rhs: &Self) -> Self::Output

Performs the + operation. Read more
source§

impl AddAssign<&ZalgoString> for ZalgoString

Implements the += operator for appending to a ZalgoString.

This just calls push_zalgo_str.

source§

fn add_assign(&mut self, rhs: &ZalgoString)

Performs the += operation. Read more
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

Displays the encoded form of the 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 Ord for ZalgoString

source§

fn cmp(&self, other: &ZalgoString) -> Ordering

This method returns an Ordering between self and other. Read more
1.21.0 · source§

fn max(self, other: Self) -> Self
where Self: Sized,

Compares and returns the maximum of two values. Read more
1.21.0 · source§

fn min(self, other: Self) -> Self
where Self: Sized,

Compares and returns the minimum of two values. Read more
1.50.0 · source§

fn clamp(self, min: Self, max: Self) -> Self
where Self: Sized + PartialOrd,

Restrict a value to a certain interval. Read more
source§

impl 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 PartialEq<Cow<'_, str>> for ZalgoString

source§

fn eq(&self, other: &Cow<'_, 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 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 &str

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 PartialEq<ZalgoString> for Cow<'_, str>

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 PartialEq<ZalgoString> for String

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 PartialEq<ZalgoString> for str

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 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 PartialEq 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 PartialOrd for ZalgoString

source§

fn partial_cmp(&self, other: &ZalgoString) -> Option<Ordering>

This method returns an ordering between self and other values if one exists. Read more
1.0.0 · source§

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

This method tests less than (for self and other) and is used by the < operator. Read more
1.0.0 · source§

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

This method tests less than or equal to (for self and other) and is used by the <= operator. Read more
1.0.0 · source§

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

This method tests greater than (for self and other) and is used by the > operator. Read more
1.0.0 · source§

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

This method tests greater than or equal to (for self and other) and is used by the >= operator. Read more
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 Eq for ZalgoString

source§

impl StructuralEq for ZalgoString

source§

impl StructuralPartialEq for ZalgoString

Auto Trait Implementations§

Blanket Implementations§

source§

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

source§

fn type_id(&self) -> TypeId

Gets the TypeId of self. Read more
source§

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

source§

fn borrow(&self) -> &T

Immutably borrows from an owned value. Read more
source§

impl<T> BorrowMut<T> for T
where 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 T
where 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 T
where 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 T
where 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 T
where 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 T
where 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 T
where T: for<'de> Deserialize<'de>,