Skip to content

Commit 72cab5e

Browse files
committed
Split out char searcher from MultiCharSearcher
1 parent 4550ea7 commit 72cab5e

File tree

1 file changed

+58
-29
lines changed

1 file changed

+58
-29
lines changed

src/libcore/str/pattern.rs

Lines changed: 58 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -235,54 +235,49 @@ pub unsafe trait ReverseSearcher<'a>: Searcher<'a> {
235235
pub trait DoubleEndedSearcher<'a>: ReverseSearcher<'a> {}
236236

237237
/////////////////////////////////////////////////////////////////////////////
238-
// Impl for a CharEq wrapper
238+
// Impl for a MultiCharEq wrapper
239239
/////////////////////////////////////////////////////////////////////////////
240240

241241
#[doc(hidden)]
242-
trait CharEq {
242+
trait MultiCharEq {
243243
fn matches(&mut self, c: char) -> bool;
244244
}
245245

246-
impl CharEq for char {
247-
#[inline]
248-
fn matches(&mut self, c: char) -> bool { *self == c }
249-
}
250-
251-
impl<F> CharEq for F where F: FnMut(char) -> bool {
246+
impl<F> MultiCharEq for F where F: FnMut(char) -> bool {
252247
#[inline]
253248
fn matches(&mut self, c: char) -> bool { (*self)(c) }
254249
}
255250

256-
impl<'a> CharEq for &'a [char] {
251+
impl<'a> MultiCharEq for &'a [char] {
257252
#[inline]
258253
fn matches(&mut self, c: char) -> bool {
259-
self.iter().any(|&m| { let mut m = m; m.matches(c) })
254+
self.iter().any(|&m| { m == c })
260255
}
261256
}
262257

263-
struct CharEqPattern<C: CharEq>(C);
258+
struct MultiCharEqPattern<C: MultiCharEq>(C);
264259

265260
#[derive(Clone, Debug)]
266-
struct CharEqSearcher<'a, C: CharEq> {
261+
struct MultiCharEqSearcher<'a, C: MultiCharEq> {
267262
char_eq: C,
268263
haystack: &'a str,
269264
char_indices: super::CharIndices<'a>,
270265
}
271266

272-
impl<'a, C: CharEq> Pattern<'a> for CharEqPattern<C> {
273-
type Searcher = CharEqSearcher<'a, C>;
267+
impl<'a, C: MultiCharEq> Pattern<'a> for MultiCharEqPattern<C> {
268+
type Searcher = MultiCharEqSearcher<'a, C>;
274269

275270
#[inline]
276-
fn into_searcher(self, haystack: &'a str) -> CharEqSearcher<'a, C> {
277-
CharEqSearcher {
271+
fn into_searcher(self, haystack: &'a str) -> MultiCharEqSearcher<'a, C> {
272+
MultiCharEqSearcher {
278273
haystack,
279274
char_eq: self.0,
280275
char_indices: haystack.char_indices(),
281276
}
282277
}
283278
}
284279

285-
unsafe impl<'a, C: CharEq> Searcher<'a> for CharEqSearcher<'a, C> {
280+
unsafe impl<'a, C: MultiCharEq> Searcher<'a> for MultiCharEqSearcher<'a, C> {
286281
#[inline]
287282
fn haystack(&self) -> &'a str {
288283
self.haystack
@@ -307,7 +302,7 @@ unsafe impl<'a, C: CharEq> Searcher<'a> for CharEqSearcher<'a, C> {
307302
}
308303
}
309304

310-
unsafe impl<'a, C: CharEq> ReverseSearcher<'a> for CharEqSearcher<'a, C> {
305+
unsafe impl<'a, C: MultiCharEq> ReverseSearcher<'a> for MultiCharEqSearcher<'a, C> {
311306
#[inline]
312307
fn next_back(&mut self) -> SearchStep {
313308
let s = &mut self.char_indices;
@@ -327,7 +322,7 @@ unsafe impl<'a, C: CharEq> ReverseSearcher<'a> for CharEqSearcher<'a, C> {
327322
}
328323
}
329324

330-
impl<'a, C: CharEq> DoubleEndedSearcher<'a> for CharEqSearcher<'a, C> {}
325+
impl<'a, C: MultiCharEq> DoubleEndedSearcher<'a> for MultiCharEqSearcher<'a, C> {}
331326

332327
/////////////////////////////////////////////////////////////////////////////
333328

@@ -400,14 +395,40 @@ macro_rules! searcher_methods {
400395

401396
/// Associated type for `<char as Pattern<'a>>::Searcher`.
402397
#[derive(Clone, Debug)]
403-
pub struct CharSearcher<'a>(<CharEqPattern<char> as Pattern<'a>>::Searcher);
398+
pub struct CharSearcher<'a>(&'a str);
404399

405400
unsafe impl<'a> Searcher<'a> for CharSearcher<'a> {
406-
searcher_methods!(forward);
401+
#[inline]
402+
fn haystack(&self) -> &'a str {
403+
unimplemented!();
404+
}
405+
#[inline]
406+
fn next(&mut self) -> SearchStep {
407+
unimplemented!();
408+
}
409+
#[inline]
410+
fn next_match(&mut self) -> Option<(usize, usize)> {
411+
unimplemented!();
412+
}
413+
#[inline]
414+
fn next_reject(&mut self) -> Option<(usize, usize)> {
415+
unimplemented!();
416+
}
407417
}
408418

409419
unsafe impl<'a> ReverseSearcher<'a> for CharSearcher<'a> {
410-
searcher_methods!(reverse);
420+
#[inline]
421+
fn next_back(&mut self) -> SearchStep {
422+
unimplemented!();
423+
}
424+
#[inline]
425+
fn next_match_back(&mut self) -> Option<(usize, usize)> {
426+
unimplemented!();
427+
}
428+
#[inline]
429+
fn next_reject_back(&mut self) -> Option<(usize, usize)> {
430+
unimplemented!();
431+
}
411432
}
412433

413434
impl<'a> DoubleEndedSearcher<'a> for CharSearcher<'a> {}
@@ -418,7 +439,7 @@ impl<'a> Pattern<'a> for char {
418439

419440
#[inline]
420441
fn into_searcher(self, haystack: &'a str) -> Self::Searcher {
421-
CharSearcher(CharEqPattern(self).into_searcher(haystack))
442+
CharSearcher(haystack)
422443
}
423444

424445
#[inline]
@@ -433,13 +454,21 @@ impl<'a> Pattern<'a> for char {
433454

434455
#[inline]
435456
fn is_prefix_of(self, haystack: &'a str) -> bool {
436-
CharEqPattern(self).is_prefix_of(haystack)
457+
if let Some(ch) = haystack.chars().next() {
458+
self == ch
459+
} else {
460+
false
461+
}
437462
}
438463

439464
#[inline]
440465
fn is_suffix_of(self, haystack: &'a str) -> bool where Self::Searcher: ReverseSearcher<'a>
441466
{
442-
CharEqPattern(self).is_suffix_of(haystack)
467+
if let Some(ch) = haystack.chars().next_back() {
468+
self == ch
469+
} else {
470+
false
471+
}
443472
}
444473
}
445474

@@ -451,7 +480,7 @@ impl<'a> Pattern<'a> for char {
451480

452481
/// Associated type for `<&[char] as Pattern<'a>>::Searcher`.
453482
#[derive(Clone, Debug)]
454-
pub struct CharSliceSearcher<'a, 'b>(<CharEqPattern<&'b [char]> as Pattern<'a>>::Searcher);
483+
pub struct CharSliceSearcher<'a, 'b>(<MultiCharEqPattern<&'b [char]> as Pattern<'a>>::Searcher);
455484

456485
unsafe impl<'a, 'b> Searcher<'a> for CharSliceSearcher<'a, 'b> {
457486
searcher_methods!(forward);
@@ -465,7 +494,7 @@ impl<'a, 'b> DoubleEndedSearcher<'a> for CharSliceSearcher<'a, 'b> {}
465494

466495
/// Searches for chars that are equal to any of the chars in the array
467496
impl<'a, 'b> Pattern<'a> for &'b [char] {
468-
pattern_methods!(CharSliceSearcher<'a, 'b>, CharEqPattern, CharSliceSearcher);
497+
pattern_methods!(CharSliceSearcher<'a, 'b>, MultiCharEqPattern, CharSliceSearcher);
469498
}
470499

471500
/////////////////////////////////////////////////////////////////////////////
@@ -474,7 +503,7 @@ impl<'a, 'b> Pattern<'a> for &'b [char] {
474503

475504
/// Associated type for `<F as Pattern<'a>>::Searcher`.
476505
#[derive(Clone)]
477-
pub struct CharPredicateSearcher<'a, F>(<CharEqPattern<F> as Pattern<'a>>::Searcher)
506+
pub struct CharPredicateSearcher<'a, F>(<MultiCharEqPattern<F> as Pattern<'a>>::Searcher)
478507
where F: FnMut(char) -> bool;
479508

480509
impl<'a, F> fmt::Debug for CharPredicateSearcher<'a, F>
@@ -504,7 +533,7 @@ impl<'a, F> DoubleEndedSearcher<'a> for CharPredicateSearcher<'a, F>
504533

505534
/// Searches for chars that match the given predicate
506535
impl<'a, F> Pattern<'a> for F where F: FnMut(char) -> bool {
507-
pattern_methods!(CharPredicateSearcher<'a, F>, CharEqPattern, CharPredicateSearcher);
536+
pattern_methods!(CharPredicateSearcher<'a, F>, MultiCharEqPattern, CharPredicateSearcher);
508537
}
509538

510539
/////////////////////////////////////////////////////////////////////////////

0 commit comments

Comments
 (0)