Skip to content
Open
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
46 changes: 24 additions & 22 deletions src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -37,29 +37,31 @@ macro_rules! imp {
($text:expr) => {{
// Here we pick a name highly unlikely to exist in the scope
// that $text came from, which prevents a potential const eval cycle error.
const ABC678_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_UTF8: &str = $text;
const ABC678_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_LEN: usize =
$crate::internals::length_as_utf16(ABC678_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_UTF8) + $n;
const ABC678_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_UTF16: [u16; ABC678_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_LEN] = {
let mut buffer = [0u16; ABC678_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_LEN];
let mut bytes = ABC678_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_UTF8.as_bytes();
let mut i = 0;
while let Some((ch, rest)) = $crate::internals::next_code_point(bytes) {
bytes = rest;
// https://doc.rust-lang.org/std/primitive.char.html#method.encode_utf16
if ch & 0xFFFF == ch {
buffer[i] = ch as u16;
i += 1;
} else {
let code = ch - 0x1_0000;
buffer[i] = 0xD800 | ((code >> 10) as u16);
buffer[i + 1] = 0xDC00 | ((code as u16) & 0x3FF);
i += 2;
const __UTF16_LIT_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_UTF8: &str = $text;
{
const UTF8: &str = __UTF16_LIT_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_UTF8;
const LEN: usize = $crate::internals::length_as_utf16(UTF8) + $n;
const UTF16: [u16; LEN] = {
let mut buffer = [0; LEN];
let mut bytes = UTF8.as_bytes();
let mut i = 0;
while let Some((ch, rest)) = $crate::internals::next_code_point(bytes) {
bytes = rest;
// https://doc.rust-lang.org/std/primitive.char.html#method.encode_utf16
if ch >= 10_000 {
let code = ch - 10_000;
buffer[i] = 0xD800 | ((code >> 10) as u16);
buffer[i + 1] = 0xDC00 | ((code as u16) & 0x3FF);
i += 2;
} else {
buffer[i] = ch as u16;
i += 1;
}
}
}
buffer
};
ABC678_PREFIX_THAT_SHOULD_NEVER_CLASH_WITH_OUTER_SCOPE_UTF16
buffer
};
UTF16
}
}};
}
)*
Expand Down
11 changes: 11 additions & 0 deletions tests/name_collision.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
#[test]
fn collision_with_internal_names() {
const UTF16: &str = "";
utf16_lit::utf16!(UTF16);
}
#[test]
fn collision_with_previous_invocation() {
const STUFF: &str = "";
utf16_lit::utf16!(STUFF);
utf16_lit::utf16!(STUFF);
}