Struct zalgo_codec_common::zalgo_string::ZalgoString
source · 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
impl ZalgoString
sourcepub fn new(s: &str) -> Result<Self, Error>
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());
sourcepub fn as_str(&self) -> &str
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, "È̯͈͂͏͙́");
sourcepub fn chars(&self) -> Chars<'_>
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 char
s:
let zs = ZalgoString::new("42")?;
let mut chars = zs.chars();
assert_eq!(chars.next(), Some('E'));
assert_eq!(chars.next(), Some('\u{314}'));
sourcepub fn char_indices(&self) -> CharIndices<'_>
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);
sourcepub fn decoded_chars(&self) -> DecodedChars<'_> ⓘ
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);
sourcepub fn into_string(self) -> String
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(), "É̺͇͌͏̨ͯ̀̀̓ͅ͏͍͓́ͅ");
sourcepub fn into_decoded_string(self) -> String
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());
sourcepub fn as_bytes(&self) -> &[u8] ⓘ
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]);
sourcepub fn bytes(&self) -> Bytes<'_>
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));
sourcepub fn decoded_bytes(&self) -> DecodedBytes<'_> ⓘ
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]);
sourcepub fn into_bytes(self) -> Vec<u8>
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]);
sourcepub fn into_decoded_bytes(self) -> Vec<u8>
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());
sourcepub fn len(&self) -> usize
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);
sourcepub fn capacity(&self) -> usize
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.
sourcepub fn decoded_len(&self) -> usize
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());
sourcepub fn decoded_is_empty(&self) -> bool
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());
sourcepub fn push_zalgo_str(&mut self, zalgo_string: &ZalgoString)
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}"));
sourcepub fn as_combining_chars(&self) -> &str
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
impl Add<&ZalgoString> for ZalgoString
Implements the +
operator for concaternating two ZalgoString
s.
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.
source§impl AddAssign<&ZalgoString> for ZalgoString
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)
fn add_assign(&mut self, rhs: &ZalgoString)
+=
operation. Read moresource§impl Clone for ZalgoString
impl Clone for ZalgoString
source§fn clone(&self) -> ZalgoString
fn clone(&self) -> ZalgoString
1.0.0 · source§fn clone_from(&mut self, source: &Self)
fn clone_from(&mut self, source: &Self)
source
. Read moresource§impl Debug for ZalgoString
impl Debug for ZalgoString
source§impl<'de> Deserialize<'de> for ZalgoString
impl<'de> Deserialize<'de> for ZalgoString
source§fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
fn deserialize<__D>(__deserializer: __D) -> Result<Self, __D::Error>where
__D: Deserializer<'de>,
source§impl Display for ZalgoString
impl Display for ZalgoString
Displays the encoded form of the ZalgoString
.
source§impl Hash for ZalgoString
impl Hash for ZalgoString
source§impl Ord for ZalgoString
impl Ord for ZalgoString
source§fn cmp(&self, other: &ZalgoString) -> Ordering
fn cmp(&self, other: &ZalgoString) -> Ordering
1.21.0 · source§fn max(self, other: Self) -> Selfwhere
Self: Sized,
fn max(self, other: Self) -> Selfwhere
Self: Sized,
source§impl PartialEq<&str> for ZalgoString
impl PartialEq<&str> for ZalgoString
source§impl PartialEq<Cow<'_, str>> for ZalgoString
impl PartialEq<Cow<'_, str>> for ZalgoString
source§impl PartialEq<String> for ZalgoString
impl PartialEq<String> for ZalgoString
source§impl PartialEq<ZalgoString> for &str
impl PartialEq<ZalgoString> for &str
source§fn eq(&self, other: &ZalgoString) -> bool
fn eq(&self, other: &ZalgoString) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<ZalgoString> for Cow<'_, str>
impl PartialEq<ZalgoString> for Cow<'_, str>
source§fn eq(&self, other: &ZalgoString) -> bool
fn eq(&self, other: &ZalgoString) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<ZalgoString> for String
impl PartialEq<ZalgoString> for String
source§fn eq(&self, other: &ZalgoString) -> bool
fn eq(&self, other: &ZalgoString) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<ZalgoString> for str
impl PartialEq<ZalgoString> for str
source§fn eq(&self, other: &ZalgoString) -> bool
fn eq(&self, other: &ZalgoString) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialEq<str> for ZalgoString
impl PartialEq<str> for ZalgoString
source§impl PartialEq for ZalgoString
impl PartialEq for ZalgoString
source§fn eq(&self, other: &ZalgoString) -> bool
fn eq(&self, other: &ZalgoString) -> bool
self
and other
values to be equal, and is used
by ==
.source§impl PartialOrd for ZalgoString
impl PartialOrd for ZalgoString
source§fn partial_cmp(&self, other: &ZalgoString) -> Option<Ordering>
fn partial_cmp(&self, other: &ZalgoString) -> Option<Ordering>
1.0.0 · source§fn le(&self, other: &Rhs) -> bool
fn le(&self, other: &Rhs) -> bool
self
and other
) and is used by the <=
operator. Read more