2 * Copyright (c) 2003 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
26 Copyright (c) 1998-2003, Apple, Inc. All rights reserved.
29 #if !defined(__COREFOUNDATION_CFSTRING__)
30 #define __COREFOUNDATION_CFSTRING__ 1
32 #include <CoreFoundation/CFBase.h>
33 #include <CoreFoundation/CFArray.h>
34 #include <CoreFoundation/CFData.h>
35 #include <CoreFoundation/CFDictionary.h>
36 #include <CoreFoundation/CFCharacterSet.h>
37 #include <CoreFoundation/CFLocale.h>
40 #if defined(__cplusplus)
45 Please note: CFStrings are conceptually an array of Unicode characters.
46 However, in general, how a CFString stores this array is an implementation
47 detail. For instance, CFString might choose to use an array of 8-bit characters;
48 to store its contents; or it might use multiple blocks of memory; or whatever.
49 Furthermore, the implementation might change depending on the default
50 system encoding, the user's language, the OS, or even a given release.
52 What this means is that you should use the following advanced functions with care:
54 CFStringGetPascalStringPtr()
55 CFStringGetCStringPtr()
56 CFStringGetCharactersPtr()
58 These functions are provided for optimization only. They will either return the desired
59 pointer quickly, in constant time, or they return NULL. They might choose to return NULL
60 for many reasons; for instance it's possible that for users running in different
61 languages these sometimes return NULL; or in a future OS release the first two might
62 switch to always returning NULL. Never observing NULL returns in your usages of these
63 functions does not mean they won't ever return NULL. (But note the CFStringGetCharactersPtr()
64 exception mentioned further below.)
66 In your usages of these functions, if you get a NULL return, use the non-Ptr version
67 of the functions as shown in this example:
70 StringPtr ptr = CFStringGetPascalStringPtr(str, encoding);
72 if (CFStringGetPascalString(str, buffer, 256, encoding)) ptr = buffer;
75 Note that CFStringGetPascalString() or CFStringGetCString() calls might still fail --- but
76 that will happen in two circumstances only: The conversion from the UniChar contents of CFString
77 to the specified encoding fails, or the buffer is too small. If they fail, that means
78 the conversion was not possible.
80 If you need a copy of the buffer in the above example, you might consider simply
81 calling CFStringGetPascalString() in all cases --- CFStringGetPascalStringPtr()
82 is simply an optimization.
84 In addition, the following functions, which create immutable CFStrings from developer
85 supplied buffers without copying the buffers, might have to actually copy
86 under certain circumstances (If they do copy, the buffer will be dealt with by the
87 "contentsDeallocator" argument.):
89 CFStringCreateWithPascalStringNoCopy()
90 CFStringCreateWithCStringNoCopy()
91 CFStringCreateWithCharactersNoCopy()
93 You should of course never depend on the backing store of these CFStrings being
94 what you provided, and in other no circumstance should you change the contents
95 of that buffer (given that would break the invariant about the CFString being immutable).
97 Having said all this, there are actually ways to create a CFString where the backing store
98 is external, and can be manipulated by the developer or CFString itself:
100 CFStringCreateMutableWithExternalCharactersNoCopy()
101 CFStringSetExternalCharactersNoCopy()
103 A "contentsAllocator" is used to realloc or free the backing store by CFString.
104 kCFAllocatorNull can be provided to assure CFString will never realloc or free the buffer.
105 Developer can call CFStringSetExternalCharactersNoCopy() to update
106 CFString's idea of what's going on, if the buffer is changed externally. In these
107 strings, CFStringGetCharactersPtr() is guaranteed to return the external buffer.
109 These functions are here to allow wrapping a buffer of UniChar characters in a CFString,
110 allowing the buffer to passed into CFString functions and also manipulated via CFString
111 mutation functions. In general, developers should not use this technique for all strings,
112 as it prevents CFString from using certain optimizations.
115 /* Identifier for character encoding; the values are the same as Text Encoding Converter TextEncoding.
117 typedef UInt32 CFStringEncoding
;
119 /* Platform-independent built-in encodings; always available on all platforms.
120 Call CFStringGetSystemEncoding() to get the default system encoding.
122 #define kCFStringEncodingInvalidId (0xffffffffU)
124 kCFStringEncodingMacRoman
= 0,
125 kCFStringEncodingWindowsLatin1
= 0x0500, /* ANSI codepage 1252 */
126 kCFStringEncodingISOLatin1
= 0x0201, /* ISO 8859-1 */
127 kCFStringEncodingNextStepLatin
= 0x0B01, /* NextStep encoding*/
128 kCFStringEncodingASCII
= 0x0600, /* 0..127 (in creating CFString, values greater than 0x7F are treated as corresponding Unicode value) */
129 kCFStringEncodingUnicode
= 0x0100, /* kTextEncodingUnicodeDefault + kTextEncodingDefaultFormat (aka kUnicode16BitFormat) */
130 kCFStringEncodingUTF8
= 0x08000100, /* kTextEncodingUnicodeDefault + kUnicodeUTF8Format */
131 kCFStringEncodingNonLossyASCII
= 0x0BFF /* 7bit Unicode variants used by Cocoa & Java */
132 } CFStringBuiltInEncodings
;
134 /* CFString type ID */
136 CFTypeID
CFStringGetTypeID(void);
138 /* Macro to allow creation of compile-time constant strings; the argument should be a constant string.
140 CFSTR(), not being a "Copy" or "Create" function, does not return a new
141 reference for you. So, you should not release the return value. This is
142 much like constant C or Pascal strings --- when you use "hello world"
143 in a program, you do not free it.
145 However, strings returned from CFSTR() can be retained and released in a
146 properly nested fashion, just like any other CF type. That is, if you pass
147 a CFSTR() return value to a function such as SetMenuItemWithCFString(), the
148 function can retain it, then later, when it's done with it, it can release it.
150 At this point non-7 bit characters (that is, characters > 127) in CFSTR() are not
151 supported and using them will lead to unpredictable results. This includes escaped
152 (\nnn) characters whose values are > 127. Even if it works for you in testing,
153 it might not work for a user with a different language preference.
155 #ifdef __CONSTANT_CFSTRINGS__
156 #define CFSTR(cStr) ((CFStringRef) __builtin___CFStringMakeConstantString ("" cStr ""))
158 #define CFSTR(cStr) __CFStringMakeConstantString("" cStr "")
161 /*** Immutable string creation functions ***/
163 /* Functions to create basic immutable strings. The provided allocator is used for all memory activity in these functions.
166 /* These functions copy the provided buffer into CFString's internal storage. */
168 CFStringRef
CFStringCreateWithPascalString(CFAllocatorRef alloc
, ConstStr255Param pStr
, CFStringEncoding encoding
);
171 CFStringRef
CFStringCreateWithCString(CFAllocatorRef alloc
, const char *cStr
, CFStringEncoding encoding
);
174 CFStringRef
CFStringCreateWithCharacters(CFAllocatorRef alloc
, const UniChar
*chars
, CFIndex numChars
);
176 /* These functions try not to copy the provided buffer. The buffer will be deallocated
177 with the provided contentsDeallocator when it's no longer needed; to not free
178 the buffer, specify kCFAllocatorNull here. As usual, NULL means default allocator.
180 NOTE: Do not count on these buffers as being used by the string;
181 in some cases the CFString might free the buffer and use something else
182 (for instance if it decides to always use Unicode encoding internally).
184 NOTE: If you are not transferring ownership of the buffer to the CFString
185 (for instance, you supplied contentsDeallocator = kCFAllocatorNull), it is your
186 responsibility to assure the buffer does not go away during the lifetime of the string.
187 If the string is retained or copied, its lifetime might extend in ways you cannot
188 predict. So, for strings created with buffers whose lifetimes you cannot
189 guarantee, you need to be extremely careful --- do not hand it out to any
190 APIs which might retain or copy the strings.
193 CFStringRef
CFStringCreateWithPascalStringNoCopy(CFAllocatorRef alloc
, ConstStr255Param pStr
, CFStringEncoding encoding
, CFAllocatorRef contentsDeallocator
);
196 CFStringRef
CFStringCreateWithCStringNoCopy(CFAllocatorRef alloc
, const char *cStr
, CFStringEncoding encoding
, CFAllocatorRef contentsDeallocator
);
199 CFStringRef
CFStringCreateWithCharactersNoCopy(CFAllocatorRef alloc
, const UniChar
*chars
, CFIndex numChars
, CFAllocatorRef contentsDeallocator
);
201 /* Create copies of part or all of the string.
204 CFStringRef
CFStringCreateWithSubstring(CFAllocatorRef alloc
, CFStringRef str
, CFRange range
);
207 CFStringRef
CFStringCreateCopy(CFAllocatorRef alloc
, CFStringRef theString
);
209 /* These functions create a CFString from the provided printf-like format string and arguments.
212 CFStringRef
CFStringCreateWithFormat(CFAllocatorRef alloc
, CFDictionaryRef formatOptions
, CFStringRef format
, ...);
215 CFStringRef
CFStringCreateWithFormatAndArguments(CFAllocatorRef alloc
, CFDictionaryRef formatOptions
, CFStringRef format
, va_list arguments
);
217 /* 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.
220 CFMutableStringRef
CFStringCreateMutable(CFAllocatorRef alloc
, CFIndex maxLength
);
223 CFMutableStringRef
CFStringCreateMutableCopy(CFAllocatorRef alloc
, CFIndex maxLength
, CFStringRef theString
);
225 /* This function creates a mutable string that has a developer supplied and directly editable backing store.
226 The string will be manipulated within the provided buffer (if any) until it outgrows capacity; then the
227 externalCharactersAllocator will be consulted for more memory. When the CFString is deallocated, the
228 buffer will be freed with the externalCharactersAllocator. Provide kCFAllocatorNull here to prevent the buffer
229 from ever being reallocated or deallocated by CFString. See comments at top of this file for more info.
232 CFMutableStringRef
CFStringCreateMutableWithExternalCharactersNoCopy(CFAllocatorRef alloc
, UniChar
*chars
, CFIndex numChars
, CFIndex capacity
, CFAllocatorRef externalCharactersAllocator
);
234 /*** Basic accessors for the contents ***/
236 /* Number of 16-bit Unicode characters in the string.
239 CFIndex
CFStringGetLength(CFStringRef theString
);
241 /* Extracting the contents of the string. For obtaining multiple characters, calling
242 CFStringGetCharacters() is more efficient than multiple calls to CFStringGetCharacterAtIndex().
243 If the length of the string is not known (so you can't use a fixed size buffer for CFStringGetCharacters()),
244 another method is to use is CFStringGetCharacterFromInlineBuffer() (see further below).
247 UniChar
CFStringGetCharacterAtIndex(CFStringRef theString
, CFIndex idx
);
250 void CFStringGetCharacters(CFStringRef theString
, CFRange range
, UniChar
*buffer
);
252 /*** Conversion to other encodings ***/
254 /* These two convert into the provided buffer; they return false if conversion isn't possible
255 (due to conversion error, or not enough space in the provided buffer).
256 These functions do zero-terminate or put the length byte; the provided bufferSize should include
257 space for this (so pass 256 for Str255). More sophisticated usages can go through CFStringGetBytes().
258 These functions are equivalent to calling CFStringGetBytes() with
259 the range of the string; lossByte = 0; and isExternalRepresentation = false;
260 if successful, they then insert the leading length of terminating zero, as desired.
263 Boolean
CFStringGetPascalString(CFStringRef theString
, StringPtr buffer
, CFIndex bufferSize
, CFStringEncoding encoding
);
266 Boolean
CFStringGetCString(CFStringRef theString
, char *buffer
, CFIndex bufferSize
, CFStringEncoding encoding
);
268 /* These functions attempt to return in O(1) time the desired format for the string.
269 Note that although this means a pointer to the internal structure is being returned,
270 this can't always be counted on. Please see note at the top of the file for more
274 ConstStringPtr
CFStringGetPascalStringPtr(CFStringRef theString
, CFStringEncoding encoding
); /* May return NULL at any time; be prepared for NULL */
277 const char *CFStringGetCStringPtr(CFStringRef theString
, CFStringEncoding encoding
); /* May return NULL at any time; be prepared for NULL */
280 const UniChar
*CFStringGetCharactersPtr(CFStringRef theString
); /* May return NULL at any time; be prepared for NULL */
282 /* The primitive conversion routine; allows you to convert a string piece at a time
283 into a fixed size buffer. Returns number of characters converted.
284 Characters that cannot be converted to the specified encoding are represented
285 with the byte specified by lossByte; if lossByte is 0, then lossy conversion
286 is not allowed and conversion stops, returning partial results.
287 Pass buffer==NULL if you don't care about the converted string (but just the convertability,
288 or number of bytes required).
289 maxBufLength indicates the maximum number of bytes to generate. It is ignored when buffer==NULL.
290 Does not zero-terminate. If you want to create Pascal or C string, allow one extra byte at start or end.
291 Setting isExternalRepresentation causes any extra bytes that would allow
292 the data to be made persistent to be included; for instance, the Unicode BOM.
295 CFIndex
CFStringGetBytes(CFStringRef theString
, CFRange range
, CFStringEncoding encoding
, UInt8 lossByte
, Boolean isExternalRepresentation
, UInt8
*buffer
, CFIndex maxBufLen
, CFIndex
*usedBufLen
);
297 /* This one goes the other way by creating a CFString from a bag of bytes.
298 This is much like CFStringCreateWithPascalString or CFStringCreateWithCString,
299 except the length is supplied explicitly. In addition, you can specify whether
300 the data is an external format --- that is, whether to pay attention to the
301 BOM character (if any) and do byte swapping if necessary
304 CFStringRef
CFStringCreateWithBytes(CFAllocatorRef alloc
, const UInt8
*bytes
, CFIndex numBytes
, CFStringEncoding encoding
, Boolean isExternalRepresentation
);
306 /* Convenience functions String <-> Data. These generate "external" formats, that is, formats that
307 can be written out to disk. For instance, if the encoding is Unicode, CFStringCreateFromExternalRepresentation()
308 pays attention to the BOM character (if any) and does byte swapping if necessary.
309 Similarly CFStringCreateExternalRepresentation() will always include a BOM character if the encoding is
310 Unicode. See above for description of lossByte.
313 CFStringRef
CFStringCreateFromExternalRepresentation(CFAllocatorRef alloc
, CFDataRef data
, CFStringEncoding encoding
); /* May return NULL on conversion error */
316 CFDataRef
CFStringCreateExternalRepresentation(CFAllocatorRef alloc
, CFStringRef theString
, CFStringEncoding encoding
, UInt8 lossByte
); /* May return NULL on conversion error */
318 /* Hints about the contents of a string
321 CFStringEncoding
CFStringGetSmallestEncoding(CFStringRef theString
); /* Result in O(n) time max */
324 CFStringEncoding
CFStringGetFastestEncoding(CFStringRef theString
); /* Result in O(1) time max */
326 /* General encoding info
329 CFStringEncoding
CFStringGetSystemEncoding(void); /* The default encoding for the system; untagged 8-bit characters are usually in this encoding */
332 CFIndex
CFStringGetMaximumSizeForEncoding(CFIndex length
, CFStringEncoding encoding
); /* Max bytes a string of specified length (in UniChars) will take up if encoded */
334 /*** Comparison functions. ***/
336 /* Find and compare flags; these are OR'ed together as compareOptions or searchOptions in the various functions.
337 This typedef doesn't appear in the functions; instead the argument is CFOptionFlags.
340 kCFCompareCaseInsensitive
= 1,
341 kCFCompareBackwards
= 4, /* Starting from the end of the string */
342 kCFCompareAnchored
= 8, /* Only at the specified starting point */
343 kCFCompareNonliteral
= 16, /* If specified, loose equivalence is performed (o-umlaut == o, umlaut) */
344 kCFCompareLocalized
= 32, /* User's default locale is used for the comparisons */
345 kCFCompareNumerically
= 64 /* Numeric comparison is used; that is, Foo2.txt < Foo7.txt < Foo25.txt */
346 } CFStringCompareFlags
;
348 /* The main comparison routine; compares specified range of the first string to (the full range of) the second string.
349 locale == NULL indicates canonical locale.
350 kCFCompareNumerically, added in 10.2, does not work if kCFCompareLocalized is specified on systems before 10.3
351 kCFCompareBackwards and kCFCompareAnchored are not applicable.
354 CFComparisonResult
CFStringCompareWithOptions(CFStringRef theString1
, CFStringRef theString2
, CFRange rangeToCompare
, CFOptionFlags compareOptions
);
356 /* Comparison convenience suitable for passing as sorting functions.
357 kCFCompareNumerically, added in 10.2, does not work if kCFCompareLocalized is specified on systems before 10.3
358 kCFCompareBackwards and kCFCompareAnchored are not applicable.
361 CFComparisonResult
CFStringCompare(CFStringRef theString1
, CFStringRef theString2
, CFOptionFlags compareOptions
);
363 /* CFStringFindWithOptions() returns the found range in the CFRange * argument; you can pass NULL for simple discovery check.
364 If stringToFind is the empty string (zero length), nothing is found.
365 Ignores the kCFCompareNumerically option.
368 Boolean
CFStringFindWithOptions(CFStringRef theString
, CFStringRef stringToFind
, CFRange rangeToSearch
, CFOptionFlags searchOptions
, CFRange
*result
);
370 /* CFStringCreateArrayWithFindResults() returns an array of CFRange pointers, or NULL if there are no matches.
371 Overlapping instances are not found; so looking for "AA" in "AAA" finds just one range.
372 Post 10.1: If kCFCompareBackwards is provided, the scan is done from the end (which can give a different result), and
373 the results are stored in the array backwards (last found range in slot 0).
374 If stringToFind is the empty string (zero length), nothing is found.
375 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.
376 Ignores the kCFCompareNumerically option.
379 CFArrayRef
CFStringCreateArrayWithFindResults(CFAllocatorRef alloc
, CFStringRef theString
, CFStringRef stringToFind
, CFRange rangeToSearch
, CFOptionFlags compareOptions
);
381 /* Find conveniences; see comments above concerning empty string and options.
384 CFRange
CFStringFind(CFStringRef theString
, CFStringRef stringToFind
, CFOptionFlags compareOptions
);
387 Boolean
CFStringHasPrefix(CFStringRef theString
, CFStringRef prefix
);
390 Boolean
CFStringHasSuffix(CFStringRef theString
, CFStringRef suffix
);
392 #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
394 @function CFStringGetRangeOfComposedCharactersAtIndex
395 Returns the range of the composed character sequence at the specified index.
396 @param theString The CFString which is to be searched. If this
397 parameter is not a valid CFString, the behavior is
399 @param theIndex The index of the character contained in the
400 composed character sequence. If the index is
401 outside the index space of the string (0 to N-1 inclusive,
402 where N is the length of the string), the behavior is
404 @result The range of the composed character sequence.
406 CF_EXPORT CFRange
CFStringGetRangeOfComposedCharactersAtIndex(CFStringRef theString
, CFIndex theIndex
);
409 @function CFStringFindCharacterFromSet
410 Query the range of the first character contained in the specified character set.
411 @param theString The CFString which is to be searched. If this
412 parameter is not a valid CFString, the behavior is
414 @param theSet The CFCharacterSet against which the membership
415 of characters is checked. If this parameter is not a valid
416 CFCharacterSet, the behavior is undefined.
417 @param range The range of characters within the string to search. If
418 the range location or end point (defined by the location
419 plus length minus 1) are outside the index space of the
420 string (0 to N-1 inclusive, where N is the length of the
421 string), the behavior is undefined. If the range length is
422 negative, the behavior is undefined. The range may be empty
423 (length 0), in which case no search is performed.
424 @param searchOptions The bitwise-or'ed option flags to control
425 the search behavior. The supported options are
426 kCFCompareBackwards andkCFCompareAnchored.
427 If other option flags are specified, the behavior
429 @param result The pointer to a CFRange supplied by the caller in
430 which the search result is stored. Note that the length
431 of this range could be more than If a pointer to an invalid
432 memory is specified, the behavior is undefined.
433 @result true, if at least a character which is a member of the character
434 set is found and result is filled, otherwise, false.
436 CF_EXPORT Boolean
CFStringFindCharacterFromSet(CFStringRef theString
, CFCharacterSetRef theSet
, CFRange rangeToSearch
, CFOptionFlags searchOptions
, CFRange
*result
);
439 /* Find range of bounds of the line(s) that span the indicated range (startIndex, numChars),
440 taking into account various possible line separator sequences (CR, CRLF, LF, and Unicode LS, PS).
441 All return values are "optional" (provide NULL if you don't want them)
442 lineStartIndex: index of first character in line
443 lineEndIndex: index of first character of the next line (including terminating line separator characters)
444 contentsEndIndex: index of the first line separator character
445 Thus, lineEndIndex - lineStartIndex is the number of chars in the line, including the line separators
446 contentsEndIndex - lineStartIndex is the number of chars in the line w/out the line separators
449 void CFStringGetLineBounds(CFStringRef theString
, CFRange range
, CFIndex
*lineBeginIndex
, CFIndex
*lineEndIndex
, CFIndex
*contentsEndIndex
);
451 /*** Exploding and joining strings with a separator string ***/
454 CFStringRef
CFStringCreateByCombiningStrings(CFAllocatorRef alloc
, CFArrayRef theArray
, CFStringRef separatorString
); /* Empty array returns empty string; one element array returns the element */
457 CFArrayRef
CFStringCreateArrayBySeparatingStrings(CFAllocatorRef alloc
, CFStringRef theString
, CFStringRef separatorString
); /* No separators in the string returns array with that string; string == sep returns two empty strings */
459 /*** Parsing non-localized numbers from strings ***/
462 SInt32
CFStringGetIntValue(CFStringRef str
); /* Skips whitespace; returns 0 on error, MAX or -MAX on overflow */
465 double CFStringGetDoubleValue(CFStringRef str
); /* Skips whitespace; returns 0.0 on error */
467 /*** MutableString functions ***/
469 /* CFStringAppend("abcdef", "xxxxx") -> "abcdefxxxxx"
470 CFStringDelete("abcdef", CFRangeMake(2, 3)) -> "abf"
471 CFStringReplace("abcdef", CFRangeMake(2, 3), "xxxxx") -> "abxxxxxf"
472 CFStringReplaceAll("abcdef", "xxxxx") -> "xxxxx"
475 void CFStringAppend(CFMutableStringRef theString
, CFStringRef appendedString
);
478 void CFStringAppendCharacters(CFMutableStringRef theString
, const UniChar
*chars
, CFIndex numChars
);
481 void CFStringAppendPascalString(CFMutableStringRef theString
, ConstStr255Param pStr
, CFStringEncoding encoding
);
484 void CFStringAppendCString(CFMutableStringRef theString
, const char *cStr
, CFStringEncoding encoding
);
487 void CFStringAppendFormat(CFMutableStringRef theString
, CFDictionaryRef formatOptions
, CFStringRef format
, ...);
490 void CFStringAppendFormatAndArguments(CFMutableStringRef theString
, CFDictionaryRef formatOptions
, CFStringRef format
, va_list arguments
);
493 void CFStringInsert(CFMutableStringRef str
, CFIndex idx
, CFStringRef insertedStr
);
496 void CFStringDelete(CFMutableStringRef theString
, CFRange range
);
499 void CFStringReplace(CFMutableStringRef theString
, CFRange range
, CFStringRef replacement
);
502 void CFStringReplaceAll(CFMutableStringRef theString
, CFStringRef replacement
); /* Replaces whole string */
504 #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
505 /* Replace all occurrences of target in rangeToSearch of theString with replacement.
506 Pays attention to kCFCompareCaseInsensitive, kCFCompareBackwards, kCFCompareNonliteral, and kCFCompareAnchored.
507 kCFCompareBackwards can be used to do the replacement starting from the end, which could give a different result.
508 ex. AAAAA, replace AA with B -> BBA or ABB; latter if kCFCompareBackwards
509 kCFCompareAnchored assures only anchored but multiple instances are found (the instances must be consecutive at start or end)
510 ex. AAXAA, replace A with B -> BBXBB or BBXAA; latter if kCFCompareAnchored
511 Returns number of replacements performed.
514 CFIndex
CFStringFindAndReplace(CFMutableStringRef theString
, CFStringRef stringToFind
, CFStringRef replacementString
, CFRange rangeToSearch
, CFOptionFlags compareOptions
);
518 /* This function will make the contents of a mutable CFString point directly at the specified UniChar array.
519 It works only with CFStrings created with CFStringCreateMutableWithExternalCharactersNoCopy().
520 This function does not free the previous buffer.
521 The string will be manipulated within the provided buffer (if any) until it outgrows capacity; then the
522 externalCharactersAllocator will be consulted for more memory.
523 See comments at the top of this file for more info.
526 void CFStringSetExternalCharactersNoCopy(CFMutableStringRef theString
, UniChar
*chars
, CFIndex length
, CFIndex capacity
); /* Works only on specially created mutable strings! */
528 /* CFStringPad() will pad or cut down a string to the specified size.
529 The pad string is used as the fill string; indexIntoPad specifies which character to start with.
530 CFStringPad("abc", " ", 9, 0) -> "abc "
531 CFStringPad("abc", ". ", 9, 1) -> "abc . . ."
532 CFStringPad("abcdef", ?, 3, ?) -> "abc"
534 CFStringTrim() will trim the specified string from both ends of the string.
535 CFStringTrimWhitespace() will do the same with white space characters (tab, newline, etc)
536 CFStringTrim(" abc ", " ") -> "abc"
537 CFStringTrim("* * * *abc * ", "* ") -> "*abc "
540 void CFStringPad(CFMutableStringRef theString
, CFStringRef padString
, CFIndex length
, CFIndex indexIntoPad
);
543 void CFStringTrim(CFMutableStringRef theString
, CFStringRef trimString
);
546 void CFStringTrimWhitespace(CFMutableStringRef theString
);
548 #if MAC_OS_X_VERSION_10_3 <= MAC_OS_X_VERSION_MAX_ALLOWED
550 void CFStringLowercase(CFMutableStringRef theString
, CFLocaleRef locale
);
553 void CFStringUppercase(CFMutableStringRef theString
, CFLocaleRef locale
);
556 void CFStringCapitalize(CFMutableStringRef theString
, CFLocaleRef locale
);
559 void CFStringLowercase(CFMutableStringRef theString
, const void *localeTBD
); // localeTBD must be NULL on pre-10.3
562 void CFStringUppercase(CFMutableStringRef theString
, const void *localeTBD
); // localeTBD must be NULL on pre-10.3
565 void CFStringCapitalize(CFMutableStringRef theString
, const void *localeTBD
); // localeTBD must be NULL on pre-10.3
568 #if MAC_OS_X_VERSION_10_2 <= MAC_OS_X_VERSION_MAX_ALLOWED
570 @typedef CFStringNormalizationForm
571 This is the type of Unicode normalization forms as described in
572 Unicode Technical Report #15.
575 kCFStringNormalizationFormD
= 0, // Canonical Decomposition
576 kCFStringNormalizationFormKD
, // Compatibility Decomposition
577 kCFStringNormalizationFormC
, // Canonical Decomposition followed by Canonical Composition
578 kCFStringNormalizationFormKC
// Compatibility Decomposition followed by Canonical Composition
579 } CFStringNormalizationForm
;
582 @function CFStringNormalize
583 Normalizes the string into the specified form as described in
584 Unicode Technical Report #15.
585 @param theString The string which is to be normalized. If this
586 parameter is not a valid mutable CFString, the behavior is
588 @param theForm The form into which the string is to be normalized.
589 If this parameter is not a valid CFStringNormalizationForm value,
590 the behavior is undefined.
592 CF_EXPORT
void CFStringNormalize(CFMutableStringRef theString
, CFStringNormalizationForm theForm
);
595 /* This returns availability of the encoding on the system
598 Boolean
CFStringIsEncodingAvailable(CFStringEncoding encoding
);
600 /* This function returns list of available encodings. The returned list is terminated with kCFStringEncodingInvalidId and owned by the system.
603 const CFStringEncoding
*CFStringGetListOfAvailableEncodings(void);
605 /* Returns name of the encoding; non-localized.
608 CFStringRef
CFStringGetNameOfEncoding(CFStringEncoding encoding
);
610 /* ID mapping functions from/to Cocoa NSStringEncoding. Returns kCFStringEncodingInvalidId if no mapping exists.
613 UInt32
CFStringConvertEncodingToNSStringEncoding(CFStringEncoding encoding
);
616 CFStringEncoding
CFStringConvertNSStringEncodingToEncoding(UInt32 encoding
);
618 /* ID mapping functions from/to Microsoft Windows codepage (covers both OEM & ANSI). Returns kCFStringEncodingInvalidId if no mapping exists.
621 UInt32
CFStringConvertEncodingToWindowsCodepage(CFStringEncoding encoding
);
624 CFStringEncoding
CFStringConvertWindowsCodepageToEncoding(UInt32 codepage
);
626 /* ID mapping functions from/to IANA registery charset names. Returns kCFStringEncodingInvalidId if no mapping exists.
629 CFStringEncoding
CFStringConvertIANACharSetNameToEncoding(CFStringRef theString
);
632 CFStringRef
CFStringConvertEncodingToIANACharSetName(CFStringEncoding encoding
);
634 /* Returns the most compatible MacOS script value for the input encoding */
635 /* i.e. kCFStringEncodingMacRoman -> kCFStringEncodingMacRoman */
636 /* kCFStringEncodingWindowsLatin1 -> kCFStringEncodingMacRoman */
637 /* kCFStringEncodingISO_2022_JP -> kCFStringEncodingMacJapanese */
639 CFStringEncoding
CFStringGetMostCompatibleMacStringEncoding(CFStringEncoding encoding
);
641 /* The next two functions allow fast access to the contents of a string,
642 assuming you are doing sequential or localized accesses. To use, call
643 CFStringInitInlineBuffer() with a CFStringInlineBuffer (on the stack, say),
644 and a range in the string to look at. Then call CFStringGetCharacterFromInlineBuffer()
645 as many times as you want, with a index into that range (relative to the start
646 of that range). These are INLINE functions and will end up calling CFString only
647 once in a while, to fill a buffer. CFStringGetCharacterFromInlineBuffer() returns 0 if
648 a location outside the original range is specified.
650 #define __kCFStringInlineBufferLength 64
652 UniChar buffer
[__kCFStringInlineBufferLength
];
653 CFStringRef theString
;
654 const UniChar
*directBuffer
;
655 CFRange rangeToBuffer
; /* Range in string to buffer */
656 CFIndex bufferedRangeStart
; /* Start of range currently buffered (relative to rangeToBuffer.location) */
657 CFIndex bufferedRangeEnd
; /* bufferedRangeStart + number of chars actually buffered */
658 } CFStringInlineBuffer
;
660 #if defined(CF_INLINE)
661 CF_INLINE
void CFStringInitInlineBuffer(CFStringRef str
, CFStringInlineBuffer
*buf
, CFRange range
) {
662 buf
->theString
= str
;
663 buf
->rangeToBuffer
= range
;
664 buf
->directBuffer
= CFStringGetCharactersPtr(str
);
665 buf
->bufferedRangeStart
= buf
->bufferedRangeEnd
= 0;
668 CF_INLINE UniChar
CFStringGetCharacterFromInlineBuffer(CFStringInlineBuffer
*buf
, CFIndex idx
) {
669 if (buf
->directBuffer
) {
670 if (idx
< 0 || idx
>= buf
->rangeToBuffer
.length
) return 0;
671 return buf
->directBuffer
[idx
+ buf
->rangeToBuffer
.location
];
673 if (idx
>= buf
->bufferedRangeEnd
|| idx
< buf
->bufferedRangeStart
) {
674 if (idx
< 0 || idx
>= buf
->rangeToBuffer
.length
) return 0;
675 if ((buf
->bufferedRangeStart
= idx
- 4) < 0) buf
->bufferedRangeStart
= 0;
676 buf
->bufferedRangeEnd
= buf
->bufferedRangeStart
+ __kCFStringInlineBufferLength
;
677 if (buf
->bufferedRangeEnd
> buf
->rangeToBuffer
.length
) buf
->bufferedRangeEnd
= buf
->rangeToBuffer
.length
;
678 CFStringGetCharacters(buf
->theString
, CFRangeMake(buf
->rangeToBuffer
.location
+ buf
->bufferedRangeStart
, buf
->bufferedRangeEnd
- buf
->bufferedRangeStart
), buf
->buffer
);
680 return buf
->buffer
[idx
- buf
->bufferedRangeStart
];
684 /* 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).
686 #define CFStringInitInlineBuffer(str, buf, range) \
687 do {(buf)->theString = str; (buf)->rangeToBuffer = range; (buf)->directBuffer = CFStringGetCharactersPtr(str);} while (0)
689 #define CFStringGetCharacterFromInlineBuffer(buf, idx) \
690 (((idx) < 0 || (idx) >= (buf)->rangeToBuffer.length) ? 0 : ((buf)->directBuffer ? (buf)->directBuffer[(idx) + (buf)->rangeToBuffer.location] : CFStringGetCharacterAtIndex((buf)->theString, (idx) + (buf)->rangeToBuffer.location)))
692 #endif /* CF_INLINE */
695 /* Rest of the stuff in this file is private and should not be used directly
697 /* For debugging only
698 Use CFShow() to printf the description of any CFType;
699 Use CFShowStr() to printf detailed info about a CFString
702 void CFShow(CFTypeRef obj
);
705 void CFShowStr(CFStringRef str
);
707 /* This function is private and should not be used directly */
709 CFStringRef
__CFStringMakeConstantString(const char *cStr
); /* Private; do not use */
711 #if defined(__cplusplus)
715 #endif /* !__COREFOUNDATION_CFSTRING__ */