]>
git.proxmox.com Git - rustc.git/blob - vendor/windows-sys-0.42.0/src/core/literals.rs
1 /// A literal UTF-8 string with a trailing null terminator.
5 ::core
::concat
!($s
, '
\0'
).as_ptr()
9 /// A literal UTF-16 wide string with a trailing null terminator.
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
) {
21 if code_point
<= 0xffff {
22 buffer
[output_pos
] = code_point
as u16;
25 code_point
-= 0x10000;
26 buffer
[output_pos
] = 0xd800 + (code_point
>> 10) as u16;
28 buffer
[output_pos
] = 0xdc00 + (code_point
& 0x3ff) as u16;
38 // Ensures that the macros are exported from the `windows::core` module.
43 pub const fn decode_utf8_char(bytes
: &[u8], mut pos
: usize) -> Option
<(u32, usize)> {
44 if bytes
.len() == pos
{
47 let ch
= bytes
[pos
] as u32;
50 return Some((ch
, pos
));
52 if (ch
& 0xe0) == 0xc0 {
53 if bytes
.len() - pos
< 1 {
56 let ch2
= bytes
[pos
] as u32;
58 if (ch2
& 0xc0) != 0x80 {
61 let result
: u32 = ((ch
& 0x1f) << 6) | (ch2
& 0x3f);
65 return Some((result
, pos
));
67 if (ch
& 0xf0) == 0xe0 {
68 if bytes
.len() - pos
< 2 {
71 let ch2
= bytes
[pos
] as u32;
73 let ch3
= bytes
[pos
] as u32;
75 if (ch2
& 0xc0) != 0x80 || (ch3
& 0xc0) != 0x80 {
78 let result
= ((ch
& 0x0f) << 12) | ((ch2
& 0x3f) << 6) | (ch3
& 0x3f);
79 if result
<= 0x7ff || (0xd800 <= result
&& result
<= 0xdfff) {
82 return Some((result
, pos
));
84 if (ch
& 0xf8) == 0xf0 {
85 if bytes
.len() - pos
< 3 {
88 let ch2
= bytes
[pos
] as u32;
90 let ch3
= bytes
[pos
] as u32;
92 let ch4
= bytes
[pos
] as u32;
94 if (ch2
& 0xc0) != 0x80 || (ch3
& 0xc0) != 0x80 || (ch4
& 0xc0) != 0x80 {
97 let result
= ((ch
& 0x07) << 18) | ((ch2
& 0x3f) << 12) | ((ch3
& 0x3f) << 6) | (ch4
& 0x3f);
98 if result
<= 0xffff || 0x10ffff < result
{
101 return Some((result
, pos
));
107 pub const fn utf16_len(bytes
: &[u8]) -> usize {
110 while let Some((code_point
, new_pos
)) = decode_utf8_char(bytes
, pos
) {
112 len
+= if code_point
<= 0xffff { 1 }
else { 2 }
;