Skip to content

Commit b5f16a1

Browse files
committed
Replace Utf8Error::resume_from with Utf8Error::error_len
Their relationship is: * `resume_from = error_len.map(|l| l + valid_up_to)` * error_len is always one of None, Some(1), Some(2), or Some(3). When I started using resume_from I almost always ended up subtracting valid_up_to to obtain error_len. Therefore the latter is what should be provided in the first place.
1 parent 1820442 commit b5f16a1

File tree

3 files changed

+28
-26
lines changed

3 files changed

+28
-26
lines changed

src/libcollectionstest/lib.rs

+1-1
Original file line numberDiff line numberDiff line change
@@ -28,7 +28,7 @@
2828
#![feature(test)]
2929
#![feature(unboxed_closures)]
3030
#![feature(unicode)]
31-
#![feature(utf8_error_resume_from)]
31+
#![feature(utf8_error_error_len)]
3232

3333
extern crate collections;
3434
extern crate test;

src/libcollectionstest/str.rs

+15-15
Original file line numberDiff line numberDiff line change
@@ -543,31 +543,31 @@ fn from_utf8_mostly_ascii() {
543543
#[test]
544544
fn from_utf8_error() {
545545
macro_rules! test {
546-
($input: expr, $expected_valid_up_to: expr, $expected_resume_from: expr) => {
546+
($input: expr, $expected_valid_up_to: expr, $expected_error_len: expr) => {
547547
let error = from_utf8($input).unwrap_err();
548548
assert_eq!(error.valid_up_to(), $expected_valid_up_to);
549-
assert_eq!(error.resume_from(), $expected_resume_from);
549+
assert_eq!(error.error_len(), $expected_error_len);
550550
}
551551
}
552-
test!(b"A\xC3\xA9 \xFF ", 4, Some(5));
553-
test!(b"A\xC3\xA9 \x80 ", 4, Some(5));
554-
test!(b"A\xC3\xA9 \xC1 ", 4, Some(5));
555-
test!(b"A\xC3\xA9 \xC1", 4, Some(5));
552+
test!(b"A\xC3\xA9 \xFF ", 4, Some(1));
553+
test!(b"A\xC3\xA9 \x80 ", 4, Some(1));
554+
test!(b"A\xC3\xA9 \xC1 ", 4, Some(1));
555+
test!(b"A\xC3\xA9 \xC1", 4, Some(1));
556556
test!(b"A\xC3\xA9 \xC2", 4, None);
557-
test!(b"A\xC3\xA9 \xC2 ", 4, Some(5));
558-
test!(b"A\xC3\xA9 \xC2\xC0", 4, Some(5));
557+
test!(b"A\xC3\xA9 \xC2 ", 4, Some(1));
558+
test!(b"A\xC3\xA9 \xC2\xC0", 4, Some(1));
559559
test!(b"A\xC3\xA9 \xE0", 4, None);
560-
test!(b"A\xC3\xA9 \xE0\x9F", 4, Some(5));
560+
test!(b"A\xC3\xA9 \xE0\x9F", 4, Some(1));
561561
test!(b"A\xC3\xA9 \xE0\xA0", 4, None);
562-
test!(b"A\xC3\xA9 \xE0\xA0\xC0", 4, Some(6));
563-
test!(b"A\xC3\xA9 \xE0\xA0 ", 4, Some(6));
564-
test!(b"A\xC3\xA9 \xED\xA0\x80 ", 4, Some(5));
562+
test!(b"A\xC3\xA9 \xE0\xA0\xC0", 4, Some(2));
563+
test!(b"A\xC3\xA9 \xE0\xA0 ", 4, Some(2));
564+
test!(b"A\xC3\xA9 \xED\xA0\x80 ", 4, Some(1));
565565
test!(b"A\xC3\xA9 \xF1", 4, None);
566566
test!(b"A\xC3\xA9 \xF1\x80", 4, None);
567567
test!(b"A\xC3\xA9 \xF1\x80\x80", 4, None);
568-
test!(b"A\xC3\xA9 \xF1 ", 4, Some(5));
569-
test!(b"A\xC3\xA9 \xF1\x80 ", 4, Some(6));
570-
test!(b"A\xC3\xA9 \xF1\x80\x80 ", 4, Some(7));
568+
test!(b"A\xC3\xA9 \xF1 ", 4, Some(1));
569+
test!(b"A\xC3\xA9 \xF1\x80 ", 4, Some(2));
570+
test!(b"A\xC3\xA9 \xF1\x80\x80 ", 4, Some(3));
571571
}
572572

573573
#[test]

src/libcore/str/mod.rs

+12-10
Original file line numberDiff line numberDiff line change
@@ -125,7 +125,7 @@ Section: Creating a string
125125
#[stable(feature = "rust1", since = "1.0.0")]
126126
pub struct Utf8Error {
127127
valid_up_to: usize,
128-
invalid_length: Option<u8>,
128+
error_len: Option<u8>,
129129
}
130130

131131
impl Utf8Error {
@@ -161,12 +161,14 @@ impl Utf8Error {
161161
/// If a byte stream (such as a file or a network socket) is being decoded incrementally,
162162
/// this could be a valid `char` whose UTF-8 byte sequence is spanning multiple chunks.
163163
///
164-
/// * `Some(index)`: an unexpected byte was encountered.
165-
/// The index provided is where decoding should resume
164+
/// * `Some(len)`: an unexpected byte was encountered.
165+
/// The length provided is that of the invalid byte sequence
166+
/// that starts at the index given by `valid_up_to()`.
167+
/// Decoding should resume after that sequence
166168
/// (after inserting a U+FFFD REPLACEMENT CHARACTER) in case of lossy decoding.
167-
#[unstable(feature = "utf8_error_resume_from", reason ="new", issue = "0")]
168-
pub fn resume_from(&self) -> Option<usize> {
169-
self.invalid_length.map(|l| self.valid_up_to + l as usize)
169+
#[unstable(feature = "utf8_error_error_len", reason ="new", issue = "0")]
170+
pub fn error_len(&self) -> Option<usize> {
171+
self.error_len.map(|len| len as usize)
170172
}
171173
}
172174

@@ -316,9 +318,9 @@ pub unsafe fn from_utf8_unchecked(v: &[u8]) -> &str {
316318
#[stable(feature = "rust1", since = "1.0.0")]
317319
impl fmt::Display for Utf8Error {
318320
fn fmt(&self, f: &mut fmt::Formatter) -> fmt::Result {
319-
if let Some(invalid_length) = self.invalid_length {
321+
if let Some(error_len) = self.error_len {
320322
write!(f, "invalid utf-8 sequence of {} bytes from index {}",
321-
invalid_length, self.valid_up_to)
323+
error_len, self.valid_up_to)
322324
} else {
323325
write!(f, "incomplete utf-8 byte sequence from index {}", self.valid_up_to)
324326
}
@@ -1263,10 +1265,10 @@ fn run_utf8_validation(v: &[u8]) -> Result<(), Utf8Error> {
12631265
while index < len {
12641266
let old_offset = index;
12651267
macro_rules! err {
1266-
($invalid_length: expr) => {
1268+
($error_len: expr) => {
12671269
return Err(Utf8Error {
12681270
valid_up_to: old_offset,
1269-
invalid_length: $invalid_length,
1271+
error_len: $error_len,
12701272
})
12711273
}
12721274
}

0 commit comments

Comments
 (0)