{"action":"created","comment":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/comments/64618377","id":64618377,"diff_hunk":"@@ -1549,93 +1594,182 @@ impl<R: Read> Iterator for Bytes<R> {\n \n /// An iterator over the `char`s of a reader.\n ///\n-/// This struct is generally created by calling [`chars()`][chars] on a reader.\n-/// Please see the documentation of `chars()` for more details.\n+/// This struct is generally created by calling [`utf8_chars()`][utf8_chars] on a reader.\n+/// Please see the documentation of `utf8_chars()` for more details.\n ///\n-/// [chars]: trait.Read.html#method.chars\n-#[unstable(feature = \"io\", reason = \"awaiting stability of Read::chars\",\n+/// [utf8_chars]: trait.BufRead.html#method.utf8_chars\n+#[unstable(feature = \"io\", reason = \"awaiting stability of BufRead::utf8_chars\",\n            issue = \"27802\")]\n-pub struct Chars<R> {\n+pub struct Utf8Chars<R> {\n     inner: R,\n }\n \n-/// An enumeration of possible errors that can be generated from the `Chars`\n+/// An enumeration of possible errors that can be generated from the `Utf8Chars`\n /// adapter.\n #[derive(Debug)]\n-#[unstable(feature = \"io\", reason = \"awaiting stability of Read::chars\",\n+#[unstable(feature = \"io\", reason = \"awaiting stability of BufRead::utf8_chars\",\n            issue = \"27802\")]\n-pub enum CharsError {\n+pub enum Utf8CharsError {\n     /// Variant representing that the underlying stream was read successfully\n-    /// but it did not contain valid utf8 data.\n-    NotUtf8,\n+    /// but contains a byte sequence ill-formed in UTF-8.\n+    InvalidUtf8,\n+\n+    /// Variant representing that the underlying stream contains the start\n+    /// of a byte sequence well-formed in UTF-8, but ends prematurely.\n+    IncompleteUtf8,\n \n     /// Variant representing that an I/O error occurred.\n-    Other(Error),\n+    Io(Error),\n }\n \n-#[unstable(feature = \"io\", reason = \"awaiting stability of Read::chars\",\n+#[unstable(feature = \"io\", reason = \"awaiting stability of BufRead::utf8_chars\",\n            issue = \"27802\")]\n-impl<R: Read> Iterator for Chars<R> {\n-    type Item = result::Result<char, CharsError>;\n+impl<R: BufRead> Iterator for Utf8Chars<R> {\n+    type Item = result::Result<char, Utf8CharsError>;\n+\n+    fn next(&mut self) -> Option<result::Result<char, Utf8CharsError>> {\n+        macro_rules! read_byte {\n+            (EOF => $on_eof: expr) => {\n+                {\n+                    let byte;\n+                    loop {\n+                        match self.inner.fill_buf() {\n+                            Ok(buffer) => {\n+                                if let Some(&b) = buffer.first() {\n+                                    byte = b;\n+                                    break\n+                                } else {\n+                                    $on_eof\n+                                }\n+                            }\n+                            Err(ref e) if e.kind() == ErrorKind::Interrupted => {}\n+                            Err(e) => return Some(Err(Utf8CharsError::Io(e))),\n+                        }\n+                    }\n+                    byte\n+                }\n+            }\n+        }\n \n-    fn next(&mut self) -> Option<result::Result<char, CharsError>> {\n-        let mut buf = [0];\n-        let first_byte = match self.inner.read(&mut buf) {\n-            Ok(0) => return None,\n-            Ok(..) => buf[0],\n-            Err(e) => return Some(Err(CharsError::Other(e))),\n-        };\n-        let width = core_str::utf8_char_width(first_byte);\n-        if width == 1 { return Some(Ok(first_byte as char)) }\n-        if width == 0 { return Some(Err(CharsError::NotUtf8)) }\n-        let mut buf = [first_byte, 0, 0, 0];\n-        {\n-            let mut start = 1;\n-            while start < width {\n-                match self.inner.read(&mut buf[start..width]) {\n-                    Ok(0) => return Some(Err(CharsError::NotUtf8)),\n-                    Ok(n) => start += n,\n-                    Err(e) => return Some(Err(CharsError::Other(e))),\n+        let first = read_byte!(EOF => return None);\n+        self.inner.consume(1);\n+\n+        macro_rules! continuation_byte {\n+            ($range: pat) => {\n+                {\n+                    match read_byte!(EOF => return Some(Err(Utf8CharsError::IncompleteUtf8))) {\n+                        byte @ $range => {\n+                            self.inner.consume(1);\n+                            (byte & 0b0011_1111) as u32\n+                        }\n+                        _ => return Some(Err(Utf8CharsError::InvalidUtf8))\n+                    }\n                 }\n             }\n         }\n-        Some(match str::from_utf8(&buf[..width]).ok() {\n-            Some(s) => Ok(s.chars().next().unwrap()),\n-            None => Err(CharsError::NotUtf8),\n-        })\n+\n+        // Ranges can be checked against https://tools.ietf.org/html/rfc3629#section-4\n+        let code_point = match core_str::utf8_char_width(first) {\n+            1 => return Some(Ok(first as char)),\n+            2 => {\n+                let second = continuation_byte!(0x80...0xBF);\n+                ((first & 0b0001_1111) as u32) << 6 | second\n+            }\n+            3 => {\n+                let second = match first {\n+                    0xE0        => continuation_byte!(0xA0...0xBF),","path":"src/libstd/io/mod.rs","position":265,"original_position":265,"commit_id":"73353f20ac57f719e4f5c8c27b05c6b59f44a42c","original_commit_id":"73353f20ac57f719e4f5c8c27b05c6b59f44a42c","user":{"login":"SimonSapin","id":291359,"avatar_url":"https://avatars.githubusercontent.com/u/291359?v=3","gravatar_id":"","url":"https://api.github.com/users/SimonSapin","html_url":"https://github.com/SimonSapin","followers_url":"https://api.github.com/users/SimonSapin/followers","following_url":"https://api.github.com/users/SimonSapin/following{/other_user}","gists_url":"https://api.github.com/users/SimonSapin/gists{/gist_id}","starred_url":"https://api.github.com/users/SimonSapin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SimonSapin/subscriptions","organizations_url":"https://api.github.com/users/SimonSapin/orgs","repos_url":"https://api.github.com/users/SimonSapin/repos","events_url":"https://api.github.com/users/SimonSapin/events{/privacy}","received_events_url":"https://api.github.com/users/SimonSapin/received_events","type":"User","site_admin":false},"body":"I don’t understand what you mean by substitute.\r\n\r\nhttp://www.cl.cam.ac.uk/%7Emgk25/ucs/examples/UTF-8-test.txt doesn’t really have test cases:\r\n> This file is not meant to be a conformance test. It does not prescribe any particular outcome. Therefore, there is no way to \"pass\" or \"fail\" this test file","created_at":"2016-05-25T17:41:21Z","updated_at":"2016-05-25T17:41:21Z","html_url":"https://github.com/rust-lang/rust/pull/33801#discussion_r64618377","pull_request_url":"https://api.github.com/repos/rust-lang/rust/pulls/33801","_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/comments/64618377"},"html":{"href":"https://github.com/rust-lang/rust/pull/33801#discussion_r64618377"},"pull_request":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/33801"}}},"pull_request":{"url":"https://api.github.com/repos/rust-lang/rust/pulls/33801","id":70991490,"html_url":"https://github.com/rust-lang/rust/pull/33801","diff_url":"https://github.com/rust-lang/rust/pull/33801.diff","patch_url":"https://github.com/rust-lang/rust/pull/33801.patch","issue_url":"https://api.github.com/repos/rust-lang/rust/issues/33801","number":33801,"state":"open","locked":false,"title":"Read::chars reform","user":{"login":"SimonSapin","id":291359,"avatar_url":"https://avatars.githubusercontent.com/u/291359?v=3","gravatar_id":"","url":"https://api.github.com/users/SimonSapin","html_url":"https://github.com/SimonSapin","followers_url":"https://api.github.com/users/SimonSapin/followers","following_url":"https://api.github.com/users/SimonSapin/following{/other_user}","gists_url":"https://api.github.com/users/SimonSapin/gists{/gist_id}","starred_url":"https://api.github.com/users/SimonSapin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SimonSapin/subscriptions","organizations_url":"https://api.github.com/users/SimonSapin/orgs","repos_url":"https://api.github.com/users/SimonSapin/repos","events_url":"https://api.github.com/users/SimonSapin/events{/privacy}","received_events_url":"https://api.github.com/users/SimonSapin/received_events","type":"User","site_admin":false},"body":"* Don’t swallow valid code point on UTF-8 decoding errors, as discussed in https://github.com/rust-lang/rust/issues/27802#issuecomment-172525439\r\n* Rename/split error variants\r\n* Rename the method to `utf8_chars`, as previously proposed and discussed in #33761.\r\n* Add `Read::utf8_chars_lossy` which replaces UTF-8 errors with U+FFFD, leaving only I/O errors.\r\n* **Edit 2016-05-25**: per discussion in #27802, move the methods to `BufRead`. `Read::chars` is now deprecated and returns `Utf8Chars<BufReader<Self>>` with a one-byte buffer to stay closer to the previous behavior.\r\n\r\nInternally this duplicates the UTF-8 decoding logic from `String::from_utf8_lossy`, but let’s decide on the desired behavior first and we can refactor internals later.\r\n\r\nWith this, I’d like to propose the affected methods and types for stabilization (which would complete #27802).\r\n\r\nr? @alexcrichton ","created_at":"2016-05-22T23:09:17Z","updated_at":"2016-05-25T17:41:21Z","closed_at":null,"merged_at":null,"merge_commit_sha":"16438d1f00a27cd8651ee9087afa1da697ffcaa5","assignee":{"login":"alexcrichton","id":64996,"avatar_url":"https://avatars.githubusercontent.com/u/64996?v=3","gravatar_id":"","url":"https://api.github.com/users/alexcrichton","html_url":"https://github.com/alexcrichton","followers_url":"https://api.github.com/users/alexcrichton/followers","following_url":"https://api.github.com/users/alexcrichton/following{/other_user}","gists_url":"https://api.github.com/users/alexcrichton/gists{/gist_id}","starred_url":"https://api.github.com/users/alexcrichton/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/alexcrichton/subscriptions","organizations_url":"https://api.github.com/users/alexcrichton/orgs","repos_url":"https://api.github.com/users/alexcrichton/repos","events_url":"https://api.github.com/users/alexcrichton/events{/privacy}","received_events_url":"https://api.github.com/users/alexcrichton/received_events","type":"User","site_admin":false},"milestone":null,"commits_url":"https://api.github.com/repos/rust-lang/rust/pulls/33801/commits","review_comments_url":"https://api.github.com/repos/rust-lang/rust/pulls/33801/comments","review_comment_url":"https://api.github.com/repos/rust-lang/rust/pulls/comments{/number}","comments_url":"https://api.github.com/repos/rust-lang/rust/issues/33801/comments","statuses_url":"https://api.github.com/repos/rust-lang/rust/statuses/73353f20ac57f719e4f5c8c27b05c6b59f44a42c","head":{"label":"SimonSapin:io-chars-error-handling","ref":"io-chars-error-handling","sha":"73353f20ac57f719e4f5c8c27b05c6b59f44a42c","user":{"login":"SimonSapin","id":291359,"avatar_url":"https://avatars.githubusercontent.com/u/291359?v=3","gravatar_id":"","url":"https://api.github.com/users/SimonSapin","html_url":"https://github.com/SimonSapin","followers_url":"https://api.github.com/users/SimonSapin/followers","following_url":"https://api.github.com/users/SimonSapin/following{/other_user}","gists_url":"https://api.github.com/users/SimonSapin/gists{/gist_id}","starred_url":"https://api.github.com/users/SimonSapin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SimonSapin/subscriptions","organizations_url":"https://api.github.com/users/SimonSapin/orgs","repos_url":"https://api.github.com/users/SimonSapin/repos","events_url":"https://api.github.com/users/SimonSapin/events{/privacy}","received_events_url":"https://api.github.com/users/SimonSapin/received_events","type":"User","site_admin":false},"repo":{"id":7715452,"name":"rust","full_name":"SimonSapin/rust","owner":{"login":"SimonSapin","id":291359,"avatar_url":"https://avatars.githubusercontent.com/u/291359?v=3","gravatar_id":"","url":"https://api.github.com/users/SimonSapin","html_url":"https://github.com/SimonSapin","followers_url":"https://api.github.com/users/SimonSapin/followers","following_url":"https://api.github.com/users/SimonSapin/following{/other_user}","gists_url":"https://api.github.com/users/SimonSapin/gists{/gist_id}","starred_url":"https://api.github.com/users/SimonSapin/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/SimonSapin/subscriptions","organizations_url":"https://api.github.com/users/SimonSapin/orgs","repos_url":"https://api.github.com/users/SimonSapin/repos","events_url":"https://api.github.com/users/SimonSapin/events{/privacy}","received_events_url":"https://api.github.com/users/SimonSapin/received_events","type":"User","site_admin":false},"private":false,"html_url":"https://github.com/SimonSapin/rust","description":"a safe, concurrent, practical language","fork":true,"url":"https://api.github.com/repos/SimonSapin/rust","forks_url":"https://api.github.com/repos/SimonSapin/rust/forks","keys_url":"https://api.github.com/repos/SimonSapin/rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/SimonSapin/rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/SimonSapin/rust/teams","hooks_url":"https://api.github.com/repos/SimonSapin/rust/hooks","issue_events_url":"https://api.github.com/repos/SimonSapin/rust/issues/events{/number}","events_url":"https://api.github.com/repos/SimonSapin/rust/events","assignees_url":"https://api.github.com/repos/SimonSapin/rust/assignees{/user}","branches_url":"https://api.github.com/repos/SimonSapin/rust/branches{/branch}","tags_url":"https://api.github.com/repos/SimonSapin/rust/tags","blobs_url":"https://api.github.com/repos/SimonSapin/rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/SimonSapin/rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/SimonSapin/rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/SimonSapin/rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/SimonSapin/rust/statuses/{sha}","languages_url":"https://api.github.com/repos/SimonSapin/rust/languages","stargazers_url":"https://api.github.com/repos/SimonSapin/rust/stargazers","contributors_url":"https://api.github.com/repos/SimonSapin/rust/contributors","subscribers_url":"https://api.github.com/repos/SimonSapin/rust/subscribers","subscription_url":"https://api.github.com/repos/SimonSapin/rust/subscription","commits_url":"https://api.github.com/repos/SimonSapin/rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/SimonSapin/rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/SimonSapin/rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/SimonSapin/rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/SimonSapin/rust/contents/{+path}","compare_url":"https://api.github.com/repos/SimonSapin/rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/SimonSapin/rust/merges","archive_url":"https://api.github.com/repos/SimonSapin/rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/SimonSapin/rust/downloads","issues_url":"https://api.github.com/repos/SimonSapin/rust/issues{/number}","pulls_url":"https://api.github.com/repos/SimonSapin/rust/pulls{/number}","milestones_url":"https://api.github.com/repos/SimonSapin/rust/milestones{/number}","notifications_url":"https://api.github.com/repos/SimonSapin/rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/SimonSapin/rust/labels{/name}","releases_url":"https://api.github.com/repos/SimonSapin/rust/releases{/id}","deployments_url":"https://api.github.com/repos/SimonSapin/rust/deployments","created_at":"2013-01-20T11:03:20Z","updated_at":"2014-08-09T22:01:15Z","pushed_at":"2016-05-25T08:13:51Z","git_url":"git://github.com/SimonSapin/rust.git","ssh_url":"git@github.com:SimonSapin/rust.git","clone_url":"https://github.com/SimonSapin/rust.git","svn_url":"https://github.com/SimonSapin/rust","homepage":"http://www.rust-lang.org","size":295362,"stargazers_count":0,"watchers_count":0,"language":"Rust","has_issues":false,"has_downloads":true,"has_wiki":true,"has_pages":false,"forks_count":0,"mirror_url":null,"open_issues_count":0,"forks":0,"open_issues":0,"watchers":0,"default_branch":"master"}},"base":{"label":"rust-lang:master","ref":"master","sha":"e24d621fcacf59fa8ecc1c1af1c4adeded4bddee","user":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"repo":{"id":724712,"name":"rust","full_name":"rust-lang/rust","owner":{"login":"rust-lang","id":5430905,"avatar_url":"https://avatars.githubusercontent.com/u/5430905?v=3","gravatar_id":"","url":"https://api.github.com/users/rust-lang","html_url":"https://github.com/rust-lang","followers_url":"https://api.github.com/users/rust-lang/followers","following_url":"https://api.github.com/users/rust-lang/following{/other_user}","gists_url":"https://api.github.com/users/rust-lang/gists{/gist_id}","starred_url":"https://api.github.com/users/rust-lang/starred{/owner}{/repo}","subscriptions_url":"https://api.github.com/users/rust-lang/subscriptions","organizations_url":"https://api.github.com/users/rust-lang/orgs","repos_url":"https://api.github.com/users/rust-lang/repos","events_url":"https://api.github.com/users/rust-lang/events{/privacy}","received_events_url":"https://api.github.com/users/rust-lang/received_events","type":"Organization","site_admin":false},"private":false,"html_url":"https://github.com/rust-lang/rust","description":"A safe, concurrent, practical language.","fork":false,"url":"https://api.github.com/repos/rust-lang/rust","forks_url":"https://api.github.com/repos/rust-lang/rust/forks","keys_url":"https://api.github.com/repos/rust-lang/rust/keys{/key_id}","collaborators_url":"https://api.github.com/repos/rust-lang/rust/collaborators{/collaborator}","teams_url":"https://api.github.com/repos/rust-lang/rust/teams","hooks_url":"https://api.github.com/repos/rust-lang/rust/hooks","issue_events_url":"https://api.github.com/repos/rust-lang/rust/issues/events{/number}","events_url":"https://api.github.com/repos/rust-lang/rust/events","assignees_url":"https://api.github.com/repos/rust-lang/rust/assignees{/user}","branches_url":"https://api.github.com/repos/rust-lang/rust/branches{/branch}","tags_url":"https://api.github.com/repos/rust-lang/rust/tags","blobs_url":"https://api.github.com/repos/rust-lang/rust/git/blobs{/sha}","git_tags_url":"https://api.github.com/repos/rust-lang/rust/git/tags{/sha}","git_refs_url":"https://api.github.com/repos/rust-lang/rust/git/refs{/sha}","trees_url":"https://api.github.com/repos/rust-lang/rust/git/trees{/sha}","statuses_url":"https://api.github.com/repos/rust-lang/rust/statuses/{sha}","languages_url":"https://api.github.com/repos/rust-lang/rust/languages","stargazers_url":"https://api.github.com/repos/rust-lang/rust/stargazers","contributors_url":"https://api.github.com/repos/rust-lang/rust/contributors","subscribers_url":"https://api.github.com/repos/rust-lang/rust/subscribers","subscription_url":"https://api.github.com/repos/rust-lang/rust/subscription","commits_url":"https://api.github.com/repos/rust-lang/rust/commits{/sha}","git_commits_url":"https://api.github.com/repos/rust-lang/rust/git/commits{/sha}","comments_url":"https://api.github.com/repos/rust-lang/rust/comments{/number}","issue_comment_url":"https://api.github.com/repos/rust-lang/rust/issues/comments{/number}","contents_url":"https://api.github.com/repos/rust-lang/rust/contents/{+path}","compare_url":"https://api.github.com/repos/rust-lang/rust/compare/{base}...{head}","merges_url":"https://api.github.com/repos/rust-lang/rust/merges","archive_url":"https://api.github.com/repos/rust-lang/rust/{archive_format}{/ref}","downloads_url":"https://api.github.com/repos/rust-lang/rust/downloads","issues_url":"https://api.github.com/repos/rust-lang/rust/issues{/number}","pulls_url":"https://api.github.com/repos/rust-lang/rust/pulls{/number}","milestones_url":"https://api.github.com/repos/rust-lang/rust/milestones{/number}","notifications_url":"https://api.github.com/repos/rust-lang/rust/notifications{?since,all,participating}","labels_url":"https://api.github.com/repos/rust-lang/rust/labels{/name}","releases_url":"https://api.github.com/repos/rust-lang/rust/releases{/id}","deployments_url":"https://api.github.com/repos/rust-lang/rust/deployments","created_at":"2010-06-16T20:39:03Z","updated_at":"2016-05-25T17:21:44Z","pushed_at":"2016-05-25T16:40:06Z","git_url":"git://github.com/rust-lang/rust.git","ssh_url":"git@github.com:rust-lang/rust.git","clone_url":"https://github.com/rust-lang/rust.git","svn_url":"https://github.com/rust-lang/rust","homepage":"https://www.rust-lang.org","size":496062,"stargazers_count":16801,"watchers_count":16801,"language":"Rust","has_issues":true,"has_downloads":true,"has_wiki":false,"has_pages":false,"forks_count":3198,"mirror_url":null,"open_issues_count":2514,"forks":3198,"open_issues":2514,"watchers":16801,"default_branch":"master"}},"_links":{"self":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/33801"},"html":{"href":"https://github.com/rust-lang/rust/pull/33801"},"issue":{"href":"https://api.github.com/repos/rust-lang/rust/issues/33801"},"comments":{"href":"https://api.github.com/repos/rust-lang/rust/issues/33801/comments"},"review_comments":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/33801/comments"},"review_comment":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/comments{/number}"},"commits":{"href":"https://api.github.com/repos/rust-lang/rust/pulls/33801/commits"},"statuses":{"href":"https://api.github.com/repos/rust-lang/rust/statuses/73353f20ac57f719e4f5c8c27b05c6b59f44a42c"}}}}
{"actor":{"display_login":"SimonSapin"}}