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
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
use {
log::trace, rust_icu_common as common, rust_icu_sys as sys, rust_icu_sys::*,
std::convert::TryFrom, std::os::raw,
};
#[derive(Debug)]
pub struct UChar {
rep: Vec<rust_icu_sys::UChar>,
}
impl TryFrom<&str> for crate::UChar {
type Error = common::Error;
fn try_from(rust_string: &str) -> Result<Self, Self::Error> {
let mut status = common::Error::OK_CODE;
let mut dest_length: i32 = 0;
trace!("utf8->UChar*: {}, {:?}", rust_string.len(), rust_string);
unsafe {
assert!(common::Error::is_ok(status));
versioned_function!(u_strFromUTF8)(
0 as *mut sys::UChar,
0,
&mut dest_length,
rust_string.as_ptr() as *const raw::c_char,
rust_string.len() as i32,
&mut status,
);
}
trace!("before error check");
common::Error::ok_preflight(status)?;
trace!("input utf8->UChar*: {:?}", rust_string);
let mut rep: Vec<sys::UChar> = vec![0; dest_length as usize];
let mut status = common::Error::OK_CODE;
unsafe {
assert!(common::Error::is_ok(status));
versioned_function!(u_strFromUTF8)(
rep.as_mut_ptr(),
rep.len() as i32,
&mut dest_length,
rust_string.as_ptr() as *const raw::c_char,
rust_string.len() as i32,
&mut status,
);
}
common::Error::ok_or_warning(status)?;
trace!("result utf8->uchar*[{}]: {:?}", dest_length, rep);
Ok(crate::UChar { rep })
}
}
impl TryFrom<&UChar> for String {
type Error = common::Error;
fn try_from(u: &UChar) -> Result<String, Self::Error> {
let mut status = common::Error::OK_CODE;
let mut dest_length: i32 = 0;
unsafe {
assert!(common::Error::is_ok(status));
versioned_function!(u_strToUTF8)(
0 as *mut raw::c_char,
0,
&mut dest_length,
u.rep.as_ptr(),
u.rep.len() as i32,
&mut status,
);
}
trace!("preflight UChar*->utf8 buf[{}]", dest_length);
common::Error::ok_preflight(status)?;
let mut buf: Vec<u8> = vec![0; dest_length as usize];
trace!("pre: result UChar*->utf8 buf[{}]: {:?}", buf.len(), buf);
let mut status = common::Error::OK_CODE;
unsafe {
assert!(common::Error::is_ok(status));
versioned_function!(u_strToUTF8)(
buf.as_mut_ptr() as *mut raw::c_char,
buf.len() as i32,
&mut dest_length,
u.rep.as_ptr(),
u.rep.len() as i32,
&mut status,
);
}
trace!("post: result UChar*->utf8 buf[{}]: {:?}", buf.len(), buf);
common::Error::ok_or_warning(status)?;
let s = String::from_utf8(buf);
match s {
Err(e) => Err(e.into()),
Ok(x) => {
trace!("result UChar*->utf8: {:?}", x);
Ok(x)
}
}
}
}
impl crate::UChar {
pub fn new_with_capacity(capacity: usize) -> crate::UChar {
let rep: Vec<sys::UChar> = vec![0; capacity];
crate::UChar { rep: rep }
}
pub fn make_z(&mut self) {
self.rep.push(0);
}
pub fn as_c_ptr(&self) -> *const rust_icu_sys::UChar {
self.rep.as_ptr()
}
pub fn len(&self) -> usize {
self.rep.len()
}
pub fn as_mut_c_ptr(&mut self) -> *mut sys::UChar {
self.rep.as_mut_ptr()
}
pub fn resize(&mut self, new_size: usize) {
self.rep.resize(new_size, 0);
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn round_trip_conversion() {
let samples = vec!["", "Hello world!", "❤ Hello world ❤"];
for s in samples.iter() {
let uchar =
crate::UChar::try_from(*s).expect(&format!("forward conversion succeeds: {}", s));
let res =
String::try_from(&uchar).expect(&format!("back conversion succeeds: {:?}", uchar));
assert_eq!(*s, res);
}
}
}