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

Commit

Permalink
Merge pull request #78 from dtolnay/hexchar
Browse files Browse the repository at this point in the history
Parse unicode character literals
  • Loading branch information
dtolnay authored Mar 27, 2022
2 parents 9b5ae9d + dbf1be1 commit e48dda7
Show file tree
Hide file tree
Showing 2 changed files with 14 additions and 0 deletions.
11 changes: 11 additions & 0 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -155,6 +155,7 @@ use crate::attr::expand_attr;
use crate::error::{Error, Result};
use crate::segment::Segment;
use proc_macro::{Delimiter, Group, Ident, Punct, Spacing, Span, TokenStream, TokenTree};
use std::char;
use std::iter;
use std::panic;

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

for segment in &mut segments {
if let Segment::String(string) = segment {
if string.value.starts_with("'\\u{") {
let hex = &string.value[4..string.value.len() - 2];
if let Ok(unsigned) = u32::from_str_radix(hex, 16) {
if let Some(ch) = char::from_u32(unsigned) {
string.value.clear();
string.value.push(ch);
continue;
}
}
}
if string.value.contains(&['#', '\\', '.', '+'][..])
|| string.value.starts_with("b'")
|| string.value.starts_with("b\"")
Expand Down
3 changes: 3 additions & 0 deletions tests/test_expr.rs
Original file line number Diff line number Diff line change
Expand Up @@ -40,6 +40,9 @@ fn test_literals() {

let pasted = paste!([<CONST r"0">]);
assert_eq!(pasted, CONST0);

let pasted = paste!([<CONST '\u{30}'>]);
assert_eq!(pasted, CONST0);
}

#[test]
Expand Down

0 comments on commit e48dda7

Please sign in to comment.