fn highlight(&self, line: &str) -> Vec<Highlight> {

        // Check if any keyword in keywords is a prefix of the string
        // return Some(match) for the first match
        fn keyword_match<'a>(keywords: &'a [&'a str], remainder: &str) -> Option<&'a str> {
            keywords
                .iter()
                .find(|&kw| &remainder[..min(remainder.len(), kw.len())] == *kw)
                .map(|s| *s)
        }

        // Copy the parsed highlights into existing and return the amount moved. None if
        // parsed was empty
        fn add_parsed(parsed: &[Highlight], existing: &mut [Highlight]) -> Option<usize> {
            let len = parsed.len();
            if len == 0 {
                return None;
            }
            for (muth, h) in existing[..len].iter_mut().zip(parsed.iter()) {
                *muth = *h;
            }
            Some(len)
        }

        let mut highlights = vec![Highlight::Normal; line.len()];
        if self.flags.is_empty() {
            return highlights;
        }
        let bs = line.as_bytes();
        let comment_len = self.comment_start.len();

        let mut prev_sep = true;
        let mut i = 0;
        while i < line.len() {
            // general pattern:
            // try to parse a prefix of the line starting at i
            // if we find something, increment i by the amount that the parser consumed
            // if nothing parses, just increment i by 1
            let c = bs[i] as char;
            if self.flags.contains(HL_STRINGS) {
                if let Some(n) = add_parsed(&Self::highlight_string(&line[i..]),
                                            &mut highlights[i..]) {
                    i += n;
                    prev_sep = false;
                    continue;
                }

                if let Some(n) = add_parsed(&Self::highlight_char_literal(&line[i..]),
                                            &mut highlights[i..]) {
                    i += n;
                    prev_sep = false;
                    continue;
                }
            }
            if self.flags.contains(HL_NUMBERS) && prev_sep {
                if let Some(n) = add_parsed(&Self::highlight_number(&line[i..]),
                                            &mut highlights[i..]) {
                    i += n;
                    prev_sep = false;
                    continue;
                }
            }
            if comment_len > 0 && &line[i..min(i + comment_len, line.len())] == self.comment_start {
                for x in &mut highlights[i..].iter_mut() {
                    *x = Highlight::Comment;
                }
                // line comments set the Highlight for the entire line suffx, so we are done
                break;
            }

            if prev_sep {
                // find if any keyword matched from either list and
                // return the correct highlight code for it
                let keyword_matched = keyword_match(self.keywords1, &line[i..])
                    .map(|k| (k, Highlight::Keyword1))
                    .or_else(|| {
                        keyword_match(self.keywords2, &line[i..]).map(|k| (k, Highlight::Keyword2))
                    });

                if let Some((kw, hl)) = keyword_matched {
                    // keywords must also be suffixed by a seperator, otherwise
                    // words like `fort` would match a `for` keyword
                    if bs.len() > i + kw.len() && Self::is_separator(bs[i + kw.len()] as char) {
                        for x in (&mut highlights[i..i + kw.len()]).iter_mut() {
                            *x = hl;
                        }
                        i += kw.len();
                        prev_sep = false;
                        continue;
                    }
                }
            }
            prev_sep = Self::is_separator(c);
            i += 1;
        }
        highlights
    }
/// Get the Highlight code to use for every character in `line`
///
/// * `line` the line to highlight
/// # Return value
/// A `Vector<Highlight>` with a code for every character in the original `line`
Get the Highlight code to use for every character in `line`
`line` the line to highlight
Get the Highlight code to use for every character in ` line ` ` line ` the line to highlight
fn highlight(&self, line: &str) -> Vec<Highlight> {
        // Check if any keyword in keywords is a prefix of the string
        // return Some(match) for the first match
        fn keyword_match<'a>(keywords: &'a [&'a str], remainder: &str) -> Option<&'a str> {
            keywords
                .iter()
                .find(|&kw| &remainder[..min(remainder.len(), kw.len())] == *kw)
                .map(|s| *s)
        }
        // Copy the parsed highlights into existing and return the amount moved. None if
        // parsed was empty
        fn add_parsed(parsed: &[Highlight], existing: &mut [Highlight]) -> Option<usize> {
            let len = parsed.len();
            if len == 0 {
                return None;
            }
            for (muth, h) in existing[..len].iter_mut().zip(parsed.iter()) {
                *muth = *h;
            }
            Some(len)
        }
        let mut highlights = vec![Highlight::Normal; line.len()];
        if self.flags.is_empty() {
            return highlights;
        }
        let bs = line.as_bytes();
        let comment_len = self.comment_start.len();
        let mut prev_sep = true;
        let mut i = 0;
        while i < line.len() {
            // general pattern:
            // try to parse a prefix of the line starting at i
            // if we find something, increment i by the amount that the parser consumed
            // if nothing parses, just increment i by 1
            let c = bs[i] as char;
            if self.flags.contains(HL_STRINGS) {
                if let Some(n) = add_parsed(&Self::highlight_string(&line[i..]),
                                            &mut highlights[i..]) {
                    i += n;
                    prev_sep = false;
                    continue;
                }
                if let Some(n) = add_parsed(&Self::highlight_char_literal(&line[i..]),
                                            &mut highlights[i..]) {
                    i += n;
                    prev_sep = false;
                    continue;
                }
            }
            if self.flags.contains(HL_NUMBERS) && prev_sep {
                if let Some(n) = add_parsed(&Self::highlight_number(&line[i..]),
                                            &mut highlights[i..]) {
                    i += n;
                    prev_sep = false;
                    continue;
                }
            }
            if comment_len > 0 && &line[i..min(i + comment_len, line.len())] == self.comment_start {
                for x in &mut highlights[i..].iter_mut() {
                    *x = Highlight::Comment;
                }
                // line comments set the Highlight for the entire line suffx, so we are done
                break;
            }
            if prev_sep {
                // find if any keyword matched from either list and
                // return the correct highlight code for it
                let keyword_matched = keyword_match(self.keywords1, &line[i..])
                    .map(|k| (k, Highlight::Keyword1))
                    .or_else(|| {
                        keyword_match(self.keywords2, &line[i..]).map(|k| (k, Highlight::Keyword2))
                    });
                if let Some((kw, hl)) = keyword_matched {
                    // keywords must also be suffixed by a seperator, otherwise
                    // words like `fort` would match a `for` keyword
                    if bs.len() > i + kw.len() && Self::is_separator(bs[i + kw.len()] as char) {
                        for x in (&mut highlights[i..i + kw.len()]).iter_mut() {
                            *x = hl;
                        }
                        i += kw.len();
                        prev_sep = false;
                        continue;
                    }
                }
            }
            prev_sep = Self::is_separator(c);
            i += 1;
        }
        highlights
    }
fn highlight ( & self , line : & str ) -> Vec < Highlight > { // Check if any keyword in keywords is a prefix of the string // return Some(match) for the first match fn keyword_match < ' a > ( keywords : & ' a [ & ' a str ] , remainder : & str ) -> Option < & ' a str > { keywords . iter ( ) . find ( | & kw | & remainder [ .. min ( remainder . len ( ) , kw . len ( ) ) ] == * kw ) . map ( | s | * s ) } // Copy the parsed highlights into existing and return the amount moved. None if // parsed was empty fn add_parsed ( parsed : & [ Highlight ] , existing : & mut [ Highlight ] ) -> Option < usize > { let len = parsed . len ( ) ; if len == 0 { return None ; } for ( muth , h ) in existing [ .. len ] . iter_mut ( ) . zip ( parsed . iter ( ) ) { * muth = * h ; } Some ( len ) } let mut highlights = vec ! [ Highlight Normal line len ( ) ] ; if self . flags . is_empty ( ) { return highlights ; } let bs = line . as_bytes ( ) ; let comment_len = self . comment_start . len ( ) ; let mut prev_sep = true ; let mut i = 0 ; while i < line . len ( ) { // general pattern: // try to parse a prefix of the line starting at i // if we find something, increment i by the amount that the parser consumed // if nothing parses, just increment i by 1 let c = bs [ i ] as char ; if self . flags . contains ( HL_STRINGS ) { if let Some ( n ) = add_parsed ( & Self :: highlight_string ( & line [ i .. ] ) , & mut highlights [ i .. ] ) { i += n ; prev_sep = false ; continue ; } if let Some ( n ) = add_parsed ( & Self :: highlight_char_literal ( & line [ i .. ] ) , & mut highlights [ i .. ] ) { i += n ; prev_sep = false ; continue ; } } if self . flags . contains ( HL_NUMBERS ) && prev_sep { if let Some ( n ) = add_parsed ( & Self :: highlight_number ( & line [ i .. ] ) , & mut highlights [ i .. ] ) { i += n ; prev_sep = false ; continue ; } } if comment_len > 0 && & line [ i .. min ( i + comment_len , line . len ( ) ) ] == self . comment_start { for x in & mut highlights [ i .. ] . iter_mut ( ) { * x = Highlight :: Comment ; } // line comments set the Highlight for the entire line suffx, so we are done break ; } if prev_sep { // find if any keyword matched from either list and // return the correct highlight code for it let keyword_matched = keyword_match ( self . keywords1 , & line [ i .. ] ) . map ( | k | ( k , Highlight :: Keyword1 ) ) . or_else ( | | { keyword_match ( self . keywords2 , & line [ i .. ] ) . map ( | k | ( k , Highlight :: Keyword2 ) ) } ) ; if let Some ( ( kw , hl ) ) = keyword_matched { // keywords must also be suffixed by a seperator, otherwise // words like `fort` would match a `for` keyword if bs . len ( ) > i + kw . len ( ) && Self :: is_separator ( bs [ i + kw . len ( ) ] as char ) { for x in ( & mut highlights [ i .. i + kw . len ( ) ] ) . iter_mut ( ) { * x = hl ; } i += kw . len ( ) ; prev_sep = false ; continue ; } } } prev_sep = Self :: is_separator ( c ) ; i += 1 ; } highlights }
Get the Highlight code to use for every character in `line`
 `line` the line to highlight
Get the Highlight code to use for every character in ` line ` ` line ` the line to highlight
&self
line
&str
return value
A `Vector` with a code for every character in the original `line`
A ` Vector ` with a code for every character in the original ` line `