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
12/// Guesses the language of a string and returns the BCP 47 string of the
13/// language.
14///
15/// Parameter `string`: The string whose language is to be guessed.
16///
17/// Parameter `range`: The range of characters in string whose language to be
18/// guessed. The specified range must not exceed the bounds of the string.
19///
20/// Returns: A language represented in BCP 47 string. NULL is returned either if
21/// string is NULL, the location of range is negative, the length of range
22/// is 0, or the language of the string cannot be guessed.
23///
24/// The result is not guaranteed to be accurate. Typically 200-400
25/// characters are required to reliably guess the language of a string.
26#[cfg(feature = "CFBase")]
27#[inline]
28pub unsafe extern "C-unwind" fn CFStringTokenizerCopyBestStringLanguage(
29 string: &CFString,
30 range: CFRange,
31) -> Option<CFRetained<CFString>> {
32 extern "C-unwind" {
33 fn CFStringTokenizerCopyBestStringLanguage(
34 string: &CFString,
35 range: CFRange,
36 ) -> Option<NonNull<CFString>>;
37 }
38 let ret = unsafe { CFStringTokenizerCopyBestStringLanguage(string, range) };
39 ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
40}
41
42/// [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfstringtokenizer?language=objc)
43#[repr(C)]
44pub struct CFStringTokenizer {
45 inner: [u8; 0],
46 _p: UnsafeCell<PhantomData<(*const UnsafeCell<()>, PhantomPinned)>>,
47}
48
49cf_type!(
50 #[encoding_name = "__CFStringTokenizer"]
51 unsafe impl CFStringTokenizer {}
52);
53
54/// Tokenization Unit
55/// Use one of tokenization unit options with CFStringTokenizerCreate to
56/// specify how the string should be tokenized.
57///
58/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitword?language=objc)
59#[cfg(feature = "CFBase")]
60pub const kCFStringTokenizerUnitWord: CFOptionFlags = 0;
61/// Tokenization Unit
62/// Use one of tokenization unit options with CFStringTokenizerCreate to
63/// specify how the string should be tokenized.
64///
65/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitsentence?language=objc)
66#[cfg(feature = "CFBase")]
67pub const kCFStringTokenizerUnitSentence: CFOptionFlags = 1;
68/// Tokenization Unit
69/// Use one of tokenization unit options with CFStringTokenizerCreate to
70/// specify how the string should be tokenized.
71///
72/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitparagraph?language=objc)
73#[cfg(feature = "CFBase")]
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)
80#[cfg(feature = "CFBase")]
81pub const kCFStringTokenizerUnitLineBreak: CFOptionFlags = 3;
82/// Tokenization Unit
83/// Use one of tokenization unit options with CFStringTokenizerCreate to
84/// specify how the string should be tokenized.
85///
86/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerunitwordboundary?language=objc)
87#[cfg(feature = "CFBase")]
88pub const kCFStringTokenizerUnitWordBoundary: CFOptionFlags = 4;
89/// Attribute Specifier
90/// Use attribute specifier to tell tokenizer to prepare the specified attribute
91/// when it tokenizes the given string. The attribute value can be retrieved by
92/// calling CFStringTokenizerCopyCurrentTokenAttribute with one of the attribute
93/// option.
94///
95/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerattributelatintranscription?language=objc)
96#[cfg(feature = "CFBase")]
97pub const kCFStringTokenizerAttributeLatinTranscription: CFOptionFlags = 1 << 16;
98/// Attribute Specifier
99/// Use attribute specifier to tell tokenizer to prepare the specified attribute
100/// when it tokenizes the given string. The attribute value can be retrieved by
101/// calling CFStringTokenizerCopyCurrentTokenAttribute with one of the attribute
102/// option.
103///
104/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/kcfstringtokenizerattributelanguage?language=objc)
105#[cfg(feature = "CFBase")]
106pub const kCFStringTokenizerAttributeLanguage: CFOptionFlags = 1 << 17;
107
108/// Token type
109/// CFStringTokenizerGoToTokenAtIndex / CFStringTokenizerAdvanceToNextToken returns
110/// the type of current token.
111///
112/// See also [Apple's documentation](https://developer.apple.com/documentation/corefoundation/cfstringtokenizertokentype?language=objc)
113// NS_OPTIONS
114#[cfg(feature = "CFBase")]
115#[repr(transparent)]
116#[derive(Clone, Copy, Debug, PartialEq, Eq, Hash, PartialOrd, Ord)]
117pub struct CFStringTokenizerTokenType(pub CFOptionFlags);
118#[cfg(feature = "CFBase")]
119bitflags::bitflags! {
120 impl CFStringTokenizerTokenType: CFOptionFlags {
121 #[doc(alias = "kCFStringTokenizerTokenNone")]
122 const None = 0;
123 #[doc(alias = "kCFStringTokenizerTokenNormal")]
124 const Normal = 1<<0;
125/// Compound token which may contain subtokens but with no derived subtokens.
126/// Its subtokens can be obtained by calling CFStringTokenizerGetCurrentSubTokens.
127 #[doc(alias = "kCFStringTokenizerTokenHasSubTokensMask")]
128 const HasSubTokensMask = 1<<1;
129/// Compound token which may contain derived subtokens.
130/// Its subtokens and derived subtokens can be obtained by calling
131/// CFStringTokenizerGetCurrentSubTokens.
132 #[doc(alias = "kCFStringTokenizerTokenHasDerivedSubTokensMask")]
133 const HasDerivedSubTokensMask = 1<<2;
134/// Compound token which may contain derived subtokens.
135/// Its subtokens and derived subtokens can be obtained by calling
136/// CFStringTokenizerGetCurrentSubTokens.
137 #[doc(alias = "kCFStringTokenizerTokenHasHasNumbersMask")]
138 const HasHasNumbersMask = 1<<3;
139/// Compound token which may contain derived subtokens.
140/// Its subtokens and derived subtokens can be obtained by calling
141/// CFStringTokenizerGetCurrentSubTokens.
142 #[doc(alias = "kCFStringTokenizerTokenHasNonLettersMask")]
143 const HasNonLettersMask = 1<<4;
144/// Compound token which may contain derived subtokens.
145/// Its subtokens and derived subtokens can be obtained by calling
146/// CFStringTokenizerGetCurrentSubTokens.
147 #[doc(alias = "kCFStringTokenizerTokenIsCJWordMask")]
148 const IsCJWordMask = 1<<5;
149 }
150}
151
152#[cfg(all(feature = "CFBase", feature = "objc2"))]
153unsafe impl Encode for CFStringTokenizerTokenType {
154 const ENCODING: Encoding = CFOptionFlags::ENCODING;
155}
156
157#[cfg(all(feature = "CFBase", feature = "objc2"))]
158unsafe impl RefEncode for CFStringTokenizerTokenType {
159 const ENCODING_REF: Encoding = Encoding::Pointer(&Self::ENCODING);
160}
161
162#[cfg(feature = "CFBase")]
163unsafe impl ConcreteType for CFStringTokenizer {
164 /// Get the type identifier.
165 ///
166 /// Returns: the type identifier of all CFStringTokenizer instances.
167 #[doc(alias = "CFStringTokenizerGetTypeID")]
168 #[inline]
169 fn type_id() -> CFTypeID {
170 extern "C-unwind" {
171 fn CFStringTokenizerGetTypeID() -> CFTypeID;
172 }
173 unsafe { CFStringTokenizerGetTypeID() }
174 }
175}
176
177/// Creates a tokenizer instance.
178///
179/// Parameter `alloc`: The CFAllocator which should be used to allocate memory for the
180/// tokenizer and its storage for values. This parameter may be NULL in which
181/// case the current default CFAllocator is used.
182///
183/// Parameter `string`: The string to tokenize.
184///
185/// Parameter `range`: The range of characters within the string to be tokenized. The
186/// specified range must not exceed the length of the string.
187///
188/// Parameter `options`: Use one of the Tokenization Unit options to specify how the
189/// string should be tokenized. Optionally specify one or more attribute
190/// specifiers to tell the tokenizer to prepare specified attributes when it
191/// tokenizes the string.
192///
193/// Parameter `locale`: The locale to specify language or region specific behavior. Pass
194/// NULL if you want tokenizer to identify the locale automatically.
195///
196/// Returns: A reference to the new CFStringTokenizer.
197#[cfg(all(feature = "CFBase", feature = "CFLocale"))]
198#[inline]
199pub unsafe extern "C-unwind" fn CFStringTokenizerCreate(
200 alloc: Option<&CFAllocator>,
201 string: Option<&CFString>,
202 range: CFRange,
203 options: CFOptionFlags,
204 locale: Option<&CFLocale>,
205) -> Option<CFRetained<CFStringTokenizer>> {
206 extern "C-unwind" {
207 fn CFStringTokenizerCreate(
208 alloc: Option<&CFAllocator>,
209 string: Option<&CFString>,
210 range: CFRange,
211 options: CFOptionFlags,
212 locale: Option<&CFLocale>,
213 ) -> Option<NonNull<CFStringTokenizer>>;
214 }
215 let ret = unsafe { CFStringTokenizerCreate(alloc, string, range, options, locale) };
216 ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
217}
218
219extern "C-unwind" {
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 #[cfg(feature = "CFBase")]
230 pub fn CFStringTokenizerSetString(
231 tokenizer: &CFStringTokenizer,
232 string: Option<&CFString>,
233 range: CFRange,
234 );
235}
236
237extern "C-unwind" {
238 /// Random access to a token. Find a token that includes the character specified
239 /// by character index, and set it as the current token.
240 ///
241 /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
242 /// CFStringTokenizerCreate.
243 ///
244 /// Parameter `index`: The index of the Unicode character in the CFString.
245 ///
246 /// Returns: Type of the token if succeeded in finding a token and setting it as
247 /// current token. kCFStringTokenizerTokenNone if failed in finding a token.
248 ///
249 /// The range and attribute of the token can be obtained by calling
250 /// CFStringTokenizerGetCurrentTokenRange and CFStringTokenizerCopyCurrentTokenAttribute.
251 /// If the token is a compound (with type kCFStringTokenizerTokenHasSubTokensMask or
252 /// kCFStringTokenizerTokenHasDerivedSubTokensMask), its subtokens and
253 /// (or) derived subtokens can be obtained by calling CFStringTokenizerGetCurrentSubTokens.
254 #[cfg(feature = "CFBase")]
255 pub fn CFStringTokenizerGoToTokenAtIndex(
256 tokenizer: &CFStringTokenizer,
257 index: CFIndex,
258 ) -> CFStringTokenizerTokenType;
259}
260
261extern "C-unwind" {
262 /// Token enumerator.
263 ///
264 /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
265 /// CFStringTokenizerCreate.
266 ///
267 /// Returns: Type of the token if succeeded in finding a token and setting it as
268 /// current token. kCFStringTokenizerTokenNone if failed in finding a token.
269 ///
270 /// If there is no preceding call to CFStringTokenizerGoToTokenAtIndex
271 /// or CFStringTokenizerAdvanceToNextToken, it finds the first token in the range
272 /// specified to CFStringTokenizerCreate. If there is a current token after successful
273 /// call to CFStringTokenizerGoToTokenAtIndex or CFStringTokenizerAdvanceToNextToken,
274 /// it proceeds to the next token. If succeeded in finding a token, set it as current
275 /// token and return its token type. Otherwise invalidate current token and return
276 /// kCFStringTokenizerTokenNone.
277 /// The range and attribute of the token can be obtained by calling
278 /// CFStringTokenizerGetCurrentTokenRange and
279 /// CFStringTokenizerCopyCurrentTokenAttribute. If the token is a compound
280 /// (with type kCFStringTokenizerTokenHasSubTokensMask or
281 /// kCFStringTokenizerTokenHasDerivedSubTokensMask), its subtokens and
282 /// (or) derived subtokens can be obtained by calling CFStringTokenizerGetCurrentSubTokens.
283 #[cfg(feature = "CFBase")]
284 pub fn CFStringTokenizerAdvanceToNextToken(
285 tokenizer: &CFStringTokenizer,
286 ) -> CFStringTokenizerTokenType;
287}
288
289extern "C-unwind" {
290 /// Returns the range of current token.
291 ///
292 /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
293 /// CFStringTokenizerCreate.
294 ///
295 /// Returns: Range of current token, or {kCFNotFound,0} if there is no current token.
296 #[cfg(feature = "CFBase")]
297 pub fn CFStringTokenizerGetCurrentTokenRange(tokenizer: &CFStringTokenizer) -> CFRange;
298}
299
300/// Copies the specified attribute of current token.
301///
302/// Parameter `tokenizer`: The reference to CFStringTokenizer returned by
303/// CFStringTokenizerCreate.
304///
305/// Parameter `attribute`: Specify a token attribute you want to obtain. The value is
306/// one of kCFStringTokenizerAttributeLatinTranscription or
307/// kCFStringTokenizerAttributeLanguage.
308///
309/// Returns: Token attribute, or NULL if current token does not have the specified
310/// attribute or if there is no current token.
311#[cfg(feature = "CFBase")]
312#[inline]
313pub unsafe extern "C-unwind" fn CFStringTokenizerCopyCurrentTokenAttribute(
314 tokenizer: &CFStringTokenizer,
315 attribute: CFOptionFlags,
316) -> Option<CFRetained<CFType>> {
317 extern "C-unwind" {
318 fn CFStringTokenizerCopyCurrentTokenAttribute(
319 tokenizer: &CFStringTokenizer,
320 attribute: CFOptionFlags,
321 ) -> Option<NonNull<CFType>>;
322 }
323 let ret = unsafe { CFStringTokenizerCopyCurrentTokenAttribute(tokenizer, attribute) };
324 ret.map(|ret| unsafe { CFRetained::from_raw(ret) })
325}
326
327extern "C-unwind" {
328 /// Retrieves the subtokens or derived subtokens contained in the compound token.
329 ///
330 /// Parameter `tokenizer`: The reference to CFStringTokenizer returned by CFStringTokenizerCreate.
331 ///
332 /// Parameter `ranges`: An array of CFRange to fill in with the ranges of subtokens. The filled in
333 /// ranges are relative to the string specified to CFStringTokenizerCreate. This parameter
334 /// can be NULL.
335 ///
336 /// Parameter `maxRangeLength`: The maximum number of ranges to return.
337 ///
338 /// Parameter `derivedSubTokens`: An array of CFMutableArray to which the derived subtokens are to
339 /// be added. This parameter can be NULL.
340 ///
341 /// Returns: number of subtokens.
342 ///
343 /// If token type is kCFStringTokenizerTokenNone, the ranges array and
344 /// derivedSubTokens array are untouched and the return value is 0.
345 /// If token type is kCFStringTokenizerTokenNormal, the ranges array has one item
346 /// filled in with the entire range of the token (if maxRangeLength >= 1) and a string
347 /// taken from the entire token range is added to the derivedSubTokens array and the
348 /// return value is 1.
349 /// If token type is kCFStringTokenizerTokenHasSubTokensMask or
350 /// kCFStringTokenizerTokenHasDerivedSubTokensMask, the ranges array is filled
351 /// in with as many items as there are subtokens (up to a limit of maxRangeLength).
352 /// The derivedSubTokens array will have sub tokens added even when the sub token is a
353 /// substring of the token. If token type is kCFStringTokenizerTokenHasSubTokensMask,
354 /// the ordinary non-derived subtokens are added to the derivedSubTokens array.
355 #[cfg(all(feature = "CFArray", feature = "CFBase"))]
356 pub fn CFStringTokenizerGetCurrentSubTokens(
357 tokenizer: &CFStringTokenizer,
358 ranges: *mut CFRange,
359 max_range_length: CFIndex,
360 derived_sub_tokens: Option<&CFMutableArray>,
361 ) -> CFIndex;
362}