mirror of
https://github.com/hexedtech/codemp.git
synced 2024-11-22 15:24:48 +01:00
fix: apply() panic when out of bounds
This commit is contained in:
parent
10e4a458ce
commit
1f50f75eb4
1 changed files with 4 additions and 2 deletions
|
@ -73,14 +73,16 @@ impl TextChange {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn apply(&self, txt: &str) -> String {
|
pub fn apply(&self, txt: &str) -> String {
|
||||||
let pre = txt[..self.span.start].to_string();
|
let pre_index = std::cmp::min(self.span.start, txt.len());
|
||||||
let post = txt[self.span.end..].to_string();
|
let pre = txt.get(..pre_index).unwrap_or("").to_string();
|
||||||
|
let post = txt.get(self.span.end..).unwrap_or("").to_string();
|
||||||
format!("{}{}{}", pre, self.content, post)
|
format!("{}{}{}", pre, self.content, post)
|
||||||
}
|
}
|
||||||
|
|
||||||
/// convert from byte index to row and column
|
/// convert from byte index to row and column
|
||||||
/// txt must be the whole content of the buffer, in order to count lines
|
/// txt must be the whole content of the buffer, in order to count lines
|
||||||
pub fn index_to_rowcol(txt: &str, index: usize) -> crate::proto::RowCol {
|
pub fn index_to_rowcol(txt: &str, index: usize) -> crate::proto::RowCol {
|
||||||
|
// FIXME might panic, use .get()
|
||||||
let row = txt[..index].matches('\n').count() as i32;
|
let row = txt[..index].matches('\n').count() as i32;
|
||||||
let col = txt[..index].split('\n').last().unwrap_or("").len() as i32;
|
let col = txt[..index].split('\n').last().unwrap_or("").len() as i32;
|
||||||
crate::proto::RowCol { row, col }
|
crate::proto::RowCol { row, col }
|
||||||
|
|
Loading…
Reference in a new issue