-
Notifications
You must be signed in to change notification settings - Fork 10
/
Copy pathlib.rs
286 lines (267 loc) · 8.89 KB
/
lib.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
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
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
/*******************************************************************************
* ___ _ ____ ____
* / _ \ _ _ ___ ___| |_| _ \| __ )
* | | | | | | |/ _ \/ __| __| | | | _ \
* | |_| | |_| | __/\__ \ |_| |_| | |_) |
* \__\_\\__,_|\___||___/\__|____/|____/
*
* Copyright (c) 2014-2019 Appsicle
* Copyright (c) 2019-2024 QuestDB
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*
******************************************************************************/
use std::ffi::c_char;
use std::slice::from_raw_parts;
#[allow(non_camel_case_types)]
pub struct qdb_pystr_buf(Vec<String>);
#[repr(C)]
#[allow(non_camel_case_types)]
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct qdb_pystr_pos {
pub chain: usize,
pub string: usize
}
/// Prepare a new buffer. The buffer must be freed with `qdb_pystr_free`.
/// The `qdb_ucsX_to_utf8` functions will write to this buffer.
#[no_mangle]
pub unsafe extern "C" fn qdb_pystr_buf_new() -> *mut qdb_pystr_buf {
Box::into_raw(Box::new(qdb_pystr_buf(Vec::new())))
}
/// Get current position. Use in conjunction with `truncate`.
#[no_mangle]
pub unsafe extern "C" fn qdb_pystr_buf_tell(
b: *const qdb_pystr_buf) -> qdb_pystr_pos {
let b = &*b;
let chain_pos = b.0.len();
let string_pos = if chain_pos > 0 {
b.0[chain_pos - 1].len()
} else {
0
};
qdb_pystr_pos { chain: chain_pos, string: string_pos }
}
/// Trim the buffer to the given position. Use in conjunction with `tell`.
#[no_mangle]
pub unsafe extern "C" fn qdb_pystr_buf_truncate(
b: *mut qdb_pystr_buf, pos: qdb_pystr_pos) {
let b = &mut *b;
b.0.truncate(pos.chain);
if !b.0.is_empty() {
b.0[pos.chain - 1].truncate(pos.string);
}
}
/// Reset the converter's buffer to zero length.
#[no_mangle]
pub unsafe extern "C" fn qdb_pystr_buf_clear(b: *mut qdb_pystr_buf) {
let b = &mut *b;
if !b.0.is_empty() {
b.0.truncate(1);
b.0[0].clear();
}
}
/// Free the buffer. Must be called after `qdb_pystr_buf_new`.
#[no_mangle]
pub unsafe extern "C" fn qdb_pystr_buf_free(b: *mut qdb_pystr_buf) {
if !b.is_null() {
drop(Box::from_raw(b));
}
}
const MIN_BUF_LEN: usize = 1024;
/// A carefully crafted buffer with spare capacity for `len` bytes.
/// This is necessary to return "stable" addresses and avoid segfaults.
/// Rust is unaware we are borrowing its memory and could try to free it as
/// part of a reallocation if we were to use a `String` directly.
fn get_dest(chain: &mut Vec<String>, len: usize) -> &mut String {
if !chain.is_empty() {
let last = chain.last_mut().unwrap();
if last.capacity() - last.len() >= len {
return chain.last_mut().unwrap();
}
}
chain.push(String::with_capacity(std::cmp::max(len, MIN_BUF_LEN)));
chain.last_mut().unwrap()
}
#[inline(always)]
fn encode_loop<'a, 'b, T, F>(
utf8_mult: usize,
chain: &'a mut Vec<String>,
buf: &'b [T],
get_char: F) -> Result<&'a str, u32>
where
F: Fn(T) -> Option<char>,
T: Copy + Into<u32>
{
let dest = get_dest(chain, utf8_mult * buf.len());
let last = dest.len();
// for &b in buf.iter() {
// // Checking for validity is not optional:
// // >>> for n in range(2 ** 16):
// // >>> chr(n).encode('utf-8')
// // UnicodeEncodeError: 'utf-8' codec can't encode character '\ud800'
// // in position 0: surrogates not allowed
// match get_char(b) {
// Some(c) => dest.push(c),
// None => {
// dest.truncate(last);
// return Err(b.into());
// }
// }
// }
// Ok(&dest[last..])
unsafe {
let v = dest.as_mut_vec();
v.set_len(v.capacity());
let mut index = last;
for &b in buf.iter() {
let c = match get_char(b) {
Some(c) => c,
None => {
v.set_len(last);
return Err(b.into())
}
};
let utf_c_len = c.len_utf8();
match utf_c_len {
1 => {
v[index] = c as u8;
},
2 => {
let mut codepoint_buf = [0; 4];
let bytes = c
.encode_utf8(&mut codepoint_buf).as_bytes();
*v.get_unchecked_mut(index) =
*bytes.get_unchecked(0);
*v.get_unchecked_mut(index + 1) =
*bytes.get_unchecked(1);
},
3 => {
let mut codepoint_buf = [0; 4];
let bytes = c
.encode_utf8(&mut codepoint_buf).as_bytes();
*v.get_unchecked_mut(index) =
*bytes.get_unchecked(0);
*v.get_unchecked_mut(index + 1) =
*bytes.get_unchecked(1);
*v.get_unchecked_mut(index + 2) =
*bytes.get_unchecked(2);
},
4 => {
let mut codepoint_buf = [0; 4];
let bytes = c
.encode_utf8(&mut codepoint_buf).as_bytes();
*v.get_unchecked_mut(index) =
*bytes.get_unchecked(0);
*v.get_unchecked_mut(index + 1) =
*bytes.get_unchecked(1);
*v.get_unchecked_mut(index + 2) =
*bytes.get_unchecked(2);
*v.get_unchecked_mut(index + 3) =
*bytes.get_unchecked(3);
},
_ => unreachable!()
}
index += utf_c_len;
}
v.set_len(index);
}
Ok(&dest[last..])
}
/// Convert a Py_UCS1 string to UTF-8.
/// Returns a `buf_out` borrowed ptr of `size_out` len.
/// The buffer is borrowed from `b`.
#[no_mangle]
pub unsafe extern "C" fn qdb_ucs1_to_utf8(
b: *mut qdb_pystr_buf,
count: usize, input: *const u8,
size_out: *mut usize, buf_out: *mut *const c_char) {
let b = &mut *b;
let i = from_raw_parts(input, count);
// len(chr(2 ** 8 - 1).encode('utf-8')) == 2
let utf8_mult = 2;
let res = encode_loop(
utf8_mult,
&mut b.0,
i,
|c| Some(c as char)).unwrap();
*size_out = res.len();
*buf_out = res.as_ptr() as *const c_char;
}
/// Convert a Py_UCS2 string to UTF-8.
/// Returns a `buf_out` borrowed ptr of `size_out` len.
/// The buffer is borrowed from `b`.
/// In case of errors, returns `false` and bad_codepoint_out is set to the
/// offending codepoint.
#[no_mangle]
pub unsafe extern "C" fn qdb_ucs2_to_utf8(b: *mut qdb_pystr_buf,
count: usize,
input: *const u16,
size_out: *mut usize,
buf_out: *mut *const c_char,
bad_codepoint_out: *mut u32) -> bool {
let b = &mut *b;
let i = from_raw_parts(input, count);
// len(chr(2 ** 16 - 1).encode('utf-8')) == 3
let utf8_mult = 3;
let res = encode_loop(
utf8_mult,
&mut b.0,
i,
|c| char::from_u32(c as u32));
match res {
Ok(s) => {
*size_out = s.len();
*buf_out = s.as_ptr() as *const c_char;
true
}
Err(bad) => {
*bad_codepoint_out = bad;
false
}
}
}
/// Convert a Py_UCS4 string to UTF-8.
/// Returns a `buf_out` borrowed ptr of `size_out` len.
/// The buffer is borrowed from `b`.
/// In case of errors, returns `false` and bad_codepoint_out is set to the
/// offending codepoint.
#[no_mangle]
pub unsafe extern "C" fn qdb_ucs4_to_utf8(b: *mut qdb_pystr_buf,
count: usize,
input: *const u32,
size_out: *mut usize,
buf_out: *mut *const c_char,
bad_codepoint_out: *mut u32) -> bool {
let b = &mut *b;
let i = from_raw_parts(input, count);
// Max 4 bytes allowed by RFC: https://www.rfc-editor.org/rfc/rfc3629#page-4
let utf8_mult = 4;
let res = encode_loop(
utf8_mult,
&mut b.0,
i,
|c| char::from_u32(c));
match res {
Ok(s) => {
*size_out = s.len();
*buf_out = s.as_ptr() as *const c_char;
true
}
Err(bad) => {
*bad_codepoint_out = bad;
false
}
}
}
#[cfg(test)]
mod tests;