| Crates.io | fast_whitespace_collapse |
| lib.rs | fast_whitespace_collapse |
| version | 0.1.0 |
| created_at | 2025-02-20 21:26:36.032625+00 |
| updated_at | 2025-02-20 21:26:36.032625+00 |
| description | Collapse consecutive spaces and tabs into a single space using SIMD |
| homepage | |
| repository | https://github.com/digitalcortex/fast_whitespace_collapse |
| max_upload_size | |
| id | 1563183 |
| size | 34,232 |
A high-performance Rust crate for collapsing consecutive spaces and tabs into a single space.
Uses SIMD (u8x16) via the wide crate for efficient processing.
Automatically falls back to a scalar implementation if SIMD is unavailable.
u8x16) when supported to process 16 bytes at a time.Add this to your Cargo.toml:
[dependencies]
fast_whitespace_collapse = "0.1"
Or run the following command:
cargo add fast_whitespace_collapse
By default, SIMD acceleration is enabled. You can control it via Cargo features:
cargo build --no-default-features
cargo build --features simd-optimized
use fast_whitespace_collapse::collapse_whitespace;
let input = "This is \t a test.";
let output = collapse_whitespace(input);
assert_eq!(output, "This is a test.");
u8x16), handling 16 bytes in parallel.| Method | Time |
|---|---|
| Regex approach | 11.289 Β΅s |
| collapse crate | 1.2624 Β΅s |
| Iterative approach | 629.60 ns |
| Iterative bytes | 428.00 ns |
| fast_whitespace_collapse crate | 388.73 ns |
π fast_whitespace_collapse outperforms other methods, achieving the lowest execution time.
π Benchmark executed on Apple M1 Pro (NEON SIMD enabled).
cargo bench
fast_whitespace_collapse supports multiple architectures:
SSE2, AVX2) for maximum performance.use fast_whitespace_collapse::collapse_whitespace;
assert_eq!(collapse_whitespace("Hello world"), "Hello world");
assert_eq!(collapse_whitespace(" Trim spaces " ), "Trim spaces");
assert_eq!(collapse_whitespace("Tabs\t\tconverted"), "Tabs converted");
assert_eq!(collapse_whitespace("γγγ«γ‘γ― δΈη"), "γγγ«γ‘γ― δΈη"); // Japanese
assert_eq!(collapse_whitespace("δ½ ε₯½ δΈη"), "δ½ ε₯½ δΈη"); // Chinese
assert_eq!(collapse_whitespace("π π π"), "π π π"); // Emojis
assert_eq!(collapse_whitespace("Line1\n Line2\nLine3"), "Line1\n Line2\nLine3");
Run tests with:
cargo test
This project is licensed under the MIT License.