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
#[cfg(feature = "compile-with-external-structures")]
pub(crate) mod external {
use crate::blobs::{Blob, HasBlob};
use std::ops::Deref;
#[repr(C)]
pub struct StringPtr {
pub(crate) blob: Blob<StringPtr>,
}
extern "C" {
fn lib_ruby_parser__external__string_ptr__new(ptr: *const u8, len: u64) -> Blob<StringPtr>;
fn lib_ruby_parser__external__string_ptr__drop(blob: *mut Blob<StringPtr>);
fn lib_ruby_parser__external__string_ptr__as_raw(
blob_ptr: *const Blob<StringPtr>,
) -> *const u8;
fn lib_ruby_parser__external__string_ptr__into_raw(blob_ptr: Blob<StringPtr>) -> *mut u8;
fn lib_ruby_parser__external__string_ptr__get_len(blob: *const Blob<StringPtr>) -> u64;
}
impl Drop for StringPtr {
fn drop(&mut self) {
unsafe { lib_ruby_parser__external__string_ptr__drop(&mut self.blob) }
}
}
impl std::fmt::Debug for StringPtr {
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
std::fmt::Debug::fmt(self.as_str(), f)
}
}
impl Clone for StringPtr {
fn clone(&self) -> Self {
let vec: &[u8] = self.into();
Self::from(vec.to_vec())
}
}
impl Default for StringPtr {
fn default() -> Self {
Self::from("")
}
}
impl StringPtr {
pub(crate) fn from_raw(ptr: *const u8, len: u64) -> Self {
let blob = unsafe { lib_ruby_parser__external__string_ptr__new(ptr, len) };
Self { blob }
}
pub fn to_uppercase(&self) -> Self {
Self::from(self.as_str().to_uppercase())
}
pub fn as_str(&self) -> &str {
let bytes = self.deref();
std::str::from_utf8(bytes).unwrap()
}
pub fn len(&self) -> usize {
unsafe { lib_ruby_parser__external__string_ptr__get_len(&self.blob) as usize }
}
pub fn is_empty(&self) -> bool {
self.len() == 0
}
pub(crate) fn as_ptr(&self) -> *const u8 {
unsafe { lib_ruby_parser__external__string_ptr__as_raw(&self.blob) }
}
pub(crate) fn into_ptr(self) -> *mut u8 {
unsafe { lib_ruby_parser__external__string_ptr__into_raw(self.into_blob()) }
}
}
impl Deref for StringPtr {
type Target = [u8];
fn deref(&self) -> &Self::Target {
let s: &[u8] = self.into();
s
}
}
impl From<String> for StringPtr {
fn from(s: String) -> Self {
Self::from(s.into_bytes())
}
}
impl From<&String> for StringPtr {
fn from(s: &String) -> Self {
Self::from(s.to_owned())
}
}
impl From<&str> for StringPtr {
fn from(s: &str) -> Self {
Self::from(s.to_string())
}
}
impl From<Vec<u8>> for StringPtr {
fn from(mut bytes: Vec<u8>) -> Self {
bytes.shrink_to_fit();
let len = bytes.len() as u64;
let ptr = if len == 0 {
std::ptr::null_mut()
} else {
bytes.as_ptr()
};
Self::from_raw(ptr, len)
}
}
impl From<&[u8]> for StringPtr {
fn from(bytes: &[u8]) -> Self {
Self::from(bytes.to_vec())
}
}
impl From<StringPtr> for String {
fn from(value: StringPtr) -> Self {
String::from_utf8(value.to_vec()).unwrap()
}
}
impl From<StringPtr> for Vec<u8> {
fn from(value: StringPtr) -> Self {
let len = value.len();
let ptr = value.into_ptr();
unsafe { Vec::from_raw_parts(ptr, len, len) }
}
}
impl From<&StringPtr> for &[u8] {
fn from(value: &StringPtr) -> Self {
let ptr = value.as_ptr();
let len = value.len();
unsafe { std::slice::from_raw_parts(ptr, len) }
}
}
impl PartialEq<&str> for StringPtr {
fn eq(&self, other: &&str) -> bool {
self.as_ref() == other.as_bytes()
}
}
impl PartialEq<str> for StringPtr {
fn eq(&self, other: &str) -> bool {
self.as_ref() == other.as_bytes()
}
}
impl PartialEq<StringPtr> for &str {
fn eq(&self, other: &StringPtr) -> bool {
self.as_bytes() == other.as_ref()
}
}
impl PartialEq<String> for StringPtr {
fn eq(&self, other: &String) -> bool {
self.as_ref() == other.as_bytes()
}
}
impl PartialEq for StringPtr {
fn eq(&self, other: &StringPtr) -> bool {
self.deref() == other.deref()
}
}
impl Eq for StringPtr {}
use crate::containers::ExternalList;
impl From<StringPtr> for ExternalList<u8> {
fn from(s: StringPtr) -> Self {
ExternalList::from(s.to_vec())
}
}
#[cfg(test)]
mod tests {
use super::StringPtr;
use std::ops::Deref;
const SHORT_STR: &str = "a";
const LONG_STR: &str = "aaaaaaaaaaaaaaaaaaaaaaaaa";
#[test]
fn test_new() {
let short = StringPtr::from(SHORT_STR);
assert_eq!(short, SHORT_STR);
let long = StringPtr::from(LONG_STR);
assert_eq!(long, LONG_STR)
}
#[test]
fn test_len() {
let short = String::from(SHORT_STR);
assert_eq!(short.len(), 1);
let long = String::from(LONG_STR);
assert_eq!(long.len(), 25);
}
#[test]
fn test_as_ptr() {
let short = StringPtr::from(SHORT_STR);
assert_eq!(short.as_ptr(), short.as_ptr());
assert_ne!(short.as_ptr(), std::ptr::null_mut());
let long = StringPtr::from(LONG_STR);
assert_eq!(long.as_ptr(), long.as_ptr());
assert_ne!(long.as_ptr(), std::ptr::null_mut());
}
#[test]
fn test_clone() {
let short = StringPtr::from(SHORT_STR);
assert_eq!(short.clone(), SHORT_STR);
assert_ne!(short.as_ptr(), short.clone().as_ptr());
let long = StringPtr::from(LONG_STR);
assert_eq!(long.clone(), LONG_STR);
assert_ne!(long.as_ptr(), long.clone().as_ptr());
}
#[test]
fn test_deref() {
let short = StringPtr::from(SHORT_STR);
let short_ref = short.deref();
assert_eq!(short_ref, SHORT_STR.as_bytes());
let long = StringPtr::from(LONG_STR);
let long_ref = long.deref();
assert_eq!(long_ref, LONG_STR.as_bytes());
}
#[test]
fn test_empty() {
let empty = StringPtr::from("");
assert_eq!(empty.len(), 0);
assert_eq!(empty.as_ptr(), std::ptr::null_mut());
assert_eq!(empty.as_str(), "");
let empty_ref = empty.deref();
assert_eq!(empty_ref, &[]);
}
#[test]
fn test_from() {
let string_ptr = StringPtr::from("foo");
drop(string_ptr);
let string_ptr = StringPtr::from(String::from("foo"));
drop(string_ptr);
let string_ptr = StringPtr::from(&String::from("foo"));
drop(string_ptr);
let string_ptr = StringPtr::from(vec![1, 2, 3]);
drop(string_ptr);
}
}
}