mirror of
https://github.com/zed-industries/zed.git
synced 2025-02-10 20:29:05 +00:00
Don't slice midway through multi-byte char when detecting line ending
This commit is contained in:
parent
5e2306d0e0
commit
113eb9b94f
2 changed files with 18 additions and 3 deletions
|
@ -154,6 +154,17 @@ fn test_random_edits(mut rng: StdRng) {
|
||||||
|
|
||||||
#[test]
|
#[test]
|
||||||
fn test_line_endings() {
|
fn test_line_endings() {
|
||||||
|
assert_eq!(LineEnding::detect(&"🍐✅\n".repeat(1000)), LineEnding::Unix);
|
||||||
|
assert_eq!(LineEnding::detect(&"abcd\n".repeat(1000)), LineEnding::Unix);
|
||||||
|
assert_eq!(
|
||||||
|
LineEnding::detect(&"🍐✅\r\n".repeat(1000)),
|
||||||
|
LineEnding::Windows
|
||||||
|
);
|
||||||
|
assert_eq!(
|
||||||
|
LineEnding::detect(&"abcd\r\n".repeat(1000)),
|
||||||
|
LineEnding::Windows
|
||||||
|
);
|
||||||
|
|
||||||
let mut buffer = Buffer::new(0, 0, "one\r\ntwo".into());
|
let mut buffer = Buffer::new(0, 0, "one\r\ntwo".into());
|
||||||
assert_eq!(buffer.text(), "one\ntwo");
|
assert_eq!(buffer.text(), "one\ntwo");
|
||||||
assert_eq!(buffer.line_ending(), LineEnding::Windows);
|
assert_eq!(buffer.line_ending(), LineEnding::Windows);
|
||||||
|
|
|
@ -2353,9 +2353,13 @@ impl LineEnding {
|
||||||
}
|
}
|
||||||
|
|
||||||
pub fn detect(text: &str) -> Self {
|
pub fn detect(text: &str) -> Self {
|
||||||
if let Some(ix) = text[..cmp::min(text.len(), 1000)].find(&['\n']) {
|
let mut max_ix = cmp::min(text.len(), 1000);
|
||||||
let text = text.as_bytes();
|
while !text.is_char_boundary(max_ix) {
|
||||||
if ix > 0 && text[ix - 1] == b'\r' {
|
max_ix -= 1;
|
||||||
|
}
|
||||||
|
|
||||||
|
if let Some(ix) = text[..max_ix].find(&['\n']) {
|
||||||
|
if ix > 0 && text.as_bytes()[ix - 1] == b'\r' {
|
||||||
Self::Windows
|
Self::Windows
|
||||||
} else {
|
} else {
|
||||||
Self::Unix
|
Self::Unix
|
||||||
|
|
Loading…
Reference in a new issue