From fec35a471cc80c27ba1eb619cca627b1820c799e Mon Sep 17 00:00:00 2001 From: Josh Stoik Date: Tue, 29 Nov 2022 19:03:12 -0800 Subject: [PATCH 1/6] replace: actions-rs/toolchain with dtolnay/rust-toolchain --- .github/workflows/ci.yaml | 7 ++----- 1 file changed, 2 insertions(+), 5 deletions(-) diff --git a/.github/workflows/ci.yaml b/.github/workflows/ci.yaml index 6e3108f..04c184c 100644 --- a/.github/workflows/ci.yaml +++ b/.github/workflows/ci.yaml @@ -38,12 +38,9 @@ jobs: steps: - uses: actions/checkout@v3 - - uses: actions-rs/toolchain@v1 + - uses: dtolnay/rust-toolchain@stable with: - toolchain: stable - target: ${{ matrix.target }} - profile: minimal - override: true + targets: ${{ matrix.target }} components: clippy - name: Info From 91a06f76377615a1642cba6c1beee6ce50cf8af4 Mon Sep 17 00:00:00 2001 From: Josh Stoik Date: Sat, 24 Dec 2022 22:44:14 -0800 Subject: [PATCH 2/6] fix: ci badge --- README.md | 2 +- src/lib.rs | 2 +- 2 files changed, 2 insertions(+), 2 deletions(-) diff --git a/README.md b/README.md index caef8bd..c94767b 100644 --- a/README.md +++ b/README.md @@ -3,7 +3,7 @@ [![docs.rs](https://img.shields.io/docsrs/trimothy.svg?style=flat-square&label=docs.rs)](https://docs.rs/trimothy/) [![changelog](https://img.shields.io/crates/v/trimothy.svg?style=flat-square&label=changelog&color=9b59b6)](https://github.com/Blobfolio/trimothy/blob/master/CHANGELOG.md)
[![crates.io](https://img.shields.io/crates/v/trimothy.svg?style=flat-square&label=crates.io)](https://crates.io/crates/trimothy) -[![ci](https://img.shields.io/github/workflow/status/Blobfolio/trimothy/Build.svg?style=flat-square&label=ci)](https://github.com/Blobfolio/trimothy/actions) +[![ci](https://img.shields.io/github/actions/workflow/status/Blobfolio/trimothy/ci.yaml?style=flat-square&label=ci)](https://github.com/Blobfolio/trimothy/actions) [![deps.rs](https://deps.rs/repo/github/blobfolio/trimothy/status.svg?style=flat-square&label=deps.rs)](https://deps.rs/repo/github/blobfolio/trimothy)
[![license](https://img.shields.io/badge/license-wtfpl-ff1493?style=flat-square)](https://en.wikipedia.org/wiki/WTFPL) [![contributions welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square&label=contributions)](https://github.com/Blobfolio/trimothy/issues) diff --git a/src/lib.rs b/src/lib.rs index 658632d..8567ab0 100644 --- a/src/lib.rs +++ b/src/lib.rs @@ -4,7 +4,7 @@ [![docs.rs](https://img.shields.io/docsrs/trimothy.svg?style=flat-square&label=docs.rs)](https://docs.rs/trimothy/) [![changelog](https://img.shields.io/crates/v/trimothy.svg?style=flat-square&label=changelog&color=9b59b6)](https://github.com/Blobfolio/trimothy/blob/master/CHANGELOG.md)
[![crates.io](https://img.shields.io/crates/v/trimothy.svg?style=flat-square&label=crates.io)](https://crates.io/crates/trimothy) -[![ci](https://img.shields.io/github/workflow/status/Blobfolio/trimothy/Build.svg?style=flat-square&label=ci)](https://github.com/Blobfolio/trimothy/actions) +[![ci](https://img.shields.io/github/actions/workflow/status/Blobfolio/trimothy/ci.yaml?style=flat-square&label=ci)](https://github.com/Blobfolio/trimothy/actions) [![deps.rs](https://deps.rs/repo/github/blobfolio/trimothy/status.svg?style=flat-square&label=deps.rs)](https://deps.rs/repo/github/blobfolio/trimothy)
[![license](https://img.shields.io/badge/license-wtfpl-ff1493?style=flat-square)](https://en.wikipedia.org/wiki/WTFPL) [![contributions welcome](https://img.shields.io/badge/PRs-welcome-brightgreen.svg?style=flat-square&label=contributions)](https://github.com/Blobfolio/trimothy/issues) From c4e7a09774ad2eaaeda37b5cdc94f42f6c93a251 Mon Sep 17 00:00:00 2001 From: Josh Stoik Date: Tue, 27 Dec 2022 11:16:12 -0800 Subject: [PATCH 3/6] misc: bench end matches --- benches/fn_trim_slice.rs | 12 ++++++++++-- 1 file changed, 10 insertions(+), 2 deletions(-) diff --git a/benches/fn_trim_slice.rs b/benches/fn_trim_slice.rs index 0b3f2ee..1322482 100644 --- a/benches/fn_trim_slice.rs +++ b/benches/fn_trim_slice.rs @@ -44,8 +44,16 @@ benches!( Bench::spacer(), Bench::new("&[u8]::trim_start_matches()") - .run(|| BYTES.trim_start_matches(|b| matches!(b, b'\t' | b' ' | b'\n' | b'H' | b'e'))), + .run(|| BYTES.trim_start_matches(|b| b.is_ascii_whitespace() || matches!(b, b'H' | b'e'))), Bench::new("&str::trim_start_matches()") - .run(|| STR.trim_start_matches(|c| matches!(c, '\t' | ' ' | '\n' | 'H' | 'e'))), + .run(|| STR.trim_start_matches(|c: char| c.is_ascii_whitespace() || matches!(c, 'H' | 'e'))), + + Bench::spacer(), + + Bench::new("&[u8]::trim_end_matches()") + .run(|| BYTES.trim_end_matches(|b| b.is_ascii_whitespace() || matches!(b, b'd' | b'!'))), + + Bench::new("&str::trim_end_matches()") + .run(|| STR.trim_end_matches(|c: char| c.is_ascii_whitespace() || matches!(c, 'd' | '!'))), ); From f4f95edf4e2904f2d91dee708391b9766db758b1 Mon Sep 17 00:00:00 2001 From: Josh Stoik Date: Tue, 27 Dec 2022 11:16:48 -0800 Subject: [PATCH 4/6] misc: destructure instead of iter/slice for match trimming --- src/trim_slice.rs | 38 +++++++++++++++++++++++--------------- 1 file changed, 23 insertions(+), 15 deletions(-) diff --git a/src/trim_slice.rs b/src/trim_slice.rs index 2f1fc99..cea2bda 100644 --- a/src/trim_slice.rs +++ b/src/trim_slice.rs @@ -157,15 +157,17 @@ macro_rules! trim_slice { /// callback, where a return value of `true` means trim. fn trim_matches(&self, cb: F) -> &[u8] where F: Fn(u8) -> bool { - let cb = |b: &u8| ! cb(*b); - - self.iter() - .position(cb) - .map_or(&[], |start| { - // We know there is an end because there's a beginning. - let end = self.iter().rposition(cb).unwrap(); - &self[start..=end] - }) + let mut src: &[u8] = &self; + while let [first, rest @ ..] = src { + if cb(*first) { src = rest; } + else { break; } + } + + while let [rest @ .., last] = src { + if cb(*last) { src = rest; } + else { break; } + } + src } /// # Trim Start Matches. @@ -174,9 +176,12 @@ macro_rules! trim_slice { /// where a return value of `true` means trim. fn trim_start_matches(&self, cb: F) -> &[u8] where F: Fn(u8) -> bool { - self.iter() - .position(|b: &u8| ! cb(*b)) - .map_or(&[], |p| &self[p..]) + let mut src: &[u8] = &self; + while let [first, rest @ ..] = src { + if cb(*first) { src = rest; } + else { break; } + } + src } /// # Trim Start Matches. @@ -185,9 +190,12 @@ macro_rules! trim_slice { /// where a return value of `true` means trim. fn trim_end_matches(&self, cb: F) -> &[u8] where F: Fn(u8) -> bool { - self.iter() - .rposition(|b: &u8| ! cb(*b)) - .map_or(&[], |p| &self[..=p]) + let mut src: &[u8] = &self; + while let [rest @ .., last] = src { + if cb(*last) { src = rest; } + else { break; } + } + src } } )+); From 6ac3f4a35a2837e649baf91a7624260a5a3c97dc Mon Sep 17 00:00:00 2001 From: Josh Stoik Date: Tue, 27 Dec 2022 11:29:35 -0800 Subject: [PATCH 5/6] cleanup: consolidate trim/trim-match --- src/trim_slice.rs | 39 ++++++++++----------------------------- 1 file changed, 10 insertions(+), 29 deletions(-) diff --git a/src/trim_slice.rs b/src/trim_slice.rs index cea2bda..8f5644e 100644 --- a/src/trim_slice.rs +++ b/src/trim_slice.rs @@ -132,22 +132,29 @@ pub trait TrimSliceMatches { macro_rules! trim_slice { ($($ty:ty),+ $(,)?) => ($( impl TrimSlice for $ty { + #[inline] /// # Trim. /// /// Trim leading and trailing (ASCII) whitespace from a slice. - fn trim(&self) -> &[u8] { trim_end(trim_start(&self)) } + fn trim(&self) -> &[u8] { + self.trim_matches(|b| b.is_ascii_whitespace()) + } #[inline] /// # Trim Start. /// /// Trim leading (ASCII) whitespace from a slice. - fn trim_start(&self) -> &[u8] { trim_start(&self) } + fn trim_start(&self) -> &[u8] { + self.trim_start_matches(|b| b.is_ascii_whitespace()) + } #[inline] /// # Trim End. /// /// Trim trailing (ASCII) whitespace from a slice. - fn trim_end(&self) -> &[u8] { trim_end(&self) } + fn trim_end(&self) -> &[u8] { + self.trim_end_matches(|b| b.is_ascii_whitespace()) + } } impl TrimSliceMatches for $ty { @@ -205,32 +212,6 @@ trim_slice!([u8], Box<[u8]>, Vec); -/// # Trim Slice Start. -/// -/// This is a copy of the nightly `trim_ascii_start` so it can be used on -/// stable. If/when that feature is stabilized, we'll use it directly. -const fn trim_start(mut src: &[u8]) -> &[u8] { - while let [first, rest @ ..] = src { - if first.is_ascii_whitespace() { src = rest; } - else { break; } - } - src -} - -/// # Trim Slice End. -/// -/// This is a copy of the nightly `trim_ascii_end` so it can be used on -/// stable. If/when that feature is stabilized, we'll use it directly. -const fn trim_end(mut src: &[u8]) -> &[u8] { - while let [rest @ .., last] = src { - if last.is_ascii_whitespace() { src = rest; } - else { break; } - } - src -} - - - #[cfg(test)] mod tests { use super::*; From 2a245cc6228f2a3e93f835140b4198d6adfb809b Mon Sep 17 00:00:00 2001 From: Josh Stoik Date: Tue, 27 Dec 2022 11:32:06 -0800 Subject: [PATCH 6/6] bump: 0.1.5 --- CHANGELOG.md | 9 +++++++++ CREDITS.md | 4 ++-- Cargo.toml | 2 +- 3 files changed, 12 insertions(+), 3 deletions(-) diff --git a/CHANGELOG.md b/CHANGELOG.md index 3f769e8..8f8d6d6 100644 --- a/CHANGELOG.md +++ b/CHANGELOG.md @@ -2,6 +2,15 @@ +## [0.1.5](https://github.com/Blobfolio/trimothy/releases/tag/v0.1.5) - 2022-12-27 + +### Changed + +* Minor slice trim performance improvements +* Update ci badge syntax (docs) + + + ## [0.1.4](https://github.com/Blobfolio/trimothy/releases/tag/v0.1.4) - 2022-09-22 ### Changed diff --git a/CREDITS.md b/CREDITS.md index d8d7657..2748a25 100644 --- a/CREDITS.md +++ b/CREDITS.md @@ -1,6 +1,6 @@ # Project Dependencies Package: trimothy - Version: 0.1.4 - Generated: 2022-09-22 18:17:40 UTC + Version: 0.1.5 + Generated: 2022-12-27 19:32:00 UTC This package has no dependencies. diff --git a/Cargo.toml b/Cargo.toml index 6cd40a9..826930b 100644 --- a/Cargo.toml +++ b/Cargo.toml @@ -1,6 +1,6 @@ [package] name = "trimothy" -version = "0.1.4" +version = "0.1.5" authors = ["Blobfolio, LLC. "] edition = "2021" rust-version = "1.56"