Skip to content

Make UnicodeEscapedChars use new unicode escape syntax. #19948

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
57 changes: 39 additions & 18 deletions src/libcore/char.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,7 +19,7 @@ use mem::transmute;
use ops::FnMut;
use option::Option;
use option::Option::{None, Some};
use iter::{range_step, Iterator, RangeStep};
use iter::Iterator;
use slice::SliceExt;

// UTF-8 ranges and tags for encoding characters
Expand Down Expand Up @@ -459,7 +459,9 @@ pub struct UnicodeEscapedChars {
enum UnicodeEscapedCharsState {
Backslash,
Type,
Value(RangeStep<i32>),
Open,
Value(i32),
Close,
}

impl Iterator<char> for UnicodeEscapedChars {
Expand All @@ -468,25 +470,44 @@ impl Iterator<char> for UnicodeEscapedChars {
UnicodeEscapedCharsState::Backslash => {
self.state = UnicodeEscapedCharsState::Type;
Some('\\')
}
},
UnicodeEscapedCharsState::Type => {
let (typechar, pad) = if self.c <= '\x7f' { ('x', 2) }
else if self.c <= '\u{ffff}' { ('u', 4) }
else { ('U', 8) };
self.state = UnicodeEscapedCharsState::Value(range_step(4 * (pad - 1), -1, -4i32));
Some(typechar)
}
UnicodeEscapedCharsState::Value(ref mut range_step) => match range_step.next() {
Some(offset) => {
let offset = offset as uint;
let v = match ((self.c as i32) >> offset) & 0xf {
i @ 0 ... 9 => '0' as i32 + i,
i => 'a' as i32 + (i - 10)
self.state = UnicodeEscapedCharsState::Open;
Some('u')
},
UnicodeEscapedCharsState::Open => {
let len = match self.c as u32 {
0x0...0xf => 1,
0x10...0xff => 2,
0x100...0xfff => 3,
0x1000...0xffff => 4,
0x10000...0xfffff => 5,
0x100000...0xffffff => 6,
0x1000000...0xfffffff => 7,
0x10000000...0xffffffff => 8,
_ => unreachable!(), // FIXME - Exhaustiveness check
};
self.state = UnicodeEscapedCharsState::Value(len);
Some('{')
},
UnicodeEscapedCharsState::Value(index) => match index {
0 => {
self.state = UnicodeEscapedCharsState::Close;
Some('}')
},
index => {
let offset = ((index - 1) * 4) as uint;
let v = match ((self.c as u32) >> offset) & 0xf {
i @ 0...9 => '0' as u32 + i,
i => 'a' as u32 + (i - 10),
};
self.state = UnicodeEscapedCharsState::Value(index - 1);
Some(unsafe { transmute(v) })
}
None => None
}
},
},
UnicodeEscapedCharsState::Close => {
None
},
}
}
}
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -11,7 +11,7 @@
// aux-build:macro_crate_test.rs
// ignore-stage1
// ignore-android
// error-pattern: unknown start of token: \x00
// error-pattern: unknown start of token: \u{0}

// Issue #15750 and #15962 : this test is checking that the standard
// parser rejects embedded idents. pnkfelix did not want to attempt
Expand Down