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

Parse unicode character literals #78

Merged
merged 2 commits into from
Mar 27, 2022
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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