decode

Function decode 

Source
pub fn decode(content: &[Byte]) -> DecodedData<'_>
Expand description

Decode html entities in utf-8 bytes.

ยงExamples

use htmlentity::entity::*;
use htmlentity::types::AnyhowResult;
let html = "<div class='header'></div>";
let orig_bytes = html.as_bytes();
let encoded_data = encode(orig_bytes, &EncodeType::Named, &CharacterSet::SpecialChars);
let encode_bytes = encoded_data.to_bytes();
assert_eq!(encode_bytes, b"&lt;div class=&apos;header&apos;&gt;&lt;/div&gt;");
// decode the bytes
let decoded_data = decode(&encode_bytes);
let data_to_string = decoded_data.to_string();
assert!(data_to_string.is_ok());
assert_eq!(data_to_string?, String::from(html));
// Convert encoded data to Vec<char>.
let data_to_chars = decoded_data.to_chars();
assert!(data_to_chars.is_ok());
assert_eq!(data_to_chars?, String::from(html).chars().collect::<Vec<char>>());
// Convert encoded data to bytes(Vec<u8>).
let data_to_bytes = decoded_data.to_bytes();
assert_eq!(data_to_bytes, html.as_bytes());
// Decoded data can be also iterated by byte
for (idx, (byte, _)) in decoded_data.into_iter().enumerate(){
   assert_eq!(*byte, orig_bytes[idx]);
}
// Get the total bytes size through the 'bytes_len' method and visit the byte through the 'byte(idx)' method.
for idx in 0..decoded_data.bytes_len(){
   assert_eq!(decoded_data.byte(idx), Some(&orig_bytes[idx]));
}