2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_OSREFERENCE_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
10 * License may not be used to create, or enable the creation or
11 * redistribution of, unlawful or unlicensed copies of an Apple operating
12 * system, or to circumvent, violate, or enable the circumvention or
13 * violation of, any terms of an Apple operating system software license
16 * Please obtain a copy of the License at
17 * http://www.opensource.apple.com/apsl/ and read it before using this
20 * The Original Code and all software distributed under the License are
21 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
22 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
23 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
24 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
25 * Please see the License for the specific language governing rights and
26 * limitations under the License.
28 * @APPLE_LICENSE_OSREFERENCE_HEADER_END@
30 /* IOString.m created by rsulack on Wed 17-Sep-1997 */
31 /* IOString.cpp converted to C++ on Tue 1998-9-22 */
35 #include <libkern/c++/OSString.h>
36 #include <libkern/c++/OSSerialize.h>
37 #include <libkern/c++/OSLib.h>
38 #include <libkern/c++/OSData.h>
41 #define super OSObject
43 OSDefineMetaClassAndStructors(OSString
, OSObject
)
44 OSMetaClassDefineReservedUnused(OSString
, 0);
45 OSMetaClassDefineReservedUnused(OSString
, 1);
46 OSMetaClassDefineReservedUnused(OSString
, 2);
47 OSMetaClassDefineReservedUnused(OSString
, 3);
48 OSMetaClassDefineReservedUnused(OSString
, 4);
49 OSMetaClassDefineReservedUnused(OSString
, 5);
50 OSMetaClassDefineReservedUnused(OSString
, 6);
51 OSMetaClassDefineReservedUnused(OSString
, 7);
52 OSMetaClassDefineReservedUnused(OSString
, 8);
53 OSMetaClassDefineReservedUnused(OSString
, 9);
54 OSMetaClassDefineReservedUnused(OSString
, 10);
55 OSMetaClassDefineReservedUnused(OSString
, 11);
56 OSMetaClassDefineReservedUnused(OSString
, 12);
57 OSMetaClassDefineReservedUnused(OSString
, 13);
58 OSMetaClassDefineReservedUnused(OSString
, 14);
59 OSMetaClassDefineReservedUnused(OSString
, 15);
63 extern int debug_container_malloc_size
;
65 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
70 bool OSString::initWithString(const OSString
*aString
)
72 return initWithCString(aString
->string
);
75 bool OSString::initWithCString(const char *cString
)
77 if (!cString
|| !super::init())
80 length
= strlen(cString
) + 1;
81 string
= (char *) kalloc(length
);
85 bcopy(cString
, string
, length
);
92 bool OSString::initWithCStringNoCopy(const char *cString
)
94 if (!cString
|| !super::init())
97 length
= strlen(cString
) + 1;
98 flags
|= kOSStringNoCopy
;
99 string
= (char *) cString
;
104 OSString
*OSString::withString(const OSString
*aString
)
106 OSString
*me
= new OSString
;
108 if (me
&& !me
->initWithString(aString
)) {
116 OSString
*OSString::withCString(const char *cString
)
118 OSString
*me
= new OSString
;
120 if (me
&& !me
->initWithCString(cString
)) {
128 OSString
*OSString::withCStringNoCopy(const char *cString
)
130 OSString
*me
= new OSString
;
132 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
142 OSString
*OSString::stringWithFormat(const char *format
, ...)
144 #ifndef KERNEL // mach3xxx
151 va_start(argList
, format
);
152 me
= stringWithCapacity(256);
153 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
154 me
->length
++; // we include the null in the length
155 if (me
->Length
> 256)
166 void OSString::free()
168 if ( !(flags
& kOSStringNoCopy
) && string
) {
169 kfree((vm_offset_t
)string
, (vm_size_t
)length
);
176 unsigned int OSString::getLength() const { return length
- 1; }
178 const char *OSString::getCStringNoCopy() const
183 bool OSString::setChar(char aChar
, unsigned int index
)
185 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
186 string
[index
] = aChar
;
194 char OSString::getChar(unsigned int index
) const
197 return string
[index
];
203 bool OSString::isEqualTo(const OSString
*aString
) const
205 if (length
!= aString
->length
)
208 return isEqualTo((const char *) aString
->string
);
211 bool OSString::isEqualTo(const char *aCString
) const
213 return strcmp(string
, aCString
) == 0;
216 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
221 if ((str
= OSDynamicCast(OSString
, obj
)))
222 return isEqualTo(str
);
223 else if ((data
= OSDynamicCast (OSData
, obj
)))
224 return isEqualTo(data
);
229 bool OSString::isEqualTo(const OSData
*obj
) const
234 unsigned int dataLen
= obj
->getLength ();;
235 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
237 if (dataLen
!= length
) {
239 // check for the fact that OSData may be a buffer that
240 // that includes a termination byte and will thus have
241 // a length of the actual string length PLUS 1. In this
242 // case we verify that the additional byte is a terminator
243 // and if so count the two lengths as being the same.
245 if ( (dataLen
- length
) == 1 ) {
246 if (dataPtr
[dataLen
-1] != 0)
254 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
255 if ( *dataPtr
++ != string
[i
] )
262 bool OSString::serialize(OSSerialize
*s
) const
266 if (s
->previouslySerialized(this)) return true;
268 if (!s
->addXMLStartTag(this, "string")) return false;
271 if (!s
->addString("<")) return false;
272 } else if (*c
== '>') {
273 if (!s
->addString(">")) return false;
274 } else if (*c
== '&') {
275 if (!s
->addString("&")) return false;
277 if (!s
->addChar(*c
)) return false;
282 return s
->addXMLEndTag("string");