]> git.saurik.com Git - apple/cf.git/blame - CFString.h
CF-635.15.tar.gz
[apple/cf.git] / CFString.h
CommitLineData
9ce05555 1/*
8ca704e1 2 * Copyright (c) 2011 Apple Inc. All rights reserved.
9ce05555
A
3 *
4 * @APPLE_LICENSE_HEADER_START@
5 *
9ce05555
A
6 * This file contains Original Code and/or Modifications of Original Code
7 * as defined in and that are subject to the Apple Public Source License
8 * Version 2.0 (the 'License'). You may not use this file except in
9 * compliance with the License. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
11 * file.
12 *
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
20 *
21 * @APPLE_LICENSE_HEADER_END@
22 */
f64f9b69 23
9ce05555 24/* CFString.h
8ca704e1 25 Copyright (c) 1998-2011, Apple Inc. All rights reserved.
9ce05555
A
26*/
27
28#if !defined(__COREFOUNDATION_CFSTRING__)
29#define __COREFOUNDATION_CFSTRING__ 1
30
31#include <CoreFoundation/CFBase.h>
32#include <CoreFoundation/CFArray.h>
33#include <CoreFoundation/CFData.h>
34#include <CoreFoundation/CFDictionary.h>
35#include <CoreFoundation/CFCharacterSet.h>
36#include <CoreFoundation/CFLocale.h>
37#include <stdarg.h>
38
bd5b749c 39CF_EXTERN_C_BEGIN
9ce05555
A
40
41/*
42Please note: CFStrings are conceptually an array of Unicode characters.
43However, in general, how a CFString stores this array is an implementation
cf7d2af9
A
44detail. For instance, CFString might choose to use an array of 8-bit characters
45to store its contents, or it might use multiple blocks of memory, or whatever.
46This is especially true since CFString is toll-free bridged with NSString, enabling
47any NSString instance to be used as a CFString. Furthermore, the implementation
48may change depending on the default system encoding, the user's language,
49or even a release or update of the OS.
9ce05555
A
50
51What this means is that you should use the following advanced functions with care:
52
53 CFStringGetPascalStringPtr()
54 CFStringGetCStringPtr()
55 CFStringGetCharactersPtr()
56
57These functions are provided for optimization only. They will either return the desired
58pointer quickly, in constant time, or they return NULL. They might choose to return NULL
59for many reasons; for instance it's possible that for users running in different
60languages these sometimes return NULL; or in a future OS release the first two might
61switch to always returning NULL. Never observing NULL returns in your usages of these
62functions does not mean they won't ever return NULL. (But note the CFStringGetCharactersPtr()
63exception mentioned further below.)
64
65In your usages of these functions, if you get a NULL return, use the non-Ptr version
66of the functions as shown in this example:
67
cf7d2af9
A
68 char buffer[BUFSIZE];
69 const char *ptr = CFStringGetCStringPtr(str, encoding);
9ce05555 70 if (ptr == NULL) {
cf7d2af9 71 if (CFStringGetCString(str, buffer, BUFSIZE, encoding)) ptr = buffer;
9ce05555
A
72 }
73
cf7d2af9 74Note that CFStringGetCString() or CFStringGetPascalString() calls might still fail --- but
9ce05555
A
75that will happen in two circumstances only: The conversion from the UniChar contents of CFString
76to the specified encoding fails, or the buffer is too small. If they fail, that means
77the conversion was not possible.
78
cf7d2af9
A
79If you need a copy of the buffer in the above example, you might consider simply calling
80CFStringGetCString() in all cases --- CFStringGetCStringPtr() is simply an optimization.
9ce05555
A
81
82In addition, the following functions, which create immutable CFStrings from developer
83supplied buffers without copying the buffers, might have to actually copy
84under certain circumstances (If they do copy, the buffer will be dealt with by the
85"contentsDeallocator" argument.):
86
87 CFStringCreateWithPascalStringNoCopy()
88 CFStringCreateWithCStringNoCopy()
89 CFStringCreateWithCharactersNoCopy()
90
91You should of course never depend on the backing store of these CFStrings being
92what you provided, and in other no circumstance should you change the contents
93of that buffer (given that would break the invariant about the CFString being immutable).
94
95Having said all this, there are actually ways to create a CFString where the backing store
96is external, and can be manipulated by the developer or CFString itself:
97
98 CFStringCreateMutableWithExternalCharactersNoCopy()
99 CFStringSetExternalCharactersNoCopy()
100
101A "contentsAllocator" is used to realloc or free the backing store by CFString.
102kCFAllocatorNull can be provided to assure CFString will never realloc or free the buffer.
103Developer can call CFStringSetExternalCharactersNoCopy() to update
104CFString's idea of what's going on, if the buffer is changed externally. In these
105strings, CFStringGetCharactersPtr() is guaranteed to return the external buffer.
106
107These functions are here to allow wrapping a buffer of UniChar characters in a CFString,
108allowing the buffer to passed into CFString functions and also manipulated via CFString
109mutation functions. In general, developers should not use this technique for all strings,
110as it prevents CFString from using certain optimizations.
111*/
112
113/* Identifier for character encoding; the values are the same as Text Encoding Converter TextEncoding.
114*/
115typedef UInt32 CFStringEncoding;
116
117/* Platform-independent built-in encodings; always available on all platforms.
118 Call CFStringGetSystemEncoding() to get the default system encoding.
119*/
120#define kCFStringEncodingInvalidId (0xffffffffU)
bd5b749c 121enum {
9ce05555
A
122 kCFStringEncodingMacRoman = 0,
123 kCFStringEncodingWindowsLatin1 = 0x0500, /* ANSI codepage 1252 */
124 kCFStringEncodingISOLatin1 = 0x0201, /* ISO 8859-1 */
125 kCFStringEncodingNextStepLatin = 0x0B01, /* NextStep encoding*/
126 kCFStringEncodingASCII = 0x0600, /* 0..127 (in creating CFString, values greater than 0x7F are treated as corresponding Unicode value) */
127 kCFStringEncodingUnicode = 0x0100, /* kTextEncodingUnicodeDefault + kTextEncodingDefaultFormat (aka kUnicode16BitFormat) */
128 kCFStringEncodingUTF8 = 0x08000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF8Format */
8ca704e1
A
129 kCFStringEncodingNonLossyASCII = 0x0BFF, /* 7bit Unicode variants used by Cocoa & Java */
130
d8925383
A
131 kCFStringEncodingUTF16 = 0x0100, /* kTextEncodingUnicodeDefault + kUnicodeUTF16Format (alias of kCFStringEncodingUnicode) */
132 kCFStringEncodingUTF16BE = 0x10000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF16BEFormat */
133 kCFStringEncodingUTF16LE = 0x14000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF16LEFormat */
134
135 kCFStringEncodingUTF32 = 0x0c000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF32Format */
136 kCFStringEncodingUTF32BE = 0x18000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF32BEFormat */
137 kCFStringEncodingUTF32LE = 0x1c000100 /* kTextEncodingUnicodeDefault + kUnicodeUTF32LEFormat */
bd5b749c
A
138};
139typedef CFStringEncoding CFStringBuiltInEncodings;
9ce05555 140
cf7d2af9 141
9ce05555
A
142/* CFString type ID */
143CF_EXPORT
144CFTypeID CFStringGetTypeID(void);
145
cf7d2af9
A
146/* CFSTR() allows creation of compile-time constant CFStringRefs; the argument
147should be a constant C-string.
9ce05555
A
148
149CFSTR(), not being a "Copy" or "Create" function, does not return a new
150reference for you. So, you should not release the return value. This is
151much like constant C or Pascal strings --- when you use "hello world"
152in a program, you do not free it.
153
154However, strings returned from CFSTR() can be retained and released in a
155properly nested fashion, just like any other CF type. That is, if you pass
156a CFSTR() return value to a function such as SetMenuItemWithCFString(), the
157function can retain it, then later, when it's done with it, it can release it.
158
cf7d2af9
A
159Non-7 bit characters (that is, above 127) in CFSTR() are supported, although care must
160be taken in dealing with files containing them. If you can trust your editor and tools
161to deal with non-ASCII characters in the source code, then you can use them directly
162in CFSTR(); otherwise, you can represent such characters with their escaped octal
163equivalents in the encoding the compiler will use to interpret them (for instance,
164O-umlaut is \303\226 in UTF-8). UTF-8 is the recommended encoding here,
165since it is the default choice with Mac OS X developer tools.
9ce05555 166*/
8ca704e1 167#if TARGET_OS_WIN32 || TARGET_OS_LINUX
cf7d2af9
A
168#undef __CONSTANT_CFSTRINGS__
169#endif
170
9ce05555
A
171#ifdef __CONSTANT_CFSTRINGS__
172#define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr ""))
173#else
174#define CFSTR(cStr) __CFStringMakeConstantString("" cStr "")
175#endif
176
8ca704e1 177#if defined(__GNUC__) && (__GNUC__*10+__GNUC_MINOR__ >= 42) && defined(__APPLE_CC__) && (__APPLE_CC__ > 1) && !defined(__INTEL_COMPILER) && (TARGET_OS_MAC || TARGET_OS_EMBEDDED)
cf7d2af9
A
178#define CF_FORMAT_FUNCTION(F,A) __attribute__((format(CFString, F, A)))
179#define CF_FORMAT_ARGUMENT(A) __attribute__((format_arg(A)))
180#else
181#define CF_FORMAT_FUNCTION(F,A)
182#define CF_FORMAT_ARGUMENT(A)
183#endif
184
9ce05555
A
185/*** Immutable string creation functions ***/
186
187/* Functions to create basic immutable strings. The provided allocator is used for all memory activity in these functions.
188*/
189
bd5b749c 190/* The following four functions copy the provided buffer into CFString's internal storage. */
9ce05555
A
191CF_EXPORT
192CFStringRef CFStringCreateWithPascalString(CFAllocatorRef alloc, ConstStr255Param pStr, CFStringEncoding encoding);
193
194CF_EXPORT
195CFStringRef CFStringCreateWithCString(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding);
196
bd5b749c
A
197/* The following takes an explicit length, and allows you to specify whether the data is an external format --- that is, whether to pay attention to the BOM character (if any) and do byte swapping if necessary
198*/
199CF_EXPORT
200CFStringRef CFStringCreateWithBytes(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean isExternalRepresentation);
201
9ce05555
A
202CF_EXPORT
203CFStringRef CFStringCreateWithCharacters(CFAllocatorRef alloc, const UniChar *chars, CFIndex numChars);
204
bd5b749c 205
9ce05555
A
206/* These functions try not to copy the provided buffer. The buffer will be deallocated
207with the provided contentsDeallocator when it's no longer needed; to not free
208the buffer, specify kCFAllocatorNull here. As usual, NULL means default allocator.
209
210NOTE: Do not count on these buffers as being used by the string;
211in some cases the CFString might free the buffer and use something else
212(for instance if it decides to always use Unicode encoding internally).
213
214NOTE: If you are not transferring ownership of the buffer to the CFString
215(for instance, you supplied contentsDeallocator = kCFAllocatorNull), it is your
216responsibility to assure the buffer does not go away during the lifetime of the string.
217If the string is retained or copied, its lifetime might extend in ways you cannot
218predict. So, for strings created with buffers whose lifetimes you cannot
219guarantee, you need to be extremely careful --- do not hand it out to any
220APIs which might retain or copy the strings.
221*/
222CF_EXPORT
223CFStringRef CFStringCreateWithPascalStringNoCopy(CFAllocatorRef alloc, ConstStr255Param pStr, CFStringEncoding encoding, CFAllocatorRef contentsDeallocator);
224
225CF_EXPORT
226CFStringRef CFStringCreateWithCStringNoCopy(CFAllocatorRef alloc, const char *cStr, CFStringEncoding encoding, CFAllocatorRef contentsDeallocator);
227
bd5b749c
A
228/* The following takes an explicit length, and allows you to specify whether the data is an external format --- that is, whether to pay attention to the BOM character (if any) and do byte swapping if necessary
229*/
230CF_EXPORT
8ca704e1 231CFStringRef CFStringCreateWithBytesNoCopy(CFAllocatorRef alloc, const UInt8 *bytes, CFIndex numBytes, CFStringEncoding encoding, Boolean isExternalRepresentation, CFAllocatorRef contentsDeallocator);
bd5b749c 232
9ce05555
A
233CF_EXPORT
234CFStringRef CFStringCreateWithCharactersNoCopy(CFAllocatorRef alloc, const UniChar *chars, CFIndex numChars, CFAllocatorRef contentsDeallocator);
235
236/* Create copies of part or all of the string.
237*/
238CF_EXPORT
239CFStringRef CFStringCreateWithSubstring(CFAllocatorRef alloc, CFStringRef str, CFRange range);
240
241CF_EXPORT
242CFStringRef CFStringCreateCopy(CFAllocatorRef alloc, CFStringRef theString);
243
244/* These functions create a CFString from the provided printf-like format string and arguments.
245*/
246CF_EXPORT
cf7d2af9 247CFStringRef CFStringCreateWithFormat(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, ...) CF_FORMAT_FUNCTION(3,4);
9ce05555
A
248
249CF_EXPORT
cf7d2af9 250CFStringRef CFStringCreateWithFormatAndArguments(CFAllocatorRef alloc, CFDictionaryRef formatOptions, CFStringRef format, va_list arguments) CF_FORMAT_FUNCTION(3,0);
9ce05555
A
251
252/* Functions to create mutable strings. "maxLength", if not 0, is a hard bound on the length of the string. If 0, there is no limit on the length.
253*/
254CF_EXPORT
255CFMutableStringRef CFStringCreateMutable(CFAllocatorRef alloc, CFIndex maxLength);
256
257CF_EXPORT
258CFMutableStringRef CFStringCreateMutableCopy(CFAllocatorRef alloc, CFIndex maxLength, CFStringRef theString);
259
260/* This function creates a mutable string that has a developer supplied and directly editable backing store.
261The string will be manipulated within the provided buffer (if any) until it outgrows capacity; then the
262externalCharactersAllocator will be consulted for more memory. When the CFString is deallocated, the
263buffer will be freed with the externalCharactersAllocator. Provide kCFAllocatorNull here to prevent the buffer
264from ever being reallocated or deallocated by CFString. See comments at top of this file for more info.
265*/
266CF_EXPORT
267CFMutableStringRef CFStringCreateMutableWithExternalCharactersNoCopy(CFAllocatorRef alloc, UniChar *chars, CFIndex numChars, CFIndex capacity, CFAllocatorRef externalCharactersAllocator);
268
269/*** Basic accessors for the contents ***/
270
271/* Number of 16-bit Unicode characters in the string.
272*/
273CF_EXPORT
274CFIndex CFStringGetLength(CFStringRef theString);
275
276/* Extracting the contents of the string. For obtaining multiple characters, calling
277CFStringGetCharacters() is more efficient than multiple calls to CFStringGetCharacterAtIndex().
278If the length of the string is not known (so you can't use a fixed size buffer for CFStringGetCharacters()),
279another method is to use is CFStringGetCharacterFromInlineBuffer() (see further below).
280*/
281CF_EXPORT
282UniChar CFStringGetCharacterAtIndex(CFStringRef theString, CFIndex idx);
283
284CF_EXPORT
285void CFStringGetCharacters(CFStringRef theString, CFRange range, UniChar *buffer);
286
d8925383 287
9ce05555
A
288/*** Conversion to other encodings ***/
289
290/* These two convert into the provided buffer; they return false if conversion isn't possible
291(due to conversion error, or not enough space in the provided buffer).
292These functions do zero-terminate or put the length byte; the provided bufferSize should include
293space for this (so pass 256 for Str255). More sophisticated usages can go through CFStringGetBytes().
294These functions are equivalent to calling CFStringGetBytes() with
295the range of the string; lossByte = 0; and isExternalRepresentation = false;
bd5b749c 296if successful, they then insert the leading length or terminating zero, as desired.
9ce05555
A
297*/
298CF_EXPORT
299Boolean CFStringGetPascalString(CFStringRef theString, StringPtr buffer, CFIndex bufferSize, CFStringEncoding encoding);
300
301CF_EXPORT
302Boolean CFStringGetCString(CFStringRef theString, char *buffer, CFIndex bufferSize, CFStringEncoding encoding);
303
304/* These functions attempt to return in O(1) time the desired format for the string.
305Note that although this means a pointer to the internal structure is being returned,
306this can't always be counted on. Please see note at the top of the file for more
307details.
308*/
309CF_EXPORT
310ConstStringPtr CFStringGetPascalStringPtr(CFStringRef theString, CFStringEncoding encoding); /* May return NULL at any time; be prepared for NULL */
311
312CF_EXPORT
313const char *CFStringGetCStringPtr(CFStringRef theString, CFStringEncoding encoding); /* May return NULL at any time; be prepared for NULL */
314
315CF_EXPORT
316const UniChar *CFStringGetCharactersPtr(CFStringRef theString); /* May return NULL at any time; be prepared for NULL */
317
318/* The primitive conversion routine; allows you to convert a string piece at a time
cf7d2af9 319 into a fixed size buffer. Returns number of characters converted.
9ce05555 320 Characters that cannot be converted to the specified encoding are represented
cf7d2af9
A
321 with the byte specified by lossByte; if lossByte is 0, then lossy conversion
322 is not allowed and conversion stops, returning partial results.
9ce05555 323 Pass buffer==NULL if you don't care about the converted string (but just the convertability,
cf7d2af9 324 or number of bytes required).
9ce05555
A
325 maxBufLength indicates the maximum number of bytes to generate. It is ignored when buffer==NULL.
326 Does not zero-terminate. If you want to create Pascal or C string, allow one extra byte at start or end.
327 Setting isExternalRepresentation causes any extra bytes that would allow
cf7d2af9
A
328 the data to be made persistent to be included; for instance, the Unicode BOM. Note that
329 CFString prepends UTF encoded data with the Unicode BOM <http://www.unicode.org/faq/utf_bom.html>
330 when generating external representation if the target encoding allows. It's important to note that
331 only UTF-8, UTF-16, and UTF-32 define the handling of the byte order mark character, and the "LE"
332 and "BE" variants of UTF-16 and UTF-32 don't.
9ce05555
A
333*/
334CF_EXPORT
335CFIndex CFStringGetBytes(CFStringRef theString, CFRange range, CFStringEncoding encoding, UInt8 lossByte, Boolean isExternalRepresentation, UInt8 *buffer, CFIndex maxBufLen, CFIndex *usedBufLen);
336
9ce05555 337/* Convenience functions String <-> Data. These generate "external" formats, that is, formats that
cf7d2af9
A
338 can be written out to disk. For instance, if the encoding is Unicode,
339 CFStringCreateFromExternalRepresentation() pays attention to the BOM character (if any)
340 and does byte swapping if necessary. Similarly CFStringCreateExternalRepresentation() will
341 include a BOM character if appropriate. See CFStringGetBytes() for more on this and lossByte.
9ce05555
A
342*/
343CF_EXPORT
344CFStringRef CFStringCreateFromExternalRepresentation(CFAllocatorRef alloc, CFDataRef data, CFStringEncoding encoding); /* May return NULL on conversion error */
345
346CF_EXPORT
347CFDataRef CFStringCreateExternalRepresentation(CFAllocatorRef alloc, CFStringRef theString, CFStringEncoding encoding, UInt8 lossByte); /* May return NULL on conversion error */
348
349/* Hints about the contents of a string
350*/
351CF_EXPORT
352CFStringEncoding CFStringGetSmallestEncoding(CFStringRef theString); /* Result in O(n) time max */
353
354CF_EXPORT
355CFStringEncoding CFStringGetFastestEncoding(CFStringRef theString); /* Result in O(1) time max */
356
357/* General encoding info
358*/
359CF_EXPORT
360CFStringEncoding CFStringGetSystemEncoding(void); /* The default encoding for the system; untagged 8-bit characters are usually in this encoding */
361
362CF_EXPORT
363CFIndex CFStringGetMaximumSizeForEncoding(CFIndex length, CFStringEncoding encoding); /* Max bytes a string of specified length (in UniChars) will take up if encoded */
364
d8925383
A
365
366/*** FileSystem path conversion functions ***/
367
bd5b749c 368/* Extract the contents of the string as a NULL-terminated 8-bit string appropriate for passing to POSIX APIs (for example, normalized for HFS+). The string is zero-terminated. false will be returned if the conversion results don't fit into the buffer. Use CFStringGetMaximumSizeOfFileSystemRepresentation() if you want to make sure the buffer is of sufficient length.
d8925383
A
369*/
370CF_EXPORT
8ca704e1 371Boolean CFStringGetFileSystemRepresentation(CFStringRef string, char *buffer, CFIndex maxBufLen);
d8925383
A
372
373/* Get the upper bound on the number of bytes required to hold the file system representation for the string. This result is returned quickly as a very rough approximation, and could be much larger than the actual space required. The result includes space for the zero termination. If you are allocating a buffer for long-term keeping, it's recommended that you reallocate it smaller (to be the right size) after calling CFStringGetFileSystemRepresentation().
374*/
375CF_EXPORT
8ca704e1 376CFIndex CFStringGetMaximumSizeOfFileSystemRepresentation(CFStringRef string);
d8925383
A
377
378/* Create a CFString from the specified zero-terminated POSIX file system representation. If the conversion fails (possible due to bytes in the buffer not being a valid sequence of bytes for the appropriate character encoding), NULL is returned.
379*/
380CF_EXPORT
8ca704e1 381CFStringRef CFStringCreateWithFileSystemRepresentation(CFAllocatorRef alloc, const char *buffer);
d8925383
A
382
383
9ce05555
A
384/*** Comparison functions. ***/
385
cf7d2af9 386/* Find and compare flags; these are OR'ed together and provided as CFStringCompareFlags in the various functions.
9ce05555 387*/
bd5b749c 388enum {
9ce05555
A
389 kCFCompareCaseInsensitive = 1,
390 kCFCompareBackwards = 4, /* Starting from the end of the string */
391 kCFCompareAnchored = 8, /* Only at the specified starting point */
392 kCFCompareNonliteral = 16, /* If specified, loose equivalence is performed (o-umlaut == o, umlaut) */
393 kCFCompareLocalized = 32, /* User's default locale is used for the comparisons */
394 kCFCompareNumerically = 64 /* Numeric comparison is used; that is, Foo2.txt < Foo7.txt < Foo25.txt */
cf7d2af9 395#if MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED
bd5b749c
A
396 ,
397 kCFCompareDiacriticInsensitive = 128, /* If specified, ignores diacritics (o-umlaut == o) */
398 kCFCompareWidthInsensitive = 256, /* If specified, ignores width differences ('a' == UFF41) */
399 kCFCompareForcedOrdering = 512 /* If specified, comparisons are forced to return either kCFCompareLessThan or kCFCompareGreaterThan if the strings are equivalent but not strictly equal, for stability when sorting (e.g. "aaa" > "AAA" with kCFCompareCaseInsensitive specified) */
cf7d2af9 400#endif /* MAC_OS_X_VERSION_10_5 <= MAC_OS_X_VERSION_MAX_ALLOWED */
bd5b749c
A
401};
402typedef CFOptionFlags CFStringCompareFlags;
9ce05555
A
403
404/* The main comparison routine; compares specified range of the first string to (the full range of) the second string.
bd5b749c
A
405locale == NULL indicates canonical locale (the return value from CFLocaleGetSystem()).
406kCFCompareNumerically, added in 10.2, does not work if kCFCompareLocalized is specified on systems before 10.3
407kCFCompareBackwards and kCFCompareAnchored are not applicable.
408*/
bd5b749c 409CF_EXPORT
8ca704e1 410CFComparisonResult CFStringCompareWithOptionsAndLocale(CFStringRef theString1, CFStringRef theString2, CFRange rangeToCompare, CFStringCompareFlags compareOptions, CFLocaleRef locale) CF_AVAILABLE(10_5, 2_0);
bd5b749c
A
411
412/* Comparison convenience. Uses the current user locale (the return value from CFLocaleCopyCurrent()) if kCFCompareLocalized.
9ce05555
A
413*/
414CF_EXPORT
cf7d2af9 415CFComparisonResult CFStringCompareWithOptions(CFStringRef theString1, CFStringRef theString2, CFRange rangeToCompare, CFStringCompareFlags compareOptions);
9ce05555
A
416
417/* Comparison convenience suitable for passing as sorting functions.
418 kCFCompareNumerically, added in 10.2, does not work if kCFCompareLocalized is specified on systems before 10.3
419 kCFCompareBackwards and kCFCompareAnchored are not applicable.
420*/
421CF_EXPORT
cf7d2af9 422CFComparisonResult CFStringCompare(CFStringRef theString1, CFStringRef theString2, CFStringCompareFlags compareOptions);
9ce05555 423
bd5b749c
A
424/* CFStringFindWithOptionsAndLocale() returns the found range in the CFRange * argument; you can pass NULL for simple discovery check.
425 locale == NULL indicates canonical locale (the return value from CFLocaleGetSystem()).
426 If stringToFind is the empty string (zero length), nothing is found.
427 Ignores the kCFCompareNumerically option.
428*/
bd5b749c 429CF_EXPORT
8ca704e1 430Boolean CFStringFindWithOptionsAndLocale(CFStringRef theString, CFStringRef stringToFind, CFRange rangeToSearch, CFStringCompareFlags searchOptions, CFLocaleRef locale, CFRange *result) CF_AVAILABLE(10_5, 2_0);
bd5b749c
A
431
432/* Find convenience. Uses the current user locale (the return value from CFLocaleCopyCurrent()) if kCFCompareLocalized.
9ce05555
A
433*/
434CF_EXPORT
cf7d2af9 435Boolean CFStringFindWithOptions(CFStringRef theString, CFStringRef stringToFind, CFRange rangeToSearch, CFStringCompareFlags searchOptions, CFRange *result);
9ce05555
A
436
437/* CFStringCreateArrayWithFindResults() returns an array of CFRange pointers, or NULL if there are no matches.
438 Overlapping instances are not found; so looking for "AA" in "AAA" finds just one range.
439 Post 10.1: If kCFCompareBackwards is provided, the scan is done from the end (which can give a different result), and
440 the results are stored in the array backwards (last found range in slot 0).
441 If stringToFind is the empty string (zero length), nothing is found.
442 kCFCompareAnchored causes just the consecutive instances at start (or end, if kCFCompareBackwards) to be reported. So, searching for "AB" in "ABABXAB..." you just get the first two occurrences.
443 Ignores the kCFCompareNumerically option.
444*/
445CF_EXPORT
cf7d2af9 446CFArrayRef CFStringCreateArrayWithFindResults(CFAllocatorRef alloc, CFStringRef theString, CFStringRef stringToFind, CFRange rangeToSearch, CFStringCompareFlags compareOptions);
9ce05555
A
447
448/* Find conveniences; see comments above concerning empty string and options.
449*/
450CF_EXPORT
cf7d2af9 451CFRange CFStringFind(CFStringRef theString, CFStringRef stringToFind, CFStringCompareFlags compareOptions);
9ce05555
A
452
453CF_EXPORT
454Boolean CFStringHasPrefix(CFStringRef theString, CFStringRef prefix);
455
456CF_EXPORT
457Boolean CFStringHasSuffix(CFStringRef theString, CFStringRef suffix);
458
9ce05555
A
459/*!
460 @function CFStringGetRangeOfComposedCharactersAtIndex
461 Returns the range of the composed character sequence at the specified index.
462 @param theString The CFString which is to be searched. If this
463 parameter is not a valid CFString, the behavior is
464 undefined.
465 @param theIndex The index of the character contained in the
466 composed character sequence. If the index is
467 outside the index space of the string (0 to N-1 inclusive,
468 where N is the length of the string), the behavior is
469 undefined.
470 @result The range of the composed character sequence.
471*/
472CF_EXPORT CFRange CFStringGetRangeOfComposedCharactersAtIndex(CFStringRef theString, CFIndex theIndex);
473
474/*!
475 @function CFStringFindCharacterFromSet
476 Query the range of the first character contained in the specified character set.
477 @param theString The CFString which is to be searched. If this
478 parameter is not a valid CFString, the behavior is
479 undefined.
480 @param theSet The CFCharacterSet against which the membership
481 of characters is checked. If this parameter is not a valid
482 CFCharacterSet, the behavior is undefined.
483 @param range The range of characters within the string to search. If
484 the range location or end point (defined by the location
485 plus length minus 1) are outside the index space of the
486 string (0 to N-1 inclusive, where N is the length of the
487 string), the behavior is undefined. If the range length is
488 negative, the behavior is undefined. The range may be empty
489 (length 0), in which case no search is performed.
490 @param searchOptions The bitwise-or'ed option flags to control
491 the search behavior. The supported options are
492 kCFCompareBackwards andkCFCompareAnchored.
493 If other option flags are specified, the behavior
494 is undefined.
495 @param result The pointer to a CFRange supplied by the caller in
496 which the search result is stored. Note that the length
bd5b749c
A
497 of this range can be more than 1, if for instance the
498 result is a composed character. If a pointer to an invalid
9ce05555
A
499 memory is specified, the behavior is undefined.
500 @result true, if at least a character which is a member of the character
501 set is found and result is filled, otherwise, false.
502*/
cf7d2af9 503CF_EXPORT Boolean CFStringFindCharacterFromSet(CFStringRef theString, CFCharacterSetRef theSet, CFRange rangeToSearch, CFStringCompareFlags searchOptions, CFRange *result);
9ce05555
A
504
505/* Find range of bounds of the line(s) that span the indicated range (startIndex, numChars),
bd5b749c 506 taking into account various possible line separator sequences (CR, CRLF, LF, and Unicode NextLine, LineSeparator, ParagraphSeparator).
9ce05555 507 All return values are "optional" (provide NULL if you don't want them)
bd5b749c 508 lineBeginIndex: index of first character in line
9ce05555
A
509 lineEndIndex: index of first character of the next line (including terminating line separator characters)
510 contentsEndIndex: index of the first line separator character
bd5b749c
A
511 Thus, lineEndIndex - lineBeginIndex is the number of chars in the line, including the line separators
512 contentsEndIndex - lineBeginIndex is the number of chars in the line w/out the line separators
9ce05555
A
513*/
514CF_EXPORT
515void CFStringGetLineBounds(CFStringRef theString, CFRange range, CFIndex *lineBeginIndex, CFIndex *lineEndIndex, CFIndex *contentsEndIndex);
516
bd5b749c
A
517/* Same as CFStringGetLineBounds(), however, will only look for paragraphs. Won't stop at Unicode NextLine or LineSeparator characters.
518*/
519CF_EXPORT
8ca704e1
A
520void CFStringGetParagraphBounds(CFStringRef string, CFRange range, CFIndex *parBeginIndex, CFIndex *parEndIndex, CFIndex *contentsEndIndex) CF_AVAILABLE(10_5, 2_0);
521
522/*!
523 @function CFStringGetHyphenationLocationBeforeIndex
524 Retrieve the first potential hyphenation location found before the specified location.
525 @param string The CFString which is to be hyphenated. If this
526 parameter is not a valid CFString, the behavior is
527 undefined.
528 @param location An index in the string. If a valid hyphen index is returned, it
529 will be before this index.
530 @param limitRange The range of characters within the string to search. If
531 the range location or end point (defined by the location
532 plus length minus 1) are outside the index space of the
533 string (0 to N-1 inclusive, where N is the length of the
534 string), the behavior is undefined. If the range length is
535 negative, the behavior is undefined. The range may be empty
536 (length 0), in which case no hyphen location is generated.
537 @param options Reserved for future use.
538 @param locale Specifies which language's hyphenation conventions to use.
539 This must be a valid locale. Hyphenation data is not available
540 for all locales. You can use CFStringIsHyphenationAvailableForLocale
541 to test for availability of hyphenation data.
542 @param character The suggested hyphen character to insert. Pass NULL if you
543 do not need this information.
544 @result an index in the string where it is appropriate to insert a hyphen, if
545 one exists; else kCFNotFound
546*/
547CF_EXPORT
548CFIndex CFStringGetHyphenationLocationBeforeIndex(CFStringRef string, CFIndex location, CFRange limitRange, CFOptionFlags options, CFLocaleRef locale, UTF32Char *character) CF_AVAILABLE(10_7, 4_2);
549
550CF_EXPORT
551Boolean CFStringIsHyphenationAvailableForLocale(CFLocaleRef locale) CF_AVAILABLE(10_7, 4_3);
d8925383 552
9ce05555
A
553/*** Exploding and joining strings with a separator string ***/
554
555CF_EXPORT
556CFStringRef CFStringCreateByCombiningStrings(CFAllocatorRef alloc, CFArrayRef theArray, CFStringRef separatorString); /* Empty array returns empty string; one element array returns the element */
557
558CF_EXPORT
559CFArrayRef CFStringCreateArrayBySeparatingStrings(CFAllocatorRef alloc, CFStringRef theString, CFStringRef separatorString); /* No separators in the string returns array with that string; string == sep returns two empty strings */
560
d8925383 561
9ce05555
A
562/*** Parsing non-localized numbers from strings ***/
563
564CF_EXPORT
565SInt32 CFStringGetIntValue(CFStringRef str); /* Skips whitespace; returns 0 on error, MAX or -MAX on overflow */
566
567CF_EXPORT
568double CFStringGetDoubleValue(CFStringRef str); /* Skips whitespace; returns 0.0 on error */
569
d8925383 570
9ce05555
A
571/*** MutableString functions ***/
572
573/* CFStringAppend("abcdef", "xxxxx") -> "abcdefxxxxx"
574 CFStringDelete("abcdef", CFRangeMake(2, 3)) -> "abf"
575 CFStringReplace("abcdef", CFRangeMake(2, 3), "xxxxx") -> "abxxxxxf"
576 CFStringReplaceAll("abcdef", "xxxxx") -> "xxxxx"
577*/
578CF_EXPORT
579void CFStringAppend(CFMutableStringRef theString, CFStringRef appendedString);
580
581CF_EXPORT
582void CFStringAppendCharacters(CFMutableStringRef theString, const UniChar *chars, CFIndex numChars);
583
584CF_EXPORT
585void CFStringAppendPascalString(CFMutableStringRef theString, ConstStr255Param pStr, CFStringEncoding encoding);
586
587CF_EXPORT
588void CFStringAppendCString(CFMutableStringRef theString, const char *cStr, CFStringEncoding encoding);
589
590CF_EXPORT
cf7d2af9 591void CFStringAppendFormat(CFMutableStringRef theString, CFDictionaryRef formatOptions, CFStringRef format, ...) CF_FORMAT_FUNCTION(3,4);
9ce05555
A
592
593CF_EXPORT
cf7d2af9 594void CFStringAppendFormatAndArguments(CFMutableStringRef theString, CFDictionaryRef formatOptions, CFStringRef format, va_list arguments) CF_FORMAT_FUNCTION(3,0);
9ce05555
A
595
596CF_EXPORT
597void CFStringInsert(CFMutableStringRef str, CFIndex idx, CFStringRef insertedStr);
598
599CF_EXPORT
600void CFStringDelete(CFMutableStringRef theString, CFRange range);
601
602CF_EXPORT
603void CFStringReplace(CFMutableStringRef theString, CFRange range, CFStringRef replacement);
604
605CF_EXPORT
606void CFStringReplaceAll(CFMutableStringRef theString, CFStringRef replacement); /* Replaces whole string */
607
9ce05555
A
608/* Replace all occurrences of target in rangeToSearch of theString with replacement.
609 Pays attention to kCFCompareCaseInsensitive, kCFCompareBackwards, kCFCompareNonliteral, and kCFCompareAnchored.
610 kCFCompareBackwards can be used to do the replacement starting from the end, which could give a different result.
611 ex. AAAAA, replace AA with B -> BBA or ABB; latter if kCFCompareBackwards
612 kCFCompareAnchored assures only anchored but multiple instances are found (the instances must be consecutive at start or end)
613 ex. AAXAA, replace A with B -> BBXBB or BBXAA; latter if kCFCompareAnchored
614 Returns number of replacements performed.
615*/
616CF_EXPORT
cf7d2af9 617CFIndex CFStringFindAndReplace(CFMutableStringRef theString, CFStringRef stringToFind, CFStringRef replacementString, CFRange rangeToSearch, CFStringCompareFlags compareOptions);
9ce05555 618
9ce05555
A
619
620/* This function will make the contents of a mutable CFString point directly at the specified UniChar array.
621 It works only with CFStrings created with CFStringCreateMutableWithExternalCharactersNoCopy().
622 This function does not free the previous buffer.
623 The string will be manipulated within the provided buffer (if any) until it outgrows capacity; then the
624 externalCharactersAllocator will be consulted for more memory.
625 See comments at the top of this file for more info.
626*/
627CF_EXPORT
628void CFStringSetExternalCharactersNoCopy(CFMutableStringRef theString, UniChar *chars, CFIndex length, CFIndex capacity); /* Works only on specially created mutable strings! */
629
630/* CFStringPad() will pad or cut down a string to the specified size.
631 The pad string is used as the fill string; indexIntoPad specifies which character to start with.
632 CFStringPad("abc", " ", 9, 0) -> "abc "
633 CFStringPad("abc", ". ", 9, 1) -> "abc . . ."
634 CFStringPad("abcdef", ?, 3, ?) -> "abc"
635
636 CFStringTrim() will trim the specified string from both ends of the string.
637 CFStringTrimWhitespace() will do the same with white space characters (tab, newline, etc)
638 CFStringTrim(" abc ", " ") -> "abc"
639 CFStringTrim("* * * *abc * ", "* ") -> "*abc "
640*/
641CF_EXPORT
642void CFStringPad(CFMutableStringRef theString, CFStringRef padString, CFIndex length, CFIndex indexIntoPad);
643
644CF_EXPORT
645void CFStringTrim(CFMutableStringRef theString, CFStringRef trimString);
646
647CF_EXPORT
648void CFStringTrimWhitespace(CFMutableStringRef theString);
649
9ce05555
A
650CF_EXPORT
651void CFStringLowercase(CFMutableStringRef theString, CFLocaleRef locale);
652
653CF_EXPORT
654void CFStringUppercase(CFMutableStringRef theString, CFLocaleRef locale);
655
656CF_EXPORT
657void CFStringCapitalize(CFMutableStringRef theString, CFLocaleRef locale);
9ce05555 658
9ce05555
A
659/*!
660 @typedef CFStringNormalizationForm
661 This is the type of Unicode normalization forms as described in
bd5b749c
A
662 Unicode Technical Report #15. To normalize for use with file
663 system calls, use CFStringGetFileSystemRepresentation().
9ce05555 664*/
bd5b749c 665enum {
9ce05555
A
666 kCFStringNormalizationFormD = 0, // Canonical Decomposition
667 kCFStringNormalizationFormKD, // Compatibility Decomposition
668 kCFStringNormalizationFormC, // Canonical Decomposition followed by Canonical Composition
669 kCFStringNormalizationFormKC // Compatibility Decomposition followed by Canonical Composition
bd5b749c
A
670};
671typedef CFIndex CFStringNormalizationForm;
9ce05555
A
672
673/*!
674 @function CFStringNormalize
675 Normalizes the string into the specified form as described in
676 Unicode Technical Report #15.
677 @param theString The string which is to be normalized. If this
678 parameter is not a valid mutable CFString, the behavior is
679 undefined.
680 @param theForm The form into which the string is to be normalized.
681 If this parameter is not a valid CFStringNormalizationForm value,
682 the behavior is undefined.
683*/
684CF_EXPORT void CFStringNormalize(CFMutableStringRef theString, CFStringNormalizationForm theForm);
9ce05555 685
8ca704e1 686
bd5b749c
A
687/*!
688 @function CFStringFold
689 Folds the string into the form specified by the flags.
690 Character foldings are operations that convert any of a set of characters
691 sharing similar semantics into a single representative from that set.
692 This function can be used to preprocess strings that are to be compared,
693 searched, or indexed.
694 Note that folding does not include normalization, so it is necessary
695 to use CFStringNormalize in addition to CFStringFold in order to obtain
696 the effect of kCFCompareNonliteral.
697 @param theString The string which is to be folded. If this parameter is not
698 a valid mutable CFString, the behavior is undefined.
699 @param theFlag The equivalency flags which describes the character folding form.
700 Only those flags containing the word "insensitive" are recognized here; other flags are ignored.
701 Folding with kCFCompareCaseInsensitive removes case distinctions in accordance with the mapping
702 specified by ftp://ftp.unicode.org/Public/UNIDATA/CaseFolding.txt. Folding with
703 kCFCompareDiacriticInsensitive removes distinctions of accents and other diacritics. Folding
704 with kCFCompareWidthInsensitive removes character width distinctions by mapping characters in
705 the range U+FF00-U+FFEF to their ordinary equivalents.
706 @param theLocale The locale tailoring the character folding behavior. If NULL,
707 it's considered to be the system locale returned from CFLocaleGetSystem().
708 If non-NULL and not a valid CFLocale object, the behavior is undefined.
709*/
710
711CF_EXPORT
8ca704e1 712void CFStringFold(CFMutableStringRef theString, CFOptionFlags theFlags, CFLocaleRef theLocale) CF_AVAILABLE(10_5, 2_0);
bd5b749c
A
713
714/* Perform string transliteration. The transformation represented by transform is applied to the given range of string, modifying it in place. Only the specified range will be modified, but the transform may look at portions of the string outside that range for context. NULL range pointer causes the whole string to be transformed. On return, range is modified to reflect the new range corresponding to the original range. reverse indicates that the inverse transform should be used instead, if it exists. If the transform is successful, true is returned; if unsuccessful, false. Reasons for the transform being unsuccessful include an invalid transform identifier, or attempting to reverse an irreversible transform.
715
716You can pass one of the predefined transforms below, or any valid ICU transform ID as defined in the ICU User Guide. Note that we do not support arbitrary set of ICU transform rules.
d8925383 717*/
cf7d2af9 718CF_EXPORT
8ca704e1 719Boolean CFStringTransform(CFMutableStringRef string, CFRange *range, CFStringRef transform, Boolean reverse);
d8925383
A
720
721/* Transform identifiers for CFStringTransform()
722*/
8ca704e1
A
723CF_EXPORT const CFStringRef kCFStringTransformStripCombiningMarks;
724CF_EXPORT const CFStringRef kCFStringTransformToLatin;
725CF_EXPORT const CFStringRef kCFStringTransformFullwidthHalfwidth;
726CF_EXPORT const CFStringRef kCFStringTransformLatinKatakana;
727CF_EXPORT const CFStringRef kCFStringTransformLatinHiragana;
728CF_EXPORT const CFStringRef kCFStringTransformHiraganaKatakana;
729CF_EXPORT const CFStringRef kCFStringTransformMandarinLatin;
730CF_EXPORT const CFStringRef kCFStringTransformLatinHangul;
731CF_EXPORT const CFStringRef kCFStringTransformLatinArabic;
732CF_EXPORT const CFStringRef kCFStringTransformLatinHebrew;
733CF_EXPORT const CFStringRef kCFStringTransformLatinThai;
734CF_EXPORT const CFStringRef kCFStringTransformLatinCyrillic;
735CF_EXPORT const CFStringRef kCFStringTransformLatinGreek;
736CF_EXPORT const CFStringRef kCFStringTransformToXMLHex;
737CF_EXPORT const CFStringRef kCFStringTransformToUnicodeName;
738CF_EXPORT const CFStringRef kCFStringTransformStripDiacritics CF_AVAILABLE(10_5, 2_0);
d8925383
A
739
740
741/*** General encoding related functionality ***/
742
9ce05555
A
743/* This returns availability of the encoding on the system
744*/
745CF_EXPORT
746Boolean CFStringIsEncodingAvailable(CFStringEncoding encoding);
747
748/* This function returns list of available encodings. The returned list is terminated with kCFStringEncodingInvalidId and owned by the system.
749*/
750CF_EXPORT
751const CFStringEncoding *CFStringGetListOfAvailableEncodings(void);
752
753/* Returns name of the encoding; non-localized.
754*/
755CF_EXPORT
756CFStringRef CFStringGetNameOfEncoding(CFStringEncoding encoding);
757
758/* ID mapping functions from/to Cocoa NSStringEncoding. Returns kCFStringEncodingInvalidId if no mapping exists.
759*/
760CF_EXPORT
bd5b749c 761unsigned long CFStringConvertEncodingToNSStringEncoding(CFStringEncoding encoding);
9ce05555
A
762
763CF_EXPORT
bd5b749c 764CFStringEncoding CFStringConvertNSStringEncodingToEncoding(unsigned long encoding);
9ce05555
A
765
766/* ID mapping functions from/to Microsoft Windows codepage (covers both OEM & ANSI). Returns kCFStringEncodingInvalidId if no mapping exists.
767*/
768CF_EXPORT
769UInt32 CFStringConvertEncodingToWindowsCodepage(CFStringEncoding encoding);
770
771CF_EXPORT
772CFStringEncoding CFStringConvertWindowsCodepageToEncoding(UInt32 codepage);
773
774/* ID mapping functions from/to IANA registery charset names. Returns kCFStringEncodingInvalidId if no mapping exists.
775*/
776CF_EXPORT
777CFStringEncoding CFStringConvertIANACharSetNameToEncoding(CFStringRef theString);
778
779CF_EXPORT
780CFStringRef CFStringConvertEncodingToIANACharSetName(CFStringEncoding encoding);
781
782/* Returns the most compatible MacOS script value for the input encoding */
783/* i.e. kCFStringEncodingMacRoman -> kCFStringEncodingMacRoman */
784/* kCFStringEncodingWindowsLatin1 -> kCFStringEncodingMacRoman */
785/* kCFStringEncodingISO_2022_JP -> kCFStringEncodingMacJapanese */
786CF_EXPORT
787CFStringEncoding CFStringGetMostCompatibleMacStringEncoding(CFStringEncoding encoding);
788
d8925383
A
789
790
9ce05555
A
791/* The next two functions allow fast access to the contents of a string,
792 assuming you are doing sequential or localized accesses. To use, call
793 CFStringInitInlineBuffer() with a CFStringInlineBuffer (on the stack, say),
794 and a range in the string to look at. Then call CFStringGetCharacterFromInlineBuffer()
795 as many times as you want, with a index into that range (relative to the start
796 of that range). These are INLINE functions and will end up calling CFString only
797 once in a while, to fill a buffer. CFStringGetCharacterFromInlineBuffer() returns 0 if
798 a location outside the original range is specified.
799*/
800#define __kCFStringInlineBufferLength 64
801typedef struct {
802 UniChar buffer[__kCFStringInlineBufferLength];
803 CFStringRef theString;
804 const UniChar *directBuffer;
805 CFRange rangeToBuffer; /* Range in string to buffer */
806 CFIndex bufferedRangeStart; /* Start of range currently buffered (relative to rangeToBuffer.location) */
807 CFIndex bufferedRangeEnd; /* bufferedRangeStart + number of chars actually buffered */
808} CFStringInlineBuffer;
809
810#if defined(CF_INLINE)
811CF_INLINE void CFStringInitInlineBuffer(CFStringRef str, CFStringInlineBuffer *buf, CFRange range) {
812 buf->theString = str;
813 buf->rangeToBuffer = range;
814 buf->directBuffer = CFStringGetCharactersPtr(str);
815 buf->bufferedRangeStart = buf->bufferedRangeEnd = 0;
816}
817
818CF_INLINE UniChar CFStringGetCharacterFromInlineBuffer(CFStringInlineBuffer *buf, CFIndex idx) {
819 if (buf->directBuffer) {
820 if (idx < 0 || idx >= buf->rangeToBuffer.length) return 0;
821 return buf->directBuffer[idx + buf->rangeToBuffer.location];
822 }
823 if (idx >= buf->bufferedRangeEnd || idx < buf->bufferedRangeStart) {
824 if (idx < 0 || idx >= buf->rangeToBuffer.length) return 0;
825 if ((buf->bufferedRangeStart = idx - 4) < 0) buf->bufferedRangeStart = 0;
826 buf->bufferedRangeEnd = buf->bufferedRangeStart + __kCFStringInlineBufferLength;
827 if (buf->bufferedRangeEnd > buf->rangeToBuffer.length) buf->bufferedRangeEnd = buf->rangeToBuffer.length;
828 CFStringGetCharacters(buf->theString, CFRangeMake(buf->rangeToBuffer.location + buf->bufferedRangeStart, buf->bufferedRangeEnd - buf->bufferedRangeStart), buf->buffer);
829 }
830 return buf->buffer[idx - buf->bufferedRangeStart];
831}
832
833#else
834/* If INLINE functions are not available, we do somewhat less powerful macros that work similarly (except be aware that the buf argument is evaluated multiple times).
835*/
836#define CFStringInitInlineBuffer(str, buf, range) \
837 do {(buf)->theString = str; (buf)->rangeToBuffer = range; (buf)->directBuffer = CFStringGetCharactersPtr(str);} while (0)
838
839#define CFStringGetCharacterFromInlineBuffer(buf, idx) \
840 (((idx) < 0 || (idx) >= (buf)->rangeToBuffer.length) ? 0 : ((buf)->directBuffer ? (buf)->directBuffer[(idx) + (buf)->rangeToBuffer.location] : CFStringGetCharacterAtIndex((buf)->theString, (idx) + (buf)->rangeToBuffer.location)))
841
842#endif /* CF_INLINE */
843
844
d8925383 845
cf7d2af9
A
846/* UTF-16 surrogate support
847 */
848CF_INLINE Boolean CFStringIsSurrogateHighCharacter(UniChar character) {
849 return ((character >= 0xD800UL) && (character <= 0xDBFFUL) ? true : false);
850}
851
852CF_INLINE Boolean CFStringIsSurrogateLowCharacter(UniChar character) {
853 return ((character >= 0xDC00UL) && (character <= 0xDFFFUL) ? true : false);
854}
855
856CF_INLINE UTF32Char CFStringGetLongCharacterForSurrogatePair(UniChar surrogateHigh, UniChar surrogateLow) {
8ca704e1 857 return (UTF32Char)(((surrogateHigh - 0xD800UL) << 10) + (surrogateLow - 0xDC00UL) + 0x0010000UL);
cf7d2af9 858}
d8925383 859
cf7d2af9
A
860// Maps a UTF-32 character to a pair of UTF-16 surrogate characters. The buffer pointed by surrogates has to have space for at least 2 UTF-16 characters. Returns true if mapped to a surrogate pair.
861CF_INLINE Boolean CFStringGetSurrogatePairForLongCharacter(UTF32Char character, UniChar *surrogates) {
862 if ((character > 0xFFFFUL) && (character < 0x110000UL)) { // Non-BMP character
863 character -= 0x10000;
864 if (NULL != surrogates) {
865 surrogates[0] = (UniChar)((character >> 10) + 0xD800UL);
866 surrogates[1] = (UniChar)((character & 0x3FF) + 0xDC00UL);
867 }
868 return true;
869 } else {
870 if (NULL != surrogates) *surrogates = (UniChar)character;
871 return false;
872 }
873}
d8925383 874
9ce05555
A
875/* Rest of the stuff in this file is private and should not be used directly
876*/
cf7d2af9 877/* For debugging only; output goes to stderr
9ce05555
A
878 Use CFShow() to printf the description of any CFType;
879 Use CFShowStr() to printf detailed info about a CFString
880*/
881CF_EXPORT
882void CFShow(CFTypeRef obj);
883
884CF_EXPORT
885void CFShowStr(CFStringRef str);
886
887/* This function is private and should not be used directly */
888CF_EXPORT
889CFStringRef __CFStringMakeConstantString(const char *cStr); /* Private; do not use */
890
bd5b749c 891CF_EXTERN_C_END
9ce05555 892
bd5b749c 893#endif /* ! __COREFOUNDATION_CFSTRING__ */