Struct zalgo_codec_common::zalgo_string::ZalgoString
source · 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
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.
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());
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!").unwrap();
assert_eq!(zs.as_str(), "È̯͈͂͏͙́");
Iterate through the encoded char
s
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);
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").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);
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!").unwrap();
let es = "É̺͇͌͏̨ͯ̀̀̓ͅ͏͍͓́ͅ";
assert_eq!(zs.to_string(), es);
assert_eq!(zs.into_string(), es);
// println!("{zs}"); // Error: value used after move
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).unwrap();
assert_eq!(s, zs.into_decoded_string());
// println!("{zs}"); // Error: value used after move
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").unwrap();
let bytes = zs.as_bytes();
assert_eq!(bytes[0], 69);
assert_eq!(&bytes[1..5], &[204, 186, 205, 129]);
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").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]);
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").unwrap();
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").unwrap();
assert_eq!(b"Zalgo".to_vec(), zs.into_decoded_bytes());
// println!("{zs}"); // Error: value used after move
sourcepub fn len(&self) -> usize
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);
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).unwrap();
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("").unwrap();
assert!(zs.decoded_is_empty());
let zs = ZalgoString::new("Blargh").unwrap();
assert!(!zs.decoded_is_empty());
Trait Implementations§
source§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
source§impl Hash for ZalgoString
impl Hash for ZalgoString
source§impl<'a> PartialEq<&str> for ZalgoString
impl<'a> PartialEq<&str> for ZalgoString
source§impl<'a> PartialEq<Cow<'a, str>> for ZalgoString
impl<'a> PartialEq<Cow<'a, str>> for ZalgoString
source§impl<'a> PartialEq<String> for ZalgoString
impl<'a> PartialEq<String> for ZalgoString
source§impl PartialEq<ZalgoString> for ZalgoString
impl PartialEq<ZalgoString> 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 ==
.