strip-prefix-suffix-sane

Crates.iostrip-prefix-suffix-sane
lib.rsstrip-prefix-suffix-sane
version0.1.0
created_at2025-06-01 01:42:37.416429+00
updated_at2025-06-01 01:42:37.416429+00
descriptionA small utility crate providing 'sane' `strip_prefix` and `strip_suffix` methods for `str` that return `self` instead of an `Option` when the prefix/suffix is not found.
homepage
repositoryhttps://github.com/juzeon/strip-prefix-suffix-sane
max_upload_size
id1696828
size15,295
(juzeon)

documentation

https://docs.rs/strip-prefix-suffix-sane

README

strip-prefix-suffix-sane

Crates.io Docs.rs License

A small Rust utility crate providing "sane" strip_prefix and strip_suffix methods for str that return &self instead of an Option when the prefix/suffix is not found.

💡 Why use this?

The standard library's str::strip_prefix and str::strip_suffix methods return an Option<&str>. While this is idiomatic Rust for indicating success or failure, it often leads to boilerplate code like:

let path = "/usr/local/bin";
let stripped_path = path.strip_prefix("/").unwrap_or(path);
// or
let stripped_path = if let Some(s) = path.strip_prefix("/") { s } else { path };

This crate provides a convenience trait, StripPrefixSuffixSane, that encapsulates this common unwrap_or(self) pattern, leading to more concise and readable code when you simply want the original string back if the prefix/suffix isn't present.

✨ Features

  • strip_prefix_sane(&self, prefix: &str) -> &str: Removes prefix if present; otherwise, returns self.
  • strip_suffix_sane(&self, suffix: &str) -> &str: Removes suffix if present; otherwise, returns self.
  • Implemented for str.
  • No external dependencies.
  • no_std compatible.

📦 Installation

Add this to your Cargo.toml:

[dependencies]
strip-prefix-suffix-sane = "0.1" # Use the latest version

🚀 Usage

Simply bring the StripPrefixSuffixSane trait into scope to use the new methods on &str types.

use strip-prefix-suffix-sane::StripPrefixSuffixSane;

fn main() {
    let s = "hello_world";

    // --- strip_prefix_sane ---
    // Stripping a present prefix
    assert_eq!(s.strip_prefix_sane("hello_"), "world");

    // Stripping a non-present prefix returns the original string
    assert_eq!(s.strip_prefix_sane("foo_"), "hello_world");

    // Empty prefix returns original string
    assert_eq!(s.strip_prefix_sane(""), "hello_world");

    // Prefix longer than string returns original string
    assert_eq!("foo".strip_prefix_sane("foobar"), "foo");

    // --- strip_suffix_sane ---
    // Stripping a present suffix
    assert_eq!(s.strip_suffix_sane("_world"), "hello");

    // Stripping a non-present suffix returns the original string
    assert_eq!(s.strip_suffix_sane("_foo"), "hello_world");

    // Empty suffix returns original string
    assert_eq!(s.strip_suffix_sane(""), "hello_world");

    // Suffix longer than string returns original string
    assert_eq!("bar".strip_suffix_sane("foobar"), "bar");

    // --- Chaining operations ---
    let path = "/usr/local/bin/";
    assert_eq!(path.strip_prefix_sane("/").strip_suffix_sane("/"), "usr/local/bin");

    let filename = "image.png.bak";
    assert_eq!(filename.strip_suffix_sane(".bak").strip_suffix_sane(".png"), "image");

    // --- Edge cases: Empty strings ---
    assert_eq!("".strip_prefix_sane("foo"), "");
    assert_eq!("".strip_suffix_sane("bar"), "");
    assert_eq!("".strip_prefix_sane(""), "");
    assert_eq!("".strip_suffix_sane(""), "");
}
Commit count: 4

cargo fmt