objc2_core_foundation/generated/
CFStringTokenizer.rs

1//! This file has been automatically generated by `objc2`'s `header-translator`.
2//! DO NOT EDIT
3use core::cell::UnsafeCell;
4use core::ffi::*;
5use core::marker::{PhantomData, PhantomPinned};
6use core::ptr::NonNull;
7#[cfg(feature = "objc2")]
8use objc2::__framework_prelude::*;
9
10use crate::*;
11
12impl CFStringTokenizer {
13    /// Guesses the language of a string and returns the BCP 47 string of the
14    /// language.
15    ///
16    /// Parameter `string`: The string whose language is to be guessed.
17    ///
18    /// Parameter `range`: The range of characters in string whose language to be
19    /// guessed. The specified range must not exceed the bounds of the string.
20    ///
21    /// Returns: A language represented in BCP 47 string. NULL is returned either if
22    /// string is NULL, the location of range is negative, the length of range
23    /// is 0, or the language of the string cannot be guessed.
24    ///
25    /// The result is not guaranteed to be accurate. Typically 200-400
26    /// characters are required to reliably guess the language of a string.
27    #[doc(alias = "CFStringTokenizerCopyBestStringLanguage")]
28    #[inline]
29    pub fn best_string_language(string: &CFString, range: CFRange) -> Option<CFRetained<CFString>> {
30        extern "C-unwind" {
31            fn CFStringTokenizerCopyBestStringLanguage(
32                string: &CFString,
33                range: CFRange,
34            ) -> Option<NonNull<CFString>>;
35        }
36        let ret = unsafe { CFStringTokenizerCopyBestStringLanguage(string, range) };
37        ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
38    }
39}
40
41/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfstringtokenizer?language=objc)
42#[doc(alias = "CFStringTokenizerRef")]
43#[repr(C)]
44pub struct CFStringTokenizer {
45    inner: [u8; 0],
46    _p: UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>,
47}
48
49cf_type!(
50    unsafe impl CFStringTokenizer {}
51);
52#[cfg(feature = "objc2")]
53cf_objc2_type!(
54    unsafe impl RefEncode<"__CFStringTokenizer"> for CFStringTokenizer {}
55);
56
57/// Tokenization Unit
58/// Use one of tokenization unit options with CFStringTokenizerCreate to
59/// specify how the string should be tokenized.
60///
61/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitword?language=objc)
62pub const kCFStringTokenizerUnitWord: CFOptionFlags = 0;
63/// Tokenization Unit
64/// Use one of tokenization unit options with CFStringTokenizerCreate to
65/// specify how the string should be tokenized.
66///
67/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitsentence?language=objc)
68pub const kCFStringTokenizerUnitSentence: CFOptionFlags = 1;
69/// Tokenization Unit
70/// Use one of tokenization unit options with CFStringTokenizerCreate to
71/// specify how the string should be tokenized.
72///
73/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitparagraph?language=objc)
74pub const kCFStringTokenizerUnitParagraph: CFOptionFlags = 2;
75/// Tokenization Unit
76/// Use one of tokenization unit options with CFStringTokenizerCreate to
77/// specify how the string should be tokenized.
78///
79/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitlinebreak?language=objc)
80pub const kCFStringTokenizerUnitLineBreak: CFOptionFlags = 3;
81/// Tokenization Unit
82/// Use one of tokenization unit options with CFStringTokenizerCreate to
83/// specify how the string should be tokenized.
84///
85/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitwordboundary?language=objc)
86pub const kCFStringTokenizerUnitWordBoundary: CFOptionFlags = 4;
87/// Attribute Specifier
88/// Use attribute specifier to tell tokenizer to prepare the specified attribute
89/// when it tokenizes the given string. The attribute value can be retrieved by
90/// calling CFStringTokenizerCopyCurrentTokenAttribute with one of the attribute
91/// option.
92///
93/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerattributelatintranscription?language=objc)
94pub const kCFStringTokenizerAttributeLatinTranscription: CFOptionFlags = 1 << 16;
95/// Attribute Specifier
96/// Use attribute specifier to tell tokenizer to prepare the specified attribute
97/// when it tokenizes the given string. The attribute value can be retrieved by
98/// calling CFStringTokenizerCopyCurrentTokenAttribute with one of the attribute
99/// option.
100///
101/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerattributelanguage?language=objc)
102pub const kCFStringTokenizerAttributeLanguage: CFOptionFlags = 1 << 17;
103
104/// Token type
105/// CFStringTokenizerGoToTokenAtIndex / CFStringTokenizerAdvanceToNextToken returns
106/// the type of current token.
107///
108/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfstringtokenizertokentype?language=objc)
109// NS_OPTIONS
110#[repr(transparent)]
111#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
112pub struct CFStringTokenizerTokenType(pub CFOptionFlags);
113bitflags::bitflags! {
114    impl CFStringTokenizerTokenType: CFOptionFlags {
115        #[doc(alias = "kCFStringTokenizerTokenNone")]
116        const None = 0;
117        #[doc(alias = "kCFStringTokenizerTokenNormal")]
118        const Normal = 1<<0;
119/// Compound token which may contain subtokens but with no derived subtokens.
120/// Its subtokens can be obtained by calling CFStringTokenizerGetCurrentSubTokens.
121        #[doc(alias = "kCFStringTokenizerTokenHasSubTokensMask")]
122        const HasSubTokensMask = 1<<1;
123/// Compound token which may contain derived subtokens.
124/// Its subtokens and derived subtokens can be obtained by calling
125/// CFStringTokenizerGetCurrentSubTokens.
126        #[doc(alias = "kCFStringTokenizerTokenHasDerivedSubTokensMask")]
127        const HasDerivedSubTokensMask = 1<<2;
128/// Compound token which may contain derived subtokens.
129/// Its subtokens and derived subtokens can be obtained by calling
130/// CFStringTokenizerGetCurrentSubTokens.
131        #[doc(alias = "kCFStringTokenizerTokenHasHasNumbersMask")]
132        const HasHasNumbersMask = 1<<3;
133/// Compound token which may contain derived subtokens.
134/// Its subtokens and derived subtokens can be obtained by calling
135/// CFStringTokenizerGetCurrentSubTokens.
136        #[doc(alias = "kCFStringTokenizerTokenHasNonLettersMask")]
137        const HasNonLettersMask = 1<<4;
138/// Compound token which may contain derived subtokens.
139/// Its subtokens and derived subtokens can be obtained by calling
140/// CFStringTokenizerGetCurrentSubTokens.
141        #[doc(alias = "kCFStringTokenizerTokenIsCJWordMask")]
142        const IsCJWordMask = 1<<5;
143    }
144}
145
146#[cfg(feature = "objc2")]
147unsafe impl Encode for CFStringTokenizerTokenType {
148    const ENCODING: Encoding = CFOptionFlags::ENCODING;
149}
150
151#[cfg(feature = "objc2")]
152unsafe impl RefEncode for CFStringTokenizerTokenType {
153    const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
154}
155
156unsafe impl ConcreteType for CFStringTokenizer {
157    /// Get the type identifier.
158    ///
159    /// Returns: the type identifier of all CFStringTokenizer instances.
160    #[doc(alias = "CFStringTokenizerGetTypeID")]
161    #[inline]
162    fn type_id() -> CFTypeID {
163        extern "C-unwind" {
164            fn CFStringTokenizerGetTypeID() -> CFTypeID;
165        }
166        unsafe { CFStringTokenizerGetTypeID() }
167    }
168}
169
170impl CFStringTokenizer {
171    /// Creates a tokenizer instance.
172    ///
173    /// Parameter `alloc`: The CFAllocator which should be used to allocate memory for the
174    /// tokenizer and its storage for values. This parameter may be NULL in which
175    /// case the current default CFAllocator is used.
176    ///
177    /// Parameter `string`: The string to tokenize.
178    ///
179    /// Parameter `range`: The range of characters within the string to be tokenized. The
180    /// specified range must not exceed the length of the string.
181    ///
182    /// Parameter `options`: Use one of the Tokenization Unit options to specify how the
183    /// string should be tokenized. Optionally specify one or more attribute
184    /// specifiers to tell the tokenizer to prepare specified attributes when it
185    /// tokenizes the string.
186    ///
187    /// Parameter `locale`: The locale to specify language or region specific behavior. Pass
188    /// NULL if you want tokenizer to identify the locale automatically.
189    ///
190    /// Returns: A reference to the new CFStringTokenizer.
191    ///
192    /// # Safety
193    ///
194    /// - `alloc` might not allow `None`.
195    /// - `string` might not allow `None`.
196    /// - `locale` might not allow `None`.
197    #[doc(alias = "CFStringTokenizerCreate")]
198    #[cfg(feature = "CFLocale")]
199    #[inline]
200    pub unsafe fn new(
201        alloc: Option<&CFAllocator>,
202        string: Option<&CFString>,
203        range: CFRange,
204        options: CFOptionFlags,
205        locale: Option<&CFLocale>,
206    ) -> Option<CFRetained<CFStringTokenizer>> {
207        extern "C-unwind" {
208            fn CFStringTokenizerCreate(
209                alloc: Option<&CFAllocator>,
210                string: Option<&CFString>,
211                range: CFRange,
212                options: CFOptionFlags,
213                locale: Option<&CFLocale>,
214            ) -> Option<NonNull<CFStringTokenizer>>;
215        }
216        let ret = unsafe { CFStringTokenizerCreate(alloc, string, range, options, locale) };
217        ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
218    }
219
220    /// Set the string to tokenize.
221    ///
222    /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
223    /// CFStringTokenizerCreate.
224    ///
225    /// Parameter `string`: The string to tokenize.
226    ///
227    /// Parameter `range`: The range of characters within the string to be tokenized. The
228    /// specified range must not exceed the length of the string.
229    ///
230    /// # Safety
231    ///
232    /// `string` might not allow `None`.
233    #[doc(alias = "CFStringTokenizerSetString")]
234    #[inline]
235    pub unsafe fn set_string(&self, string: Option<&CFString>, range: CFRange) {
236        extern "C-unwind" {
237            fn CFStringTokenizerSetString(
238                tokenizer: &CFStringTokenizer,
239                string: Option<&CFString>,
240                range: CFRange,
241            );
242        }
243        unsafe { CFStringTokenizerSetString(self, string, range) }
244    }
245
246    /// Random access to a token. Find a token that includes the character specified
247    /// by character index, and set it as the current token.
248    ///
249    /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
250    /// CFStringTokenizerCreate.
251    ///
252    /// Parameter `index`: The index of the Unicode character in the CFString.
253    ///
254    /// Returns: Type of the token if succeeded in finding a token and setting it as
255    /// current token. kCFStringTokenizerTokenNone if failed in finding a token.
256    ///
257    /// The range and attribute of the token can be obtained by calling
258    /// CFStringTokenizerGetCurrentTokenRange and CFStringTokenizerCopyCurrentTokenAttribute.
259    /// If the token is a compound (with type kCFStringTokenizerTokenHasSubTokensMask or
260    /// kCFStringTokenizerTokenHasDerivedSubTokensMask), its subtokens and
261    /// (or) derived subtokens can be obtained by calling CFStringTokenizerGetCurrentSubTokens.
262    #[doc(alias = "CFStringTokenizerGoToTokenAtIndex")]
263    #[inline]
264    pub fn go_to_token_at_index(&self, index: CFIndex) -> CFStringTokenizerTokenType {
265        extern "C-unwind" {
266            fn CFStringTokenizerGoToTokenAtIndex(
267                tokenizer: &CFStringTokenizer,
268                index: CFIndex,
269            ) -> CFStringTokenizerTokenType;
270        }
271        unsafe { CFStringTokenizerGoToTokenAtIndex(self, index) }
272    }
273
274    /// Token enumerator.
275    ///
276    /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
277    /// CFStringTokenizerCreate.
278    ///
279    /// Returns: Type of the token if succeeded in finding a token and setting it as
280    /// current token. kCFStringTokenizerTokenNone if failed in finding a token.
281    ///
282    /// If there is no preceding call to CFStringTokenizerGoToTokenAtIndex
283    /// or CFStringTokenizerAdvanceToNextToken, it finds the first token in the range
284    /// specified to CFStringTokenizerCreate. If there is a current token after successful
285    /// call to CFStringTokenizerGoToTokenAtIndex or CFStringTokenizerAdvanceToNextToken,
286    /// it proceeds to the next token. If succeeded in finding a token, set it as current
287    /// token and return its token type. Otherwise invalidate current token and return
288    /// kCFStringTokenizerTokenNone.
289    /// The range and attribute of the token can be obtained by calling
290    /// CFStringTokenizerGetCurrentTokenRange and
291    /// CFStringTokenizerCopyCurrentTokenAttribute. If the token is a compound
292    /// (with type kCFStringTokenizerTokenHasSubTokensMask or
293    /// kCFStringTokenizerTokenHasDerivedSubTokensMask), its subtokens and
294    /// (or) derived subtokens can be obtained by calling CFStringTokenizerGetCurrentSubTokens.
295    #[doc(alias = "CFStringTokenizerAdvanceToNextToken")]
296    #[inline]
297    pub fn advance_to_next_token(&self) -> CFStringTokenizerTokenType {
298        extern "C-unwind" {
299            fn CFStringTokenizerAdvanceToNextToken(
300                tokenizer: &CFStringTokenizer,
301            ) -> CFStringTokenizerTokenType;
302        }
303        unsafe { CFStringTokenizerAdvanceToNextToken(self) }
304    }
305
306    /// Returns the range of current token.
307    ///
308    /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
309    /// CFStringTokenizerCreate.
310    ///
311    /// Returns: Range of current token, or {kCFNotFound,0} if there is no current token.
312    #[doc(alias = "CFStringTokenizerGetCurrentTokenRange")]
313    #[inline]
314    pub fn current_token_range(&self) -> CFRange {
315        extern "C-unwind" {
316            fn CFStringTokenizerGetCurrentTokenRange(tokenizer: &CFStringTokenizer) -> CFRange;
317        }
318        unsafe { CFStringTokenizerGetCurrentTokenRange(self) }
319    }
320
321    /// Copies the specified attribute of current token.
322    ///
323    /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
324    /// CFStringTokenizerCreate.
325    ///
326    /// Parameter `attribute`: Specify a token attribute you want to obtain. The value is
327    /// one of kCFStringTokenizerAttributeLatinTranscription or
328    /// kCFStringTokenizerAttributeLanguage.
329    ///
330    /// Returns: Token attribute, or NULL if current token does not have the specified
331    /// attribute or if there is no current token.
332    #[doc(alias = "CFStringTokenizerCopyCurrentTokenAttribute")]
333    #[inline]
334    pub fn current_token_attribute(&self, attribute: CFOptionFlags) -> Option<CFRetained<CFType>> {
335        extern "C-unwind" {
336            fn CFStringTokenizerCopyCurrentTokenAttribute(
337                tokenizer: &CFStringTokenizer,
338                attribute: CFOptionFlags,
339            ) -> Option<NonNull<CFType>>;
340        }
341        let ret = unsafe { CFStringTokenizerCopyCurrentTokenAttribute(self, attribute) };
342        ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
343    }
344
345    /// Retrieves the subtokens or derived subtokens contained in the compound token.
346    ///
347    /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by CFStringTokenizerCreate.
348    ///
349    /// Parameter `ranges`: An array of CFRange to fill in with the ranges of subtokens. The filled in
350    /// ranges are relative to the string specified to CFStringTokenizerCreate. This parameter
351    /// can be NULL.
352    ///
353    /// Parameter `maxRangeLength`: The maximum number of ranges to return.
354    ///
355    /// Parameter `derivedSubTokens`: An array of CFMutableArray to which the derived subtokens are to
356    /// be added. This parameter can be NULL.
357    ///
358    /// Returns: number of subtokens.
359    ///
360    /// If token type is kCFStringTokenizerTokenNone, the ranges array and
361    /// derivedSubTokens array are untouched and the return value is 0.
362    /// If token type is kCFStringTokenizerTokenNormal, the ranges array has one item
363    /// filled in with the entire range of the token (if maxRangeLength >= 1) and a string
364    /// taken from the entire token range is added to the derivedSubTokens array and the
365    /// return value is 1.
366    /// If token type is kCFStringTokenizerTokenHasSubTokensMask or
367    /// kCFStringTokenizerTokenHasDerivedSubTokensMask, the ranges array is filled
368    /// in with as many items as there are subtokens (up to a limit of maxRangeLength).
369    /// The derivedSubTokens array will have sub tokens added even when the sub token is a
370    /// substring of the token. If token type is kCFStringTokenizerTokenHasSubTokensMask,
371    /// the ordinary non-derived subtokens are added to the derivedSubTokens array.
372    ///
373    /// # Safety
374    ///
375    /// - `ranges` must be a valid pointer.
376    /// - `derived_sub_tokens` generic must be of the correct type.
377    /// - `derived_sub_tokens` might not allow `None`.
378    #[doc(alias = "CFStringTokenizerGetCurrentSubTokens")]
379    #[cfg(feature = "CFArray")]
380    #[inline]
381    pub unsafe fn current_sub_tokens(
382        &self,
383        ranges: *mut CFRange,
384        max_range_length: CFIndex,
385        derived_sub_tokens: Option<&CFMutableArray>,
386    ) -> CFIndex {
387        extern "C-unwind" {
388            fn CFStringTokenizerGetCurrentSubTokens(
389                tokenizer: &CFStringTokenizer,
390                ranges: *mut CFRange,
391                max_range_length: CFIndex,
392                derived_sub_tokens: Option<&CFMutableArray>,
393            ) -> CFIndex;
394        }
395        unsafe {
396            CFStringTokenizerGetCurrentSubTokens(self, ranges, max_range_length, derived_sub_tokens)
397        }
398    }
399}
400
401#[deprecated = "renamed to `CFStringTokenizer::best_string_language`"]
402#[inline]
403pub extern "C-unwind" fn CFStringTokenizerCopyBestStringLanguage(
404    string: &CFString,
405    range: CFRange,
406) -> Option<CFRetained<CFString>> {
407    extern "C-unwind" {
408        fn CFStringTokenizerCopyBestStringLanguage(
409            string: &CFString,
410            range: CFRange,
411        ) -> Option<NonNull<CFString>>;
412    }
413    let ret = unsafe { CFStringTokenizerCopyBestStringLanguage(string, range) };
414    ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
415}
416
417#[cfg(feature = "CFLocale")]
418#[deprecated = "renamed to `CFStringTokenizer::new`"]
419#[inline]
420pub unsafe extern "C-unwind" fn CFStringTokenizerCreate(
421    alloc: Option<&CFAllocator>,
422    string: Option<&CFString>,
423    range: CFRange,
424    options: CFOptionFlags,
425    locale: Option<&CFLocale>,
426) -> Option<CFRetained<CFStringTokenizer>> {
427    extern "C-unwind" {
428        fn CFStringTokenizerCreate(
429            alloc: Option<&CFAllocator>,
430            string: Option<&CFString>,
431            range: CFRange,
432            options: CFOptionFlags,
433            locale: Option<&CFLocale>,
434        ) -> Option<NonNull<CFStringTokenizer>>;
435    }
436    let ret = unsafe { CFStringTokenizerCreate(alloc, string, range, options, locale) };
437    ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
438}
439
440extern "C-unwind" {
441    #[deprecated = "renamed to `CFStringTokenizer::set_string`"]
442    pub fn CFStringTokenizerSetString(
443        tokenizer: &CFStringTokenizer,
444        string: Option<&CFString>,
445        range: CFRange,
446    );
447}
448
449#[deprecated = "renamed to `CFStringTokenizer::go_to_token_at_index`"]
450#[inline]
451pub extern "C-unwind" fn CFStringTokenizerGoToTokenAtIndex(
452    tokenizer: &CFStringTokenizer,
453    index: CFIndex,
454) -> CFStringTokenizerTokenType {
455    extern "C-unwind" {
456        fn CFStringTokenizerGoToTokenAtIndex(
457            tokenizer: &CFStringTokenizer,
458            index: CFIndex,
459        ) -> CFStringTokenizerTokenType;
460    }
461    unsafe { CFStringTokenizerGoToTokenAtIndex(tokenizer, index) }
462}
463
464#[deprecated = "renamed to `CFStringTokenizer::advance_to_next_token`"]
465#[inline]
466pub extern "C-unwind" fn CFStringTokenizerAdvanceToNextToken(
467    tokenizer: &CFStringTokenizer,
468) -> CFStringTokenizerTokenType {
469    extern "C-unwind" {
470        fn CFStringTokenizerAdvanceToNextToken(
471            tokenizer: &CFStringTokenizer,
472        ) -> CFStringTokenizerTokenType;
473    }
474    unsafe { CFStringTokenizerAdvanceToNextToken(tokenizer) }
475}
476
477#[deprecated = "renamed to `CFStringTokenizer::current_token_range`"]
478#[inline]
479pub extern "C-unwind" fn CFStringTokenizerGetCurrentTokenRange(
480    tokenizer: &CFStringTokenizer,
481) -> CFRange {
482    extern "C-unwind" {
483        fn CFStringTokenizerGetCurrentTokenRange(tokenizer: &CFStringTokenizer) -> CFRange;
484    }
485    unsafe { CFStringTokenizerGetCurrentTokenRange(tokenizer) }
486}
487
488#[deprecated = "renamed to `CFStringTokenizer::current_token_attribute`"]
489#[inline]
490pub extern "C-unwind" fn CFStringTokenizerCopyCurrentTokenAttribute(
491    tokenizer: &CFStringTokenizer,
492    attribute: CFOptionFlags,
493) -> Option<CFRetained<CFType>> {
494    extern "C-unwind" {
495        fn CFStringTokenizerCopyCurrentTokenAttribute(
496            tokenizer: &CFStringTokenizer,
497            attribute: CFOptionFlags,
498        ) -> Option<NonNull<CFType>>;
499    }
500    let ret = unsafe { CFStringTokenizerCopyCurrentTokenAttribute(tokenizer, attribute) };
501    ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
502}
503
504extern "C-unwind" {
505    #[cfg(feature = "CFArray")]
506    #[deprecated = "renamed to `CFStringTokenizer::current_sub_tokens`"]
507    pub fn CFStringTokenizerGetCurrentSubTokens(
508        tokenizer: &CFStringTokenizer,
509        ranges: *mut CFRange,
510        max_range_length: CFIndex,
511        derived_sub_tokens: Option<&CFMutableArray>,
512    ) -> CFIndex;
513}