]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSString.cpp
2bd875ee6b5cbd01d247170ca02a144fe02ad1b6
2 * Copyright (c) 2000 Apple Computer, 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.m created by rsulack on Wed 17-Sep-1997 */
29 /* IOString.cpp converted to C++ on Tue 1998-9-22 */
33 #include <libkern/c++/OSString.h>
34 #include <libkern/c++/OSSerialize.h>
35 #include <libkern/c++/OSLib.h>
36 #include <libkern/c++/OSData.h>
39 #define super OSObject
41 OSDefineMetaClassAndStructors(OSString
, OSObject
)
42 OSMetaClassDefineReservedUnused(OSString
, 0);
43 OSMetaClassDefineReservedUnused(OSString
, 1);
44 OSMetaClassDefineReservedUnused(OSString
, 2);
45 OSMetaClassDefineReservedUnused(OSString
, 3);
46 OSMetaClassDefineReservedUnused(OSString
, 4);
47 OSMetaClassDefineReservedUnused(OSString
, 5);
48 OSMetaClassDefineReservedUnused(OSString
, 6);
49 OSMetaClassDefineReservedUnused(OSString
, 7);
50 OSMetaClassDefineReservedUnused(OSString
, 8);
51 OSMetaClassDefineReservedUnused(OSString
, 9);
52 OSMetaClassDefineReservedUnused(OSString
, 10);
53 OSMetaClassDefineReservedUnused(OSString
, 11);
54 OSMetaClassDefineReservedUnused(OSString
, 12);
55 OSMetaClassDefineReservedUnused(OSString
, 13);
56 OSMetaClassDefineReservedUnused(OSString
, 14);
57 OSMetaClassDefineReservedUnused(OSString
, 15);
59 bool OSString::initWithString(const OSString
*aString
)
61 return initWithCString(aString
->string
);
64 bool OSString::initWithCString(const char *cString
)
66 unsigned int newLength
;
69 if (!cString
|| !super::init()) return false;
71 newLength
= strlen(cString
) + 1;
72 newString
= (char *) kalloc_container(newLength
);
73 if (!newString
) return false;
75 bcopy(cString
, newString
, newLength
);
77 if ( !(flags
& kOSStringNoCopy
) && string
) {
78 kfree(string
, (vm_size_t
)length
);
79 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
83 flags
&= ~kOSStringNoCopy
;
85 OSCONTAINER_ACCUMSIZE(length
);
90 bool OSString::initWithStringOfLength(const char *cString
, size_t inlength
)
92 unsigned int newLength
;
95 if (!cString
|| !super::init()) return false;
97 newLength
= inlength
+ 1;
98 newString
= (char *) kalloc_container(newLength
);
99 if (!newString
) return false;
101 bcopy(cString
, newString
, inlength
);
102 newString
[inlength
] = 0;
104 if ( !(flags
& kOSStringNoCopy
) && string
) {
105 kfree(string
, (vm_size_t
)length
);
106 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
111 flags
&= ~kOSStringNoCopy
;
113 OSCONTAINER_ACCUMSIZE(length
);
118 bool OSString::initWithCStringNoCopy(const char *cString
)
120 if (!cString
|| !super::init())
123 length
= strlen(cString
) + 1;
124 flags
|= kOSStringNoCopy
;
125 string
= const_cast<char *>(cString
);
130 OSString
*OSString::withString(const OSString
*aString
)
132 OSString
*me
= new OSString
;
134 if (me
&& !me
->initWithString(aString
)) {
142 OSString
*OSString::withCString(const char *cString
)
144 OSString
*me
= new OSString
;
146 if (me
&& !me
->initWithCString(cString
)) {
154 OSString
*OSString::withCStringNoCopy(const char *cString
)
156 OSString
*me
= new OSString
;
158 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
166 OSString
*OSString::withStringOfLength(const char *cString
, size_t length
)
168 OSString
*me
= new OSString
;
170 if (me
&& !me
->initWithStringOfLength(cString
, length
)) {
182 OSString
*OSString::stringWithFormat(const char *format
, ...)
184 #ifndef KERNEL // mach3xxx
191 va_start(argList
, format
);
192 me
= stringWithCapacity(256);
193 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
194 me
->length
++; // we include the null in the length
195 if (me
->Length
> 256)
206 void OSString::free()
208 if ( !(flags
& kOSStringNoCopy
) && string
) {
209 kfree(string
, (vm_size_t
)length
);
210 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
216 unsigned int OSString::getLength() const { return length
- 1; }
218 const char *OSString::getCStringNoCopy() const
223 bool OSString::setChar(char aChar
, unsigned int index
)
225 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
226 string
[index
] = aChar
;
234 char OSString::getChar(unsigned int index
) const
237 return string
[index
];
243 bool OSString::isEqualTo(const OSString
*aString
) const
245 if (length
!= aString
->length
)
248 return isEqualTo((const char *) aString
->string
);
251 bool OSString::isEqualTo(const char *aCString
) const
253 return strncmp(string
, aCString
, length
) == 0;
256 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
261 if ((str
= OSDynamicCast(OSString
, obj
)))
262 return isEqualTo(str
);
263 else if ((data
= OSDynamicCast (OSData
, obj
)))
264 return isEqualTo(data
);
269 bool OSString::isEqualTo(const OSData
*obj
) const
274 unsigned int dataLen
= obj
->getLength ();;
275 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
277 if (dataLen
!= length
) {
279 // check for the fact that OSData may be a buffer that
280 // that includes a termination byte and will thus have
281 // a length of the actual string length PLUS 1. In this
282 // case we verify that the additional byte is a terminator
283 // and if so count the two lengths as being the same.
285 if ( (dataLen
- length
) == 1 ) {
286 if (dataPtr
[dataLen
-1] != 0)
294 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
295 if ( *dataPtr
++ != string
[i
] )
302 bool OSString::serialize(OSSerialize
*s
) const
306 if (s
->previouslySerialized(this)) return true;
308 if (!s
->addXMLStartTag(this, "string")) return false;
311 if (!s
->addString("<")) return false;
312 } else if (*c
== '>') {
313 if (!s
->addString(">")) return false;
314 } else if (*c
== '&') {
315 if (!s
->addString("&")) return false;
317 if (!s
->addChar(*c
)) return false;
322 return s
->addXMLEndTag("string");