fn parse_language_tag(langtag: &mut LanguageTag, t: &str) -> Result<u8> {
    let mut position: u8 = 0;
    for subtag in t.split('-') {
        if subtag.len() > 8 {
            // All subtags have a maximum length of eight characters.
            return Err(Error::SubtagTooLong);
        }
        if position == 6 {
            langtag.privateuse.push(subtag.to_owned());
        } else if subtag.eq_ignore_ascii_case("x") {
            position = 6;
        } else if position == 0 {
            // Primary language
            if subtag.len() < 2 || !is_alphabetic(subtag) {
                return Err(Error::InvalidLanguage);
            }
            langtag.language = Some(subtag.to_owned());
            if subtag.len() < 4 {
                // extlangs are only allowed for short language tags
                position = 1;
            } else {
                position = 2;
            }
        } else if position == 1 && subtag.len() == 3 && is_alphabetic(subtag) {
            // extlangs
            langtag.extlangs.push(subtag.to_owned());
        } else if position <= 2 && subtag.len() == 4 && is_alphabetic(subtag) {
            // Script
            langtag.script = Some(subtag.to_owned());
            position = 3;
        } else if position <= 3 &&
           (subtag.len() == 2 && is_alphabetic(subtag) || subtag.len() == 3 && is_numeric(subtag)) {
            langtag.region = Some(subtag.to_owned());
            position = 4;
        } else if position <= 4 &&
           (subtag.len() >= 5 && is_alphabetic(&subtag[0..1]) ||
            subtag.len() >= 4 && is_numeric(&subtag[0..1])) {
            // Variant
            langtag.variants.push(subtag.to_owned());
            position = 4;
        } else if subtag.len() == 1 {
            position = subtag.as_bytes()[0] as u8;
            if langtag.extensions.contains_key(&position) {
                return Err(Error::DuplicateExtension);
            }
            langtag.extensions.insert(position, Vec::new());
        } else if position > 6 {
            langtag.extensions
                   .get_mut(&position)
                   .expect("no entry found for key")
                   .push(subtag.to_owned());
        } else {
            return Err(Error::InvalidSubtag);
        }
    }
    Ok(position)
}
/// Handles normal tags.
/// The parser has a position from 0 to 6. Bigger positions reepresent the ASCII codes of
/// single character extensions
/// language-extlangs-script-region-variant-extension-privateuse
/// --- 0 -- -- 1 -- -- 2 - -- 3 - -- 4 -- --- x --- ---- 6 ---
Handles normal tags.
The parser has a position from 0 to 6.
Handles normal tags . The parser has a position from 0 to 6 .
fn parse_language_tag(langtag: &mut LanguageTag, t: &str) -> Result<u8> {
    let mut position: u8 = 0;
    for subtag in t.split('-') {
        if subtag.len() > 8 {
            // All subtags have a maximum length of eight characters.
            return Err(Error::SubtagTooLong);
        }
        if position == 6 {
            langtag.privateuse.push(subtag.to_owned());
        } else if subtag.eq_ignore_ascii_case("x") {
            position = 6;
        } else if position == 0 {
            // Primary language
            if subtag.len() < 2 || !is_alphabetic(subtag) {
                return Err(Error::InvalidLanguage);
            }
            langtag.language = Some(subtag.to_owned());
            if subtag.len() < 4 {
                // extlangs are only allowed for short language tags
                position = 1;
            } else {
                position = 2;
            }
        } else if position == 1 && subtag.len() == 3 && is_alphabetic(subtag) {
            // extlangs
            langtag.extlangs.push(subtag.to_owned());
        } else if position <= 2 && subtag.len() == 4 && is_alphabetic(subtag) {
            // Script
            langtag.script = Some(subtag.to_owned());
            position = 3;
        } else if position <= 3 &&
           (subtag.len() == 2 && is_alphabetic(subtag) || subtag.len() == 3 && is_numeric(subtag)) {
            langtag.region = Some(subtag.to_owned());
            position = 4;
        } else if position <= 4 &&
           (subtag.len() >= 5 && is_alphabetic(&subtag[0..1]) ||
            subtag.len() >= 4 && is_numeric(&subtag[0..1])) {
            // Variant
            langtag.variants.push(subtag.to_owned());
            position = 4;
        } else if subtag.len() == 1 {
            position = subtag.as_bytes()[0] as u8;
            if langtag.extensions.contains_key(&position) {
                return Err(Error::DuplicateExtension);
            }
            langtag.extensions.insert(position, Vec::new());
        } else if position > 6 {
            langtag.extensions
                   .get_mut(&position)
                   .expect("no entry found for key")
                   .push(subtag.to_owned());
        } else {
            return Err(Error::InvalidSubtag);
        }
    }
    Ok(position)
}
fn parse_language_tag ( langtag : & mut LanguageTag , t : & str ) -> Result < u8 > { let mut position : u8 = 0 ; for subtag in t . split ( '-' ) { if subtag . len ( ) > 8 { // All subtags have a maximum length of eight characters. return Err ( Error :: SubtagTooLong ) ; } if position == 6 { langtag . privateuse . push ( subtag . to_owned ( ) ) ; } else if subtag . eq_ignore_ascii_case ( " " ) { position = 6 ; } else if position == 0 { // Primary language if subtag . len ( ) < 2 || ! is_alphabetic ( subtag ) { return Err ( Error :: InvalidLanguage ) ; } langtag . language = Some ( subtag . to_owned ( ) ) ; if subtag . len ( ) < 4 { // extlangs are only allowed for short language tags position = 1 ; } else { position = 2 ; } } else if position == 1 && subtag . len ( ) == 3 && is_alphabetic ( subtag ) { // extlangs langtag . extlangs . push ( subtag . to_owned ( ) ) ; } else if position <= 2 && subtag . len ( ) == 4 && is_alphabetic ( subtag ) { // Script langtag . script = Some ( subtag . to_owned ( ) ) ; position = 3 ; } else if position <= 3 && ( subtag . len ( ) == 2 && is_alphabetic ( subtag ) || subtag . len ( ) == 3 && is_numeric ( subtag ) ) { langtag . region = Some ( subtag . to_owned ( ) ) ; position = 4 ; } else if position <= 4 && ( subtag . len ( ) >= 5 && is_alphabetic ( & subtag [ 0 .. 1 ] ) || subtag . len ( ) >= 4 && is_numeric ( & subtag [ 0 .. 1 ] ) ) { // Variant langtag . variants . push ( subtag . to_owned ( ) ) ; position = 4 ; } else if subtag . len ( ) == 1 { position = subtag . as_bytes ( ) [ 0 ] as u8 ; if langtag . extensions . contains_key ( & position ) { return Err ( Error :: DuplicateExtension ) ; } langtag . extensions . insert ( position , Vec :: new ( ) ) ; } else if position > 6 { langtag . extensions . get_mut ( & position ) . expect ( " " ) . push ( subtag . to_owned ( ) ) ; } else { return Err ( Error :: InvalidSubtag ) ; } } Ok ( position ) }
Handles normal tags.
Handles normal tags .
langtag
&mut LanguageTag
t
&str
langtag
&mut LanguageTag
t
&str