rust-analyzer / smol_str

Apache License 2.0
447 stars 54 forks source link

Add StrExt::replace_smolstr, replacen_smolstr #72

Closed alexheretic closed 7 months ago

alexheretic commented 9 months ago

Add StrExt::replace_smolstr, StrExt::replacen_smolstr.

This is useful to avoid allocation on small strings, e.g:

let result = "foo-bar_baz".replace_smolstr("-", "_");
assert_eq!(result, "foo_bar_baz");
assert!(!result.is_heap_allocated());

Impl adapted from std str::replacen.

One undesirable difference from std is from uses Pattern in str::replace but this is not stable and can't be used here. However, I still think this version with from: &str is quite useful & worth having. Also if/when Pattern stabilizes we can start using it as a non-breaking change, since &str will implement it.

Note: While not needed for this PR this kind of usage is an example of why https://github.com/rust-analyzer/smol_str/issues/70 would be useful for external use.

alexheretic commented 7 months ago

@Veykril did you get a chance to look at this, any issues?

Veykril commented 7 months ago

Ah sorry, I missed this. Sounds reasonable

alexheretic commented 6 months ago

It would be nice to have a new release with this

Veykril commented 6 months ago

Sorry, published 0.2.2 now

Veykril commented 6 months ago

Or not, need to ask someone regarding perms as the team that managed them (which i was part of) has been deleted

alexheretic commented 6 months ago

I guess you need to replace your ci CRATES_IO_TOKEN env var. You are a owner on crates.io so could also publish manually if you can't access to the repo settings.

Veykril commented 6 months ago

I wasn't an owner a couple of minutes ago, just sorted it out publish succeeded now

alexheretic commented 6 months ago

Wonderful cheers mate!