]>
git.saurik.com Git - apple/xnu.git/blob - libkern/libkern/c++/OSString.h
2 * Copyright (c) 2019 Apple Inc. All rights reserved.
4 * @APPLE_OSREFERENCE_LICENSE_HEADER_START@
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. The rights granted to you under the License
10 * may not be used to create, or enable the creation or redistribution of,
11 * unlawful or unlicensed copies of an Apple operating system, or to
12 * circumvent, violate, or enable the circumvention or violation of, any
13 * terms of an Apple operating system software license agreement.
15 * Please obtain a copy of the License at
16 * http://www.opensource.apple.com/apsl/ and read it before using this file.
18 * The Original Code and all software distributed under the License are
19 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
20 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
21 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
22 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
23 * Please see the License for the specific language governing rights and
24 * limitations under the License.
26 * @APPLE_OSREFERENCE_LICENSE_HEADER_END@
28 /* IOString.h created by rsulack on Wed 17-Sep-1997 */
29 /* IOString.h converted to C++ by gvdl on Fri 1998-10-30 */
31 #ifndef _OS_OSSTRING_H
32 #define _OS_OSSTRING_H
34 #include <libkern/c++/OSObject.h>
35 #include <libkern/c++/OSPtr.h>
40 typedef OSPtr
<OSString
> OSStringPtr
;
41 typedef OSPtr
<const OSString
> OSStringConstPtr
;
48 * This header declares the OSString container class.
52 /* Not to be included in headerdoc.
56 enum { kOSStringNoCopy
= 0x00000001 };
63 * OSString wraps a C string in a C++ object for use in Libkern collections.
66 * OSString is a container class for managing arrays of characters.
67 * An OSString normally maintains its own character buffer and allows changes,
68 * but you can create an "immutable" OSString
69 * that references an external C string
70 * buffer using the "NoCopy" creator functions.
71 * Functions called to change the contents of an immutable OSString will fail.
75 * OSString makes no provisions for different character encodings and
76 * assumes that a string is a nul-terminated sequence of single-byte characters.
77 * User-space code must either assume an encoding (typically ASCII or UTF-8)
78 * or determine it in some other way (such as an IORegistryEntry property).
80 * <b>Altering Strings</b>
82 * OSString's indended use is as a reference-counted object container
83 * for a C string and little more.
84 * While OSString provides full access to the underlying C string,
85 * it provides little in the way of string object manipulation;
86 * there are no append or insert functions,
87 * only a set-character function.
88 * If you need to manipulate OSStrings,
89 * it's generally best to get the C strings,
90 * alter them as necessary, and create a new OSString object
91 * from the resulting C string.
93 * <b>Use Restrictions</b>
95 * With very few exceptions in the I/O Kit, all Libkern-based C++
96 * classes, functions, and macros are <b>unsafe</b>
97 * to use in a primary interrupt context.
98 * Consult the I/O Kit documentation related to primary interrupts
99 * for more information.
101 * OSString provides no concurrency protection;
102 * it's up to the usage context to provide any protection necessary.
103 * Some portions of the I/O Kit, such as
104 * @link //apple_ref/doc/class/IORegistryEntry IORegistryEntry@/link,
105 * handle synchronization via defined member functions for setting
108 class OSString
: public OSObject
110 OSDeclareDefaultStructors(OSString
);
112 enum { kMaxStringLength
= 262142 };
114 #if APPLE_KEXT_ALIGN_CONTAINERS
118 unsigned int flags
:14,
122 #else /* APPLE_KEXT_ALIGN_CONTAINERS */
129 #endif /* APPLE_KEXT_ALIGN_CONTAINERS */
134 * @function withString
137 * Creates and initializes an OSString from another OSString.
139 * @param aString The OSString object whose contents to copy.
142 * An instance of OSString representing
143 * the same characters as <code>aString</code>,
144 * and with a reference count of 1;
145 * <code>NULL</code> on failure.
148 * The new OSString is a distinct instance from <code>aString</code>,
149 * and is not merely the original object
150 * with the reference count incremented.
151 * Changes to one will not be reflected in the other.
153 static OSStringPtr
withString(const OSString
* aString
);
157 * @function withCString
160 * Creates and initializes an OSString from a C string.
162 * @param cString The C string to copy into the new OSString.
165 * An instance of OSString representing
166 * the same characters as <code>aString</code>,
167 * and with a reference count of 1;
168 * <code>NULL</code> on failure.
170 static OSStringPtr
withCString(const char * cString
);
174 * @function withCStringNoCopy
177 * Creates and initializes an immutable OSString
178 * that shares the provided C string buffer.
180 * @param cString The C string to reference.
183 * An instance of OSString containing <code>cString</code>,
184 * and with a reference count of 1;
185 * <code>NULL</code> on failure.
188 * An OSString object created with this function
189 * does not claim ownership of the C string,
190 * but shares it with the caller.
191 * When the caller determines that the OSString object has actually been freed,
192 * it can safely dispose of the data buffer.
193 * Conversely, if it frees the shared data buffer,
194 * it must not attempt to use the OSString object and should release it.
196 * An OSString object created with this function does not
197 * allow changing the string via <code>@link setChar setChar@/link</code>.
199 static OSStringPtr
withCStringNoCopy(const char * cString
);
201 #if XNU_KERNEL_PRIVATE
202 static OSStringPtr
withStringOfLength(const char *cString
, size_t length
);
203 #endif /* XNU_KERNEL_PRIVATE */
206 * @function initWithString
209 * Initializes an OSString from another OSString.
211 * @param aString The OSString object whose contents to copy.
214 * <code>true</code> on success, <code>false</code> on failure.
217 * Not for general use. Use the static instance creation method
218 * <code>@link withString withString@/link</code> instead.
220 virtual bool initWithString(const OSString
* aString
);
224 * @function initWithCString
227 * Initializes an OSString from a C string.
229 * @param cString The C string to copy into the new OSString.
232 * <code>true</code> on success, <code>false</code> on failure.
235 * Not for general use. Use the static instance creation method
236 * <code>@link withCString withCString@/link</code> instead.
238 virtual bool initWithCString(const char * cString
);
242 * @function initWithCStringNoCopy
245 * Initializes an immutable OSString
246 * to share the provided C string buffer.
248 * @param cString The C string to reference.
251 * <code>true</code> on success, <code>false</code> on failure.
254 * Not for general use. Use the static instance creation method
255 * <code>@link withCStringNoCopy withCStringNoCopy@/link</code> instead.
257 * An OSString object initialized with this function
258 * does not claim ownership of the C string,
259 * but shares it with the caller.
260 * When the caller determines that the OSString object has actually been freed,
261 * it can safely dispose of the data buffer.
262 * Conversely, if it frees the shared data buffer,
263 * it must not attempt to use the OSString object and should release it.
265 * An OSString object created with this function does not
266 * allow changing the string via <code>@link setChar setChar@/link</code>.
268 virtual bool initWithCStringNoCopy(const char * cString
);
270 #if XNU_KERNEL_PRIVATE
271 bool initWithStringOfLength(const char *cString
, size_t inlength
);
272 #endif /* XNU_KERNEL_PRIVATE */
278 * Deallocates or releases any resources
279 * used by the OSString instance.
282 * This function should not be called directly;
285 * //apple_ref/cpp/instm/OSObject/release/virtualvoid/()
286 * release@/link</code>
289 virtual void free() APPLE_KEXT_OVERRIDE
;
293 * @function getLength
296 * Returns the number of characters in the OSString object.
299 * The number of characters in the OSString object.
301 virtual unsigned int getLength() const;
308 * Returns the character at a given index in the string object.
310 * @param index The index into the string.
313 * The character at <code>index</code> within the string,
314 * or <code>'\0'</code> if index is past the end of the string.
316 virtual char getChar(unsigned int index
) const;
323 * Replaces a character at a given index in the string object.
325 * @param aChar The character value to set.
326 * @param index The index into the string.
329 * <code>true</code> if the character was replaced,
330 * <code>false</code> if the was created "NoCopy"
331 * or <code>index</code> is past the end of the string.
333 virtual bool setChar(char aChar
, unsigned int index
);
337 * @function getCStringNoCopy
340 * Returns a pointer to the internal C string buffer.
343 * A pointer to the internal C string buffer.
345 virtual const char * getCStringNoCopy() const;
349 * @function isEqualTo
352 * Tests the equality of two OSString objects.
354 * @param aString The OSString object being compared against the receiver.
357 * <code>true</code> if the two OSString objects are equivalent,
358 * <code>false</code> otherwise.
361 * Two OSString objects are considered equal if they have same length
362 * and if their byte buffers hold the same contents.
364 virtual bool isEqualTo(const OSString
* aString
) const;
368 * @function isEqualTo
371 * Tests the equality of an OSString object with a C string.
373 * @param cString The C string to compare against the receiver.
376 * <code>true</code> if the OSString's characters
377 * are equivalent to the C string's,
378 * <code>false</code> otherwise.
380 virtual bool isEqualTo(const char * cString
) const;
384 * @function isEqualTo
387 * Tests the equality of an OSString object to an arbitrary object.
389 * @param anObject The object to be compared against the receiver.
392 * Returns <code>true</code> if the two objects are equivalent,
393 * <code>false</code> otherwise.
396 * An OSString is considered equal to another object
397 * if that object is derived from OSString
398 * and contains the equivalent bytes of the same length.
400 virtual bool isEqualTo(const OSMetaClassBase
* anObject
) const APPLE_KEXT_OVERRIDE
;
404 * @function isEqualTo
407 * Tests the equality of an OSData object and the OSString instance.
409 * @param aDataObject An OSData object.
412 * <code>true</code> if the two objects are equivalent, <code>false</code> otherwise.
415 * This function compares the bytes of the OSData object
416 * against those of the OSString,
417 * accounting for the possibility that an OSData
418 * might explicitly include a nul
419 * character as part of its total length.
420 * Thus, for example, an OSData object containing
421 * either the bytes <'u', 's', 'b', '\0'>
423 * will compare as equal to the OSString containing "usb".
425 virtual bool isEqualTo(const OSData
* aDataObject
) const;
429 * @function serialize
432 * Archives the receiver into the provided
433 * @link //apple_ref/doc/class/OSSerialize OSSerialize@/link object.
435 * @param serializer The OSSerialize object.
438 * <code>true</code> if serialization succeeds, <code>false</code> if not.
440 virtual bool serialize(OSSerialize
* serializer
) const APPLE_KEXT_OVERRIDE
;
442 OSMetaClassDeclareReservedUnused(OSString
, 0);
443 OSMetaClassDeclareReservedUnused(OSString
, 1);
444 OSMetaClassDeclareReservedUnused(OSString
, 2);
445 OSMetaClassDeclareReservedUnused(OSString
, 3);
446 OSMetaClassDeclareReservedUnused(OSString
, 4);
447 OSMetaClassDeclareReservedUnused(OSString
, 5);
448 OSMetaClassDeclareReservedUnused(OSString
, 6);
449 OSMetaClassDeclareReservedUnused(OSString
, 7);
450 OSMetaClassDeclareReservedUnused(OSString
, 8);
451 OSMetaClassDeclareReservedUnused(OSString
, 9);
452 OSMetaClassDeclareReservedUnused(OSString
, 10);
453 OSMetaClassDeclareReservedUnused(OSString
, 11);
454 OSMetaClassDeclareReservedUnused(OSString
, 12);
455 OSMetaClassDeclareReservedUnused(OSString
, 13);
456 OSMetaClassDeclareReservedUnused(OSString
, 14);
457 OSMetaClassDeclareReservedUnused(OSString
, 15);
460 #endif /* !_OS_OSSTRING_H */