-
Notifications
You must be signed in to change notification settings - Fork 53
/
Copy pathstring.rs
251 lines (213 loc) · 7.15 KB
/
string.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
use core::ffi::c_void;
use core::fmt;
use core::ptr::NonNull;
use core::slice;
use core::str;
use std::os::raw::c_char;
use alloc::borrow::ToOwned;
use objc2::ffi;
use objc2::msg_send;
use objc2::rc::DefaultId;
use objc2::rc::{autoreleasepool, AutoreleasePool};
use objc2::rc::{Id, Shared};
use super::{INSCopying, INSObject};
#[cfg(apple)]
const UTF8_ENCODING: usize = 4;
#[cfg(gnustep)]
const UTF8_ENCODING: i32 = 4;
#[allow(unused)]
#[allow(non_upper_case_globals)]
const NSNotFound: ffi::NSInteger = ffi::NSIntegerMax;
pub unsafe trait INSString: INSObject {
fn len(&self) -> usize {
unsafe { msg_send![self, lengthOfBytesUsingEncoding: UTF8_ENCODING] }
}
fn is_empty(&self) -> bool {
self.len() == 0
}
/// TODO
///
/// ```compile_fail
/// # use objc2::rc::autoreleasepool;
/// # use objc2_foundation::{INSObject, INSString, NSString};
/// autoreleasepool(|pool| {
/// let ns_string = NSString::new();
/// let s = ns_string.as_str(pool);
/// drop(ns_string);
/// println!("{}", s);
/// });
/// ```
///
/// ```compile_fail
/// # use objc2::rc::autoreleasepool;
/// # use objc2_foundation::{INSObject, INSString, NSString};
/// let ns_string = NSString::new();
/// let s = autoreleasepool(|pool| ns_string.as_str(pool));
/// ```
fn as_str<'r, 's: 'r, 'p: 'r>(&'s self, pool: &'p AutoreleasePool) -> &'r str {
// This is necessary until `auto` types stabilizes.
pool.__verify_is_inner();
// The documentation on `UTF8String` is a bit sparse, but with
// educated guesses and testing I've determined that NSString stores
// a pointer to the string data, sometimes with an UTF-8 encoding,
// (usual for ascii data), sometimes in other encodings (UTF-16?).
//
// `UTF8String` then checks the internal encoding:
// - If the data is UTF-8 encoded, it returns the internal pointer.
// - If the data is in another encoding, it creates a new allocation,
// writes the UTF-8 representation of the string into it,
// autoreleases the allocation and returns a pointer to it.
//
// So the lifetime of the returned pointer is either the same as the
// NSString OR the lifetime of the innermost @autoreleasepool.
let bytes: *const c_char = unsafe { msg_send![self, UTF8String] };
let bytes = bytes as *const u8;
let len = self.len();
// SAFETY:
// The held AutoreleasePool is the innermost, and the reference is
// constrained both by the pool and the NSString.
//
// `len` is the length of the string in the UTF-8 encoding.
//
// `bytes` is a null-terminated C string (with length = len + 1), so
// it is never a NULL pointer.
let bytes: &'r [u8] = unsafe { slice::from_raw_parts(bytes, len) };
// TODO: Always UTF-8, so should we use `from_utf8_unchecked`?
str::from_utf8(bytes).unwrap()
}
fn from_str(string: &str) -> Id<Self, Shared> {
let cls = Self::class();
let bytes = string.as_ptr() as *const c_void;
unsafe {
let obj: *mut Self = msg_send![cls, alloc];
let obj: *mut Self = msg_send![
obj,
initWithBytes: bytes,
length: string.len(),
encoding: UTF8_ENCODING,
];
Id::new(NonNull::new_unchecked(obj))
}
}
}
object!(unsafe pub struct NSString);
// TODO: SAFETY
unsafe impl Sync for NSString {}
unsafe impl Send for NSString {}
impl NSString {
unsafe_def_fn!(pub fn new -> Shared);
}
impl DefaultId for NSString {
type Ownership = Shared;
#[inline]
fn default_id() -> Id<Self, Self::Ownership> {
Self::new()
}
}
unsafe impl INSString for NSString {}
unsafe impl INSCopying for NSString {
type Ownership = Shared;
type Output = NSString;
}
impl fmt::Display for NSString {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
// The call to `to_owned` is unfortunate, but is required to work
// around `f` not being AutoreleaseSafe.
// TODO: Fix this!
let s = autoreleasepool(|pool| self.as_str(pool).to_owned());
fmt::Display::fmt(&s, f)
}
}
#[cfg(test)]
mod tests {
use super::*;
use alloc::format;
#[cfg(gnustep)]
#[test]
fn ensure_linkage() {
unsafe { objc2::__gnustep_hack::get_class_to_force_linkage() };
}
#[test]
fn test_equality() {
let s1 = NSString::from_str("abc");
let s2 = NSString::from_str("abc");
assert_eq!(s1, s1);
assert_eq!(s1, s2);
let s3 = NSString::from_str("def");
assert_ne!(s1, s3);
}
#[test]
fn test_debug_display() {
let s = "xyz123";
let obj = NSString::from_str(s);
assert_eq!(format!("{:?}", obj), format!("{:?}", s));
assert_eq!(format!("{}", obj), format!("{}", s));
}
#[test]
fn test_empty() {
let s1 = NSString::from_str("");
let s2 = NSString::new();
assert_eq!(s1.len(), 0);
assert_eq!(s2.len(), 0);
assert_eq!(s1, s2);
autoreleasepool(|pool| {
assert_eq!(s1.as_str(pool), "");
assert_eq!(s2.as_str(pool), "");
});
}
#[test]
fn test_utf8() {
let expected = "ประเทศไทย中华Việt Nam";
let s = NSString::from_str(expected);
assert_eq!(s.len(), expected.len());
autoreleasepool(|pool| {
assert_eq!(s.as_str(pool), expected);
});
}
#[test]
fn test_interior_nul() {
let expected = "Hello\0World";
let s = NSString::from_str(expected);
assert_eq!(s.len(), expected.len());
autoreleasepool(|pool| {
assert_eq!(s.as_str(pool), expected);
});
}
#[test]
fn test_copy() {
let s = NSString::from_str("Hello!");
let copied = s.copy();
autoreleasepool(|pool| {
assert_eq!(copied.as_str(pool), s.as_str(pool));
});
}
#[test]
fn test_copy_nsstring_is_same() {
let string1 = NSString::from_str("Hello, world!");
let string2 = string1.copy();
let s1: *const NSString = &*string1;
let s2: *const NSString = &*string2;
assert_eq!(s1, s2, "Cloned NSString didn't have the same address");
}
#[test]
/// Apparently NSString does this for some reason?
fn test_strips_first_leading_zero_width_no_break_space() {
let ns_string = NSString::from_str("\u{feff}");
let expected = "";
autoreleasepool(|pool| {
assert_eq!(ns_string.as_str(pool), expected);
});
assert_eq!(ns_string.len(), 0);
let s = "\u{feff}\u{feff}a\u{feff}";
// Huh, this difference might be a GNUStep bug?
#[cfg(apple)]
let expected = "\u{feff}a\u{feff}";
#[cfg(gnustep)]
let expected = "a\u{feff}";
let ns_string = NSString::from_str(s);
autoreleasepool(|pool| {
assert_eq!(ns_string.as_str(pool), expected);
});
assert_eq!(ns_string.len(), expected.len());
}
}