Skip to content

Commit

Permalink
release: 0.1.5
Browse files Browse the repository at this point in the history
  • Loading branch information
joshstoik1 committed Dec 27, 2022
2 parents b9c2764 + 2a245cc commit 99dbb0a
Show file tree
Hide file tree
Showing 8 changed files with 59 additions and 56 deletions.
7 changes: 2 additions & 5 deletions .github/workflows/ci.yaml
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
9 changes: 9 additions & 0 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down
4 changes: 2 additions & 2 deletions CREDITS.md
Original file line number Diff line number Diff line change
@@ -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.
2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "trimothy"
version = "0.1.4"
version = "0.1.5"
authors = ["Blobfolio, LLC. <[email protected]>"]
edition = "2021"
rust-version = "1.56"
Expand Down
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)<br>
[![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)<br>
[![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)
Expand Down
12 changes: 10 additions & 2 deletions benches/fn_trim_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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' | '!'))),
);
2 changes: 1 addition & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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)<br>
[![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)<br>
[![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)
Expand Down
77 changes: 33 additions & 44 deletions src/trim_slice.rs
Original file line number Diff line number Diff line change
Expand Up @@ -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 {
Expand All @@ -157,15 +164,17 @@ macro_rules! trim_slice {
/// callback, where a return value of `true` means trim.
fn trim_matches<F>(&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.
Expand All @@ -174,9 +183,12 @@ macro_rules! trim_slice {
/// where a return value of `true` means trim.
fn trim_start_matches<F>(&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.
Expand All @@ -185,9 +197,12 @@ macro_rules! trim_slice {
/// where a return value of `true` means trim.
fn trim_end_matches<F>(&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
}
}
)+);
Expand All @@ -197,32 +212,6 @@ trim_slice!([u8], Box<[u8]>, Vec<u8>);



/// # 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::*;
Expand Down

0 comments on commit 99dbb0a

Please sign in to comment.