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);
61 extern int debug_container_malloc_size
;
63 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
68 bool OSString::initWithString(const OSString
*aString
)
70 return initWithCString(aString
->string
);
73 bool OSString::initWithCString(const char *cString
)
75 if (!cString
|| !super::init())
78 length
= strlen(cString
) + 1;
79 string
= (char *) kalloc(length
);
83 bcopy(cString
, string
, length
);
90 bool OSString::initWithCStringNoCopy(const char *cString
)
92 if (!cString
|| !super::init())
95 length
= strlen(cString
) + 1;
96 flags
|= kOSStringNoCopy
;
97 string
= (char *) cString
;
102 OSString
*OSString::withString(const OSString
*aString
)
104 OSString
*me
= new OSString
;
106 if (me
&& !me
->initWithString(aString
)) {
114 OSString
*OSString::withCString(const char *cString
)
116 OSString
*me
= new OSString
;
118 if (me
&& !me
->initWithCString(cString
)) {
126 OSString
*OSString::withCStringNoCopy(const char *cString
)
128 OSString
*me
= new OSString
;
130 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
140 OSString
*OSString::stringWithFormat(const char *format
, ...)
142 #ifndef KERNEL // mach3xxx
149 va_start(argList
, format
);
150 me
= stringWithCapacity(256);
151 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
152 me
->length
++; // we include the null in the length
153 if (me
->Length
> 256)
164 void OSString::free()
166 if ( !(flags
& kOSStringNoCopy
) && string
) {
167 kfree(string
, (vm_size_t
)length
);
174 unsigned int OSString::getLength() const { return length
- 1; }
176 const char *OSString::getCStringNoCopy() const
181 bool OSString::setChar(char aChar
, unsigned int index
)
183 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
184 string
[index
] = aChar
;
192 char OSString::getChar(unsigned int index
) const
195 return string
[index
];
201 bool OSString::isEqualTo(const OSString
*aString
) const
203 if (length
!= aString
->length
)
206 return isEqualTo((const char *) aString
->string
);
209 bool OSString::isEqualTo(const char *aCString
) const
211 return strcmp(string
, aCString
) == 0;
214 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
219 if ((str
= OSDynamicCast(OSString
, obj
)))
220 return isEqualTo(str
);
221 else if ((data
= OSDynamicCast (OSData
, obj
)))
222 return isEqualTo(data
);
227 bool OSString::isEqualTo(const OSData
*obj
) const
232 unsigned int dataLen
= obj
->getLength ();;
233 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
235 if (dataLen
!= length
) {
237 // check for the fact that OSData may be a buffer that
238 // that includes a termination byte and will thus have
239 // a length of the actual string length PLUS 1. In this
240 // case we verify that the additional byte is a terminator
241 // and if so count the two lengths as being the same.
243 if ( (dataLen
- length
) == 1 ) {
244 if (dataPtr
[dataLen
-1] != 0)
252 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
253 if ( *dataPtr
++ != string
[i
] )
260 bool OSString::serialize(OSSerialize
*s
) const
264 if (s
->previouslySerialized(this)) return true;
266 if (!s
->addXMLStartTag(this, "string")) return false;
269 if (!s
->addString("<")) return false;
270 } else if (*c
== '>') {
271 if (!s
->addString(">")) return false;
272 } else if (*c
== '&') {
273 if (!s
->addString("&")) return false;
275 if (!s
->addChar(*c
)) return false;
280 return s
->addXMLEndTag("string");