Clamp UTF-16 to point conversions
This commit is contained in:
parent
bb32599ded
commit
074e3cfbd6
6 changed files with 65 additions and 45 deletions
|
@ -259,7 +259,7 @@ impl Rope {
|
|||
.map_or(0, |chunk| chunk.point_to_offset(overshoot))
|
||||
}
|
||||
|
||||
pub fn point_utf16_to_offset(&self, point: PointUtf16, clamp: bool) -> usize {
|
||||
pub fn point_utf16_to_offset_clamped(&self, point: PointUtf16) -> usize {
|
||||
if point >= self.summary().lines_utf16() {
|
||||
return self.summary().len;
|
||||
}
|
||||
|
@ -269,7 +269,7 @@ impl Rope {
|
|||
cursor.start().1
|
||||
+ cursor
|
||||
.item()
|
||||
.map_or(0, |chunk| chunk.point_utf16_to_offset(overshoot, clamp))
|
||||
.map_or(0, |chunk| chunk.point_utf16_to_offset_clamped(overshoot))
|
||||
}
|
||||
|
||||
pub fn point_utf16_to_point(&self, point: PointUtf16) -> Point {
|
||||
|
@ -711,7 +711,7 @@ impl Chunk {
|
|||
point_utf16
|
||||
}
|
||||
|
||||
fn point_utf16_to_offset(&self, target: PointUtf16, clamp: bool) -> usize {
|
||||
fn point_utf16_to_offset_clamped(&self, target: PointUtf16) -> usize {
|
||||
let mut offset = 0;
|
||||
let mut point = PointUtf16::new(0, 0);
|
||||
for ch in self.0.chars() {
|
||||
|
@ -724,26 +724,19 @@ impl Chunk {
|
|||
point.column = 0;
|
||||
|
||||
if point.row > target.row {
|
||||
if clamp {
|
||||
//Return the offset up to but not including the newline
|
||||
return offset;
|
||||
}
|
||||
panic!(
|
||||
"point {:?} is beyond the end of a line with length {}",
|
||||
target, point.column
|
||||
);
|
||||
//point is beyond the end of the line
|
||||
//Return the offset up to but not including the newline
|
||||
return offset;
|
||||
}
|
||||
} else {
|
||||
point.column += ch.len_utf16() as u32;
|
||||
}
|
||||
|
||||
if point > target {
|
||||
if clamp {
|
||||
//Return the offset before adding the len of the codepoint which
|
||||
//we have landed within, bias left
|
||||
return offset;
|
||||
}
|
||||
panic!("point {:?} is inside of character {:?}", target, ch);
|
||||
//point is inside of a codepoint
|
||||
//Return the offset before adding the len of the codepoint which
|
||||
//we have landed within, bias left
|
||||
return offset;
|
||||
}
|
||||
|
||||
offset += ch.len_utf8();
|
||||
|
@ -1222,7 +1215,7 @@ mod tests {
|
|||
point
|
||||
);
|
||||
assert_eq!(
|
||||
actual.point_utf16_to_offset(point_utf16, false),
|
||||
actual.point_utf16_to_offset_clamped(point_utf16),
|
||||
ix,
|
||||
"point_utf16_to_offset({:?})",
|
||||
point_utf16
|
||||
|
@ -1262,9 +1255,9 @@ mod tests {
|
|||
let left_point = actual.clip_point_utf16(point_utf16, Bias::Left);
|
||||
let right_point = actual.clip_point_utf16(point_utf16, Bias::Right);
|
||||
assert!(right_point >= left_point);
|
||||
// Ensure translating valid UTF-16 points to offsets doesn't panic.
|
||||
actual.point_utf16_to_offset(left_point, false);
|
||||
actual.point_utf16_to_offset(right_point, false);
|
||||
// Ensure translating UTF-16 points to offsets doesn't panic.
|
||||
actual.point_utf16_to_offset_clamped(left_point);
|
||||
actual.point_utf16_to_offset_clamped(right_point);
|
||||
|
||||
offset_utf16.0 += 1;
|
||||
if unit == b'\n' as u16 {
|
||||
|
|
Loading…
Add table
Add a link
Reference in a new issue