Skip to content

Add str::copy_from_str and other mutating str methodsΒ #709

@a1phyr

Description

@a1phyr

Proposal

Problem statement

The standard library has nearly no string manipulation that do not allocate (expect methods that give a substring like spit or trim families). This comes from the fact that UTF8 characters have a variable length, which makes them hard to change. This is why the only mutating str methods are ASCII ones.

Even having a String often means creating a new one to edit it.

Motivating examples or use cases

This ACP proposes simple functions on which more complex ones can be built. Eg

/// Panics if `from.len() != to.len()`
fn replace_in_place(mut src: &mut str, from: &str, to: &str) {
    while let Some(i) = src.find(from) {
        let (pat, tail) = src[i..].split_at_mut(from.len());
        debug_assert_eq!(pat, from); 
        pat.copy_from_str(to); // Not possible without `unsafe` today
        src = tail;
    }
}

I'd love to see some kind of replace_in_place method in std one day, but let's start easy and (hopefully) uncontroversial. Giving people tools to build what they need is neat too!

Solution sketch

Add these functions to core, taken from slices and adapted to str:

impl str {
    // Panics if `self.len() != src.len()`
    fn copy_from_str(&mut self, src: &str) { ... }
    // Panics if `src` is invalid for `self` or if the source or the destination are not on UTF8 bondaries
    fn copy_within<R: RangeBounds<usize>>(&mut self, src: R, dest: usize) { ... }
    // Panics if `self.len() != src.len()`
    fn swap_with_str(&mut self, other: &str) { ... }
}

These are all safe thanks to the construction of UTF8: no character has a strict sub-string that is also valid UTF8 (it is self-recovering).

I think that copy_from_str is the most important (and easy) one, and that swap_with_str is easy too. copy_within is more complicated because it needs many additional UTF8 checks, but it may be useful in some cases, and consistent with slices.

Alternatives

Do nothing

What happens now?

This issue contains an API change proposal (or ACP) and is part of the libs-api team feature lifecycle. Once this issue is filed, the libs-api team will review open proposals as capability becomes available. Current response times do not have a clear estimate, but may be up to several months.

Possible responses

The libs team may respond in various different ways. First, the team will consider the problem (this doesn't require any concrete solution or alternatives to have been proposed):

  • We think this problem seems worth solving, and the standard library might be the right place to solve it.
  • We think that this probably doesn't belong in the standard library.

Second, if there's a concrete solution:

  • We think this specific solution looks roughly right, approved, you or someone else should implement this. (Further review will still happen on the subsequent implementation PR.)
  • We're not sure this is the right solution, and the alternatives or other materials don't give us enough information to be sure about that. Here are some questions we have that aren't answered, or rough ideas about alternatives we'd want to see discussed.

Metadata

Metadata

Assignees

No one assigned

    Labels

    T-libs-apiapi-change-proposalA proposal to add or alter unstable APIs in the standard libraries

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions