/// Find the charset of the webpage. ``data`` is not a String as this might not be utf8.
/// Returned String is lower cased
/// This is a hack and should be check in case of a bug
Find the charset of the webpage. ``data`` is not a String as this might not be utf8.
Returned String is lower cased
This is a hack and should be check in case of a bug
Find the charset of the webpage . ` ` data ` ` is not a String as this might not be utf8 . Returned String is lower cased This is a hack and should be check in case of a bug
fn find_charset(data: &[u8], http_charset: Option<String>) -> Option<String> {
        lazy_static! {
            static ref CHARSET_REGEX: Regex =
                Regex::new(r#"<meta.*charset\s*=\s*["']?([^"'\s;>]+).*>"#).unwrap();
        }
        // We don't know the real charset yet. We hope that the charset is ASCII
        // compatible, because Rust String are in UTF-8 (also ASCII compatible).
        let data_utf8 = unsafe { String::from_utf8_unchecked(Vec::from(data)) };
        let captures = CHARSET_REGEX.captures_iter(&data_utf8).next();
        // We use the first one, hopping we are in the <head> of the page... or if nothing is found
        // we used the http charset (if any).
        captures
            .map(|first| String::from(first.get(1).unwrap().as_str().to_lowercase()))
            .or(http_charset)
    }
fn find_charset ( data : & [ u8 ] , http_charset : Option < String > ) -> Option < String > { lazy_static ! { static ref CHARSET_REGEX Regex Regex new ( r#"<meta.*charset\s*=\s*["']?([^"'\s;>]+).*>"# ) unwrap ( ) } // We don't know the real charset yet. We hope that the charset is ASCII // compatible, because Rust String are in UTF-8 (also ASCII compatible). let data_utf8 = unsafe { String :: from_utf8_unchecked ( Vec :: from ( data ) ) } ; let captures = CHARSET_REGEX . captures_iter ( & data_utf8 ) . next ( ) ; // We use the first one, hopping we are in the <head> of the page... or if nothing is found // we used the http charset (if any). captures . map ( | first | String :: from ( first . get ( 1 ) . unwrap ( ) . as_str ( ) . to_lowercase ( ) ) ) . or ( http_charset ) }
Find the charset of the webpage.
Find the charset of the webpage .
data
&[u8]
http_charset
Option<String>