Skip to content

Commit 2e2e0df

Browse files
committed
Improved comments to clarify sasumptions in str::strip_prefix
1 parent 9badc33 commit 2e2e0df

File tree

1 file changed

+7
-5
lines changed

1 file changed

+7
-5
lines changed

src/libcore/str/mod.rs

+7-5
Original file line numberDiff line numberDiff line change
@@ -3797,7 +3797,7 @@ impl str {
37973797
/// the prefix is removed. Unlike `trim_start_matches`, this method removes the prefix exactly
37983798
/// once.
37993799
///
3800-
/// If the string does not start with `prefix`, it is removed.
3800+
/// If the string does not start with `prefix`, `None` is returned.
38013801
///
38023802
/// # Examples
38033803
///
@@ -3814,8 +3814,9 @@ impl str {
38143814
pub fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str> {
38153815
let mut matcher = prefix.into_searcher(self);
38163816
if let SearchStep::Match(start, len) = matcher.next() {
3817-
debug_assert_eq!(start, 0);
3817+
debug_assert_eq!(start, 0, "The first search step from Searcher must start from the front");
38183818
unsafe {
3819+
// Searcher is known to return valid indices.
38193820
Some(self.get_unchecked(len..))
38203821
}
38213822
} else {
@@ -3825,11 +3826,11 @@ impl str {
38253826

38263827
/// Returns a string slice with the suffix removed.
38273828
///
3828-
/// If the string starts with the pattern `suffix`, `Some` is returned with the substring where
3829+
/// If the string ends with the pattern `suffix`, `Some` is returned with the substring where
38293830
/// the suffix is removed. Unlike `trim_end_matches`, this method removes the suffix exactly
38303831
/// once.
38313832
///
3832-
/// If the string does not start with `suffix`, it is removed.
3833+
/// If the string does not end with `suffix`, `None` is returned.
38333834
///
38343835
/// # Examples
38353836
///
@@ -3849,8 +3850,9 @@ impl str {
38493850
{
38503851
let mut matcher = suffix.into_searcher(self);
38513852
if let SearchStep::Match(start, end) = matcher.next_back() {
3852-
debug_assert_eq!(end, self.len());
3853+
debug_assert_eq!(end, self.len(), "The first search step from ReverseSearcher must include the last character");
38533854
unsafe {
3855+
// Searcher is known to return valid indices.
38543856
Some(self.get_unchecked(..start))
38553857
}
38563858
} else {

0 commit comments

Comments
 (0)