Coder Social home page Coder Social logo

rreverser / cow-utils-rs Goto Github PK

View Code? Open in Web Editor NEW
153.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 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.