pub fn char_range(&self, r: Range<usize>) -> Range<usize> {
        if r.end <= self.ascii_len {
            // short-circuit if the range is entirely ASCII.
            return r;
        }

        let input_len = self.bytes.len();
        // `it` is an iterator of byte indices pointing to start of each
        // character appearing after `ascii_len`.
        let mut it = self.bytes[self.ascii_len..]
            .iter()
            .zip(self.ascii_len..)
            .filter_map(|(b, i)| is_utf8_leading_byte(*b).then(|| i))
            .fuse();

        let start;
        let end;
        if let Some(n) = r.start.checked_sub(self.ascii_len) {
            // this branch means `r.start > self.ascii_len`.
            start = it.nth(n).unwrap_or(input_len);
            // note that `it.nth(n)` positions `it` at index `n+1` after execution.
            // therefore, we want `it.nth(r.end - r.start - 1)` to read the end index.
            let range_len = r.len().checked_sub(1);
            end = range_len.map_or(start, |len| it.nth(len).unwrap_or(input_len));
        } else {
            start = r.start;
            end = it.nth(r.end - self.ascii_len).unwrap_or(input_len);
        }
        start..end
    }
/// Translates a range of characters into range of bytes.
///
/// If the input overflows the input length, it will be clamped so the ends
/// never exceed `self.len()`.
Translates a range of characters into range of bytes.
If the input overflows the input length, it will be clamped so the ends
never exceed `self.len()`.
Translates a range of characters into range of bytes . If the input overflows the input length it will be clamped so the ends never exceed ` self . len () ` .
pub fn char_range(&self, r: Range<usize>) -> Range<usize> {
        if r.end <= self.ascii_len {
            // short-circuit if the range is entirely ASCII.
            return r;
        }
        let input_len = self.bytes.len();
        // `it` is an iterator of byte indices pointing to start of each
        // character appearing after `ascii_len`.
        let mut it = self.bytes[self.ascii_len..]
            .iter()
            .zip(self.ascii_len..)
            .filter_map(|(b, i)| is_utf8_leading_byte(*b).then(|| i))
            .fuse();
        let start;
        let end;
        if let Some(n) = r.start.checked_sub(self.ascii_len) {
            // this branch means `r.start > self.ascii_len`.
            start = it.nth(n).unwrap_or(input_len);
            // note that `it.nth(n)` positions `it` at index `n+1` after execution.
            // therefore, we want `it.nth(r.end - r.start - 1)` to read the end index.
            let range_len = r.len().checked_sub(1);
            end = range_len.map_or(start, |len| it.nth(len).unwrap_or(input_len));
        } else {
            start = r.start;
            end = it.nth(r.end - self.ascii_len).unwrap_or(input_len);
        }
        start..end
    }
pub fn char_range ( & self , r : Range < usize > ) -> Range < usize > { if r . end <= self . ascii_len { // short-circuit if the range is entirely ASCII. return r ; } let input_len = self . bytes . len ( ) ; // `it` is an iterator of byte indices pointing to start of each // character appearing after `ascii_len`. let mut it = self . bytes [ self . ascii_len .. ] . iter ( ) . zip ( self . ascii_len .. ) . filter_map ( | ( b , i ) | is_utf8_leading_byte ( * b ) . then ( | | i ) ) . fuse ( ) ; let start ; let end ; if let Some ( n ) = r . start . checked_sub ( self . ascii_len ) { // this branch means `r.start > self.ascii_len`. start = it . nth ( n ) . unwrap_or ( input_len ) ; // note that `it.nth(n)` positions `it` at index `n+1` after execution. // therefore, we want `it.nth(r.end - r.start - 1)` to read the end index. let range_len = r . len ( ) . checked_sub ( 1 ) ; end = range_len . map_or ( start , | len | it . nth ( len ) . unwrap_or ( input_len ) ) ; } else { start = r . start ; end = it . nth ( r . end - self . ascii_len ) . unwrap_or ( input_len ) ; } start .. end }
Translates a range of characters into range of bytes.
Translates a range of characters into range of bytes .
&self
r
Range<usize>
&self
r
Range<usize>