| 1 |
/// A literal UTF-8 string with a trailing null terminator. |
| 2 |
#[macro_export] |
| 3 |
macro_rules! s { |
| 4 |
($s:literal) => { |
| 5 |
::core::concat!($s, '\0').as_ptr() |
| 6 |
}; |
| 7 |
} |
| 8 |
|
| 9 |
/// A literal UTF-16 wide string with a trailing null terminator. |
| 10 |
#[macro_export] |
| 11 |
macro_rules! w { |
| 12 |
($s:literal) => {{ |
| 13 |
const INPUT: &[u8] = $s.as_bytes(); |
| 14 |
const OUTPUT_LEN: usize = $crate::core::utf16_len(INPUT) + 1; |
| 15 |
const OUTPUT: &[u16; OUTPUT_LEN] = { |
| 16 |
let mut buffer = [0; OUTPUT_LEN]; |
| 17 |
let mut input_pos = 0; |
| 18 |
let mut output_pos = 0; |
| 19 |
while let Some((mut code_point, new_pos)) = $crate::core::decode_utf8_char(INPUT, input_pos) { |
| 20 |
input_pos = new_pos; |
| 21 |
if code_point <= 0xffff { |
| 22 |
buffer[output_pos] = code_point as u16; |
| 23 |
output_pos += 1; |
| 24 |
} else { |
| 25 |
code_point -= 0x10000; |
| 26 |
buffer[output_pos] = 0xd800 + (code_point >> 10) as u16; |
| 27 |
output_pos += 1; |
| 28 |
buffer[output_pos] = 0xdc00 + (code_point & 0x3ff) as u16; |
| 29 |
output_pos += 1; |
| 30 |
} |
| 31 |
} |
| 32 |
&{ buffer } |
| 33 |
}; |
| 34 |
OUTPUT.as_ptr() |
| 35 |
}}; |
| 36 |
} |
| 37 |
|
| 38 |
pub use s; |
| 39 |
pub use w; |
| 40 |
|
| 41 |
#[doc(hidden)] |
| 42 |
pub const fn decode_utf8_char(bytes: &[u8], mut pos: usize) -> Option<(u32, usize)> { |
| 43 |
if bytes.len() == pos { |
| 44 |
return None; |
| 45 |
} |
| 46 |
let ch = bytes[pos] as u32; |
| 47 |
pos += 1; |
| 48 |
if ch <= 0x7f { |
| 49 |
return Some((ch, pos)); |
| 50 |
} |
| 51 |
if (ch & 0xe0) == 0xc0 { |
| 52 |
if bytes.len() - pos < 1 { |
| 53 |
return None; |
| 54 |
} |
| 55 |
let ch2 = bytes[pos] as u32; |
| 56 |
pos += 1; |
| 57 |
if (ch2 & 0xc0) != 0x80 { |
| 58 |
return None; |
| 59 |
} |
| 60 |
let result: u32 = ((ch & 0x1f) << 6) | (ch2 & 0x3f); |
| 61 |
if result <= 0x7f { |
| 62 |
return None; |
| 63 |
} |
| 64 |
return Some((result, pos)); |
| 65 |
} |
| 66 |
if (ch & 0xf0) == 0xe0 { |
| 67 |
if bytes.len() - pos < 2 { |
| 68 |
return None; |
| 69 |
} |
| 70 |
let ch2 = bytes[pos] as u32; |
| 71 |
pos += 1; |
| 72 |
let ch3 = bytes[pos] as u32; |
| 73 |
pos += 1; |
| 74 |
if (ch2 & 0xc0) != 0x80 || (ch3 & 0xc0) != 0x80 { |
| 75 |
return None; |
| 76 |
} |
| 77 |
let result = ((ch & 0x0f) << 12) | ((ch2 & 0x3f) << 6) | (ch3 & 0x3f); |
| 78 |
if result <= 0x7ff || (0xd800 <= result && result <= 0xdfff) { |
| 79 |
return None; |
| 80 |
} |
| 81 |
return Some((result, pos)); |
| 82 |
} |
| 83 |
if (ch & 0xf8) == 0xf0 { |
| 84 |
if bytes.len() - pos < 3 { |
| 85 |
return None; |
| 86 |
} |
| 87 |
let ch2 = bytes[pos] as u32; |
| 88 |
pos += 1; |
| 89 |
let ch3 = bytes[pos] as u32; |
| 90 |
pos += 1; |
| 91 |
let ch4 = bytes[pos] as u32; |
| 92 |
pos += 1; |
| 93 |
if (ch2 & 0xc0) != 0x80 || (ch3 & 0xc0) != 0x80 || (ch4 & 0xc0) != 0x80 { |
| 94 |
return None; |
| 95 |
} |
| 96 |
let result = ((ch & 0x07) << 18) | ((ch2 & 0x3f) << 12) | ((ch3 & 0x3f) << 6) | (ch4 & 0x3f); |
| 97 |
if result <= 0xffff || 0x10ffff < result { |
| 98 |
return None; |
| 99 |
} |
| 100 |
return Some((result, pos)); |
| 101 |
} |
| 102 |
None |
| 103 |
} |
| 104 |
|
| 105 |
#[doc(hidden)] |
| 106 |
pub const fn utf16_len(bytes: &[u8]) -> usize { |
| 107 |
let mut pos = 0; |
| 108 |
let mut len = 0; |
| 109 |
while let Some((code_point, new_pos)) = decode_utf8_char(bytes, pos) { |
| 110 |
pos = new_pos; |
| 111 |
len += if code_point <= 0xffff { 1 } else { 2 }; |
| 112 |
} |
| 113 |
len |
| 114 |
} |