Skip to content

Commit a780aff

Browse files
committed
add test
1 parent b8dc822 commit a780aff

File tree

2 files changed

+28
-14
lines changed

2 files changed

+28
-14
lines changed

src/parse.rs

Lines changed: 9 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -605,9 +605,9 @@ impl<'a> Bytes<'a> {
605605
}
606606

607607
pub fn identifier(&mut self) -> Result<&'a str> {
608-
let next = self.peek_char_or_eof()?;
609-
if !is_ident_first_char(next) {
610-
if is_ident_raw_char(next) {
608+
let first = self.peek_char_or_eof()?;
609+
if !is_ident_first_char(first) {
610+
if is_ident_raw_char(first) {
611611
let ident_bytes = self.next_chars_while(is_ident_raw_char);
612612
return Err(Error::SuggestRawIdentifier(
613613
self.string[..ident_bytes].into(),
@@ -619,7 +619,7 @@ impl<'a> Bytes<'a> {
619619

620620
// If the next two bytes signify the start of a raw string literal,
621621
// return an error.
622-
let length = if next == 'r' {
622+
let length = if first == 'r' {
623623
match self.bytes().get(1).ok_or(Error::Eof)? {
624624
b'"' => return Err(Error::ExpectedIdentifier),
625625
b'#' => {
@@ -647,10 +647,8 @@ impl<'a> Bytes<'a> {
647647
}
648648
}
649649
} else {
650-
let std_ident_length = 1 + self.string
651-
[self.string.chars().next().unwrap_or_default().len_utf8()..]
652-
.find(|c| !is_xid_continue(c))
653-
.unwrap_or(self.string.len() - 1);
650+
let std_ident_length =
651+
first.len_utf8() + self.next_chars_while_from(first.len_utf8(), is_xid_continue);
654652
let raw_ident_length = self.next_chars_while(is_ident_raw_char);
655653

656654
if raw_ident_length > std_ident_length {
@@ -687,7 +685,7 @@ impl<'a> Bytes<'a> {
687685
pub fn next_chars_while_from(&self, from: usize, condition: fn(char) -> bool) -> usize {
688686
self.string[from..]
689687
.find(|c| !condition(c))
690-
.unwrap_or(self.string.len())
688+
.unwrap_or(self.string.len() - from)
691689
}
692690

693691
pub fn next_bytes_is_float(&self) -> bool {
@@ -721,10 +719,7 @@ impl<'a> Bytes<'a> {
721719
}
722720

723721
pub fn peek_byte_or_eof(&self) -> Result<u8> {
724-
self.bytes()
725-
.first()
726-
.copied()
727-
.ok_or(Error::Eof)
722+
self.bytes().first().copied().ok_or(Error::Eof)
728723
}
729724

730725
pub fn peek_char_or_eof(&self) -> Result<char> {
@@ -879,7 +874,7 @@ impl<'a> Bytes<'a> {
879874
if byte == b'}' {
880875
break;
881876
} else {
882-
num_digits += self.advance_char()?;
877+
num_digits += self.advance_char()?;
883878
}
884879

885880
let byte = self.decode_hex(byte)?;

tests/321_unicode_ident.rs

Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
use serde::{Deserialize, Serialize};
2+
3+
#[derive(Debug, Deserialize, PartialEq, Serialize)]
4+
enum EnumWithUnicode {
5+
Äöß,
6+
你好世界,
7+
}
8+
9+
#[test]
10+
fn roundtrip_unicode_ident() {
11+
let value = [EnumWithUnicode::Äöß, EnumWithUnicode::你好世界];
12+
let serial = ron::ser::to_string(&value).unwrap();
13+
14+
println!("Serialized: {}", serial);
15+
16+
let deserial = ron::de::from_str(&serial);
17+
18+
assert_eq!(Ok(value), deserial);
19+
}

0 commit comments

Comments
 (0)