2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * The contents of this file constitute Original Code as defined in and
7 * are subject to the Apple Public Source License Version 1.1 (the
8 * "License"). You may not use this file except in compliance with the
9 * License. Please obtain a copy of the License at
10 * http://www.apple.com/publicsource and read it before using this file.
12 * This Original Code and all software distributed under the License are
13 * distributed on an "AS IS" basis, WITHOUT WARRANTY OF ANY KIND, EITHER
14 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
15 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
16 * FITNESS FOR A PARTICULAR PURPOSE OR NON-INFRINGEMENT. Please see the
17 * License for the specific language governing rights and limitations
20 * @APPLE_LICENSE_HEADER_END@
22 /* IOString.m created by rsulack on Wed 17-Sep-1997 */
23 /* IOString.cpp converted to C++ on Tue 1998-9-22 */
27 #include <libkern/c++/OSString.h>
28 #include <libkern/c++/OSSerialize.h>
29 #include <libkern/c++/OSLib.h>
30 #include <libkern/c++/OSData.h>
33 #define super OSObject
35 OSDefineMetaClassAndStructors(OSString
, OSObject
)
36 OSMetaClassDefineReservedUnused(OSString
, 0);
37 OSMetaClassDefineReservedUnused(OSString
, 1);
38 OSMetaClassDefineReservedUnused(OSString
, 2);
39 OSMetaClassDefineReservedUnused(OSString
, 3);
40 OSMetaClassDefineReservedUnused(OSString
, 4);
41 OSMetaClassDefineReservedUnused(OSString
, 5);
42 OSMetaClassDefineReservedUnused(OSString
, 6);
43 OSMetaClassDefineReservedUnused(OSString
, 7);
44 OSMetaClassDefineReservedUnused(OSString
, 8);
45 OSMetaClassDefineReservedUnused(OSString
, 9);
46 OSMetaClassDefineReservedUnused(OSString
, 10);
47 OSMetaClassDefineReservedUnused(OSString
, 11);
48 OSMetaClassDefineReservedUnused(OSString
, 12);
49 OSMetaClassDefineReservedUnused(OSString
, 13);
50 OSMetaClassDefineReservedUnused(OSString
, 14);
51 OSMetaClassDefineReservedUnused(OSString
, 15);
55 extern int debug_container_malloc_size
;
57 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
62 bool OSString::initWithString(const OSString
*aString
)
64 return initWithCString(aString
->string
);
67 bool OSString::initWithCString(const char *cString
)
69 if (!cString
|| !super::init())
72 length
= strlen(cString
) + 1;
73 string
= (char *) kalloc(length
);
77 bcopy(cString
, string
, length
);
84 bool OSString::initWithCStringNoCopy(const char *cString
)
86 if (!cString
|| !super::init())
89 length
= strlen(cString
) + 1;
90 flags
|= kOSStringNoCopy
;
91 string
= (char *) cString
;
96 OSString
*OSString::withString(const OSString
*aString
)
98 OSString
*me
= new OSString
;
100 if (me
&& !me
->initWithString(aString
)) {
108 OSString
*OSString::withCString(const char *cString
)
110 OSString
*me
= new OSString
;
112 if (me
&& !me
->initWithCString(cString
)) {
120 OSString
*OSString::withCStringNoCopy(const char *cString
)
122 OSString
*me
= new OSString
;
124 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
134 OSString
*OSString::stringWithFormat(const char *format
, ...)
136 #ifndef KERNEL // mach3xxx
143 va_start(argList
, format
);
144 me
= stringWithCapacity(256);
145 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
146 me
->length
++; // we include the null in the length
147 if (me
->Length
> 256)
158 void OSString::free()
160 if ( !(flags
& kOSStringNoCopy
) && string
) {
161 kfree((vm_offset_t
)string
, (vm_size_t
)length
);
168 unsigned int OSString::getLength() const { return length
- 1; }
170 const char *OSString::getCStringNoCopy() const
175 bool OSString::setChar(char aChar
, unsigned int index
)
177 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
178 string
[index
] = aChar
;
186 char OSString::getChar(unsigned int index
) const
189 return string
[index
];
195 bool OSString::isEqualTo(const OSString
*aString
) const
197 if (length
!= aString
->length
)
200 return isEqualTo((const char *) aString
->string
);
203 bool OSString::isEqualTo(const char *aCString
) const
205 return strcmp(string
, aCString
) == 0;
208 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
213 if ((str
= OSDynamicCast(OSString
, obj
)))
214 return isEqualTo(str
);
215 else if ((data
= OSDynamicCast (OSData
, obj
)))
216 return isEqualTo(data
);
221 bool OSString::isEqualTo(const OSData
*obj
) const
226 unsigned int dataLen
= obj
->getLength ();;
227 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
229 if (dataLen
!= length
) {
231 // check for the fact that OSData may be a buffer that
232 // that includes a termination byte and will thus have
233 // a length of the actual string length PLUS 1. In this
234 // case we verify that the additional byte is a terminator
235 // and if so count the two lengths as being the same.
237 if ( (dataLen
- length
) == 1 ) {
238 if (dataPtr
[dataLen
-1] != 0)
246 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
247 if ( *dataPtr
++ != string
[i
] )
254 bool OSString::serialize(OSSerialize
*s
) const
258 if (s
->previouslySerialized(this)) return true;
260 if (!s
->addXMLStartTag(this, "string")) return false;
263 if (!s
->addString("<")) return false;
264 } else if (*c
== '>') {
265 if (!s
->addString(">")) return false;
266 } else if (*c
== '&') {
267 if (!s
->addString("&")) return false;
269 if (!s
->addChar(*c
)) return false;
274 return s
->addXMLEndTag("string");