Repository: helix-editor/helix
File: helix-stdx/src/rope.rs (Rust)

Code before:
            Bound::Unbounded => RegexInput::new(self),
        };
        input.range(byte_range)
    }
    fn first_non_whitespace_char(self) -> Option<usize> {
        self.chars().position(|ch| !ch.is_whitespace())
    }
    fn last_non_whitespace_char(self) -> Option<usize> {
        self.chars_at(self.len_chars())
            .reversed()
            .position(|ch| !ch.is_whitespace())
            .map(|pos| self.len_chars() - pos - 1)
    }

    /// returns the char idx of `byte_idx`, if `byte_idx` is
    /// a char boundary this function behaves the same as `byte_to_char`
    fn byte_to_next_char(self, mut byte_idx: usize) -> usize {
        let (chunk, chunk_byte_off, chunk_char_off, _) = self.chunk_at_byte(byte_idx);
        byte_idx -= chunk_byte_off;
        let is_char_boundary =
            is_utf8_char_boundary(chunk.as_bytes().get(byte_idx).copied().unwrap_or(0));
        chunk_char_off + byte_to_char_idx(chunk, byte_idx) + !is_char_boundary as usize
    }
}

/// copiefd froom std
#[inline]
const fn is_utf8_char_boundary(b: u8) -> bool {
    // This is bit magic equivalent to: b < 128 || b >= 192
    (b as i8) >= -0x40
}

#[cfg(test)]
mod tests {
    use ropey::RopeSlice;

    use crate::rope::RopeSliceExt;

    #[test]
    fn next_char_at_byte() {
        for i in 0..=6 {
            assert_eq!(RopeSlice::from("foobar").byte_to_next_char(i), i);
        }
        for char_idx in 0..10 {
            let len = "😆".len();
            assert_eq!(
                RopeSlice::from("😆😆😆😆😆😆😆😆😆😆").byte_to_next_char(char_idx * len),
                char_idx
            );
            for i in 1..=len {
                assert_eq!(

Review comment (suggestion):
```suggestion
// copied from std
```

typos but also I think this can be a regular comment rather than doc since it's only interesting if you're looking at this block (rather than in an lsp hover for example)

Code after:
            Bound::Unbounded => RegexInput::new(self),
        };
        input.range(byte_range)
    }
    fn first_non_whitespace_char(self) -> Option<usize> {
        self.chars().position(|ch| !ch.is_whitespace())
    }
    fn last_non_whitespace_char(self) -> Option<usize> {
        self.chars_at(self.len_chars())
            .reversed()
            .position(|ch| !ch.is_whitespace())
            .map(|pos| self.len_chars() - pos - 1)
    }

    /// returns the char idx of `byte_idx`, if `byte_idx` is
    /// a char boundary this function behaves the same as `byte_to_char`
    fn byte_to_next_char(self, mut byte_idx: usize) -> usize {
        let (chunk, chunk_byte_off, chunk_char_off, _) = self.chunk_at_byte(byte_idx);
        byte_idx -= chunk_byte_off;
        let is_char_boundary =
            is_utf8_char_boundary(chunk.as_bytes().get(byte_idx).copied().unwrap_or(0));
        chunk_char_off + byte_to_char_idx(chunk, byte_idx) + !is_char_boundary as usize
    }
}

// copied from std
#[inline]
const fn is_utf8_char_boundary(b: u8) -> bool {
    // This is bit magic equivalent to: b < 128 || b >= 192
    (b as i8) >= -0x40
}

#[cfg(test)]
mod tests {
    use ropey::RopeSlice;

    use crate::rope::RopeSliceExt;

    #[test]
    fn next_char_at_byte() {
        for i in 0..=6 {
            assert_eq!(RopeSlice::from("foobar").byte_to_next_char(i), i);
        }
        for char_idx in 0..10 {
            let len = "😆".len();
            assert_eq!(
                RopeSlice::from("😆😆😆😆😆😆😆😆😆😆").byte_to_next_char(char_idx * len),
                char_idx
            );
            for i in 1..=len {
                assert_eq!(