Skip to content

Simplify Alignment::pretty #17

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

Open
wants to merge 2 commits into
base: master
Choose a base branch
from
Open
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
36 changes: 18 additions & 18 deletions src/alignment.rs
Original file line number Diff line number Diff line change
Expand Up @@ -199,50 +199,50 @@ impl Alignment {
_ => {
x_i = self.xstart;
y_i = self.ystart;
for k in x.iter().take(self.xstart) {
x_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[*k])));
inb_pretty.push(' ');
y_pretty.push(' ')
// SAFETY: validity checked before creating an Alignment
unsafe {
x_pretty.push_str(std::str::from_utf8_unchecked(&x[..self.xstart]));
}
for k in y.iter().take(self.ystart) {
y_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[*k])));
inb_pretty.push(' ');
x_pretty.push(' ')
y_pretty.push_str(&" ".repeat(self.xstart));
unsafe {
y_pretty.push_str(std::str::from_utf8_unchecked(&y[..self.ystart]));
}
x_pretty.push_str(&" ".repeat(self.ystart));
inb_pretty.push_str(&" ".repeat(self.xstart + self.ystart));
Comment on lines +202 to +211
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'm not too fond of unnecessary unsafes, and I doubt performance is crucial for pretty printing alignments (disregarding the fact that this might not even be faster).
However, since TextSlice is just an alias for &'a [u8] and u8 as char though confusing, usually works as expected, x_pretty.push_str(...) may be replaced by &x[..self.xstart].for_each(|x| x_pretty.push(x as char)) or so.

}
}

// Process the alignment.
for i in 0..self.operations.len() {
match self.operations[i] {
AlignmentOperation::Match => {
x_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[x[x_i]])));
x_pretty.push(x[x_i] as char);
x_i += 1;

inb_pretty.push_str("|");

y_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[y[y_i]])));
y_pretty.push(y[y_i] as char);
y_i += 1;
}
AlignmentOperation::Subst => {
x_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[x[x_i]])));
x_pretty.push(x[x_i] as char);
x_i += 1;

inb_pretty.push('\\');

y_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[y[y_i]])));
y_pretty.push(y[y_i] as char);
y_i += 1;
}
AlignmentOperation::Del => {
x_pretty.push('-');

inb_pretty.push('x');

y_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[y[y_i]])));
y_pretty.push(y[y_i] as char);
y_i += 1;
}
AlignmentOperation::Ins => {
x_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[x[x_i]])));
x_pretty.push(x[x_i] as char);
x_i += 1;

inb_pretty.push('+');
Expand All @@ -251,7 +251,7 @@ impl Alignment {
}
AlignmentOperation::Xclip(len) => {
for k in x.iter().take(len) {
x_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[*k])));
x_pretty.push(*k as char);
x_i += 1;

inb_pretty.push(' ');
Expand All @@ -261,7 +261,7 @@ impl Alignment {
}
AlignmentOperation::Yclip(len) => {
for k in y.iter().take(len) {
y_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[*k])));
y_pretty.push(*k as char);
y_i += 1;

inb_pretty.push(' ');
Expand All @@ -278,12 +278,12 @@ impl Alignment {
AlignmentMode::Custom => {}
_ => {
for k in x.iter().take(self.xlen).skip(x_i) {
x_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[*k])));
x_pretty.push(*k as char);
inb_pretty.push(' ');
y_pretty.push(' ')
}
for k in y.iter().take(self.ylen).skip(y_i) {
y_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[*k])));
y_pretty.push(*k as char);
inb_pretty.push(' ');
x_pretty.push(' ')
}
Expand Down