ACP: Add ASCII whitespace trimming functions to &str
#313
Labels
ACP-accepted
API Change Proposal is accepted (seconded with no objections)
api-change-proposal
A proposal to add or alter unstable APIs in the standard libraries
T-libs-api
Proposal
Problem statement
There is no safe and efficient way to remove leading or trailing ASCII whitespace on
&str
incore
today.Motivating examples or use cases
The currently available
str::trim
function family1 is Unicode-aware, which makes users pay a performance cost due to Unicode code point processing.Additionally, using
str::trim
may increase code size due tochar::is_whitespace
using a 256 byte lookup table to determine Unicode whitespace.Today,
str::split_ascii_whitespace
exists as an alternative tostr::split_whitespace
so users do not have to sacrifice performance when they are not concerned with Unicode. Even on ASCII-only text,split_ascii_whitespace
is considerably faster thansplit_whitespace
.The added ASCII trim functions would be an extension of that performance-oriented concept and make the API more consistent between
[u8]
and&str
, mirroring the currently unstable byte slice functionstrim_ascii_start
,trim_ascii_end
, andtrim_ascii
tracked in rust-lang/rust#94035.Solution sketch
See rust-lang/rust#118523
This proposal seeks to implement the following functions on
&str
, leveraging the pre-existing functions on byte slices.Adding these functions would remove the need for users to write unsafe code to efficiently implement the behavior themselves.
Alternatives
Do nothing. This can be implemented safely by users with
trim_matches
,trim_start_matches
, andtrim_end_matches
.Alternatively, users could use
str::as_bytes
to trim usingu8::is_ascii_whitespace
, but they would have to use the unsafestr::from_utf8_unchecked
or re-validate with the safestr::from_utf8
to return a&str
.Links and related work
Add ASCII whitespace trimming functions to &str - rust-lang/rust#118523
Tracking Issue for ASCII trim functions on byte slices - rust-lang/rust#94035
rust-lang/rust#94035 contains some discussion and desire for adding these functions to
&str
.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):
Second, if there's a concrete solution:
Footnotes
str::trim
,str::trim_end
,str::trim_start
,str::trim_matches
↩The text was updated successfully, but these errors were encountered: