fn char_lit(lit: &str) -> (char, isize) {
    use std::char;

    let mut chars = lit.chars();
    let c = match (chars.next(), chars.next()) {
        (Some(c), None) if c != '\\' => return (c, 1),
        (Some('\\'), Some(c)) => match c {
            '"' => Some('"'),
            'n' => Some('\n'),
            'r' => Some('\r'),
            't' => Some('\t'),
            '\\' => Some('\\'),
            '\'' => Some('\''),
            '0' => Some('\0'),
            _ => { None }
        },
        _ => panic!("lexer accepted invalid char escape `{}`", lit)
    };

    match c {
        Some(x) => return (x, 2),
        None => { }
    }

    let msg = format!("lexer should have rejected a bad character escape {}", lit);
    let msg2 = &msg[..];

    fn esc(len: usize, lit: &str) -> Option<(char, isize)> {
        u32::from_str_radix(&lit[2..len], 16).ok()
        .and_then(char::from_u32)
        .map(|x| (x, len as isize))
    }

    let unicode_escape = || -> Option<(char, isize)> {
        if lit.as_bytes()[2] == b'{' {
            let idx = lit.find('}').expect(msg2);
            let subslice = &lit[3..idx];
            u32::from_str_radix(subslice, 16).ok()
                .and_then(char::from_u32)
                .map(|x| (x, subslice.chars().count() as isize + 4))
        } else {
            esc(6, lit)
        }
    };

    // Unicode escapes
    return match lit.as_bytes()[1] as char {
        'x' | 'X' => esc(4, lit),
        'u' => unicode_escape(),
        'U' => esc(10, lit),
        _ => None,
    }.expect(msg2);
}
/// Parse a string representing a character literal into its final form.
/// Rather than just accepting/rejecting a given literal, unescapes it as
/// well. Can take any slice prefixed by a character escape. Returns the
/// character and the number of characters consumed.
Parse a string representing a character literal into its final form.
Rather than just accepting/rejecting a given literal, unescapes it as
well. Can take any slice prefixed by a character escape. Returns the
character and the number of characters consumed.
Parse a string representing a character literal into its final form . Rather than just accepting / rejecting a given literal unescapes it as well . Can take any slice prefixed by a character escape . Returns the character and the number of characters consumed .
fn char_lit(lit: &str) -> (char, isize) {
    use std::char;
    let mut chars = lit.chars();
    let c = match (chars.next(), chars.next()) {
        (Some(c), None) if c != '\\' => return (c, 1),
        (Some('\\'), Some(c)) => match c {
            '"' => Some('"'),
            'n' => Some('\n'),
            'r' => Some('\r'),
            't' => Some('\t'),
            '\\' => Some('\\'),
            '\'' => Some('\''),
            '0' => Some('\0'),
            _ => { None }
        },
        _ => panic!("lexer accepted invalid char escape `{}`", lit)
    };
    match c {
        Some(x) => return (x, 2),
        None => { }
    }
    let msg = format!("lexer should have rejected a bad character escape {}", lit);
    let msg2 = &msg[..];
    fn esc(len: usize, lit: &str) -> Option<(char, isize)> {
        u32::from_str_radix(&lit[2..len], 16).ok()
        .and_then(char::from_u32)
        .map(|x| (x, len as isize))
    }
    let unicode_escape = || -> Option<(char, isize)> {
        if lit.as_bytes()[2] == b'{' {
            let idx = lit.find('}').expect(msg2);
            let subslice = &lit[3..idx];
            u32::from_str_radix(subslice, 16).ok()
                .and_then(char::from_u32)
                .map(|x| (x, subslice.chars().count() as isize + 4))
        } else {
            esc(6, lit)
        }
    };
    // Unicode escapes
    return match lit.as_bytes()[1] as char {
        'x' | 'X' => esc(4, lit),
        'u' => unicode_escape(),
        'U' => esc(10, lit),
        _ => None,
    }.expect(msg2);
}
fn char_lit ( lit : & str ) -> ( char , isize ) { use std :: char ; let mut chars = lit . chars ( ) ; let c = match ( chars . next ( ) , chars . next ( ) ) { ( Some ( c ) , None ) if c != '\\' => return ( c , 1 ) , ( Some ( '\\' ) , Some ( c ) ) => match c { '"' => Some ( '"' ) , 'n' => Some ( '\n' ) , 'r' => Some ( '\r' ) , 't' => Some ( '\t' ) , '\\' => Some ( '\\' ) , '\'' => Some ( '\'' ) , '0' => Some ( '\0' ) , _ => { None } } , _ => panic ! ( " " lit ) } ; match c { Some ( x ) => return ( x , 2 ) , None => { } } let msg = format ! ( " " lit ) ; let msg2 = & msg [ .. ] ; fn esc ( len : usize , lit : & str ) -> Option < ( char , isize ) > { u32 :: from_str_radix ( & lit [ 2 .. len ] , 16 ) . ok ( ) . and_then ( char :: from_u32 ) . map ( | x | ( x , len as isize ) ) } let unicode_escape = | | -> Option < ( char , isize ) > { if lit . as_bytes ( ) [ 2 ] == b'{' { let idx = lit . find ( '}' ) . expect ( msg2 ) ; let subslice = & lit [ 3 .. idx ] ; u32 :: from_str_radix ( subslice , 16 ) . ok ( ) . and_then ( char :: from_u32 ) . map ( | x | ( x , subslice . chars ( ) . count ( ) as isize + 4 ) ) } else { esc ( 6 , lit ) } } ; // Unicode escapes return match lit . as_bytes ( ) [ 1 ] as char { 'x' | 'X' => esc ( 4 , lit ) , 'u' => unicode_escape ( ) , 'U' => esc ( 10 , lit ) , _ => None , } . expect ( msg2 ) ; }
Parse a string representing a character literal into its final form.
Parse a string representing a character literal into its final form .
lit
&str
lit
&str