forked from servo/servo
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathstring_multimap.rs
138 lines (125 loc) · 4.6 KB
/
string_multimap.rs
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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
/* This Source Code Form is subject to the terms of the Mozilla Public
* License, v. 2.0. If a copy of the MPL was not distributed with this
* file, You can obtain one at http://mozilla.org/MPL/2.0/. */
use eutil::slice_to_str;
use libc::c_int;
use std::collections::BTreeMap;
use string::{cef_string_userfree_utf16_alloc, cef_string_userfree_utf16_free};
use string::{cef_string_utf16_set};
use types::{cef_string_multimap_t,cef_string_t};
//cef_string_multimap
#[no_mangle]
pub extern "C" fn cef_string_multimap_alloc() -> *mut cef_string_multimap_t {
Box::into_raw(box BTreeMap::new())
}
#[no_mangle]
pub extern "C" fn cef_string_multimap_size(smm: *mut cef_string_multimap_t) -> c_int {
unsafe {
if smm.is_null() { return 0; }
// t1 : collections::btree::map::Values<'_, collections::string::String, collections::vec::Vec<*mut types::cef_string_utf16>>`
let t1 = (*smm).values();
// t2 : collections::btree::map::BTreeMap<collections::string::String, collections::vec::Vec<*mut types::cef_string_utf16>>
let t2 : usize = t1.map(|val| (*val).len()).sum();
t2 as c_int
}
}
#[no_mangle]
pub extern "C" fn cef_string_multimap_find_count(smm: *mut cef_string_multimap_t, key: *const cef_string_t) -> c_int {
unsafe {
if smm.is_null() { return 0; }
slice_to_str((*key).str as *const u8, (*key).length as usize, |result| {
match (*smm).get(result) {
Some(s) => s.len() as c_int,
None => 0
}
})
}
}
#[no_mangle]
pub extern "C" fn cef_string_multimap_append(smm: *mut cef_string_multimap_t, key: *const cef_string_t, value: *const cef_string_t) -> c_int {
unsafe {
if smm.is_null() { return 0; }
slice_to_str((*key).str as *const u8, (*key).length as usize, |result| {
let csv = cef_string_userfree_utf16_alloc();
cef_string_utf16_set((*value).str as *const u16, (*value).length, csv, 1);
match (*smm).get_mut(result) {
Some(vc) => (*vc).push(csv),
None => { (*smm).insert(result.to_owned(), vec!(csv)); }
}
1
})
}
}
#[no_mangle]
pub extern "C" fn cef_string_multimap_enumerate(smm: *mut cef_string_multimap_t, key: *const cef_string_t, index: c_int, value: *mut cef_string_t) -> c_int {
unsafe {
if smm.is_null() { return 0; }
slice_to_str((*key).str as *const u8, (*key).length as usize, |result| {
match (*smm).get(result) {
Some(s) => {
if (*s).len() <= index as usize {
return 0;
}
let cs = (*s)[index as usize];
cef_string_utf16_set((*cs).str as *const u16, (*cs).length, value, 1)
}
None => 0
}
})
}
}
#[no_mangle]
pub extern "C" fn cef_string_multimap_key(smm: *mut cef_string_multimap_t, index: c_int, value: *mut cef_string_t) -> c_int {
unsafe {
if index < 0 || smm.is_null() { return 0; }
let mut rem = index as usize;
for (key, val) in &(*smm) {
if rem < (*val).len() {
return cef_string_utf16_set((*key).as_bytes().as_ptr() as *const u16,
(*key).len(),
value,
1);
} else {
rem -= (*val).len();
}
}
}
0
}
#[no_mangle]
pub extern "C" fn cef_string_multimap_value(smm: *mut cef_string_multimap_t, index: c_int, value: *mut cef_string_t) -> c_int {
unsafe {
if index < 0 || smm.is_null() { return 0; }
let mut rem = index as usize;
for val in (*smm).values() {
if rem < (*val).len() {
let cs = (*val)[rem as usize];
return cef_string_utf16_set((*cs).str as *const u16, (*cs).length, value, 1);
} else {
rem -= (*val).len();
}
}
}
0
}
#[no_mangle]
pub extern "C" fn cef_string_multimap_clear(smm: *mut cef_string_multimap_t) {
unsafe {
if smm.is_null() { return; }
if (*smm).is_empty() { return; }
for (_, val) in (*smm).iter_mut() {
while let Some(cs) = (*val).pop() {
cef_string_userfree_utf16_free(cs);
}
}
(*smm).clear();
}
}
#[no_mangle]
pub extern "C" fn cef_string_multimap_free(smm: *mut cef_string_multimap_t) {
unsafe {
if smm.is_null() { return; }
cef_string_multimap_clear(smm);
drop(Box::from_raw(smm));
}
}