Skip to content
This repository was archived by the owner on Oct 6, 2024. It is now read-only.

Commit e48dda7

Browse files
authored
Merge pull request #78 from dtolnay/hexchar
Parse unicode character literals
2 parents 9b5ae9d + dbf1be1 commit e48dda7

File tree

2 files changed

+14
-0
lines changed

2 files changed

+14
-0
lines changed

src/lib.rs

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -155,6 +155,7 @@ use crate::attr::expand_attr;
155155
use crate::error::{Error, Result};
156156
use crate::segment::Segment;
157157
use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
158+
use std::char;
158159
use std::iter;
159160
use std::panic;
160161

@@ -364,6 +365,16 @@ fn parse_bracket_as_segments(input: TokenStream, scope: Span) -> Result<Vec<Segm
364365

365366
for segment in &mut segments {
366367
if let Segment::String(string) = segment {
368+
if string.value.starts_with("'\\u{") {
369+
let hex = &string.value[4..string.value.len() - 2];
370+
if let Ok(unsigned) = u32::from_str_radix(hex, 16) {
371+
if let Some(ch) = char::from_u32(unsigned) {
372+
string.value.clear();
373+
string.value.push(ch);
374+
continue;
375+
}
376+
}
377+
}
367378
if string.value.contains(&['#', '\\', '.', '+'][..])
368379
|| string.value.starts_with("b'")
369380
|| string.value.starts_with("b\"")

tests/test_expr.rs

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -40,6 +40,9 @@ fn test_literals() {
4040

4141
let pasted = paste!([<CONST r"0">]);
4242
assert_eq!(pasted, CONST0);
43+
44+
let pasted = paste!([<CONST '\u{30}'>]);
45+
assert_eq!(pasted, CONST0);
4346
}
4447

4548
#[test]

0 commit comments

Comments
 (0)