Skip to content

Commit

Permalink
refactor: ♻️ extract trim-related functions to its own module
Browse files Browse the repository at this point in the history
  • Loading branch information
Jisu-Woniu committed Mar 5, 2024
1 parent cbf8d09 commit ba6f213
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 16 deletions.
22 changes: 6 additions & 16 deletions crates/rsjudge-judger/src/comparer/default_comparer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,9 @@ use tokio::{
};
use tokio_stream::{wrappers::SplitStream, StreamExt as _};

use crate::{CompareResult, Comparer};
use crate::{utils::trim::slice::trim_ascii_end, CompareResult, Comparer};

///
pub struct DefaultComparer {
ignore_trailing_whitespace: bool,
ignore_trailing_newline: bool,
Expand All @@ -22,21 +23,10 @@ impl DefaultComparer {
}
}
fn compare_line(&self, out_line: impl AsRef<[u8]>, ans_line: impl AsRef<[u8]>) -> bool {
fn trim_end(line: &[u8]) -> &[u8] {
if line.is_empty() {
line
} else {
let end = line
.iter()
.rposition(|c| !c.is_ascii_whitespace())
.unwrap_or_else(|| line.len() - 1);
&line[..=end]
}
}
let out_line = out_line.as_ref();
let ans_line = ans_line.as_ref();
let (out_line, ans_line) = if self.ignore_trailing_whitespace {
(trim_end(out_line), trim_end(ans_line))
(trim_ascii_end(out_line), trim_ascii_end(ans_line))
} else {
(out_line, ans_line)
};
Expand Down Expand Up @@ -68,17 +58,17 @@ impl Comparer for DefaultComparer {
return Ok(CompareResult::WrongAnswer);
}
}
(Some(out_line), None) => {
(Some(out_line), _) => {
if !self.ignore_trailing_newline || !self.compare_line(&out_line?, []) {
return Ok(CompareResult::WrongAnswer);
}
}
(None, Some(ans_line)) => {
(_, Some(ans_line)) => {
if !self.ignore_trailing_newline || !self.compare_line([], &ans_line?) {
return Ok(CompareResult::WrongAnswer);
}
}
(None, None) => return Ok(CompareResult::Accepted),
_ => return Ok(CompareResult::Accepted),
}
}
}
Expand Down
1 change: 1 addition & 0 deletions crates/rsjudge-judger/src/lib.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,4 @@
pub mod comparer;
mod utils;

pub use comparer::{default_comparer::DefaultComparer, CompareResult, Comparer};
1 change: 1 addition & 0 deletions crates/rsjudge-judger/src/utils/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod trim;
1 change: 1 addition & 0 deletions crates/rsjudge-judger/src/utils/trim.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1 @@
pub(crate) mod slice;
36 changes: 36 additions & 0 deletions crates/rsjudge-judger/src/utils/trim/slice.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,36 @@
#![allow(dead_code)]

#[inline]
pub(crate) const fn trim_ascii_start(input: &[u8]) -> &[u8] {
let mut bytes = input;
// Note: A pattern matching based approach (instead of indexing) allows
// making the function const.
while let [first, rest @ ..] = bytes {
if first.is_ascii_whitespace() {
bytes = rest;
} else {
break;
}
}
bytes
}

#[inline]
pub(crate) const fn trim_ascii_end(input: &[u8]) -> &[u8] {
let mut bytes = input;
// Note: A pattern matching based approach (instead of indexing) allows
// making the function const.
while let [rest @ .., last] = bytes {
if last.is_ascii_whitespace() {
bytes = rest;
} else {
break;
}
}
bytes
}

#[inline]
pub(crate) const fn trim_ascii(input: &[u8]) -> &[u8] {
trim_ascii_end(trim_ascii_start(input))
}

0 comments on commit ba6f213

Please sign in to comment.