From 469295bfa812ec613d5f85c29a98f2d8a4b6d15f Mon Sep 17 00:00:00 2001 From: Tianyi Shi Date: Thu, 1 Oct 2020 18:49:58 +0100 Subject: [PATCH 1/2] simplify --- src/alignment.rs | 24 ++++++++++++------------ 1 file changed, 12 insertions(+), 12 deletions(-) diff --git a/src/alignment.rs b/src/alignment.rs index 7284694..78fea38 100644 --- a/src/alignment.rs +++ b/src/alignment.rs @@ -200,12 +200,12 @@ 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]))); + x_pretty.push(*k as char); inb_pretty.push(' '); y_pretty.push(' ') } for k in y.iter().take(self.ystart) { - y_pretty.push_str(&format!("{}", String::from_utf8_lossy(&[*k]))); + y_pretty.push(*k as char); inb_pretty.push(' '); x_pretty.push(' ') } @@ -216,21 +216,21 @@ impl 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 => { @@ -238,11 +238,11 @@ impl Alignment { 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('+'); @@ -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(' '); @@ -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(' '); @@ -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(' ') } From 6eb2c3e9f2c98f45cc0853df600917449bc85dcb Mon Sep 17 00:00:00 2001 From: Tianyi Shi Date: Thu, 1 Oct 2020 19:04:30 +0100 Subject: [PATCH 2/2] simplify; should be faster --- src/alignment.rs | 16 ++++++++-------- 1 file changed, 8 insertions(+), 8 deletions(-) diff --git a/src/alignment.rs b/src/alignment.rs index 78fea38..56bbab8 100644 --- a/src/alignment.rs +++ b/src/alignment.rs @@ -199,16 +199,16 @@ impl Alignment { _ => { x_i = self.xstart; y_i = self.ystart; - for k in x.iter().take(self.xstart) { - x_pretty.push(*k as char); - 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(*k as char); - 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)); } }