Coder Social home page Coder Social logo

rreverser / cow-utils-rs Goto Github PK

View Code? Open in Web Editor NEW
152.0 3.0 9.0 16 KB

Copy-on-write string utilities for Rust

Home Page: https://docs.rs/cow-utils/latest/cow_utils/trait.CowUtils.html

License: MIT License

Rust 100.00%
copy-on-write rust rust-lang rustlang strings strings-manipulation

cow-utils-rs's Introduction

Copy-on-write string utils for Rust

Crate docs License

Some str methods perform destructive transformations and so they allocate, copy into and return a new String even when no modification is necessary.

This crate provides a helper trait CowUtils with drop-in variants of such methods, which behave in the same way, but avoid extra copies and allocations when no modification is necessary.

For now it's only implemented for &str and returns std::borrow::Cow<str>, but in the future might be extended to other types where even more efficient handling is possible (e.g. in-place modifications on mutable strings).

Performance

The primary motivation for this crate was ability to perform zero-alloc replacements when no match is found, so showing results only for .replace vs .cow_replace for now.

The actual results will vary depending on the inputs, but here is a taster based on "a".repeat(40) as an input and various modes (nothing matched, everything matched and replaced, everything matched from the start and deleted):

params .replace (ns) .cow_replace (ns) difference (%)
("a", "") 408.59 290.27 -29
("b", "c") 98.78 54.00 -45
("a", "b") 985.99 1,000.70 +1

Usage

First, you need to import CowUtils into the scope:

use cow_utils::CowUtils;

Then you can start invoking following .cow_-prefixed methods on strings instead of the regular ones:

Check out the docs for detailed examples.

cow-utils-rs's People

Contributors

cryze avatar rreverser avatar thomcc avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar

cow-utils-rs's Issues

Trim posing converse problem to replace

We often have to trim strings because some values contain superfluous whitespace while most values do not. To avoid reallocating a new string for the majority of the values, we currently use a little extension trait called TrimExt with works like

impl TrimExt for String {
    fn trim(self) -> Self {
        let val = self.as_str().trim();

        if val.len() != self.len() {
            val.to_owned()
        } else {
            self
        }
    }
}

i.e. it keeps to original value if the trimming is actually a no-op.

So this does not really fit into the CowUtils trait as far I can see, but I think one could say that it does fit with this crate's aim of avoiding allocations when no modifications are necessary.

Would you be interested in adding a new trait to capture this problem which I think is sort of a converse to the replace problem, i.e. we start with an owned value and want to keep it if possible.

Discrepancy with `String::to_lowercase()

use cow_utils::*;

fn main() {
    let s = "ᾈ";
    assert_eq!(s.to_lowercase(), s.cow_to_lowercase())
}
$ cargo run                 
    Finished dev [unoptimized + debuginfo] target(s) in 0.00s
     Running `target/debug/cwl`
thread 'main' panicked at 'assertion failed: `(left == right)`
  left: `"ᾀ"`,
 right: `"ᾈ"`', src/main.rs:5:5

We ran across this doing some proptesting against some of our code that uses cow_to_lowercase(). has no particular meaning to us -- it's just the reduced value that proptest found.

I see this crate hasn't been updated in about 4 years, but I thought this was useful to report. We really like cow-utils, so even if this is deemed unworthy of fixing, thanks for a great piece of software.

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. 📊📈🎉

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google ❤️ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.