fn highlight_number(line: &str) -> Vec<Highlight> {
        let bs = line.as_bytes();

        // numeric literals all start with a base 10 digit or 0
        let starts_numeric = (bs[0] as char).is_digit(10);
        if bs.len() == 1 || !starts_numeric {
            if (bs[0] as char).is_digit(10) {
                return vec![Highlight::Number];
            }
            return Vec::new();
        }
        let (radix, start) = match (bs[0], bs[1]) {
            (b'0', b'x') => (16, 2),
            (b'0', b'o') => (8, 2),
            (b'0', b'b') => (2, 2),
            _ => (10, 0),
        };

        // true if s has exactly one dot as it's prefix
        // `4.5` is a single numeric literal, however `4..5` (a range)
        // is not
        fn single_dot(s: &[u8]) -> bool {
            s[0] == b'.' && (s.len() < 2 || s[1] != b'.')
        }

        bs.iter()
            .enumerate()
            .take_while(|&(i, b)| {
                let c = *b as char;
                // when i < start, we are in the prefix (ex: `0x`)
                // which has already been validated
                i < start || c.is_digit(radix) || single_dot(&bs[i..])
            })
            .map(|_| Highlight::Number)
            .collect()
    }
/// Parse a numeric literal out of line, returning Highlight codes for it.
///
/// Numeric literals can be base10 integral, float, or binary/octal/hex
/// provided it is prefixed with the appropriate `0x`/`0o`/`0b` prefix
///
/// # Return Value
/// a `Vec<Highlight>` containing the code for the prefix of the line that is numeric.
///
/// # Examples
/// ```
/// assert_eq!(vec![Highlight::Number; 4], highlight_number("1234 test"));
/// assert_eq!(vec![Highlight::Number; 4], highlight_number("0xCA test"));
/// assert_eq!(Vec::new(), highlight_number("notnumber 1234 0xCA test"));
/// ```
Parse a numeric literal out of line, returning Highlight codes for it.
Parse a numeric literal out of line returning Highlight codes for it .
fn highlight_number(line: &str) -> Vec<Highlight> {
        let bs = line.as_bytes();
        // numeric literals all start with a base 10 digit or 0
        let starts_numeric = (bs[0] as char).is_digit(10);
        if bs.len() == 1 || !starts_numeric {
            if (bs[0] as char).is_digit(10) {
                return vec![Highlight::Number];
            }
            return Vec::new();
        }
        let (radix, start) = match (bs[0], bs[1]) {
            (b'0', b'x') => (16, 2),
            (b'0', b'o') => (8, 2),
            (b'0', b'b') => (2, 2),
            _ => (10, 0),
        };
        // true if s has exactly one dot as it's prefix
        // `4.5` is a single numeric literal, however `4..5` (a range)
        // is not
        fn single_dot(s: &[u8]) -> bool {
            s[0] == b'.' && (s.len() < 2 || s[1] != b'.')
        }
        bs.iter()
            .enumerate()
            .take_while(|&(i, b)| {
                let c = *b as char;
                // when i < start, we are in the prefix (ex: `0x`)
                // which has already been validated
                i < start || c.is_digit(radix) || single_dot(&bs[i..])
            })
            .map(|_| Highlight::Number)
            .collect()
    }
fn highlight_number ( line : & str ) -> Vec < Highlight > { let bs = line . as_bytes ( ) ; // numeric literals all start with a base 10 digit or 0 let starts_numeric = ( bs [ 0 ] as char ) . is_digit ( 10 ) ; if bs . len ( ) == 1 || ! starts_numeric { if ( bs [ 0 ] as char ) . is_digit ( 10 ) { return vec ! [ Highlight Number ] ; } return Vec :: new ( ) ; } let ( radix , start ) = match ( bs [ 0 ] , bs [ 1 ] ) { ( b'0' , b'x' ) => ( 16 , 2 ) , ( b'0' , b'o' ) => ( 8 , 2 ) , ( b'0' , b'b' ) => ( 2 , 2 ) , _ => ( 10 , 0 ) , } ; // true if s has exactly one dot as it's prefix // `4.5` is a single numeric literal, however `4..5` (a range) // is not fn single_dot ( s : & [ u8 ] ) -> bool { s [ 0 ] == b'.' && ( s . len ( ) < 2 || s [ 1 ] != b'.' ) } bs . iter ( ) . enumerate ( ) . take_while ( | & ( i , b ) | { let c = * b as char ; // when i < start, we are in the prefix (ex: `0x`) // which has already been validated i < start || c . is_digit ( radix ) || single_dot ( & bs [ i .. ] ) } ) . map ( | _ | Highlight :: Number ) . collect ( ) }
Parse a numeric literal out of line, returning Highlight codes for it.
Parse a numeric literal out of line returning Highlight codes for it .
line
&str
return value
a `Vec` containing the code for the prefix of the line that is numeric.
a ` Vec ` containing the code for the prefix of the line that is numeric .
examples
```
assert_eq!(vec![Highlight::Number; 4], highlight_number("1234 test"));
assert_eq!(vec![Highlight::Number; 4], highlight_number("0xCA test"));
assert_eq!(Vec::new(), highlight_number("notnumber 1234 0xCA test"));
```
` ` ` assert_eq! ( vec! [ Highlight :: Number ; 4 ] highlight_number ( " 1234 test " )) ; assert_eq! ( vec! [ Highlight :: Number ; 4 ] highlight_number ( " 0xCA test " )) ; assert_eq! ( Vec :: new () highlight_number ( " notnumber 1234 0xCA test " )) ; ` ` `