12
12
//! # Examples
13
13
//!
14
14
//! [`Pattern`] is [implemented][pattern-impls] in the stable API for
15
- //! [`&str`], [`char`], slices of [`char`], and functions and closures
15
+ //! [`&str`][`str`] , [`char`], slices of [`char`], and functions and closures
16
16
//! implementing `FnMut(char) -> bool`.
17
17
//!
18
18
//! ```
28
28
//! assert_eq!(s.find(|c: char| c.is_ascii_punctuation()), Some(35));
29
29
//! ```
30
30
//!
31
- //! [`&str`]: ../../../std/primitive.str.html
32
- //! [`char`]: ../../../std/primitive.char.html
33
- //! [`str`]: ../../../std/primitive.str.html
34
- //! [`DoubleEndedSearcher`]: trait.DoubleEndedSearcher.html
35
- //! [`Pattern`]: trait.Pattern.html
36
- //! [`ReverseSearcher`]: trait.ReverseSearcher.html
37
- //! [`Searcher`]: trait.Searcher.html
38
31
//! [pattern-impls]: trait.Pattern.html#implementors
39
32
40
33
#![ unstable(
@@ -52,13 +45,13 @@ use crate::slice::memchr;
52
45
/// A string pattern.
53
46
///
54
47
/// A `Pattern<'a>` expresses that the implementing type
55
- /// can be used as a string pattern for searching in a `&'a str`.
48
+ /// can be used as a string pattern for searching in a [ `&'a str`][str] .
56
49
///
57
50
/// For example, both `'a'` and `"aa"` are patterns that
58
51
/// would match at index `1` in the string `"baaaab"`.
59
52
///
60
53
/// The trait itself acts as a builder for an associated
61
- /// `Searcher` type, which does the actual work of finding
54
+ /// [ `Searcher`] type, which does the actual work of finding
62
55
/// occurrences of the pattern in a string.
63
56
///
64
57
/// Depending on the type of the pattern, the behaviour of methods like
@@ -75,6 +68,7 @@ use crate::slice::memchr;
75
68
/// | `&String` | is substring |
76
69
///
77
70
/// # Examples
71
+ ///
78
72
/// ```
79
73
/// // &str
80
74
/// assert_eq!("abaaa".find("ba"), Some(1));
@@ -94,9 +88,6 @@ use crate::slice::memchr;
94
88
/// assert_eq!("abcdef_z".find(|ch| ch > 'd' && ch < 'y'), Some(4));
95
89
/// assert_eq!("abcddd_z".find(|ch| ch > 'd' && ch < 'y'), None);
96
90
/// ```
97
- ///
98
- /// [`str::find`]: ../../../std/primitive.str.html#method.find
99
- /// [`str::contains`]: ../../../std/primitive.str.html#method.contains
100
91
pub trait Pattern < ' a > : Sized {
101
92
/// Associated searcher for this pattern
102
93
type Searcher : Searcher < ' a > ;
@@ -165,7 +156,7 @@ pub trait Pattern<'a>: Sized {
165
156
166
157
// Searcher
167
158
168
- /// Result of calling `Searcher::next()` or `ReverseSearcher::next_back()`.
159
+ /// Result of calling [ `Searcher::next()`] or [ `ReverseSearcher::next_back()`] .
169
160
#[ derive( Copy , Clone , Eq , PartialEq , Debug ) ]
170
161
pub enum SearchStep {
171
162
/// Expresses that a match of the pattern has been found at
@@ -188,44 +179,47 @@ pub enum SearchStep {
188
179
/// matches of a pattern starting from the front (left) of a string.
189
180
///
190
181
/// It will be implemented by associated `Searcher`
191
- /// types of the `Pattern` trait.
182
+ /// types of the [ `Pattern`] trait.
192
183
///
193
184
/// The trait is marked unsafe because the indices returned by the
194
- /// `next()` methods are required to lie on valid utf8 boundaries in
195
- /// the haystack. This enables consumers of this trait to
185
+ /// [ `next()`][Searcher::next] methods are required to lie on valid utf8
186
+ /// boundaries in the haystack. This enables consumers of this trait to
196
187
/// slice the haystack without additional runtime checks.
197
188
pub unsafe trait Searcher < ' a > {
198
189
/// Getter for the underlying string to be searched in
199
190
///
200
- /// Will always return the same `&str`
191
+ /// Will always return the same [ `&str`][str].
201
192
fn haystack ( & self ) -> & ' a str ;
202
193
203
194
/// Performs the next search step starting from the front.
204
195
///
205
- /// - Returns `Match(a, b)` if `haystack[a..b]` matches the pattern.
206
- /// - Returns `Reject(a, b)` if `haystack[a..b]` can not match the
207
- /// pattern, even partially.
208
- /// - Returns `Done` if every byte of the haystack has been visited
196
+ /// - Returns [`Match(a, b)`][SearchStep::Match] if `haystack[a..b]` matches
197
+ /// the pattern.
198
+ /// - Returns [`Reject(a, b)`][SearchStep::Reject] if `haystack[a..b]` can
199
+ /// not match the pattern, even partially.
200
+ /// - Returns [`Done`][SearchStep::Done] if every byte of the haystack has
201
+ /// been visited.
209
202
///
210
- /// The stream of `Match` and `Reject` values up to a `Done`
203
+ /// The stream of [`Match`][SearchStep::Match] and
204
+ /// [`Reject`][SearchStep::Reject] values up to a [`Done`][SearchStep::Done]
211
205
/// will contain index ranges that are adjacent, non-overlapping,
212
206
/// covering the whole haystack, and laying on utf8 boundaries.
213
207
///
214
- /// A `Match` result needs to contain the whole matched pattern,
215
- /// however `Reject` results may be split up into arbitrary
216
- /// many adjacent fragments. Both ranges may have zero length.
208
+ /// A [ `Match`][SearchStep::Match] result needs to contain the whole matched
209
+ /// pattern, however [ `Reject`][SearchStep::Reject] results may be split up
210
+ /// into arbitrary many adjacent fragments. Both ranges may have zero length.
217
211
///
218
212
/// As an example, the pattern `"aaa"` and the haystack `"cbaaaaab"`
219
213
/// might produce the stream
220
214
/// `[Reject(0, 1), Reject(1, 2), Match(2, 5), Reject(5, 8)]`
221
215
fn next ( & mut self ) -> SearchStep ;
222
216
223
- /// Finds the next `Match` result. See `next()`
217
+ /// Finds the next [ `Match`][SearchStep::Match] result. See [ `next()`][Searcher::next].
224
218
///
225
- /// Unlike next(), there is no guarantee that the returned ranges
226
- /// of this and next_reject will overlap. This will return (start_match, end_match),
227
- /// where start_match is the index of where the match begins, and end_match is
228
- /// the index after the end of the match.
219
+ /// Unlike [` next()`][Searcher::next] , there is no guarantee that the returned ranges
220
+ /// of this and [` next_reject`][Searcher::next_reject] will overlap. This will return
221
+ /// `(start_match, end_match)`, where start_match is the index of where
222
+ /// the match begins, and end_match is the index after the end of the match.
229
223
#[ inline]
230
224
fn next_match ( & mut self ) -> Option < ( usize , usize ) > {
231
225
loop {
@@ -237,10 +231,11 @@ pub unsafe trait Searcher<'a> {
237
231
}
238
232
}
239
233
240
- /// Finds the next `Reject` result. See `next()` and `next_match()`
234
+ /// Finds the next [`Reject`][SearchStep::Reject] result. See [`next()`][Searcher::next]
235
+ /// and [`next_match()`][Searcher::next_match].
241
236
///
242
- /// Unlike next(), there is no guarantee that the returned ranges
243
- /// of this and next_match will overlap.
237
+ /// Unlike [` next()`][Searcher::next] , there is no guarantee that the returned ranges
238
+ /// of this and [` next_match`][Searcher::next_match] will overlap.
244
239
#[ inline]
245
240
fn next_reject ( & mut self ) -> Option < ( usize , usize ) > {
246
241
loop {
@@ -258,37 +253,41 @@ pub unsafe trait Searcher<'a> {
258
253
/// This trait provides methods for searching for non-overlapping
259
254
/// matches of a pattern starting from the back (right) of a string.
260
255
///
261
- /// It will be implemented by associated `Searcher`
262
- /// types of the `Pattern` trait if the pattern supports searching
256
+ /// It will be implemented by associated [ `Searcher`]
257
+ /// types of the [ `Pattern`] trait if the pattern supports searching
263
258
/// for it from the back.
264
259
///
265
260
/// The index ranges returned by this trait are not required
266
261
/// to exactly match those of the forward search in reverse.
267
262
///
268
263
/// For the reason why this trait is marked unsafe, see them
269
- /// parent trait `Searcher`.
264
+ /// parent trait [ `Searcher`] .
270
265
pub unsafe trait ReverseSearcher < ' a > : Searcher < ' a > {
271
266
/// Performs the next search step starting from the back.
272
267
///
273
- /// - Returns `Match(a, b)` if `haystack[a..b]` matches the pattern.
274
- /// - Returns `Reject(a, b)` if `haystack[a..b]` can not match the
275
- /// pattern, even partially.
276
- /// - Returns `Done` if every byte of the haystack has been visited
268
+ /// - Returns [`Match(a, b)`][SearchStep::Match] if `haystack[a..b]`
269
+ /// matches the pattern.
270
+ /// - Returns [`Reject(a, b)`][SearchStep::Reject] if `haystack[a..b]`
271
+ /// can not match the pattern, even partially.
272
+ /// - Returns [`Done`][SearchStep::Done] if every byte of the haystack
273
+ /// has been visited
277
274
///
278
- /// The stream of `Match` and `Reject` values up to a `Done`
275
+ /// The stream of [`Match`][SearchStep::Match] and
276
+ /// [`Reject`][SearchStep::Reject] values up to a [`Done`][SearchStep::Done]
279
277
/// will contain index ranges that are adjacent, non-overlapping,
280
278
/// covering the whole haystack, and laying on utf8 boundaries.
281
279
///
282
- /// A `Match` result needs to contain the whole matched pattern,
283
- /// however `Reject` results may be split up into arbitrary
284
- /// many adjacent fragments. Both ranges may have zero length.
280
+ /// A [ `Match`][SearchStep::Match] result needs to contain the whole matched
281
+ /// pattern, however [ `Reject`][SearchStep::Reject] results may be split up
282
+ /// into arbitrary many adjacent fragments. Both ranges may have zero length.
285
283
///
286
284
/// As an example, the pattern `"aaa"` and the haystack `"cbaaaaab"`
287
285
/// might produce the stream
288
- /// `[Reject(7, 8), Match(4, 7), Reject(1, 4), Reject(0, 1)]`
286
+ /// `[Reject(7, 8), Match(4, 7), Reject(1, 4), Reject(0, 1)]`.
289
287
fn next_back ( & mut self ) -> SearchStep ;
290
288
291
- /// Finds the next `Match` result. See `next_back()`
289
+ /// Finds the next [`Match`][SearchStep::Match] result.
290
+ /// See [`next_back()`][ReverseSearcher::next_back].
292
291
#[ inline]
293
292
fn next_match_back ( & mut self ) -> Option < ( usize , usize ) > {
294
293
loop {
@@ -300,7 +299,8 @@ pub unsafe trait ReverseSearcher<'a>: Searcher<'a> {
300
299
}
301
300
}
302
301
303
- /// Finds the next `Reject` result. See `next_back()`
302
+ /// Finds the next [`Reject`][SearchStep::Reject] result.
303
+ /// See [`next_back()`][ReverseSearcher::next_back].
304
304
#[ inline]
305
305
fn next_reject_back ( & mut self ) -> Option < ( usize , usize ) > {
306
306
loop {
@@ -313,10 +313,10 @@ pub unsafe trait ReverseSearcher<'a>: Searcher<'a> {
313
313
}
314
314
}
315
315
316
- /// A marker trait to express that a `ReverseSearcher`
317
- /// can be used for a `DoubleEndedIterator` implementation.
316
+ /// A marker trait to express that a [ `ReverseSearcher`]
317
+ /// can be used for a [ `DoubleEndedIterator`] implementation.
318
318
///
319
- /// For this, the impl of `Searcher` and `ReverseSearcher` need
319
+ /// For this, the impl of [ `Searcher`] and [ `ReverseSearcher`] need
320
320
/// to follow these conditions:
321
321
///
322
322
/// - All results of `next()` need to be identical
@@ -328,7 +328,7 @@ pub unsafe trait ReverseSearcher<'a>: Searcher<'a> {
328
328
/// # Examples
329
329
///
330
330
/// `char::Searcher` is a `DoubleEndedSearcher` because searching for a
331
- /// `char` only requires looking at one at a time, which behaves the same
331
+ /// [ `char`] only requires looking at one at a time, which behaves the same
332
332
/// from both ends.
333
333
///
334
334
/// `(&str)::Searcher` is not a `DoubleEndedSearcher` because
@@ -355,13 +355,13 @@ pub struct CharSearcher<'a> {
355
355
/// `finger_back` is the current byte index of the reverse search.
356
356
/// Imagine that it exists after the byte at its index, i.e.
357
357
/// haystack[finger_back - 1] is the last byte of the slice we must inspect during
358
- /// forward searching (and thus the first byte to be inspected when calling next_back())
358
+ /// forward searching (and thus the first byte to be inspected when calling next_back()).
359
359
finger_back : usize ,
360
360
/// The character being searched for
361
361
needle : char ,
362
362
363
363
// safety invariant: `utf8_size` must be less than 5
364
- /// The number of bytes `needle` takes up when encoded in utf8
364
+ /// The number of bytes `needle` takes up when encoded in utf8.
365
365
utf8_size : usize ,
366
366
/// A utf8 encoded copy of the `needle`
367
367
utf8_encoded : [ u8 ; 4 ] ,
@@ -521,7 +521,7 @@ unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
521
521
522
522
impl < ' a > DoubleEndedSearcher < ' a > for CharSearcher < ' a > { }
523
523
524
- /// Searches for chars that are equal to a given `char`.
524
+ /// Searches for chars that are equal to a given [ `char`] .
525
525
///
526
526
/// # Examples
527
527
///
@@ -772,7 +772,7 @@ unsafe impl<'a, 'b> ReverseSearcher<'a> for CharSliceSearcher<'a, 'b> {
772
772
773
773
impl < ' a , ' b > DoubleEndedSearcher < ' a > for CharSliceSearcher < ' a , ' b > { }
774
774
775
- /// Searches for chars that are equal to any of the chars in the slice.
775
+ /// Searches for chars that are equal to any of the [`char`]s in the slice.
776
776
///
777
777
/// # Examples
778
778
///
@@ -821,7 +821,7 @@ where
821
821
822
822
impl < ' a , F > DoubleEndedSearcher < ' a > for CharPredicateSearcher < ' a , F > where F : FnMut ( char ) -> bool { }
823
823
824
- /// Searches for chars that match the given predicate.
824
+ /// Searches for [`char`]s that match the given predicate.
825
825
///
826
826
/// # Examples
827
827
///
0 commit comments