Skip to content

Commit da03950

Browse files
committed
Move length computation to ExactSizeIterator impls
and reuse it in `size_hint`.
1 parent baa9680 commit da03950

File tree

1 file changed

+36
-24
lines changed

1 file changed

+36
-24
lines changed

src/libcore/char.rs

+36-24
Original file line numberDiff line numberDiff line change
@@ -413,12 +413,12 @@ pub struct EscapeUnicode {
413413

414414
#[derive(Clone, Debug)]
415415
enum EscapeUnicodeState {
416-
Backslash,
417-
Type,
418-
LeftBrace,
419-
Value,
420-
RightBrace,
421416
Done,
417+
RightBrace,
418+
Value,
419+
LeftBrace,
420+
Type,
421+
Backslash,
422422
}
423423

424424
#[stable(feature = "rust1", since = "1.0.0")]
@@ -457,16 +457,9 @@ impl Iterator for EscapeUnicode {
457457
}
458458
}
459459

460+
#[inline]
460461
fn size_hint(&self) -> (usize, Option<usize>) {
461-
let n = match self.state {
462-
EscapeUnicodeState::Backslash => 5,
463-
EscapeUnicodeState::Type => 4,
464-
EscapeUnicodeState::LeftBrace => 3,
465-
EscapeUnicodeState::Value => 2,
466-
EscapeUnicodeState::RightBrace => 1,
467-
EscapeUnicodeState::Done => 0,
468-
};
469-
let n = n + self.hex_digit_idx;
462+
let n = self.len();
470463
(n, Some(n))
471464
}
472465

@@ -489,7 +482,20 @@ impl Iterator for EscapeUnicode {
489482
}
490483

491484
#[stable(feature = "exact_size_escape", since = "1.11.0")]
492-
impl ExactSizeIterator for EscapeUnicode { }
485+
impl ExactSizeIterator for EscapeUnicode {
486+
#[inline]
487+
fn len(&self) -> usize {
488+
// The match is a single memory access with no branching
489+
self.hex_digit_idx + match self.state {
490+
EscapeUnicodeState::Done => 0,
491+
EscapeUnicodeState::RightBrace => 1,
492+
EscapeUnicodeState::Value => 2,
493+
EscapeUnicodeState::LeftBrace => 3,
494+
EscapeUnicodeState::Type => 4,
495+
EscapeUnicodeState::Backslash => 5,
496+
}
497+
}
498+
}
493499

494500
/// An iterator that yields the literal escape code of a `char`.
495501
///
@@ -506,9 +512,9 @@ pub struct EscapeDefault {
506512

507513
#[derive(Clone, Debug)]
508514
enum EscapeDefaultState {
509-
Backslash(char),
510-
Char(char),
511515
Done,
516+
Char(char),
517+
Backslash(char),
512518
Unicode(EscapeUnicode),
513519
}
514520

@@ -531,13 +537,10 @@ impl Iterator for EscapeDefault {
531537
}
532538
}
533539

540+
#[inline]
534541
fn size_hint(&self) -> (usize, Option<usize>) {
535-
match self.state {
536-
EscapeDefaultState::Char(_) => (1, Some(1)),
537-
EscapeDefaultState::Backslash(_) => (2, Some(2)),
538-
EscapeDefaultState::Unicode(ref iter) => iter.size_hint(),
539-
EscapeDefaultState::Done => (0, Some(0)),
540-
}
542+
let n = self.len();
543+
(n, Some(n))
541544
}
542545

543546
#[inline]
@@ -583,7 +586,16 @@ impl Iterator for EscapeDefault {
583586
}
584587

585588
#[stable(feature = "exact_size_escape", since = "1.11.0")]
586-
impl ExactSizeIterator for EscapeDefault { }
589+
impl ExactSizeIterator for EscapeDefault {
590+
fn len(&self) -> usize {
591+
match self.state {
592+
EscapeDefaultState::Done => 0,
593+
EscapeDefaultState::Char(_) => 1,
594+
EscapeDefaultState::Backslash(_) => 2,
595+
EscapeDefaultState::Unicode(ref iter) => iter.len(),
596+
}
597+
}
598+
}
587599

588600
/// An iterator over `u8` entries represending the UTF-8 encoding of a `char`
589601
/// value.

0 commit comments

Comments
 (0)