about summary refs log tree commit diff
diff options
context:
space:
mode:
authorbors <bors@rust-lang.org>2016-05-19 16:22:50 -0700
committerbors <bors@rust-lang.org>2016-05-19 16:22:50 -0700
commit1ec80f65fbc5b16e2b9238ee9888ca242bbb7c5e (patch)
treecf3c74593a16043515f6b5524bc16a5aae78d516
parent764ef92ae7a26cbb9c2121de3812a0a17739f65f (diff)
parent8169fa2fe84b6e3944f7362d3e73ba763a2da1d9 (diff)
downloadrust-1ec80f65fbc5b16e2b9238ee9888ca242bbb7c5e.tar.gz
rust-1ec80f65fbc5b16e2b9238ee9888ca242bbb7c5e.zip
Auto merge of #33103 - ranma42:escape-unicode-last, r=alexcrichton
Implement `last` for `EscapeUnicode`

The implementation is quite trivial as the last character is always `'{'`.
As a side-effect it also improves the implementation of `last` for `EscapeUnicode`.

Part of #24214, split from #31049.

Maybe this (and the other changes that I will split from #31049) should wait for a test like `ed_iterator_specializations` to be added. Would it be sufficient to do the same for each possible escape length?
-rw-r--r--src/libcore/char.rs12
-rw-r--r--src/libcoretest/char.rs33
2 files changed, 45 insertions, 0 deletions
diff --git a/src/libcore/char.rs b/src/libcore/char.rs
index 140403884b9..65b9a27bb68 100644
--- a/src/libcore/char.rs
+++ b/src/libcore/char.rs
@@ -471,6 +471,18 @@ impl Iterator for EscapeUnicode {
         let n = n + self.hex_digit_idx;
         (n, Some(n))
     }
+
+    fn last(self) -> Option<char> {
+        match self.state {
+            EscapeUnicodeState::Done => None,
+
+            EscapeUnicodeState::RightBrace |
+            EscapeUnicodeState::Value |
+            EscapeUnicodeState::LeftBrace |
+            EscapeUnicodeState::Type |
+            EscapeUnicodeState::Backslash => Some('}'),
+        }
+    }
 }
 
 /// An iterator that yields the literal escape code of a `char`.
diff --git a/src/libcoretest/char.rs b/src/libcoretest/char.rs
index 41fd742c9e0..e959e71daf7 100644
--- a/src/libcoretest/char.rs
+++ b/src/libcoretest/char.rs
@@ -262,4 +262,37 @@ fn ed_iterator_specializations() {
     assert_eq!('\''.escape_default().last(), Some('\''));
 }
 
+#[test]
+fn eu_iterator_specializations() {
+    fn check(c: char) {
+        let len = c.escape_unicode().count();
+
+        // Check OoB
+        assert_eq!(c.escape_unicode().nth(len), None);
+
+        // For all possible in-bound offsets
+        let mut iter = c.escape_unicode();
+        for offset in 0..len {
+            // Check last
+            assert_eq!(iter.clone().last(), Some('}'));
 
+            // Check counting
+            assert_eq!(iter.clone().count(), len - offset);
+
+            // Check nth
+            assert_eq!(c.escape_unicode().nth(offset), iter.next());
+        }
+
+        // Check post-last
+        assert_eq!(iter.clone().last(), None);
+        assert_eq!(iter.clone().count(), 0);
+    }
+
+    check('\u{0}');
+    check('\u{1}');
+    check('\u{12}');
+    check('\u{123}');
+    check('\u{1234}');
+    check('\u{12340}');
+    check('\u{10FFFF}');
+}