2 * Copyright (c) 2000 Apple Computer, Inc. All rights reserved.
4 * @APPLE_LICENSE_HEADER_START@
6 * Copyright (c) 1999-2003 Apple Computer, Inc. All Rights Reserved.
8 * This file contains Original Code and/or Modifications of Original Code
9 * as defined in and that are subject to the Apple Public Source License
10 * Version 2.0 (the 'License'). You may not use this file except in
11 * compliance with the License. Please obtain a copy of the License at
12 * http://www.opensource.apple.com/apsl/ and read it before using this
15 * The Original Code and all software distributed under the License are
16 * distributed on an 'AS IS' basis, WITHOUT WARRANTY OF ANY KIND, EITHER
17 * EXPRESS OR IMPLIED, AND APPLE HEREBY DISCLAIMS ALL SUCH WARRANTIES,
18 * INCLUDING WITHOUT LIMITATION, ANY WARRANTIES OF MERCHANTABILITY,
19 * FITNESS FOR A PARTICULAR PURPOSE, QUIET ENJOYMENT OR NON-INFRINGEMENT.
20 * Please see the License for the specific language governing rights and
21 * limitations under the License.
23 * @APPLE_LICENSE_HEADER_END@
25 /* IOString.m created by rsulack on Wed 17-Sep-1997 */
26 /* IOString.cpp converted to C++ on Tue 1998-9-22 */
30 #include <libkern/c++/OSString.h>
31 #include <libkern/c++/OSSerialize.h>
32 #include <libkern/c++/OSLib.h>
33 #include <libkern/c++/OSData.h>
36 #define super OSObject
38 OSDefineMetaClassAndStructors(OSString
, OSObject
)
39 OSMetaClassDefineReservedUnused(OSString
, 0);
40 OSMetaClassDefineReservedUnused(OSString
, 1);
41 OSMetaClassDefineReservedUnused(OSString
, 2);
42 OSMetaClassDefineReservedUnused(OSString
, 3);
43 OSMetaClassDefineReservedUnused(OSString
, 4);
44 OSMetaClassDefineReservedUnused(OSString
, 5);
45 OSMetaClassDefineReservedUnused(OSString
, 6);
46 OSMetaClassDefineReservedUnused(OSString
, 7);
47 OSMetaClassDefineReservedUnused(OSString
, 8);
48 OSMetaClassDefineReservedUnused(OSString
, 9);
49 OSMetaClassDefineReservedUnused(OSString
, 10);
50 OSMetaClassDefineReservedUnused(OSString
, 11);
51 OSMetaClassDefineReservedUnused(OSString
, 12);
52 OSMetaClassDefineReservedUnused(OSString
, 13);
53 OSMetaClassDefineReservedUnused(OSString
, 14);
54 OSMetaClassDefineReservedUnused(OSString
, 15);
58 extern int debug_container_malloc_size
;
60 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
65 bool OSString::initWithString(const OSString
*aString
)
67 return initWithCString(aString
->string
);
70 bool OSString::initWithCString(const char *cString
)
72 if (!cString
|| !super::init())
75 length
= strlen(cString
) + 1;
76 string
= (char *) kalloc(length
);
80 bcopy(cString
, string
, length
);
87 bool OSString::initWithCStringNoCopy(const char *cString
)
89 if (!cString
|| !super::init())
92 length
= strlen(cString
) + 1;
93 flags
|= kOSStringNoCopy
;
94 string
= (char *) cString
;
99 OSString
*OSString::withString(const OSString
*aString
)
101 OSString
*me
= new OSString
;
103 if (me
&& !me
->initWithString(aString
)) {
111 OSString
*OSString::withCString(const char *cString
)
113 OSString
*me
= new OSString
;
115 if (me
&& !me
->initWithCString(cString
)) {
123 OSString
*OSString::withCStringNoCopy(const char *cString
)
125 OSString
*me
= new OSString
;
127 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
137 OSString
*OSString::stringWithFormat(const char *format
, ...)
139 #ifndef KERNEL // mach3xxx
146 va_start(argList
, format
);
147 me
= stringWithCapacity(256);
148 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
149 me
->length
++; // we include the null in the length
150 if (me
->Length
> 256)
161 void OSString::free()
163 if ( !(flags
& kOSStringNoCopy
) && string
) {
164 kfree((vm_offset_t
)string
, (vm_size_t
)length
);
171 unsigned int OSString::getLength() const { return length
- 1; }
173 const char *OSString::getCStringNoCopy() const
178 bool OSString::setChar(char aChar
, unsigned int index
)
180 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
181 string
[index
] = aChar
;
189 char OSString::getChar(unsigned int index
) const
192 return string
[index
];
198 bool OSString::isEqualTo(const OSString
*aString
) const
200 if (length
!= aString
->length
)
203 return isEqualTo((const char *) aString
->string
);
206 bool OSString::isEqualTo(const char *aCString
) const
208 return strcmp(string
, aCString
) == 0;
211 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
216 if ((str
= OSDynamicCast(OSString
, obj
)))
217 return isEqualTo(str
);
218 else if ((data
= OSDynamicCast (OSData
, obj
)))
219 return isEqualTo(data
);
224 bool OSString::isEqualTo(const OSData
*obj
) const
229 unsigned int dataLen
= obj
->getLength ();;
230 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
232 if (dataLen
!= length
) {
234 // check for the fact that OSData may be a buffer that
235 // that includes a termination byte and will thus have
236 // a length of the actual string length PLUS 1. In this
237 // case we verify that the additional byte is a terminator
238 // and if so count the two lengths as being the same.
240 if ( (dataLen
- length
) == 1 ) {
241 if (dataPtr
[dataLen
-1] != 0)
249 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
250 if ( *dataPtr
++ != string
[i
] )
257 bool OSString::serialize(OSSerialize
*s
) const
261 if (s
->previouslySerialized(this)) return true;
263 if (!s
->addXMLStartTag(this, "string")) return false;
266 if (!s
->addString("<")) return false;
267 } else if (*c
== '>') {
268 if (!s
->addString(">")) return false;
269 } else if (*c
== '&') {
270 if (!s
->addString("&")) return false;
272 if (!s
->addChar(*c
)) return false;
277 return s
->addXMLEndTag("string");