rust-lang / rust-lang/rust

`from_utf8_lossy_owned` is not done in-place

Open
#159,034 1 comment 0 reactions 0 assignees View on GitHub

Nobody has claimed this yet.

A-str C-enhancement C-optimization T-libs
Dominant language
Rust
Stars
119k
Forks
16.1k
PR merge metrics
PR metrics pending

Description

Filing this for tracking reasons, since it is an area of the standard library that can be improved. from_utf8_lossy_owned is being stabilized soon so it does not make sense to put this in the existing issue.

Algorithmically I think this could be accomplished by:

  1. Iterate over chunks, counting the number of replacement characters needed.
  2. If no replacement characters are needed, return as-is.
  3. Allocate exact capacity for the additional replacement characters.
  4. In reverse, overlapping copy chunks to the end of the allocated capacity and push replacement characters when needed (this requires Utf8Chunks to become a DoubleEndedIterator)
  5. The Vec is now valid utf8. set_len and return.

Contributor guide

Open the contributing guide

First steps

  1. Read the whole issue, then the project's contributing guide.
  2. Comment on the issue to say you are picking it up — it saves two people doing the same work.
  3. Fork the repository and make your change on a branch.
  4. Open a pull request that references the issue number.

Research direction

Start by reading the existing from_utf8_lossy_owned implementation and the Utf8Chunks iterator, then compare them with the stabilization work referenced in the issue. The change is done when valid input is returned as-is and invalid input is repaired in-place using exact additional capacity and reverse overlapping copies, with coverage for both paths.

Written by the indexing model from the issue text.

Assessment

Tech stack
rust
Domain
performance
Issue type
Refactor
Difficulty
4/5
Estimated time
3-5 days
Activity status
Quiet
Clarity
Mostly clear
Newbie friendliness
48/100

Get new issues in your inbox

A short digest of beginner-friendly GitHub issues.