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 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.

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.