2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_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. Please obtain a copy of the License at
10 * http://www.opensource.apple.com/apsl/ and read it before using this
13 * The Original Code and all software distributed under the License are
14 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
15 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
16 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
17 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
18 * Please see the License for the specific language governing rights and
19 * limitations under the License.
21 * @APPLE_LICENSE_HEADER_END@
23 /* IOString.m created by rsulack on Wed 17-Sep-1997 */
24 /* IOString.cpp converted to C++ on Tue 1998-9-22 */
28 #include <libkern/c++/OSString.h>
29 #include <libkern/c++/OSSerialize.h>
30 #include <libkern/c++/OSLib.h>
31 #include <libkern/c++/OSData.h>
34 #define super OSObject
36 OSDefineMetaClassAndStructors(OSString
, OSObject
)
37 OSMetaClassDefineReservedUnused(OSString
, 0);
38 OSMetaClassDefineReservedUnused(OSString
, 1);
39 OSMetaClassDefineReservedUnused(OSString
, 2);
40 OSMetaClassDefineReservedUnused(OSString
, 3);
41 OSMetaClassDefineReservedUnused(OSString
, 4);
42 OSMetaClassDefineReservedUnused(OSString
, 5);
43 OSMetaClassDefineReservedUnused(OSString
, 6);
44 OSMetaClassDefineReservedUnused(OSString
, 7);
45 OSMetaClassDefineReservedUnused(OSString
, 8);
46 OSMetaClassDefineReservedUnused(OSString
, 9);
47 OSMetaClassDefineReservedUnused(OSString
, 10);
48 OSMetaClassDefineReservedUnused(OSString
, 11);
49 OSMetaClassDefineReservedUnused(OSString
, 12);
50 OSMetaClassDefineReservedUnused(OSString
, 13);
51 OSMetaClassDefineReservedUnused(OSString
, 14);
52 OSMetaClassDefineReservedUnused(OSString
, 15);
56 extern int debug_container_malloc_size
;
58 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
63 bool OSString::initWithString(const OSString
*aString
)
65 return initWithCString(aString
->string
);
68 bool OSString::initWithCString(const char *cString
)
70 if (!cString
|| !super::init())
73 length
= strlen(cString
) + 1;
74 string
= (char *) kalloc(length
);
78 bcopy(cString
, string
, length
);
85 bool OSString::initWithCStringNoCopy(const char *cString
)
87 if (!cString
|| !super::init())
90 length
= strlen(cString
) + 1;
91 flags
|= kOSStringNoCopy
;
92 string
= (char *) cString
;
97 OSString
*OSString::withString(const OSString
*aString
)
99 OSString
*me
= new OSString
;
101 if (me
&& !me
->initWithString(aString
)) {
109 OSString
*OSString::withCString(const char *cString
)
111 OSString
*me
= new OSString
;
113 if (me
&& !me
->initWithCString(cString
)) {
121 OSString
*OSString::withCStringNoCopy(const char *cString
)
123 OSString
*me
= new OSString
;
125 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
135 OSString
*OSString::stringWithFormat(const char *format
, ...)
137 #ifndef KERNEL // mach3xxx
144 va_start(argList
, format
);
145 me
= stringWithCapacity(256);
146 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
147 me
->length
++; // we include the null in the length
148 if (me
->Length
> 256)
159 void OSString::free()
161 if ( !(flags
& kOSStringNoCopy
) && string
) {
162 kfree((vm_offset_t
)string
, (vm_size_t
)length
);
169 unsigned int OSString::getLength() const { return length
- 1; }
171 const char *OSString::getCStringNoCopy() const
176 bool OSString::setChar(char aChar
, unsigned int index
)
178 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
179 string
[index
] = aChar
;
187 char OSString::getChar(unsigned int index
) const
190 return string
[index
];
196 bool OSString::isEqualTo(const OSString
*aString
) const
198 if (length
!= aString
->length
)
201 return isEqualTo((const char *) aString
->string
);
204 bool OSString::isEqualTo(const char *aCString
) const
206 return strcmp(string
, aCString
) == 0;
209 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
214 if ((str
= OSDynamicCast(OSString
, obj
)))
215 return isEqualTo(str
);
216 else if ((data
= OSDynamicCast (OSData
, obj
)))
217 return isEqualTo(data
);
222 bool OSString::isEqualTo(const OSData
*obj
) const
227 unsigned int dataLen
= obj
->getLength ();;
228 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
230 if (dataLen
!= length
) {
232 // check for the fact that OSData may be a buffer that
233 // that includes a termination byte and will thus have
234 // a length of the actual string length PLUS 1. In this
235 // case we verify that the additional byte is a terminator
236 // and if so count the two lengths as being the same.
238 if ( (dataLen
- length
) == 1 ) {
239 if (dataPtr
[dataLen
-1] != 0)
247 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
248 if ( *dataPtr
++ != string
[i
] )
255 bool OSString::serialize(OSSerialize
*s
) const
259 if (s
->previouslySerialized(this)) return true;
261 if (!s
->addXMLStartTag(this, "string")) return false;
264 if (!s
->addString("<")) return false;
265 } else if (*c
== '>') {
266 if (!s
->addString(">")) return false;
267 } else if (*c
== '&') {
268 if (!s
->addString("&")) return false;
270 if (!s
->addChar(*c
)) return false;
275 return s
->addXMLEndTag("string");