Skip to content

Commit 113b366

Browse files
committed
Move length computation to ExactSizeIterator impls
and reuse it in `size_hint`.
1 parent ad7f68d commit 113b366

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

src/libcore/char.rs

Lines changed: 36 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -436,12 +436,12 @@ pub struct EscapeUnicode {
436436

437437
#[derive(Clone)]
438438
enum EscapeUnicodeState {
439-
Backslash,
440-
Type,
441-
LeftBrace,
442-
Value,
443-
RightBrace,
444439
Done,
440+
RightBrace,
441+
Value,
442+
LeftBrace,
443+
Type,
444+
Backslash,
445445
}
446446

447447
#[stable(feature = "rust1", since = "1.0.0")]
@@ -479,16 +479,9 @@ impl Iterator for EscapeUnicode {
479479
}
480480
}
481481

482+
#[inline]
482483
fn size_hint(&self) -> (usize, Option<usize>) {
483-
let n = match self.state {
484-
EscapeUnicodeState::Backslash => 5,
485-
EscapeUnicodeState::Type => 4,
486-
EscapeUnicodeState::LeftBrace => 3,
487-
EscapeUnicodeState::Value => 2,
488-
EscapeUnicodeState::RightBrace => 1,
489-
EscapeUnicodeState::Done => 0,
490-
};
491-
let n = n + self.offset;
484+
let n = self.len();
492485
(n, Some(n))
493486
}
494487

@@ -511,7 +504,20 @@ impl Iterator for EscapeUnicode {
511504
}
512505

513506
#[stable(feature = "rust1", since = "1.7.0")]
514-
impl ExactSizeIterator for EscapeUnicode { }
507+
impl ExactSizeIterator for EscapeUnicode {
508+
#[inline]
509+
fn len(&self) -> usize {
510+
// The match is a single memory access with no branching
511+
self.offset + self.state {
512+
EscapeUnicodeState::Done => 0,
513+
EscapeUnicodeState::RightBrace => 1,
514+
EscapeUnicodeState::Value => 2,
515+
EscapeUnicodeState::LeftBrace => 3,
516+
EscapeUnicodeState::Type => 4,
517+
EscapeUnicodeState::Backslash => 5,
518+
}
519+
}
520+
}
515521

516522
/// An iterator that yields the literal escape code of a `char`.
517523
///
@@ -528,9 +534,9 @@ pub struct EscapeDefault {
528534

529535
#[derive(Clone)]
530536
enum EscapeDefaultState {
531-
Backslash(char),
532-
Char(char),
533537
Done,
538+
Char(char),
539+
Backslash(char),
534540
Unicode(EscapeUnicode),
535541
}
536542

@@ -553,13 +559,10 @@ impl Iterator for EscapeDefault {
553559
}
554560
}
555561

562+
#[inline]
556563
fn size_hint(&self) -> (usize, Option<usize>) {
557-
match self.state {
558-
EscapeDefaultState::Char(_) => (1, Some(1)),
559-
EscapeDefaultState::Backslash(_) => (2, Some(2)),
560-
EscapeDefaultState::Unicode(ref iter) => iter.size_hint(),
561-
EscapeDefaultState::Done => (0, Some(0)),
562-
}
564+
let n = self.len();
565+
(n, Some(n))
563566
}
564567

565568
#[inline]
@@ -605,4 +608,13 @@ impl Iterator for EscapeDefault {
605608
}
606609

607610
#[stable(feature = "rust1", since = "1.7.0")]
608-
impl ExactSizeIterator for EscapeDefault { }
611+
impl ExactSizeIterator for EscapeDefault {
612+
fn len(&self) -> usize {
613+
match self.state {
614+
EscapeDefaultState::Done => 0,
615+
EscapeDefaultState::Char(_) => 1,
616+
EscapeDefaultState::Backslash(_) => 2,
617+
EscapeDefaultState::Unicode(ref iter) => iter.len(),
618+
}
619+
}
620+
}

0 commit comments

Comments
 (0)