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
68
69
70
71
pub(crate) mod mem {
#[inline]
pub(crate) fn create_byte_buffer(length: usize) -> Vec<u8> {
create_sized_buffer::<u8>(length)
}
#[inline]
#[allow(clippy::uninit_vec)]
pub(crate) fn create_sized_buffer<T>(capacity: usize) -> Vec<T> {
let mut buffer = Vec::<T>::with_capacity(capacity);
unsafe {
buffer.set_len(capacity);
}
buffer
}
}
pub(crate) mod utf16le {
use utf16string::{LittleEndian, WString};
#[inline]
pub(crate) fn get_pdfium_utf16le_bytes_from_str(str: &str) -> Vec<u8> {
WString::<LittleEndian>::from(str).into_bytes()
}
#[allow(unused_mut)]
pub(crate) fn get_string_from_pdfium_utf16le_bytes(mut buffer: Vec<u8>) -> Option<String> {
#[cfg(target_arch = "wasm32")]
{
use web_sys::TextDecoder;
if let Ok(decoder) = TextDecoder::new_with_label("utf-16le") {
if let Ok(result) = decoder.decode_with_u8_array(&mut buffer) {
let result = result.trim_end_matches(char::from(0));
if !result.is_empty() {
return Some(result.to_owned());
} else {
return None;
}
}
}
}
if let Ok(string) = WString::<LittleEndian>::from_utf16(buffer) {
let result = string.to_utf8().trim_end_matches(char::from(0)).to_owned();
if !result.is_empty() {
Some(result)
} else {
None
}
} else {
None
}
}
}