cidre 0.14.0

Apple frameworks bindings for rust
Documentation
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
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
use core::fmt;
use std::{
    borrow::{Borrow, Cow},
    ffi::{CStr, c_char},
    hash::Hash,
    str::from_utf8_unchecked,
};

use crate::{
    UniChar, arc,
    cf::{self, Index, OptionFlags, Range, Type, TypeId},
    define_cf_type, define_opts,
};

#[cfg(feature = "ns")]
use crate::{ns, objc::Obj};

#[derive(Debug, Copy, Clone, PartialEq, Eq)]
#[repr(transparent)]
pub struct Encoding(u32);

impl Encoding {
    #[doc(alias = "kCFStringEncodingMacRoman")]
    pub const MAC_ROMAN: Self = Self(0);

    #[doc(alias = "kCFStringEncodingASCII")]
    pub const ASCII: Self = Self(0x0600);

    #[doc(alias = "kCFStringEncodingUTF8")]
    pub const UTF8: Self = Self(0x08000100);

    /// The default encoding for the system; untagged 8-bit characters are usually in this encoding
    ///
    /// ```
    /// use cidre::cf;
    ///
    /// let encoding = cf::StringEncoding::sys_encoding();
    /// assert_eq!(encoding, cf::StringEncoding::MAC_ROMAN);
    /// ```
    #[doc(alias = "CFStringGetSystemEncoding")]
    #[inline]
    pub fn sys_encoding() -> Self {
        unsafe { CFStringGetSystemEncoding() }
    }
}

define_opts!(
    #[doc(alias = "CFStringCompareFlags")]
    pub CompareFlags(OptionFlags)
);

impl CompareFlags {
    pub const NONE: Self = Self(OptionFlags(0));

    pub const CASE_INSENSITIVE: Self = Self(OptionFlags(1));
    pub const BACKWARDS: Self = Self(OptionFlags(4));
    pub const ANCHORED: Self = Self(OptionFlags(8));
    pub const NON_LITERAL: Self = Self(OptionFlags(16));
    pub const LOCALIZED: Self = Self(OptionFlags(32));
    pub const NUMERACALLY: Self = Self(OptionFlags(64));
    pub const DIACRITIC_INSENITIVE: Self = Self(OptionFlags(128));
    pub const WIDTH_INSENSITIVE: Self = Self(OptionFlags(256));
    pub const FORCED_ORDERING: Self = Self(OptionFlags(512));
}

// https://github.com/apportable/Foundation/blob/master/System/CoreFoundation/src/CFString.c

define_cf_type!(
    #[doc(alias = "CFStringRef")]
    String(Type)
);

impl String {
    ///```
    /// use cidre::cf;
    ///
    /// assert_eq!(cf::String::type_id(), 7);
    ///```
    #[inline]
    pub fn type_id() -> TypeId {
        unsafe { CFStringGetTypeID() }
    }

    ///```
    /// use cidre::cf;
    ///
    /// let s1 = unsafe { cf::String::from_str_no_copy("nice") };
    /// let s2 = unsafe { cf::String::from_str_no_copy("nice") };
    ///
    /// assert_eq!(4, s1.len());
    /// assert!(s1.equal(&s2));
    ///```
    /// NOTE: cf_string benchmark reveals that it is actually do copy
    #[inline]
    pub unsafe fn from_str_no_copy(str: &str) -> arc::R<Self> {
        let bytes = str.as_bytes();
        unsafe {
            Self::create_with_bytes_no_copy_in(
                bytes,
                Encoding::UTF8,
                false,
                cf::Allocator::null(),
                None,
            )
            .unwrap_unchecked()
        }
    }

    ///```
    /// use cidre::cf;
    ///
    /// let s1 = cf::String::from_str("nice");
    /// let s2 = cf::String::from_str("nice");
    /// let s3 = cf::String::from_str("nice string");
    ///
    /// assert_eq!(4, s1.len());
    /// assert!(s1.equal(&s2));
    /// assert!(s3.has_prefix(&s2));
    ///```
    #[inline]
    pub fn from_str(str: &str) -> arc::R<Self> {
        let bytes = str.as_bytes();
        unsafe { Self::create_with_bytes(None, bytes, Encoding::UTF8, false).unwrap_unchecked() }
    }

    #[inline]
    pub fn from_cstr(cstr: &CStr) -> arc::R<Self> {
        unsafe {
            Self::create_with_cstring_in(cstr.to_bytes_with_nul(), Encoding::UTF8, None)
                .unwrap_unchecked()
        }
    }

