Skip to content

Remove unnecessary unic dependencies and add \N{} unicode name escapes #1869

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

Merged
merged 4 commits into from
Apr 16, 2020
Merged
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
183 changes: 9 additions & 174 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 0 additions & 4 deletions Lib/test/test_json/test_unicode.py
Original file line number Diff line number Diff line change
Expand Up @@ -8,25 +8,21 @@ class TestUnicode:
# test_encoding1 and test_encoding2 from 2.x are irrelevant (only str
# is supported as input, not bytes).

@unittest.skip("TODO: RUSTPYTHON")
def test_encoding3(self):
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = self.dumps(u)
self.assertEqual(j, '"\\u03b1\\u03a9"')

@unittest.skip("TODO: RUSTPYTHON")
def test_encoding4(self):
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = self.dumps([u])
self.assertEqual(j, '["\\u03b1\\u03a9"]')

@unittest.skip("TODO: RUSTPYTHON")
def test_encoding5(self):
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = self.dumps(u, ensure_ascii=False)
self.assertEqual(j, '"{0}"'.format(u))

@unittest.skip("TODO: RUSTPYTHON")
def test_encoding6(self):
u = '\N{GREEK SMALL LETTER ALPHA}\N{GREEK CAPITAL LETTER OMEGA}'
j = self.dumps([u], ensure_ascii=False)
Expand Down
3 changes: 2 additions & 1 deletion parser/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -17,4 +17,5 @@ log="0.4.1"
num-bigint = "0.2"
num-traits = "0.2"
unic-emoji-char = "0.9"
unic-ucd-ident = "0.9"
unic-ucd-ident = "0.9"
unicode_names2 = "0.4"
56 changes: 53 additions & 3 deletions parser/src/lexer.rs
Original file line number Diff line number Diff line change
Expand Up @@ -475,6 +475,37 @@ where
u8::from_str_radix(&octet_content, 8).unwrap() as char
}

fn parse_unicode_name(&mut self) -> Result<char, LexicalError> {
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Maybe add a unit test for this?

let start_pos = self.get_pos();
match self.next_char() {
Some('{') => {}
_ => {
return Err(LexicalError {
error: LexicalErrorType::StringError,
location: start_pos,
})
}
}
let start_pos = self.get_pos();
let mut name = String::new();
loop {
match self.next_char() {
Some('}') => break,
Some(c) => name.push(c),
None => {
return Err(LexicalError {
error: LexicalErrorType::StringError,
location: self.get_pos(),
})
}
}
}
unicode_names2::character(&name).ok_or(LexicalError {
error: LexicalErrorType::UnicodeError,
location: start_pos,
})
}

fn lex_string(
&mut self,
is_bytes: bool,
Expand Down Expand Up @@ -532,11 +563,14 @@ where
Some('t') => {
string_content.push('\t');
}
Some('u') => string_content.push(self.unicode_literal(4)?),
Some('U') => string_content.push(self.unicode_literal(8)?),
Some('x') => string_content.push(self.unicode_literal(2)?),
Some('v') => string_content.push('\x0b'),
Some(o @ '0'..='7') => string_content.push(self.parse_octet(o)),
Some('x') => string_content.push(self.unicode_literal(2)?),
Some('u') if !is_bytes => string_content.push(self.unicode_literal(4)?),
Some('U') if !is_bytes => string_content.push(self.unicode_literal(8)?),
Some('N') if !is_bytes => {
string_content.push(self.parse_unicode_name()?)
}
Some(c) => {
string_content.push('\\');
string_content.push(c);
Expand Down Expand Up @@ -1687,4 +1721,20 @@ mod tests {
]
)
}

#[test]
fn test_escape_unicode_name() {
let source = r#""\N{EN SPACE}""#;
let tokens = lex_source(source);
assert_eq!(
tokens,
vec![
Tok::String {
value: "\u{2002}".to_owned(),
is_fstring: false,
},
Tok::Newline
]
)
}
}
Loading