SOF3
November 24, 2019, 3:37am
1
It would be useful to have a str::strip_prefix
implementation that something similar to the following (unoptimized, incorrect) function:
impl str {
fn strip_prefix(&'a self, pat: P) -> Option<&'a str>
where P: Pattern {
if self.starts_with(pat) {
Some(&self[self.find(pat) + pat.len()..])
} else {
None
}
}
}
Did I miss something similar in libstd?
4 Likes
CAD97
November 24, 2019, 4:35am
2
trim_start_matches
?
Actually, I suppose that's slightly different, in that it removes repeated copies of the prefix, whereas strip_prefix
doesn't.
josh
November 24, 2019, 6:29am
3
If self.starts_with(pat)
, then wouldn't self.find(pat)
always return Some(0)
? (And also, you can't add Some(0)
and pat.len()
.) I think you want just &self[pat.len()..]
.
That aside, I think I would suggest something like trim_start_matches_n
, with a count n
(pass 1
to get the behavior of your strip_prefix
), analogous to replacen
. And if we have trim_start_matches_n
, we would also want trim_end_matches_n
.
1 Like
SOF3
November 24, 2019, 10:44am
4
Yes, it's just some incorrect pseudocode to illustrate what the function is doing.
SOF3
November 24, 2019, 10:49am
5
josh:
That aside, I think I would suggest something like trim_start_matches_n
, with a count n
(pass 1
to get the behavior of your strip_prefix
), analogous to replacen
. And if we have trim_start_matches_n
, we would also want trim_end_matches_n
.
strip_prefix
returns an Option
so that you can handle the lack of prefix directly. What would be the return type of trim_start_matches_n
?
kornel
November 24, 2019, 1:06pm
6
Yeah, I'd really like strip_prefix
/suffix
. I don't want to use trim_matches
:
most of the time I have one specific prefix/suffix to remove, not a number of them.
often lack of prefix/suffix is an error that I'd like to handle.
e.g. if I want to trim lib
prefix from libfoo.a
filename, I shouldn't be breaking liblibby.a
. If I have some microsyntax like keyword:value
, then:
if let Some(value) = s.strip_prefix("keyword:")
is more efficient and more robust than if s.starts_with()
followed by trim_start_matches
.
3 Likes
josh
November 24, 2019, 5:05pm
7
Ah, I see! Yes, the return value you propose seems useful, and in that case trim_start_matches_n
seems like an overgeneralization.
strip_prefix
makes sense to me. As would strip_suffix
.
SOF3
November 25, 2019, 3:42am
8
Another question: Should the function really accept a Pattern
? Or does an AsRef<str>
suffice?
kornel:
Yeah, I'd really like strip_prefix
/ suffix
. I don't want to use trim_matches
:
most of the time I have one specific prefix/suffix to remove, not a number of them.
often lack of prefix/suffix is an error that I'd like to handle.
e.g. if I want to trim lib
prefix from libfoo.a
filename, I shouldn't be breaking liblibby.a
. If I have some microsyntax like keyword:value
, then:
I have wanted exactly that several times already, e.g. to strip a trailing newline. So, yes, please.
Ideally, the case where a lack of prefix/suffix is not an error (but we just want to continue with the unmodified string) should be ergonomically provided by the same API as well.
SOF3
November 25, 2019, 3:05pm
10
The function takes the str by reference, so an if let Some
+ else
could allow continuing the logic with the original string.
SOF3
November 26, 2019, 3:55am
11
The counterpart may seemed confusing when using the same name, maybe just for a few seconds or minutes. One returns an Option
and one returns a Result
.
system
Closed
March 7, 2020, 2:00pm
13
This topic was automatically closed 90 days after the last reply. New replies are no longer allowed.