Use iterator methods to make auto-detect code a little cleaner.

Also use chars instead of graphemes for indent detection code.
Graphemes are slower, and don't provide any benefit there.
This commit is contained in:
Nathan Vegdahl 2018-01-01 14:06:17 -08:00
parent 7121469cba
commit 020827806b

View File

@ -94,9 +94,8 @@ impl<T: LineFormatter> Editor<T> {
pub fn auto_detect_line_ending(&mut self) { pub fn auto_detect_line_ending(&mut self) {
let mut line_ending_histogram: [usize; 8] = [0, 0, 0, 0, 0, 0, 0, 0]; let mut line_ending_histogram: [usize; 8] = [0, 0, 0, 0, 0, 0, 0, 0];
// Collect statistics // Collect statistics on the first 100 lines
let mut line_i: usize = 0; for line in self.buffer.line_iter().take(100) {
for line in self.buffer.line_iter() {
// Get the line ending // Get the line ending
let ending = if line.len_chars() == 1 { let ending = if line.len_chars() == 1 {
let g = line.slice(line.len_chars() - 1, line.len_chars()) let g = line.slice(line.len_chars() - 1, line.len_chars())
@ -142,12 +141,6 @@ impl<T: LineFormatter> Editor<T> {
line_ending_histogram[7] += 1; line_ending_histogram[7] += 1;
} }
} }
// Stop after 100 lines
line_i += 1;
if line_i > 100 {
break;
}
} }
// Analyze stats and make a determination // Analyze stats and make a determination
@ -183,16 +176,15 @@ impl<T: LineFormatter> Editor<T> {
let mut last_indent = (false, 0usize); // (was_tabs, indent_count) let mut last_indent = (false, 0usize); // (was_tabs, indent_count)
// Collect statistics // Collect statistics on the first 1000 lines
let mut line_i: usize = 0; for line in self.buffer.line_iter().take(1000) {
for line in self.buffer.line_iter() { let mut c_iter = line.chars();
let mut g_iter = line.graphemes(); match c_iter.next() {
match g_iter.next() { Some('\t') => {
Some("\t") => {
// Count leading tabs // Count leading tabs
let mut count = 1; let mut count = 1;
for g in g_iter { for c in c_iter {
if g == "\t" { if c == '\t' {
count += 1; count += 1;
} else { } else {
break; break;
@ -208,11 +200,11 @@ impl<T: LineFormatter> Editor<T> {
last_indent = (true, count); last_indent = (true, count);
} }
Some(" ") => { Some(' ') => {
// Count leading spaces // Count leading spaces
let mut count = 1; let mut count = 1;
for g in g_iter { for c in c_iter {
if g == " " { if c == ' ' {
count += 1; count += 1;
} else { } else {
break; break;
@ -236,12 +228,6 @@ impl<T: LineFormatter> Editor<T> {
_ => {} _ => {}
} }
// Stop after 1000 lines
line_i += 1;
if line_i > 1000 {
break;
}
} }
// Analyze stats and make a determination // Analyze stats and make a determination