    #[inline]
    pub unsafe fn from_cstr_no_copy(cstr: &CStr) -> arc::R<Self> {
        unsafe {
            Self::create_with_cstring_no_copy_in(
                cstr.to_bytes_with_nul(),
                Encoding::UTF8,
                cf::Allocator::null(),
                None,
            )
            .unwrap_unchecked()
        }
    }

    #[doc(alias = "CFShowStr")]
    #[inline]
    pub fn show_str(&self) {
        unsafe { CFShowStr(self) }
    }

    #[inline]
    pub fn len(&self) -> Index {
        unsafe { CFStringGetLength(self) }
    }

    #[inline]
    pub fn is_empty(&self) -> bool {
        self.len() == 0
    }

    #[doc(alias = "CFStringHasSuffix")]
    #[inline]
    pub fn has_suffix(&self, suffix: &Self) -> bool {
        unsafe { CFStringHasSuffix(self, suffix) }
    }

    #[doc(alias = "CFStringHasPrefix")]
    #[inline]
    pub fn has_prefix(&self, prefix: &Self) -> bool {
        unsafe { CFStringHasPrefix(self, prefix) }
    }

    #[doc(alias = "CFStringGetCharacterAtIndex")]
    #[inline]
    pub fn character_at_index(&self, idx: Index) -> UniChar {
        unsafe { CFStringGetCharacterAtIndex(self, idx) }
    }

    #[doc(alias = "CFStringCreateCopy")]
    #[inline]
    pub fn copy_in(&self, alloc: Option<&cf::Allocator>) -> Option<arc::R<Self>> {
        unsafe { CFStringCreateCopy(alloc, self) }
    }

    #[doc(alias = "CFStringCreateCopy")]
    #[inline]
    pub fn copy(&self) -> arc::R<Self> {
        unsafe { std::mem::transmute(self.copy_in(None)) }
    }

    #[inline]
    pub fn create_with_bytes_no_copy_in(
        bytes: &[u8],
        encoding: Encoding,
        is_external_representation: bool,
        contents_deallocator: Option<&cf::Allocator>,
        alloc: Option<&cf::Allocator>,
    ) -> Option<arc::R<Self>> {
        unsafe {
            CFStringCreateWithBytesNoCopy(
                alloc,
                bytes.as_ptr(),
                bytes.len() as _,
                encoding,
                is_external_representation,
                contents_deallocator,
            )
        }
    }

    #[inline]
    pub unsafe fn create_with_cstring_no_copy_in(
        bytes_with_null: &[u8],
        encoding: Encoding,
        contents_deallocator: Option<&cf::Allocator>,
        alloc: Option<&cf::Allocator>,
    ) -> Option<arc::R<Self>> {
        unsafe {
            let c_str = bytes_with_null.as_ptr() as *const i8;
            CFStringCreateWithCStringNoCopy(alloc, c_str, encoding, contents_deallocator)
        }
    }

    #[inline]
    pub fn create_with_cstring_in(
        bytes_with_null: &[u8],
        encoding: Encoding,
        alloc: Option<&cf::Allocator>,
    ) -> Option<arc::R<Self>> {
        unsafe {
            let c_str = bytes_with_null.as_ptr() as *const i8;
            CFStringCreateWithCString(alloc, c_str, encoding)
        }
    }

    #[inline]
    pub fn create_with_bytes(
        alloc: Option<&cf::Allocator>,
        bytes: &[u8],
        encoding: Encoding,
        is_external_representation: bool,
    ) -> Option<arc::R<Self>> {
        unsafe {
            CFStringCreateWithBytes(
                alloc,
                bytes.as_ptr(),
                bytes.len() as _,
                encoding,
                is_external_representation,
            )
        }
    }

    #[inline]
    pub fn copy_mut_in(
        &self,
        max_length: Index,
        alloc: Option<&cf::Allocator>,
    ) -> Option<arc::R<StringMut>> {
        unsafe { CFStringCreateMutableCopy(alloc, max_length, self) }
    }

    #[inline]
    pub fn copy_mut(&self, max_length: Index) -> arc::R<StringMut> {
        unsafe { self.copy_mut_in(max_length, None).unwrap_unchecked() }
    }

    #[cfg(feature = "ns")]
    #[inline]
    pub fn as_ns(&self) -> &ns::String {
        unsafe { std::mem::transmute(self) }
    }

