Skip to content

Commit

Permalink
✨ v1.0.2
Browse files Browse the repository at this point in the history
  • Loading branch information
JumperBot committed Jun 20, 2024
1 parent 7928e4f commit 799ba9d
Show file tree
Hide file tree
Showing 3 changed files with 18 additions and 9 deletions.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "whitespace-sifter"
version = "1.0.1"
version = "1.0.2"
edition = "2021"
authors = ["JumperBot_"]
description = "Sift duplicate whitespaces away!"
Expand Down
16 changes: 10 additions & 6 deletions benches/bench.rs
Original file line number Diff line number Diff line change
Expand Up @@ -21,11 +21,11 @@ fn benchmark_sift_preserved(c: &mut Criterion) {
c.iter(|| {
input
.split("\r\n")
.map(|x| x.trim())
.map(str::trim)
.filter(|x| !x.is_empty())
.fold(String::new(), |acc, e| {
acc + "\n"
+ &e.chars()
+ e.chars()
.map(|c| (c.to_string(), c.is_ascii_whitespace(), c))
.fold(("!".to_string(), false, '!'), |(a, aw, ac), (b, bw, bc)| {
if bw && aw && (ac, bc) != ('\r', '\n') {
Expand All @@ -35,7 +35,6 @@ fn benchmark_sift_preserved(c: &mut Criterion) {
})
.0[1..]
.trim()
.to_string()
})
.trim()
.to_string()
Expand All @@ -44,13 +43,14 @@ fn benchmark_sift_preserved(c: &mut Criterion) {
g.bench_with_input("Loop Sift", &input, |c, input| {
c.iter(|| {
let mut out: String = String::with_capacity(input.len());
#[allow(clippy::str_split_at_newline)]
for val in input.trim().split('\n') {
let ends_with_carriage_return: bool = val.ends_with('\r');
let val: &str = val.trim();
if val.is_empty() {
continue;
}
out.push_str(&sift(val));
sift_preallocated(val, &mut out);
if ends_with_carriage_return {
out.push_str("\r\n");
continue;
Expand All @@ -70,8 +70,13 @@ fn benchmark_sift_preserved(c: &mut Criterion) {
g.finish();
}

fn sift(input: &str) -> String {
#[must_use] pub fn sift(input: &str) -> String {
let mut out: String = String::with_capacity(input.len());
sift_preallocated(input, &mut out);
out
}

fn sift_preallocated(input: &str, out: &mut String) {
let mut is_last_whitespace: bool = false;
let mut is_last_carriage_return: bool = false;
for c in input.trim().chars() {
Expand All @@ -90,7 +95,6 @@ fn sift(input: &str) -> String {
is_last_carriage_return = is_carriage_return;
is_last_whitespace = is_whitespace;
}
out
}

fn benchmark_sift(c: &mut Criterion) {
Expand Down
9 changes: 7 additions & 2 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,11 @@
#[must_use]
pub fn sift(input: &str) -> String {
let mut out: String = String::with_capacity(input.len());
sift_preallocated(input, &mut out);
out
}

fn sift_preallocated(input: &str, out: &mut String) {
let mut is_last_whitespace: bool = false;
let mut is_last_carriage_return: bool = false;
for c in input.trim().chars() {
Expand All @@ -44,21 +49,21 @@ pub fn sift(input: &str) -> String {
is_last_carriage_return = is_carriage_return;
is_last_whitespace = is_whitespace;
}
out
}

/// This remove duplicate [whitespaces](https://doc.rust-lang.org/reference/whitespace.html) within the `&str` while preserving deduplicated newlines.
/// This treats carriage-returns as just one `char` in the `&str`.
#[must_use]
pub fn sift_preserve_newlines(input: &str) -> String {
let mut out: String = String::with_capacity(input.len());
#[allow(clippy::str_split_at_newline)]
for val in input.trim().split('\n') {
let ends_with_carriage_return: bool = val.ends_with('\r');
let val: &str = val.trim();
if val.is_empty() {
continue;
}
out.push_str(&sift(val));
sift_preallocated(val, &mut out);
if ends_with_carriage_return {
out.push_str("\r\n");
continue;
Expand Down

0 comments on commit 799ba9d

Please sign in to comment.