support unicode literal#1043
Conversation
d857e46 to
7f2d014
Compare
Codecov Report
@@ Coverage Diff @@
## master #1043 +/- ##
==========================================
- Coverage 65.17% 65.14% -0.04%
==========================================
Files 99 99
Lines 17946 17972 +26
Branches 3994 4004 +10
==========================================
+ Hits 11697 11708 +11
- Misses 3515 3524 +9
- Partials 2734 2740 +6
Continue to review full report at Codecov.
|
|
The error handler needs to check. |
|
|
||
| fn uncicode_literal(&mut self, literal_number: usize) -> Result<char, LexicalError> { | ||
| let location = self.get_pos(); | ||
| let mut it: Vec<u32> = Vec::new(); |
There was a problem hiding this comment.
I suggest to pick a better name for the variable it. Perhaps digits would be better?
There was a problem hiding this comment.
Thanks for the suggestion, I remove the it and just put digit into p.
| }, | ||
| Some('x') if !is_bytes => match self.uncicode_literal(2) { | ||
| Ok(c) => string_content.push(c), | ||
| Err(e) => return Err(e), |
There was a problem hiding this comment.
The pattern Err(e) => return Err(e) can be short circuit like this:
Some('x') if !is_bytes => string_content.push(self.unicode_literal(2)?),Notice the lack match statement for the explicit error handling.
There was a problem hiding this comment.
Thanks. I refactor this with ?.
| } | ||
| } | ||
| let mut p: u32 = 0u32; | ||
| for i in 1..=literal_number { |
There was a problem hiding this comment.
In this case, I suggest to use enumerate to iterate over the parsed digits and construct the numerical value.
for (i, d) in it.iter().enumerate() {
p += d << i*4;
}See also: https://doc.rust-lang.org/std/iter/trait.Iterator.html#method.enumerate
There was a problem hiding this comment.
Will the compiler optimize it out though?
There was a problem hiding this comment.
Anyway, we can even do something like this
let p = it.iter().enumerate().fold(|acc, (i, d)| acc + d << i *4)But only if this all line ends up being optimized to a loop
There was a problem hiding this comment.
The it is removed, but I learn the powerful fold method, I will try to use it next time. Thank you. @michelhe
|
@yanganto great job! I left some comments for improvements, but the change look good! |
75bc12e to
4c672aa
Compare
- support unicode literal \x with 2 digits - support unicode literal \u with 4 digits - support unicode literal \U with 8 digits - avoid to parse \x as unicode literal in bytes
4c672aa to
974dc68
Compare
support Unicode literal as same as CPython #1025
\xwith 2 digits\uwith 4 digits\Uwith 8 digits