rreverser / cow-utils-rs Goto Github PK
View Code? Open in Web Editor NEWCopy-on-write string utilities for Rust
Home Page: https://docs.rs/cow-utils/latest/cow_utils/trait.CowUtils.html
License: MIT License
Copy-on-write string utilities for Rust
Home Page: https://docs.rs/cow-utils/latest/cow_utils/trait.CowUtils.html
License: MIT License
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.
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.
A declarative, efficient, and flexible JavaScript library for building user interfaces.
🖖 Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.
TypeScript is a superset of JavaScript that compiles to clean JavaScript output.
An Open Source Machine Learning Framework for Everyone
The Web framework for perfectionists with deadlines.
A PHP framework for web artisans
Bring data to life with SVG, Canvas and HTML. 📊📈🎉
JavaScript (JS) is a lightweight interpreted programming language with first-class functions.
Some thing interesting about web. New door for the world.
A server is a program made to process requests and deliver data to clients.
Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.
Some thing interesting about visualization, use data art
Some thing interesting about game, make everyone happy.
We are working to build community through open source technology. NB: members must have two-factor auth.
Open source projects and samples from Microsoft.
Google ❤️ Open Source for everyone.
Alibaba Open Source for everyone
Data-Driven Documents codes.
China tencent open source team.