1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
extern crate alloc;
type String = alloc::string::String;
use core::mem::size_of;
#[macro_export]
macro_rules! unicode_string {
($str:expr) => {
$crate::types::UnicodeString {
len: ($str.len() * 2) as _,
maximum_len: ($str.len() * 2) as _,
buffer: $crate::wide!($str).as_ptr() as _,
}
};
}
#[repr(C)]
pub struct UnicodeString {
pub len: u16,
pub maximum_len: u16,
pub buffer: *mut u16,
}
impl UnicodeString {
#[inline]
pub unsafe fn decode_utf16_unchecked(&self) -> String {
let utf16 = core::slice::from_raw_parts(self.buffer, self.len as usize / size_of::<u16>());
alloc::string::String::from_utf16_lossy(utf16)
}
#[inline]
pub unsafe fn decode_utf16(&self) -> Option<String> {
if self.is_null() {
None
} else {
Some(self.decode_utf16_unchecked())
}
}
#[inline]
pub fn is_null(&self) -> bool {
self.len == 0 || self.maximum_len == 0 || self.buffer.is_null()
}
}
impl PartialEq for UnicodeString {
fn eq(&self, other: &Self) -> bool {
if self.is_null() || other.is_null() || self.len != other.len {
false
} else {
unsafe { libc::memcmp(self.buffer as _, other.buffer as _, self.len as _) == 0 }
}
}
}