mirror of https://github.com/helix-editor/helix
Adapt stdx
parent
5f4a1ca31f
commit
5d641b1722
|
@ -269,7 +269,7 @@ pub fn get_path_suffix(src: RopeSlice<'_>, match_single_file: bool) -> Option<Ro
|
||||||
|
|
||||||
regex
|
regex
|
||||||
.find(Input::new(src))
|
.find(Input::new(src))
|
||||||
.map(|mat| src.byte_slice(mat.range()))
|
.map(|mat| src.slice(mat.range()))
|
||||||
}
|
}
|
||||||
|
|
||||||
/// Returns an iterator of the **byte** ranges in src that contain a path.
|
/// Returns an iterator of the **byte** ranges in src that contain a path.
|
||||||
|
|
|
@ -1,75 +1,23 @@
|
||||||
use std::fmt;
|
|
||||||
use std::ops::{Bound, RangeBounds};
|
use std::ops::{Bound, RangeBounds};
|
||||||
|
|
||||||
pub use regex_cursor::engines::meta::{Builder as RegexBuilder, Regex};
|
pub use regex_cursor::engines::meta::{Builder as RegexBuilder, Regex};
|
||||||
pub use regex_cursor::regex_automata::util::syntax::Config;
|
pub use regex_cursor::regex_automata::util::syntax::Config;
|
||||||
use regex_cursor::{Input as RegexInput, RopeyCursor};
|
use regex_cursor::Input as RegexInput;
|
||||||
use ropey::iter::Chunks;
|
use ropey::{ChunkCursor, RopeSlice};
|
||||||
use ropey::RopeSlice;
|
|
||||||
use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete};
|
use unicode_segmentation::{GraphemeCursor, GraphemeIncomplete};
|
||||||
|
|
||||||
pub trait RopeSliceExt<'a>: Sized {
|
pub trait RopeSliceExt<'a>: Sized {
|
||||||
fn ends_with(self, text: &str) -> bool;
|
fn ends_with(self, text: &str) -> bool;
|
||||||
fn starts_with(self, text: &str) -> bool;
|
fn starts_with(self, text: &str) -> bool;
|
||||||
fn regex_input(self) -> RegexInput<RopeyCursor<'a>>;
|
fn regex_input(self) -> RegexInput<ChunkCursor<'a>>;
|
||||||
fn regex_input_at_bytes<R: RangeBounds<usize>>(
|
fn regex_input_at_bytes<R: RangeBounds<usize>>(
|
||||||
self,
|
self,
|
||||||
byte_range: R,
|
byte_range: R,
|
||||||
) -> RegexInput<RopeyCursor<'a>>;
|
) -> RegexInput<ChunkCursor<'a>>;
|
||||||
fn regex_input_at<R: RangeBounds<usize>>(self, char_range: R) -> RegexInput<RopeyCursor<'a>>;
|
#[deprecated = "use regex_input_at_bytes instead"]
|
||||||
|
fn regex_input_at<R: RangeBounds<usize>>(self, char_range: R) -> RegexInput<ChunkCursor<'a>>;
|
||||||
fn first_non_whitespace_char(self) -> Option<usize>;
|
fn first_non_whitespace_char(self) -> Option<usize>;
|
||||||
fn last_non_whitespace_char(self) -> Option<usize>;
|
fn last_non_whitespace_char(self) -> Option<usize>;
|
||||||
/// Finds the closest byte index not exceeding `byte_idx` which lies on a character boundary.
|
|
||||||
///
|
|
||||||
/// If `byte_idx` already lies on a character boundary then it is returned as-is. When
|
|
||||||
/// `byte_idx` lies between two character boundaries, this function returns the byte index of
|
|
||||||
/// the lesser / earlier / left-hand-side boundary.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use ropey::RopeSlice;
|
|
||||||
/// # use helix_stdx::rope::RopeSliceExt;
|
|
||||||
/// let text = RopeSlice::from("⌚"); // three bytes: e2 8c 9a
|
|
||||||
/// assert_eq!(text.floor_char_boundary(0), 0);
|
|
||||||
/// assert_eq!(text.floor_char_boundary(1), 0);
|
|
||||||
/// assert_eq!(text.floor_char_boundary(2), 0);
|
|
||||||
/// assert_eq!(text.floor_char_boundary(3), 3);
|
|
||||||
/// ```
|
|
||||||
fn floor_char_boundary(self, byte_idx: usize) -> usize;
|
|
||||||
/// Finds the closest byte index not below `byte_idx` which lies on a character boundary.
|
|
||||||
///
|
|
||||||
/// If `byte_idx` already lies on a character boundary then it is returned as-is. When
|
|
||||||
/// `byte_idx` lies between two character boundaries, this function returns the byte index of
|
|
||||||
/// the greater / later / right-hand-side boundary.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use ropey::RopeSlice;
|
|
||||||
/// # use helix_stdx::rope::RopeSliceExt;
|
|
||||||
/// let text = RopeSlice::from("⌚"); // three bytes: e2 8c 9a
|
|
||||||
/// assert_eq!(text.ceil_char_boundary(0), 0);
|
|
||||||
/// assert_eq!(text.ceil_char_boundary(1), 3);
|
|
||||||
/// assert_eq!(text.ceil_char_boundary(2), 3);
|
|
||||||
/// assert_eq!(text.ceil_char_boundary(3), 3);
|
|
||||||
/// ```
|
|
||||||
fn ceil_char_boundary(self, byte_idx: usize) -> usize;
|
|
||||||
/// Checks whether the given `byte_idx` lies on a character boundary.
|
|
||||||
///
|
|
||||||
/// # Example
|
|
||||||
///
|
|
||||||
/// ```
|
|
||||||
/// # use ropey::RopeSlice;
|
|
||||||
/// # use helix_stdx::rope::RopeSliceExt;
|
|
||||||
/// let text = RopeSlice::from("⌚"); // three bytes: e2 8c 9a
|
|
||||||
/// assert!(text.is_char_boundary(0));
|
|
||||||
/// assert!(!text.is_char_boundary(1));
|
|
||||||
/// assert!(!text.is_char_boundary(2));
|
|
||||||
/// assert!(text.is_char_boundary(3));
|
|
||||||
/// ```
|
|
||||||
#[allow(clippy::wrong_self_convention)]
|
|
||||||
fn is_char_boundary(self, byte_idx: usize) -> bool;
|
|
||||||
/// Finds the closest byte index not exceeding `byte_idx` which lies on a grapheme cluster
|
/// Finds the closest byte index not exceeding `byte_idx` which lies on a grapheme cluster
|
||||||
/// boundary.
|
/// boundary.
|
||||||
///
|
///
|
||||||
|
@ -82,9 +30,10 @@ pub trait RopeSliceExt<'a>: Sized {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use ropey::RopeSlice;
|
/// # use ropey::{RopeSlice, Rope};
|
||||||
/// # use helix_stdx::rope::RopeSliceExt;
|
/// # use helix_stdx::rope::RopeSliceExt;
|
||||||
/// let text = RopeSlice::from("\r\n"); // U+000D U+000A, hex: 0d 0a
|
/// let text = Rope::from_str("\r\n"); // U+000D U+000A, hex: 0d 0a
|
||||||
|
/// let text = text.slice(..);
|
||||||
/// assert_eq!(text.floor_grapheme_boundary(0), 0);
|
/// assert_eq!(text.floor_grapheme_boundary(0), 0);
|
||||||
/// assert_eq!(text.floor_grapheme_boundary(1), 0);
|
/// assert_eq!(text.floor_grapheme_boundary(1), 0);
|
||||||
/// assert_eq!(text.floor_grapheme_boundary(2), 2);
|
/// assert_eq!(text.floor_grapheme_boundary(2), 2);
|
||||||
|
@ -102,9 +51,10 @@ pub trait RopeSliceExt<'a>: Sized {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use ropey::RopeSlice;
|
/// # use ropey::{RopeSlice, Rope};
|
||||||
/// # use helix_stdx::rope::RopeSliceExt;
|
/// # use helix_stdx::rope::RopeSliceExt;
|
||||||
/// let text = RopeSlice::from("\r\n"); // U+000D U+000A, hex: 0d 0a
|
/// let text = Rope::from_str("\r\n"); // U+000D U+000A, hex: 0d 0a
|
||||||
|
/// let text = text.slice(..);
|
||||||
/// assert_eq!(text.ceil_grapheme_boundary(0), 0);
|
/// assert_eq!(text.ceil_grapheme_boundary(0), 0);
|
||||||
/// assert_eq!(text.ceil_grapheme_boundary(1), 2);
|
/// assert_eq!(text.ceil_grapheme_boundary(1), 2);
|
||||||
/// assert_eq!(text.ceil_grapheme_boundary(2), 2);
|
/// assert_eq!(text.ceil_grapheme_boundary(2), 2);
|
||||||
|
@ -115,9 +65,10 @@ pub trait RopeSliceExt<'a>: Sized {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use ropey::RopeSlice;
|
/// # use ropey::{RopeSlice, Rope};
|
||||||
/// # use helix_stdx::rope::RopeSliceExt;
|
/// # use helix_stdx::rope::RopeSliceExt;
|
||||||
/// let text = RopeSlice::from("\r\n"); // U+000D U+000A, hex: 0d 0a
|
/// let text = Rope::from_str("\r\n"); // U+000D U+000A, hex: 0d 0a
|
||||||
|
/// let text = text.slice(..);
|
||||||
/// assert!(text.is_grapheme_boundary(0));
|
/// assert!(text.is_grapheme_boundary(0));
|
||||||
/// assert!(!text.is_grapheme_boundary(1));
|
/// assert!(!text.is_grapheme_boundary(1));
|
||||||
/// assert!(text.is_grapheme_boundary(2));
|
/// assert!(text.is_grapheme_boundary(2));
|
||||||
|
@ -129,10 +80,10 @@ pub trait RopeSliceExt<'a>: Sized {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use ropey::RopeSlice;
|
/// # use ropey::{RopeSlice, Rope};
|
||||||
/// # use helix_stdx::rope::RopeSliceExt;
|
/// # use helix_stdx::rope::RopeSliceExt;
|
||||||
/// let text = RopeSlice::from("😶🌫️🏴☠️🖼️");
|
/// let text = Rope::from_str("😶🌫️🏴☠️🖼️");
|
||||||
/// let graphemes: Vec<_> = text.graphemes().collect();
|
/// let graphemes: Vec<_> = text.slice(..).graphemes().collect();
|
||||||
/// assert_eq!(graphemes.as_slice(), &["😶🌫️", "🏴☠️", "🖼️"]);
|
/// assert_eq!(graphemes.as_slice(), &["😶🌫️", "🏴☠️", "🖼️"]);
|
||||||
/// ```
|
/// ```
|
||||||
fn graphemes(self) -> RopeGraphemes<'a>;
|
fn graphemes(self) -> RopeGraphemes<'a>;
|
||||||
|
@ -144,10 +95,10 @@ pub trait RopeSliceExt<'a>: Sized {
|
||||||
/// # Example
|
/// # Example
|
||||||
///
|
///
|
||||||
/// ```
|
/// ```
|
||||||
/// # use ropey::RopeSlice;
|
/// # use ropey::{RopeSlice, Rope};
|
||||||
/// # use helix_stdx::rope::RopeSliceExt;
|
/// # use helix_stdx::rope::RopeSliceExt;
|
||||||
/// let text = RopeSlice::from("😶🌫️🏴☠️🖼️");
|
/// let text = Rope::from_str("😶🌫️🏴☠️🖼️");
|
||||||
/// let graphemes: Vec<_> = text.graphemes_rev().collect();
|
/// let graphemes: Vec<_> = text.slice(..).graphemes_rev().collect();
|
||||||
/// assert_eq!(graphemes.as_slice(), &["🖼️", "🏴☠️", "😶🌫️"]);
|
/// assert_eq!(graphemes.as_slice(), &["🖼️", "🏴☠️", "😶🌫️"]);
|
||||||
/// ```
|
/// ```
|
||||||
fn graphemes_rev(self) -> RevRopeGraphemes<'a>;
|
fn graphemes_rev(self) -> RevRopeGraphemes<'a>;
|
||||||
|
@ -155,36 +106,36 @@ pub trait RopeSliceExt<'a>: Sized {
|
||||||
|
|
||||||
impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
|
impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
|
||||||
fn ends_with(self, text: &str) -> bool {
|
fn ends_with(self, text: &str) -> bool {
|
||||||
let len = self.len_bytes();
|
let len = self.len();
|
||||||
if len < text.len() {
|
if len < text.len() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self.get_byte_slice(len - text.len()..)
|
self.try_slice(len - text.len()..)
|
||||||
.is_some_and(|end| end == text)
|
.is_ok_and(|end| end == text)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn starts_with(self, text: &str) -> bool {
|
fn starts_with(self, text: &str) -> bool {
|
||||||
let len = self.len_bytes();
|
let len = self.len();
|
||||||
if len < text.len() {
|
if len < text.len() {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
self.get_byte_slice(..text.len())
|
self.try_slice(..text.len())
|
||||||
.is_some_and(|start| start == text)
|
.is_ok_and(|start| start == text)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn regex_input(self) -> RegexInput<RopeyCursor<'a>> {
|
fn regex_input(self) -> RegexInput<ChunkCursor<'a>> {
|
||||||
RegexInput::new(self)
|
RegexInput::new(self)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn regex_input_at<R: RangeBounds<usize>>(self, char_range: R) -> RegexInput<RopeyCursor<'a>> {
|
fn regex_input_at<R: RangeBounds<usize>>(self, char_range: R) -> RegexInput<ChunkCursor<'a>> {
|
||||||
let start_bound = match char_range.start_bound() {
|
let start_bound = match char_range.start_bound() {
|
||||||
Bound::Included(&val) => Bound::Included(self.char_to_byte(val)),
|
Bound::Included(&val) => Bound::Included(self.char_to_byte_idx(val)),
|
||||||
Bound::Excluded(&val) => Bound::Excluded(self.char_to_byte(val)),
|
Bound::Excluded(&val) => Bound::Excluded(self.char_to_byte_idx(val)),
|
||||||
Bound::Unbounded => Bound::Unbounded,
|
Bound::Unbounded => Bound::Unbounded,
|
||||||
};
|
};
|
||||||
let end_bound = match char_range.end_bound() {
|
let end_bound = match char_range.end_bound() {
|
||||||
Bound::Included(&val) => Bound::Included(self.char_to_byte(val)),
|
Bound::Included(&val) => Bound::Included(self.char_to_byte_idx(val)),
|
||||||
Bound::Excluded(&val) => Bound::Excluded(self.char_to_byte(val)),
|
Bound::Excluded(&val) => Bound::Excluded(self.char_to_byte_idx(val)),
|
||||||
Bound::Unbounded => Bound::Unbounded,
|
Bound::Unbounded => Bound::Unbounded,
|
||||||
};
|
};
|
||||||
self.regex_input_at_bytes((start_bound, end_bound))
|
self.regex_input_at_bytes((start_bound, end_bound))
|
||||||
|
@ -192,10 +143,10 @@ impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
|
||||||
fn regex_input_at_bytes<R: RangeBounds<usize>>(
|
fn regex_input_at_bytes<R: RangeBounds<usize>>(
|
||||||
self,
|
self,
|
||||||
byte_range: R,
|
byte_range: R,
|
||||||
) -> RegexInput<RopeyCursor<'a>> {
|
) -> RegexInput<ChunkCursor<'a>> {
|
||||||
let input = match byte_range.start_bound() {
|
let input = match byte_range.start_bound() {
|
||||||
Bound::Included(&pos) | Bound::Excluded(&pos) => {
|
Bound::Included(&pos) | Bound::Excluded(&pos) => {
|
||||||
RegexInput::new(RopeyCursor::at(self, pos))
|
RegexInput::new(self.chunk_cursor_at(pos))
|
||||||
}
|
}
|
||||||
Bound::Unbounded => RegexInput::new(self),
|
Bound::Unbounded => RegexInput::new(self),
|
||||||
};
|
};
|
||||||
|
@ -211,69 +162,22 @@ impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
|
||||||
.map(|pos| self.len_chars() - pos - 1)
|
.map(|pos| self.len_chars() - pos - 1)
|
||||||
}
|
}
|
||||||
|
|
||||||
// These three are adapted from std:
|
|
||||||
|
|
||||||
fn floor_char_boundary(self, byte_idx: usize) -> usize {
|
|
||||||
if byte_idx >= self.len_bytes() {
|
|
||||||
self.len_bytes()
|
|
||||||
} else {
|
|
||||||
let offset = self
|
|
||||||
.bytes_at(byte_idx + 1)
|
|
||||||
.reversed()
|
|
||||||
.take(4)
|
|
||||||
.position(is_utf8_char_boundary)
|
|
||||||
// A char can only be four bytes long so we are guaranteed to find a boundary.
|
|
||||||
.unwrap();
|
|
||||||
|
|
||||||
byte_idx - offset
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn ceil_char_boundary(self, byte_idx: usize) -> usize {
|
|
||||||
if byte_idx > self.len_bytes() {
|
|
||||||
self.len_bytes()
|
|
||||||
} else {
|
|
||||||
let upper_bound = self.len_bytes().min(byte_idx + 4);
|
|
||||||
self.bytes_at(byte_idx)
|
|
||||||
.position(is_utf8_char_boundary)
|
|
||||||
.map_or(upper_bound, |pos| pos + byte_idx)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn is_char_boundary(self, byte_idx: usize) -> bool {
|
|
||||||
if byte_idx == 0 {
|
|
||||||
return true;
|
|
||||||
}
|
|
||||||
|
|
||||||
if byte_idx >= self.len_bytes() {
|
|
||||||
byte_idx == self.len_bytes()
|
|
||||||
} else {
|
|
||||||
is_utf8_char_boundary(self.bytes_at(byte_idx).next().unwrap())
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
fn floor_grapheme_boundary(self, mut byte_idx: usize) -> usize {
|
fn floor_grapheme_boundary(self, mut byte_idx: usize) -> usize {
|
||||||
if byte_idx >= self.len_bytes() {
|
if byte_idx >= self.len() {
|
||||||
return self.len_bytes();
|
return self.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
byte_idx = self.ceil_char_boundary(byte_idx + 1);
|
byte_idx = self.ceil_char_boundary(byte_idx + 1);
|
||||||
|
|
||||||
let (mut chunk, mut chunk_byte_idx, _, _) = self.chunk_at_byte(byte_idx);
|
let mut chunk_cursor = self.chunk_cursor_at(byte_idx);
|
||||||
|
let mut cursor = GraphemeCursor::new(byte_idx, self.len(), true);
|
||||||
let mut cursor = GraphemeCursor::new(byte_idx, self.len_bytes(), true);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match cursor.prev_boundary(chunk, chunk_byte_idx) {
|
match cursor.prev_boundary(chunk_cursor.chunk(), chunk_cursor.byte_offset()) {
|
||||||
Ok(None) => return 0,
|
Ok(None) => return 0,
|
||||||
Ok(Some(boundary)) => return boundary,
|
Ok(Some(boundary)) => return boundary,
|
||||||
Err(GraphemeIncomplete::PrevChunk) => {
|
Err(GraphemeIncomplete::PrevChunk) => assert!(chunk_cursor.prev()),
|
||||||
let (ch, ch_byte_idx, _, _) = self.chunk_at_byte(chunk_byte_idx - 1);
|
|
||||||
chunk = ch;
|
|
||||||
chunk_byte_idx = ch_byte_idx;
|
|
||||||
}
|
|
||||||
Err(GraphemeIncomplete::PreContext(n)) => {
|
Err(GraphemeIncomplete::PreContext(n)) => {
|
||||||
let ctx_chunk = self.chunk_at_byte(n - 1).0;
|
let ctx_chunk = self.chunk(n - 1).0;
|
||||||
cursor.provide_context(ctx_chunk, n - ctx_chunk.len());
|
cursor.provide_context(ctx_chunk, n - ctx_chunk.len());
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
|
@ -282,8 +186,8 @@ impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn ceil_grapheme_boundary(self, mut byte_idx: usize) -> usize {
|
fn ceil_grapheme_boundary(self, mut byte_idx: usize) -> usize {
|
||||||
if byte_idx >= self.len_bytes() {
|
if byte_idx >= self.len() {
|
||||||
return self.len_bytes();
|
return self.len();
|
||||||
}
|
}
|
||||||
|
|
||||||
if byte_idx == 0 {
|
if byte_idx == 0 {
|
||||||
|
@ -292,20 +196,15 @@ impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
|
||||||
|
|
||||||
byte_idx = self.floor_char_boundary(byte_idx - 1);
|
byte_idx = self.floor_char_boundary(byte_idx - 1);
|
||||||
|
|
||||||
let (mut chunk, mut chunk_byte_idx, _, _) = self.chunk_at_byte(byte_idx);
|
let mut chunk_cursor = self.chunk_cursor_at(byte_idx);
|
||||||
|
let mut cursor = GraphemeCursor::new(byte_idx, self.len(), true);
|
||||||
let mut cursor = GraphemeCursor::new(byte_idx, self.len_bytes(), true);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match cursor.next_boundary(chunk, chunk_byte_idx) {
|
match cursor.next_boundary(chunk_cursor.chunk(), chunk_cursor.byte_offset()) {
|
||||||
Ok(None) => return self.len_bytes(),
|
Ok(None) => return self.len(),
|
||||||
Ok(Some(boundary)) => return boundary,
|
Ok(Some(boundary)) => return boundary,
|
||||||
Err(GraphemeIncomplete::NextChunk) => {
|
Err(GraphemeIncomplete::NextChunk) => assert!(chunk_cursor.next()),
|
||||||
chunk_byte_idx += chunk.len();
|
|
||||||
chunk = self.chunk_at_byte(chunk_byte_idx).0;
|
|
||||||
}
|
|
||||||
Err(GraphemeIncomplete::PreContext(n)) => {
|
Err(GraphemeIncomplete::PreContext(n)) => {
|
||||||
let ctx_chunk = self.chunk_at_byte(n - 1).0;
|
let ctx_chunk = self.chunk(n - 1).0;
|
||||||
cursor.provide_context(ctx_chunk, n - ctx_chunk.len());
|
cursor.provide_context(ctx_chunk, n - ctx_chunk.len());
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
|
@ -319,15 +218,13 @@ impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
|
||||||
return false;
|
return false;
|
||||||
}
|
}
|
||||||
|
|
||||||
let (chunk, chunk_byte_idx, _, _) = self.chunk_at_byte(byte_idx);
|
let (chunk, chunk_byte_idx) = self.chunk(byte_idx);
|
||||||
|
let mut cursor = GraphemeCursor::new(byte_idx, self.len(), true);
|
||||||
let mut cursor = GraphemeCursor::new(byte_idx, self.len_bytes(), true);
|
|
||||||
|
|
||||||
loop {
|
loop {
|
||||||
match cursor.is_boundary(chunk, chunk_byte_idx) {
|
match cursor.is_boundary(chunk, chunk_byte_idx) {
|
||||||
Ok(n) => return n,
|
Ok(n) => return n,
|
||||||
Err(GraphemeIncomplete::PreContext(n)) => {
|
Err(GraphemeIncomplete::PreContext(n)) => {
|
||||||
let (ctx_chunk, ctx_byte_start, _, _) = self.chunk_at_byte(n - 1);
|
let (ctx_chunk, ctx_byte_start) = self.chunk(n - 1);
|
||||||
cursor.provide_context(ctx_chunk, ctx_byte_start);
|
cursor.provide_context(ctx_chunk, ctx_byte_start);
|
||||||
}
|
}
|
||||||
Err(_) => unreachable!(),
|
Err(_) => unreachable!(),
|
||||||
|
@ -336,61 +233,30 @@ impl<'a> RopeSliceExt<'a> for RopeSlice<'a> {
|
||||||
}
|
}
|
||||||
|
|
||||||
fn graphemes(self) -> RopeGraphemes<'a> {
|
fn graphemes(self) -> RopeGraphemes<'a> {
|
||||||
let mut chunks = self.chunks();
|
|
||||||
let first_chunk = chunks.next().unwrap_or("");
|
|
||||||
RopeGraphemes {
|
RopeGraphemes {
|
||||||
|
chunk_cursor: self.chunk_cursor(),
|
||||||
text: self,
|
text: self,
|
||||||
chunks,
|
cursor: GraphemeCursor::new(0, self.len(), true),
|
||||||
cur_chunk: first_chunk,
|
|
||||||
cur_chunk_start: 0,
|
|
||||||
cursor: GraphemeCursor::new(0, self.len_bytes(), true),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fn graphemes_rev(self) -> RevRopeGraphemes<'a> {
|
fn graphemes_rev(self) -> RevRopeGraphemes<'a> {
|
||||||
let (mut chunks, mut cur_chunk_start, _, _) = self.chunks_at_byte(self.len_bytes());
|
|
||||||
chunks.reverse();
|
|
||||||
let first_chunk = chunks.next().unwrap_or("");
|
|
||||||
cur_chunk_start -= first_chunk.len();
|
|
||||||
RevRopeGraphemes {
|
RevRopeGraphemes {
|
||||||
|
chunk_cursor: self.chunk_cursor_at(self.len()),
|
||||||
text: self,
|
text: self,
|
||||||
chunks,
|
cursor: GraphemeCursor::new(self.len(), self.len(), true),
|
||||||
cur_chunk: first_chunk,
|
|
||||||
cur_chunk_start,
|
|
||||||
cursor: GraphemeCursor::new(self.len_bytes(), self.len_bytes(), true),
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
|
||||||
}
|
|
||||||
|
|
||||||
/// An iterator over the graphemes of a `RopeSlice`.
|
/// An iterator over the graphemes of a `RopeSlice`.
|
||||||
#[derive(Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct RopeGraphemes<'a> {
|
pub struct RopeGraphemes<'a> {
|
||||||
text: RopeSlice<'a>,
|
text: RopeSlice<'a>,
|
||||||
chunks: Chunks<'a>,
|
chunk_cursor: ChunkCursor<'a>,
|
||||||
cur_chunk: &'a str,
|
|
||||||
cur_chunk_start: usize,
|
|
||||||
cursor: GraphemeCursor,
|
cursor: GraphemeCursor,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for RopeGraphemes<'_> {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.debug_struct("RopeGraphemes")
|
|
||||||
.field("text", &self.text)
|
|
||||||
.field("chunks", &self.chunks)
|
|
||||||
.field("cur_chunk", &self.cur_chunk)
|
|
||||||
.field("cur_chunk_start", &self.cur_chunk_start)
|
|
||||||
// .field("cursor", &self.cursor)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Iterator for RopeGraphemes<'a> {
|
impl<'a> Iterator for RopeGraphemes<'a> {
|
||||||
type Item = RopeSlice<'a>;
|
type Item = RopeSlice<'a>;
|
||||||
|
|
||||||
|
@ -400,7 +266,7 @@ impl<'a> Iterator for RopeGraphemes<'a> {
|
||||||
loop {
|
loop {
|
||||||
match self
|
match self
|
||||||
.cursor
|
.cursor
|
||||||
.next_boundary(self.cur_chunk, self.cur_chunk_start)
|
.next_boundary(self.chunk_cursor.chunk(), self.chunk_cursor.byte_offset())
|
||||||
{
|
{
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
return None;
|
return None;
|
||||||
|
@ -409,50 +275,33 @@ impl<'a> Iterator for RopeGraphemes<'a> {
|
||||||
b = n;
|
b = n;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Err(GraphemeIncomplete::NextChunk) => {
|
Err(GraphemeIncomplete::NextChunk) => assert!(self.chunk_cursor.next()),
|
||||||
self.cur_chunk_start += self.cur_chunk.len();
|
|
||||||
self.cur_chunk = self.chunks.next().unwrap_or("");
|
|
||||||
}
|
|
||||||
Err(GraphemeIncomplete::PreContext(idx)) => {
|
Err(GraphemeIncomplete::PreContext(idx)) => {
|
||||||
let (chunk, byte_idx, _, _) = self.text.chunk_at_byte(idx.saturating_sub(1));
|
let (chunk, byte_idx) = self.text.chunk(idx.saturating_sub(1));
|
||||||
self.cursor.provide_context(chunk, byte_idx);
|
self.cursor.provide_context(chunk, byte_idx);
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if a < self.cur_chunk_start {
|
if a < self.chunk_cursor.byte_offset() {
|
||||||
Some(self.text.byte_slice(a..b))
|
Some(self.text.slice(a..b))
|
||||||
} else {
|
} else {
|
||||||
let a2 = a - self.cur_chunk_start;
|
let a2 = a - self.chunk_cursor.byte_offset();
|
||||||
let b2 = b - self.cur_chunk_start;
|
let b2 = b - self.chunk_cursor.byte_offset();
|
||||||
Some((&self.cur_chunk[a2..b2]).into())
|
Some((&self.chunk_cursor.chunk()[a2..b2]).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
/// An iterator over the graphemes of a `RopeSlice` in reverse.
|
/// An iterator over the graphemes of a `RopeSlice` in reverse.
|
||||||
#[derive(Clone)]
|
#[derive(Debug, Clone)]
|
||||||
pub struct RevRopeGraphemes<'a> {
|
pub struct RevRopeGraphemes<'a> {
|
||||||
text: RopeSlice<'a>,
|
text: RopeSlice<'a>,
|
||||||
chunks: Chunks<'a>,
|
chunk_cursor: ChunkCursor<'a>,
|
||||||
cur_chunk: &'a str,
|
|
||||||
cur_chunk_start: usize,
|
|
||||||
cursor: GraphemeCursor,
|
cursor: GraphemeCursor,
|
||||||
}
|
}
|
||||||
|
|
||||||
impl fmt::Debug for RevRopeGraphemes<'_> {
|
|
||||||
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
|
|
||||||
f.debug_struct("RevRopeGraphemes")
|
|
||||||
.field("text", &self.text)
|
|
||||||
.field("chunks", &self.chunks)
|
|
||||||
.field("cur_chunk", &self.cur_chunk)
|
|
||||||
.field("cur_chunk_start", &self.cur_chunk_start)
|
|
||||||
// .field("cursor", &self.cursor)
|
|
||||||
.finish()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
impl<'a> Iterator for RevRopeGraphemes<'a> {
|
impl<'a> Iterator for RevRopeGraphemes<'a> {
|
||||||
type Item = RopeSlice<'a>;
|
type Item = RopeSlice<'a>;
|
||||||
|
|
||||||
|
@ -462,7 +311,7 @@ impl<'a> Iterator for RevRopeGraphemes<'a> {
|
||||||
loop {
|
loop {
|
||||||
match self
|
match self
|
||||||
.cursor
|
.cursor
|
||||||
.prev_boundary(self.cur_chunk, self.cur_chunk_start)
|
.prev_boundary(self.chunk_cursor.chunk(), self.chunk_cursor.byte_offset())
|
||||||
{
|
{
|
||||||
Ok(None) => {
|
Ok(None) => {
|
||||||
return None;
|
return None;
|
||||||
|
@ -471,24 +320,21 @@ impl<'a> Iterator for RevRopeGraphemes<'a> {
|
||||||
b = n;
|
b = n;
|
||||||
break;
|
break;
|
||||||
}
|
}
|
||||||
Err(GraphemeIncomplete::PrevChunk) => {
|
Err(GraphemeIncomplete::PrevChunk) => assert!(self.chunk_cursor.prev()),
|
||||||
self.cur_chunk = self.chunks.next().unwrap_or("");
|
|
||||||
self.cur_chunk_start -= self.cur_chunk.len();
|
|
||||||
}
|
|
||||||
Err(GraphemeIncomplete::PreContext(idx)) => {
|
Err(GraphemeIncomplete::PreContext(idx)) => {
|
||||||
let (chunk, byte_idx, _, _) = self.text.chunk_at_byte(idx.saturating_sub(1));
|
let (chunk, byte_idx) = self.text.chunk(idx.saturating_sub(1));
|
||||||
self.cursor.provide_context(chunk, byte_idx);
|
self.cursor.provide_context(chunk, byte_idx);
|
||||||
}
|
}
|
||||||
_ => unreachable!(),
|
_ => unreachable!(),
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if a >= self.cur_chunk_start + self.cur_chunk.len() {
|
if a >= self.chunk_cursor.byte_offset() + self.chunk_cursor.chunk().len() {
|
||||||
Some(self.text.byte_slice(b..a))
|
Some(self.text.slice(b..a))
|
||||||
} else {
|
} else {
|
||||||
let a2 = a - self.cur_chunk_start;
|
let a2 = a - self.chunk_cursor.byte_offset();
|
||||||
let b2 = b - self.cur_chunk_start;
|
let b2 = b - self.chunk_cursor.byte_offset();
|
||||||
Some((&self.cur_chunk[b2..a2]).into())
|
Some((&self.chunk_cursor.chunk()[b2..a2]).into())
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
@ -509,46 +355,11 @@ mod tests {
|
||||||
assert!(RopeSlice::from("asdf").ends_with("f"));
|
assert!(RopeSlice::from("asdf").ends_with("f"));
|
||||||
}
|
}
|
||||||
|
|
||||||
#[test]
|
|
||||||
fn char_boundaries() {
|
|
||||||
let ascii = RopeSlice::from("ascii");
|
|
||||||
// When the given index lies on a character boundary, the index should not change.
|
|
||||||
for byte_idx in 0..=ascii.len_bytes() {
|
|
||||||
assert_eq!(ascii.floor_char_boundary(byte_idx), byte_idx);
|
|
||||||
assert_eq!(ascii.ceil_char_boundary(byte_idx), byte_idx);
|
|
||||||
assert!(ascii.is_char_boundary(byte_idx));
|
|
||||||
}
|
|
||||||
|
|
||||||
// This is a polyfill of a method of this trait which was replaced by ceil_char_boundary.
|
|
||||||
// It returns the _character index_ of the given byte index, rounding up if it does not
|
|
||||||
// already lie on a character boundary.
|
|
||||||
fn byte_to_next_char(slice: RopeSlice, byte_idx: usize) -> usize {
|
|
||||||
slice.byte_to_char(slice.ceil_char_boundary(byte_idx))
|
|
||||||
}
|
|
||||||
|
|
||||||
for i in 0..=6 {
|
|
||||||
assert_eq!(byte_to_next_char(RopeSlice::from("foobar"), i), i);
|
|
||||||
}
|
|
||||||
for char_idx in 0..10 {
|
|
||||||
let len = "😆".len();
|
|
||||||
assert_eq!(
|
|
||||||
byte_to_next_char(RopeSlice::from("😆😆😆😆😆😆😆😆😆😆"), char_idx * len),
|
|
||||||
char_idx
|
|
||||||
);
|
|
||||||
for i in 1..=len {
|
|
||||||
assert_eq!(
|
|
||||||
byte_to_next_char(RopeSlice::from("😆😆😆😆😆😆😆😆😆😆"), char_idx * len + i),
|
|
||||||
char_idx + 1
|
|
||||||
);
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn grapheme_boundaries() {
|
fn grapheme_boundaries() {
|
||||||
let ascii = RopeSlice::from("ascii");
|
let ascii = RopeSlice::from("ascii");
|
||||||
// When the given index lies on a grapheme boundary, the index should not change.
|
// When the given index lies on a grapheme boundary, the index should not change.
|
||||||
for byte_idx in 0..=ascii.len_bytes() {
|
for byte_idx in 0..=ascii.len() {
|
||||||
assert_eq!(ascii.floor_char_boundary(byte_idx), byte_idx);
|
assert_eq!(ascii.floor_char_boundary(byte_idx), byte_idx);
|
||||||
assert_eq!(ascii.ceil_char_boundary(byte_idx), byte_idx);
|
assert_eq!(ascii.ceil_char_boundary(byte_idx), byte_idx);
|
||||||
assert!(ascii.is_grapheme_boundary(byte_idx));
|
assert!(ascii.is_grapheme_boundary(byte_idx));
|
||||||
|
@ -558,7 +369,7 @@ mod tests {
|
||||||
// 13 bytes, hex: f0 9f 8f b4 + e2 80 8d + e2 98 a0 + ef b8 8f
|
// 13 bytes, hex: f0 9f 8f b4 + e2 80 8d + e2 98 a0 + ef b8 8f
|
||||||
let g = RopeSlice::from("🏴☠️\r\n");
|
let g = RopeSlice::from("🏴☠️\r\n");
|
||||||
let emoji_len = "🏴☠️".len();
|
let emoji_len = "🏴☠️".len();
|
||||||
let end = g.len_bytes();
|
let end = g.len();
|
||||||
|
|
||||||
for byte_idx in 0..emoji_len {
|
for byte_idx in 0..emoji_len {
|
||||||
assert_eq!(g.floor_grapheme_boundary(byte_idx), 0);
|
assert_eq!(g.floor_grapheme_boundary(byte_idx), 0);
|
||||||
|
|
Loading…
Reference in New Issue