Skip to content

Commit 5d697bc

Browse files
authored
Merge pull request #26 from eddyb/underscore-escaping
v0: allow identifiers to start with a digit.
2 parents f25ca9c + 9f43a30 commit 5d697bc

File tree

1 file changed

+16
-20
lines changed

1 file changed

+16
-20
lines changed

src/v0.rs

Lines changed: 16 additions & 20 deletions
Original file line numberDiff line numberDiff line change
@@ -164,7 +164,7 @@ impl<'s> Ident<'s> {
164164

165165
let d = match punycode_bytes.next() {
166166
Some(d @ b'a'...b'z') => d - b'a',
167-
Some(d @ b'A'...b'J') => 26 + (d - b'A'),
167+
Some(d @ b'0'...b'9') => 26 + (d - b'0'),
168168
_ => return Err(()),
169169
};
170170
let d = d as usize;
@@ -226,18 +226,12 @@ impl<'s> Display for Ident<'s> {
226226
try!(f.write_str("punycode{"));
227227

228228
// Reconstruct a standard Punycode encoding,
229-
// by using `-` as the separator and replacing
230-
// [A-J] with [0-9] (in the base36 codes).
229+
// by using `-` as the separator.
231230
if !self.ascii.is_empty() {
232231
try!(f.write_str(self.ascii));
233232
try!(f.write_str("-"));
234233
}
235-
for mut b in self.punycode.bytes() {
236-
if let b'A'...b'J' = b {
237-
b = b - b'A' + b'0';
238-
}
239-
try!((b as char).fmt(f));
240-
}
234+
try!(f.write_str(self.punycode));
241235

242236
f.write_str("}")
243237
} else {
@@ -396,6 +390,9 @@ impl<'s> Parser<'s> {
396390
}
397391
}
398392

393+
// Skip past the optional `_` separator.
394+
self.eat(b'_');
395+
399396
let start = self.next;
400397
self.next = try!(self.next.checked_add(len).ok_or(Invalid));
401398
if self.next > self.sym.len() {
@@ -688,16 +685,7 @@ impl<'a, 'b, 's> Printer<'a, 'b, 's> {
688685
let dis = parse!(self, disambiguator);
689686
let name = parse!(self, ident);
690687

691-
if name.punycode.is_empty() {
692-
// Unescape `_[0-9_]`.
693-
let mut name = name.ascii;
694-
if name.starts_with("_") {
695-
name = &name[1..];
696-
}
697-
try!(self.out.write_str(name));
698-
} else {
699-
try!(name.fmt(self.out));
700-
}
688+
try!(name.fmt(self.out));
701689
if !self.out.alternate() {
702690
try!(self.out.write_str("["));
703691
try!(fmt::LowerHex::fmt(&dis, self.out));
@@ -1015,10 +1003,18 @@ mod tests {
10151003
)
10161004
}
10171005

1006+
#[test]
1007+
fn demangle_crate_with_leading_digit() {
1008+
t_nohash!(
1009+
"_RNvC6_123foo3bar",
1010+
"123foo::bar"
1011+
);
1012+
}
1013+
10181014
#[test]
10191015
fn demangle_utf8_idents() {
10201016
t_nohash!(
1021-
"_RNqCs4fqI2P2rA04_11utf8_identsu30___HhkackfeceaBcbdathfdhJhlqGy",
1017+
"_RNqCs4fqI2P2rA04_11utf8_identsu30____7hkackfecea1cbdathfdh9hlq6y",
10221018
"utf8_idents::საჭმელად_გემრიელი_სადილი"
10231019
);
10241020
}

0 commit comments

Comments
 (0)