    #[inline]
    pub fn to_string(&self) -> std::string::String {
        unsafe {
            let range = crate::cf::Range {
                loc: 0,
                len: self.len(),
            };
            let mut bytes_required: Index = 0;
            CFStringGetBytes(
                self,
                range,
                Encoding::UTF8,
                0,
                false,
                std::ptr::null_mut(),
                0,
                &mut bytes_required,
            );

            let mut buf = Vec::with_capacity(bytes_required as _);
            buf.set_len(bytes_required as _);
            let mut used_buf_len: Index = 0;
            CFStringGetBytes(
                self,
                range,
                Encoding::UTF8,
                0,
                false,
                buf.as_mut_ptr(),
                buf.len() as _,
                &mut used_buf_len,
            );

            debug_assert_eq!(bytes_required, used_buf_len);

            std::string::String::from_utf8_unchecked(buf)
        }
    }
}

#[cfg(feature = "ns")]
impl AsRef<ns::String> for String {
    fn as_ref(&self) -> &ns::String {
        self.as_ns()
    }
}

#[cfg(feature = "ns")]
impl AsRef<ns::Id> for String {
    fn as_ref(&self) -> &ns::Id {
        self.as_ns().as_id_ref()
    }
}

#[macro_export]
macro_rules! cfstr {
    ($f:literal) => {{
        #[link(name = "CoreFoundation", kind = "framework")]
        unsafe extern "C" {
            static __CFConstantStringClassReference: std::ffi::c_void;
        }
        // #[link_section = "__TEXT,__cstring,cstring_literals"]
        const VALUE: &std::ffi::CStr = $f;

        // #[link_section = "__DATA,__cfstring"]
        static STR: $crate::cf::string::ConstStr = unsafe {
            $crate::cf::string::ConstStr {
                isa: &__CFConstantStringClassReference,
                info: 0x7c8,
                ptr: VALUE.as_ptr(),
                len: VALUE.to_bytes().len(),
            }
        };
        unsafe {
            std::mem::transmute::<&'static $crate::cf::string::ConstStr, &'static $crate::cf::String>(
                &STR,
            )
        }
    }};
}

pub use cfstr as str;

impl<'a> From<&'a String> for Cow<'a, str> {
    fn from(cfstr: &'a String) -> Self {
        unsafe {
            let c_str = CFStringGetCStringPtr(cfstr, Encoding::UTF8);
            if c_str.is_null() {
                Cow::Owned(cfstr.to_string())
            } else {
                let cstr = CStr::from_ptr(c_str);
                Cow::Borrowed(from_utf8_unchecked(cstr.to_bytes()))
            }
        }
    }
}

impl fmt::Display for String {
    /// ```
    /// use cidre::cf;
    ///
    /// let s = cf::String::from_str("nice");
    /// let ss = s.to_string();
    ///
    /// assert_eq!("nice", &ss);
    /// ```
    fn fmt(&self, fmt: &mut fmt::Formatter) -> fmt::Result {
        fmt.write_str(&Cow::from(self))
    }
}

define_cf_type!(
    #[doc(alias = "CFMutableStringRef")]
    StringMut(String)
);

impl StringMut {
    #[doc(alias = "CFStringAppend")]
    #[inline]
    pub fn append(&mut self, appended_string: &String) {
        unsafe { CFStringAppend(self, appended_string) }
    }

    #[doc(alias = "CFStringTrim")]
    #[inline]
    pub fn trim(&mut self, trim_string: &String) {
        unsafe { CFStringTrim(self, trim_string) }
    }

    #[doc(alias = "CFStringTrimWhitespace")]
    #[inline]
    pub fn trim_whitespace(&mut self) {
        unsafe { CFStringTrimWhitespace(self) }
    }

    #[doc(alias = "CFStringCreateMutable")]
    #[inline]
    pub fn create_in(max_length: Index, alloc: Option<&cf::Allocator>) -> Option<arc::R<Self>> {
        unsafe { CFStringCreateMutable(alloc, max_length) }
    }
}

