#string #cow #text #str

no-std cow-utils

Copy-on-write string utilities for Rust

4 releases

0.1.3 Sep 26, 2023
0.1.2 Feb 17, 2020
0.1.1 Feb 16, 2020
0.1.0 Feb 16, 2020

#107 in Text processing

Download history 5539/week @ 2024-03-14 5331/week @ 2024-03-21 4830/week @ 2024-03-28 4470/week @ 2024-04-04 4687/week @ 2024-04-11 4333/week @ 2024-04-18 4011/week @ 2024-04-25 4636/week @ 2024-05-02 4112/week @ 2024-05-09 4857/week @ 2024-05-16 4599/week @ 2024-05-23 4821/week @ 2024-05-30 5219/week @ 2024-06-06 4629/week @ 2024-06-13 5103/week @ 2024-06-20 4423/week @ 2024-06-27

20,057 downloads per month
Used in 63 crates (13 directly)

MIT license

16KB
115 lines

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.

No runtime deps