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::initWithStringOfLength(const char *cString
, size_t inlength
)
92 if (!cString
|| !super::init())
95 length
= inlength
+ 1;
96 string
= (char *) kalloc(length
);
100 bcopy(cString
, string
, inlength
);
101 string
[inlength
] = 0;
108 bool OSString::initWithCStringNoCopy(const char *cString
)
110 if (!cString
|| !super::init())
113 length
= strlen(cString
) + 1;
114 flags
|= kOSStringNoCopy
;
115 string
= const_cast<char *>(cString
);
120 OSString
*OSString::withString(const OSString
*aString
)
122 OSString
*me
= new OSString
;
124 if (me
&& !me
->initWithString(aString
)) {
132 OSString
*OSString::withCString(const char *cString
)
134 OSString
*me
= new OSString
;
136 if (me
&& !me
->initWithCString(cString
)) {
144 OSString
*OSString::withCStringNoCopy(const char *cString
)
146 OSString
*me
= new OSString
;
148 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
156 OSString
*OSString::withStringOfLength(const char *cString
, size_t length
)
158 OSString
*me
= new OSString
;
160 if (me
&& !me
->initWithStringOfLength(cString
, length
)) {
172 OSString
*OSString::stringWithFormat(const char *format
, ...)
174 #ifndef KERNEL // mach3xxx
181 va_start(argList
, format
);
182 me
= stringWithCapacity(256);
183 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
184 me
->length
++; // we include the null in the length
185 if (me
->Length
> 256)
196 void OSString::free()
198 if ( !(flags
& kOSStringNoCopy
) && string
) {
199 kfree(string
, (vm_size_t
)length
);
206 unsigned int OSString::getLength() const { return length
- 1; }
208 const char *OSString::getCStringNoCopy() const
213 bool OSString::setChar(char aChar
, unsigned int index
)
215 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
216 string
[index
] = aChar
;
224 char OSString::getChar(unsigned int index
) const
227 return string
[index
];
233 bool OSString::isEqualTo(const OSString
*aString
) const
235 if (length
!= aString
->length
)
238 return isEqualTo((const char *) aString
->string
);
241 bool OSString::isEqualTo(const char *aCString
) const
243 return strncmp(string
, aCString
, length
) == 0;
246 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
251 if ((str
= OSDynamicCast(OSString
, obj
)))
252 return isEqualTo(str
);
253 else if ((data
= OSDynamicCast (OSData
, obj
)))
254 return isEqualTo(data
);
259 bool OSString::isEqualTo(const OSData
*obj
) const
264 unsigned int dataLen
= obj
->getLength ();;
265 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
267 if (dataLen
!= length
) {
269 // check for the fact that OSData may be a buffer that
270 // that includes a termination byte and will thus have
271 // a length of the actual string length PLUS 1. In this
272 // case we verify that the additional byte is a terminator
273 // and if so count the two lengths as being the same.
275 if ( (dataLen
- length
) == 1 ) {
276 if (dataPtr
[dataLen
-1] != 0)
284 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
285 if ( *dataPtr
++ != string
[i
] )
292 bool OSString::serialize(OSSerialize
*s
) const
296 if (s
->previouslySerialized(this)) return true;
298 if (!s
->addXMLStartTag(this, "string")) return false;
301 if (!s
->addString("<")) return false;
302 } else if (*c
== '>') {
303 if (!s
->addString(">")) return false;
304 } else if (*c
== '&') {
305 if (!s
->addString("&")) return false;
307 if (!s
->addChar(*c
)) return false;
312 return s
->addXMLEndTag("string");