@@ -2,25 +2,33 @@ use core::{slice, str::from_utf8_unchecked_mut};
2
2
3
3
use alloc:: string:: String ;
4
4
5
- /// Extension methods on [`String`].
5
+ /// More advanced versions of [`String::retain`], implemented as extension
6
+ /// methods on [`String`].
6
7
///
7
8
/// This trait is sealed and cannot be implemented for types outside of
8
- /// `retain_more`.
9
+ /// `retain_more`
9
10
pub trait RetainMoreString : sealed:: AllocMoreSealedString {
10
- /// A version of [`String::retain`] which allows the predicate mutable
11
+ /// Retains only the characters specified by the predicate.
12
+ ///
13
+ /// In other words, remove all characters for which `f` returns false. This
14
+ /// method operates in place, visiting each character exactly once
15
+ /// once in the original order, and preserves the order of the retained
16
+ /// characters.
17
+ ///
18
+ /// This version of [`String::retain`] allows the predicate mutable
11
19
/// access to the valid parts of the full string.
12
20
///
13
21
/// The arguments of the predicate are:
14
- /// - 0: `&mut str`; The already retained parts of `self`, for which
15
- /// predicate returned `true`
16
- /// - 1: [`char`]; The current character being considered
22
+ /// - 0: `&mut str`; Contents of `self` which have already been retained,
23
+ /// i.e. those for which predicate has already returned `true`.
24
+ /// - 1: [`char`]; The current character being considered.
17
25
/// - 2: `&mut str`; The parts of `self` yet to be considered.
18
26
///
19
27
/// # Usage
20
28
///
21
29
/// ```
22
30
/// # use retain_more::RetainMoreString as _;
23
- /// let mut my_string = "Super secret code: -100054321;-78912. EOF \
31
+ /// let mut my_string = "Super secret code: -100054321-78912EOF \
24
32
/// Here is some content which shouldn't be seen"
25
33
/// .to_string();
26
34
/// /// Remove all numbers from the string, including a single leading `'-'` and
@@ -36,12 +44,13 @@ pub trait RetainMoreString: sealed::AllocMoreSealedString {
36
44
/// }
37
45
/// }
38
46
/// my_string.retain_all(cleanup);
39
- /// assert_eq!(&my_string, "Super secret code: ;. EOF");
47
+ /// assert_eq!(&my_string, "Super secret code: EOF");
40
48
/// ```
41
49
fn retain_all < F : FnMut ( & mut str , char , & mut str ) -> bool > ( & mut self , f : F ) ;
42
50
43
51
/// A helper for the common case where only access to the parts of the
44
- /// [`String`] which haven't been considered are required
52
+ /// [`String`] which haven't been considered yet is required, i.e. the
53
+ /// predicate only uses arguments 1 and 2 from [`Self::retain_all`].
45
54
fn retain_after < F : FnMut ( char , & mut str ) -> bool > ( & mut self , mut f : F ) {
46
55
self . retain_all ( move |_, current, after| f ( current, after) )
47
56
}
@@ -53,8 +62,21 @@ pub trait RetainMoreString: sealed::AllocMoreSealedString {
53
62
/// [`retain_all`](`RetainMoreString::retain_all`) is a strictly more
54
63
/// powerful abstraction than [`String::retain`] from [`alloc`].
55
64
///
65
+ /// The predicate therefore only uses argument 1 from [`Self::retain_all`].
66
+ ///
67
+ /// ## Standard retain docs
68
+ ///
69
+ /// This documentation is taken from [`String::retain`] from [`alloc`].
70
+ ///
71
+ /// Retains only the characters specified by the predicate.
72
+ ///
73
+ /// In other words, remove all characters `c` such that `f(c)` returns
74
+ /// false. This method operates in place, visiting each character exactly
75
+ /// once in the original order, and preserves the order of the retained
76
+ /// characters.
77
+ ///
56
78
/// # Examples
57
- /// (Taken from alloc docs)
79
+ ///
58
80
/// ```
59
81
/// let mut s = String::from("f_o_ob_ar");
60
82
///
@@ -131,9 +153,10 @@ impl RetainMoreString for String {
131
153
// SAFETY: UTF-8 is maintained in the before section by only copying
132
154
// a full character at a time.
133
155
from_utf8_unchecked_mut ( slice:: from_raw_parts_mut ( ptr, idx - del_bytes) ) ,
134
- // SAFETY: idx + ch_len <= len because self, hence `idx + ch_len` is within the allocation of self.
135
- // was valid UTF-8 by invariant, hence after is valid.
136
- // This does not alias with `before`, because `-del_bytes < ch_len`
156
+ // SAFETY: idx + ch_len <= len because self, hence `idx + ch_len` is within the
157
+ // allocation of self. was valid UTF-8 by invariant, hence
158
+ // after is valid. This does not alias with `before`,
159
+ // because `-del_bytes < ch_len`
137
160
from_utf8_unchecked_mut ( slice:: from_raw_parts_mut (
138
161
ptr. add ( idx + ch_len) ,
139
162
len - idx - ch_len,
@@ -156,7 +179,7 @@ impl RetainMoreString for String {
156
179
// 'Point' idx to the next char
157
180
idx += ch_len;
158
181
}
159
- // len- del_bytes <= len <= capacity
182
+ // len - del_bytes <= len <= capacity
160
183
unsafe {
161
184
self . as_mut_vec ( ) . set_len ( len - del_bytes) ;
162
185
}
0 commit comments