Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

textinput.rs replace_selection now slices wrt bytes rather than chars #20208

Closed
wants to merge 6 commits into from

textinput new len n codepoints messages

  • Loading branch information
sarkhanbayramli committed Mar 12, 2018
commit d18629feecf9f597641c4abaf04439c013fe1078
@@ -159,6 +159,27 @@ fn len_of_first_n_chars(text: &str, n: usize) -> usize {
/// The length in bytes of the first n code units a string when encoded in UTF-16.
///
/// If the string is fewer than n code units, returns the length of the whole string.


fn len_of_first_n_code_units_new(text: &str, n: usize) -> Option<usize> {//usize {
if n == 0 {
return Some(0);
}
let mut utf8_len = 0;
let mut utf16_len = 0;
for c in text.chars() {
utf16_len += c.len_utf16();
utf8_len += c.len_utf8();
if utf16_len == n {
break;
}
if utf16_len > n {
return None;
}
}
Some(utf8_len)
}

fn len_of_first_n_code_units(text: &str, n: usize) -> usize {
let mut utf8_len = 0;
let mut utf16_len = 0;
@@ -372,18 +393,27 @@ impl<T: ClipboardProvider> TextInput<T> {
} else {
usize::MAX
};

println!("line: {}", &self.lines[start.line]);
println!("line bytes:");
for x in self.lines[start.line].bytes() {
println!("byte: {}", x);
}

let last_char_index_new = len_of_first_n_code_units_new(&*insert, allowed_to_insert_count).unwrap_or(999 as usize);
let last_char_index = len_of_first_n_code_units(&*insert, allowed_to_insert_count);
println!("last_char_index_new: {} last_char_index: {}", last_char_index_new, last_char_index);
let chars_to_insert = &insert[..last_char_index];

self.clear_selection();

// Calculate byte start and end for handling multi-byte characters
let start_index_b = len_of_first_n_code_units(&self.lines[start.line], start.index);
let end_index_b = len_of_first_n_code_units(&self.lines[end.line], end.index);
let start_index_code_units_new = len_of_first_n_code_units_new(&self.lines[start.line], start.index).unwrap_or(999 as usize);
let end_index_code_units_new = len_of_first_n_code_units_new(&self.lines[end.line], end.index).unwrap_or(999 as usize);
let start_index_code_units = len_of_first_n_code_units(&self.lines[start.line], start.index);
let end_index_code_units = len_of_first_n_code_units(&self.lines[end.line], end.index);
println!("start.index: {} end.index: {} start_index_code_units: {} end_index_code_units: {} start_index_code_units_new: {} end_index_code_units_new: {}", start.index, end.index, start_index_code_units, end_index_code_units, start_index_code_units_new, end_index_code_units_new);
println!("old new start end index diff: {} {}", start.index == start_index_code_units_new, end.index == end_index_code_units_new);
let new_lines = {
let prefix = &self.lines[start.line][..start_index_b];
let suffix = &self.lines[end.line][end_index_b..];
let prefix = &self.lines[start.line][..start.index];
let suffix = &self.lines[end.line][end.index..];
let lines_prefix = &self.lines[..start.line];
let lines_suffix = &self.lines[end.line + 1..];

ProTip! Use n and p to navigate between commits in a pull request.
You can’t perform that action at this time.