#[link(name = "CoreFoundation", kind = "framework")]
unsafe extern "C-unwind" {
    fn CFStringGetTypeID() -> TypeId;
    fn CFStringGetLength(the_string: &String) -> Index;
    fn CFStringCreateMutable(
        alloc: Option<&cf::Allocator>,
        max_length: Index,
    ) -> Option<arc::R<StringMut>>;
    fn CFStringCreateCopy(
        alloc: Option<&cf::Allocator>,
        the_string: &String,
    ) -> Option<arc::R<String>>;
    fn CFStringHasPrefix(the_string: &String, prefix: &String) -> bool;
    fn CFStringHasSuffix(the_string: &String, prefix: &String) -> bool;
    fn CFStringCreateMutableCopy(
        alloc: Option<&cf::Allocator>,
        max_length: Index,
        the_string: &String,
    ) -> Option<arc::R<StringMut>>;
    fn CFStringGetCharacterAtIndex(the_string: &String, idx: Index) -> UniChar;

    fn CFStringAppend(the_string: &mut StringMut, appended_string: &String);
    fn CFStringTrim(the_string: &mut StringMut, trim_string: &String);
    fn CFStringTrimWhitespace(the_string: &mut StringMut);

    fn CFStringCreateWithBytesNoCopy(
        alloc: Option<&cf::Allocator>,
        bytes: *const u8,
        num_bytes: Index,
        encoding: Encoding,
        is_external_representation: bool,
        contents_deallocator: Option<&cf::Allocator>,
    ) -> Option<arc::R<String>>;

    fn CFStringCreateWithCStringNoCopy(
        alloc: Option<&cf::Allocator>,
        c_str: *const c_char,
        encoding: Encoding,
        contents_deallocator: Option<&cf::Allocator>,
    ) -> Option<arc::R<String>>;

    fn CFStringCreateWithCString(
        alloc: Option<&cf::Allocator>,
        c_str: *const c_char,
        encoding: Encoding,
    ) -> Option<arc::R<String>>;

    fn CFStringCreateWithBytes(
        alloc: Option<&cf::Allocator>,
        bytes: *const u8,
        num_bytes: Index,
        encoding: Encoding,
        is_external_representation: bool,
    ) -> Option<arc::R<String>>;

    fn CFShowStr(str: &String);

    fn CFStringGetCStringPtr(the_string: &String, encoding: Encoding) -> *const c_char;
    fn CFStringGetBytes(
        the_string: &String,
        range: Range,
        encoding: Encoding,
        loss_byte: u8,
        is_external_representation: bool,
        buffer: *mut u8,
        max_buflen: Index,
        used_buf_len: *mut Index,
    ) -> Index;

    fn CFStringGetSystemEncoding() -> Encoding;

    // pub static __CFConstantStringClassReference: crate::objc::Class<ns::Id>;

    // fn _builtin___CFStringMakeConstantString(str: *const i8) -> &'static String;

    // fn __CFStringMakeConstantString(str: *const c_char) -> &'static String;
}

#[repr(C)]
pub struct ConstStr {
    pub isa: &'static std::ffi::c_void,
    pub info: u32,
    pub ptr: *const i8,
    pub len: usize,
}

unsafe impl Send for ConstStr {}
unsafe impl Sync for ConstStr {}

impl From<&'static str> for arc::R<String> {
    #[inline]
    fn from(s: &'static str) -> Self {
        String::from_str(s)
    }
}

impl From<&std::string::String> for arc::R<String> {
    #[inline]
    fn from(s: &std::string::String) -> Self {
        String::from_str(s.as_str())
    }
}

impl PartialEq<str> for String {
    fn eq(&self, other: &str) -> bool {
        let ptr = unsafe { CFStringGetCStringPtr(self, Encoding::UTF8) };
        if ptr.is_null() {
            return false;
        }
        let s = unsafe { CStr::from_ptr(ptr) };
        if let Ok(s) = s.to_str() {
            return s.eq(other);
        }

        false
    }
}

impl PartialEq<std::string::String> for String {
    fn eq(&self, other: &std::string::String) -> bool {
        self.eq(other.as_str())
    }
}

impl AsRef<cf::String> for cf::String {
    fn as_ref(&self) -> &cf::String {
        self
    }
}

impl AsRef<cf::String> for cf::StringMut {
    fn as_ref(&self) -> &cf::String {
        self
    }
}

impl Borrow<cf::Type> for &cf::String {
    fn borrow(&self) -> &cf::Type {
        self.as_type_ref()
    }
}

impl Borrow<cf::Type> for cf::String {
    fn borrow(&self) -> &cf::Type {
        self.as_type_ref()
    }
}

impl std::cmp::PartialEq for cf::String {
    fn eq(&self, other: &Self) -> bool {
        self.as_type_ref().eq(other)
    }
}

impl std::cmp::Eq for cf::String {}

impl std::hash::Hash for cf::String {
    fn hash<H: std::hash::Hasher>(&self, state: &mut H) {
        let hash = self.as_type_ref().hash();
        state.write_usize(hash)
    }
}

#[cfg(test)]
mod tests {

    use crate::cf;

    #[test]
    fn it_works() {
        let s = cf::str!(c"hello");
        assert_eq!(s.len(), 5);
        let std_str = s.to_string();
        assert_eq!(std_str.chars().count(), 5);

        let ns_str = s.as_ns();
        assert_eq!(&ns_str.to_string(), "hello");
        assert_eq!(ns_str, "hello");
    }

    #[test]
    fn macro_cfstr() {
        let s = cf::str!(c"nice");
        println!("rt {}", s.retain_count());
        s.show();
        s.show_str();

        assert_eq!(s, "nice");
    }
}