diff options
| author | SOFe <sofe2038@gmail.com> | 2019-11-26 17:42:43 +0800 |
|---|---|---|
| committer | SOFe <sofe2038@gmail.com> | 2019-11-26 17:42:43 +0800 |
| commit | 2e2e0dfc1acfb69be9575d320418d04f3caf64a7 (patch) | |
| tree | 0d0b3eb9fa93d4e75aa80d2e4ab66624c0beb03c /src | |
| parent | 9badc33cda41acadc395f1597947b185f5a9b2a3 (diff) | |
| download | rust-2e2e0dfc1acfb69be9575d320418d04f3caf64a7.tar.gz rust-2e2e0dfc1acfb69be9575d320418d04f3caf64a7.zip | |
Improved comments to clarify sasumptions in str::strip_prefix
Diffstat (limited to 'src')
| -rw-r--r-- | src/libcore/str/mod.rs | 12 |
1 files changed, 7 insertions, 5 deletions
diff --git a/src/libcore/str/mod.rs b/src/libcore/str/mod.rs index 2527aee58cd..4367cc54e70 100644 --- a/src/libcore/str/mod.rs +++ b/src/libcore/str/mod.rs @@ -3797,7 +3797,7 @@ impl str { /// the prefix is removed. Unlike `trim_start_matches`, this method removes the prefix exactly /// once. /// - /// If the string does not start with `prefix`, it is removed. + /// If the string does not start with `prefix`, `None` is returned. /// /// # Examples /// @@ -3814,8 +3814,9 @@ impl str { pub fn strip_prefix<'a, P: Pattern<'a>>(&'a self, prefix: P) -> Option<&'a str> { let mut matcher = prefix.into_searcher(self); if let SearchStep::Match(start, len) = matcher.next() { - debug_assert_eq!(start, 0); + debug_assert_eq!(start, 0, "The first search step from Searcher must start from the front"); unsafe { + // Searcher is known to return valid indices. Some(self.get_unchecked(len..)) } } else { @@ -3825,11 +3826,11 @@ impl str { /// Returns a string slice with the suffix removed. /// - /// If the string starts with the pattern `suffix`, `Some` is returned with the substring where + /// If the string ends with the pattern `suffix`, `Some` is returned with the substring where /// the suffix is removed. Unlike `trim_end_matches`, this method removes the suffix exactly /// once. /// - /// If the string does not start with `suffix`, it is removed. + /// If the string does not end with `suffix`, `None` is returned. /// /// # Examples /// @@ -3849,8 +3850,9 @@ impl str { { let mut matcher = suffix.into_searcher(self); if let SearchStep::Match(start, end) = matcher.next_back() { - debug_assert_eq!(end, self.len()); + debug_assert_eq!(end, self.len(), "The first search step from ReverseSearcher must include the last character"); unsafe { + // Searcher is known to return valid indices. Some(self.get_unchecked(..start)) } } else { |
