Skip to content

Commit 4392ed3

Browse files
authored
Merge pull request #262 from epage/slovak
fix(cli): Don't panic when finding column
2 parents b4e198a + 9859e60 commit 4392ed3

File tree

3 files changed

+58
-15
lines changed

3 files changed

+58
-15
lines changed

CHANGELOG.md

+4
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ and this project adheres to [Semantic Versioning](http://semver.org/).
77
<!-- next-header -->
88
## [Unreleased] - ReleaseDate
99

10+
#### Bug Fixes
11+
12+
- Don't panic when rendering typos on lines with non-ASCII character
13+
1014
## [1.0.1] - 2021-05-27
1115

1216
#### Bug Fixes

src/bin/typos-cli/report.rs

+2-5
Original file line numberDiff line numberDiff line change
@@ -190,11 +190,8 @@ fn print_long_correction(msg: &Typo, palette: Palette) -> Result<(), std::io::Er
190190

191191
let line = String::from_utf8_lossy(msg.buffer.as_ref());
192192
let line = line.replace("\t", " ");
193-
let column = unicode_segmentation::UnicodeSegmentation::graphemes(
194-
line.get(0..msg.byte_offset).unwrap(),
195-
true,
196-
)
197-
.count();
193+
let start = String::from_utf8_lossy(&msg.buffer[0..msg.byte_offset]);
194+
let column = unicode_segmentation::UnicodeSegmentation::graphemes(start.as_ref(), true).count();
198195
match &msg.corrections {
199196
typos::Status::Valid => {}
200197
typos::Status::Invalid => {

src/file.rs

+52-10
Original file line numberDiff line numberDiff line change
@@ -702,36 +702,78 @@ mod test {
702702

703703
#[test]
704704
fn test_extract_line_single_line() {
705-
let (line, offset) = extract_line(b"hello world", 6);
706-
assert_eq!(line, b"hello world");
705+
let buffer = b"hello world";
706+
let buffer_offset = 6;
707+
let expected_line = b"hello world";
708+
let (line, offset) = extract_line(buffer, buffer_offset);
709+
assert_eq!(line, expected_line);
707710
assert_eq!(offset, 6);
711+
assert_eq!(line[offset], buffer[buffer_offset]);
708712
}
709713

710714
#[test]
711715
fn test_extract_line_first() {
712-
let (line, offset) = extract_line(b"1\n2\n3", 0);
713-
assert_eq!(line, b"1");
716+
let buffer = b"1\n2\n3";
717+
let buffer_offset = 0;
718+
let expected_line = b"1";
719+
let (line, offset) = extract_line(buffer, buffer_offset);
720+
assert_eq!(line, expected_line);
714721
assert_eq!(offset, 0);
722+
assert_eq!(line[offset], buffer[buffer_offset]);
715723
}
716724

717725
#[test]
718726
fn test_extract_line_middle() {
719-
let (line, offset) = extract_line(b"1\n2\n3", 2);
720-
assert_eq!(line, b"2");
727+
let buffer = b"1\n2\n3";
728+
let buffer_offset = 2;
729+
let expected_line = b"2";
730+
let (line, offset) = extract_line(buffer, buffer_offset);
731+
assert_eq!(line, expected_line);
721732
assert_eq!(offset, 0);
733+
assert_eq!(line[offset], buffer[buffer_offset]);
722734
}
723735

724736
#[test]
725737
fn test_extract_line_end() {
726-
let (line, offset) = extract_line(b"1\n2\n3", 4);
727-
assert_eq!(line, b"3");
738+
let buffer = b"1\n2\n3";
739+
let buffer_offset = 4;
740+
let expected_line = b"3";
741+
let (line, offset) = extract_line(buffer, buffer_offset);
742+
assert_eq!(line, expected_line);
728743
assert_eq!(offset, 0);
744+
assert_eq!(line[offset], buffer[buffer_offset]);
729745
}
730746

731747
#[test]
732748
fn test_extract_line_offset_change() {
733-
let (line, offset) = extract_line(b"1\nhello world\n2", 8);
734-
assert_eq!(line, b"hello world");
749+
let buffer = b"1\nhello world\n2";
750+
let buffer_offset = 8;
751+
let expected_line = b"hello world";
752+
let (line, offset) = extract_line(buffer, buffer_offset);
753+
assert_eq!(line, expected_line);
735754
assert_eq!(offset, 6);
755+
assert_eq!(line[offset], buffer[buffer_offset]);
756+
}
757+
758+
#[test]
759+
fn test_extract_line_windows() {
760+
let buffer = b"1\r\nhello world\r\n2";
761+
let buffer_offset = 9;
762+
let expected_line = b"hello world";
763+
let (line, offset) = extract_line(buffer, buffer_offset);
764+
assert_eq!(line, expected_line);
765+
assert_eq!(offset, 6);
766+
assert_eq!(line[offset], buffer[buffer_offset]);
767+
}
768+
769+
#[test]
770+
fn test_extract_line_slovak() {
771+
let buffer = b"LastErrorMessage=%1.%n%nChyba %2: %3\r\nSetupFileMissing=In\x9Atala\xE8n\xFD adres\xE1r neobsahuje s\xFAbor %1. Opravte, pros\xEDm, t\xFAto chybu alebo si zaobstarajte nov\xFA k\xF3piu tohto produktu.\r\nSetupFileCorrupt=S\xFAbory sprievodcu in\x9Atal\xE1ciou s\xFA po\x9Akoden\xE9. Zaobstarajte si, pros\xEDm, nov\xFA k\xF3piu tohto produktu.";
772+
let buffer_offset = 66;
773+
let expected_line = b"SetupFileMissing=In\x9Atala\xE8n\xFD adres\xE1r neobsahuje s\xFAbor %1. Opravte, pros\xEDm, t\xFAto chybu alebo si zaobstarajte nov\xFA k\xF3piu tohto produktu.";
774+
let (line, offset) = extract_line(buffer, buffer_offset);
775+
assert_eq!(line, expected_line);
776+
assert_eq!(offset, 28);
777+
assert_eq!(line[offset], buffer[buffer_offset]);
736778
}
737779
}

0 commit comments

Comments
 (0)