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 */
26 #include <libkern/c++/OSString.h>
27 #include <libkern/c++/OSSerialize.h>
28 #include <libkern/c++/OSLib.h>
29 #include <libkern/c++/OSData.h>
31 #define super OSObject
33 OSDefineMetaClassAndStructors(OSString
, OSObject
)
34 OSMetaClassDefineReservedUnused(OSString
, 0);
35 OSMetaClassDefineReservedUnused(OSString
, 1);
36 OSMetaClassDefineReservedUnused(OSString
, 2);
37 OSMetaClassDefineReservedUnused(OSString
, 3);
38 OSMetaClassDefineReservedUnused(OSString
, 4);
39 OSMetaClassDefineReservedUnused(OSString
, 5);
40 OSMetaClassDefineReservedUnused(OSString
, 6);
41 OSMetaClassDefineReservedUnused(OSString
, 7);
42 OSMetaClassDefineReservedUnused(OSString
, 8);
43 OSMetaClassDefineReservedUnused(OSString
, 9);
44 OSMetaClassDefineReservedUnused(OSString
, 10);
45 OSMetaClassDefineReservedUnused(OSString
, 11);
46 OSMetaClassDefineReservedUnused(OSString
, 12);
47 OSMetaClassDefineReservedUnused(OSString
, 13);
48 OSMetaClassDefineReservedUnused(OSString
, 14);
49 OSMetaClassDefineReservedUnused(OSString
, 15);
53 extern int debug_container_malloc_size
;
55 #define ACCUMSIZE(s) do { debug_container_malloc_size += (s); } while(0)
60 bool OSString::initWithString(const OSString
*aString
)
62 return initWithCString(aString
->string
);
65 bool OSString::initWithCString(const char *cString
)
67 if (!cString
|| !super::init())
70 length
= strlen(cString
) + 1;
71 string
= (char *) kalloc(length
);
75 bcopy(cString
, string
, length
);
82 bool OSString::initWithCStringNoCopy(const char *cString
)
84 if (!cString
|| !super::init())
87 length
= strlen(cString
) + 1;
88 flags
|= kOSStringNoCopy
;
89 string
= (char *) cString
;
94 OSString
*OSString::withString(const OSString
*aString
)
96 OSString
*me
= new OSString
;
98 if (me
&& !me
->initWithString(aString
)) {
106 OSString
*OSString::withCString(const char *cString
)
108 OSString
*me
= new OSString
;
110 if (me
&& !me
->initWithCString(cString
)) {
118 OSString
*OSString::withCStringNoCopy(const char *cString
)
120 OSString
*me
= new OSString
;
122 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
132 OSString
*OSString::stringWithFormat(const char *format
, ...)
134 #ifndef KERNEL // mach3xxx
141 va_start(argList
, format
);
142 me
= stringWithCapacity(256);
143 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
144 me
->length
++; // we include the null in the length
145 if (me
->Length
> 256)
156 void OSString::free()
158 if ( !(flags
& kOSStringNoCopy
) && string
) {
159 kfree((vm_offset_t
)string
, (vm_size_t
)length
);
166 unsigned int OSString::getLength() const { return length
- 1; }
168 const char *OSString::getCStringNoCopy() const
173 bool OSString::setChar(char aChar
, unsigned int index
)
175 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
176 string
[index
] = aChar
;
184 char OSString::getChar(unsigned int index
) const
187 return string
[index
];
193 bool OSString::isEqualTo(const OSString
*aString
) const
195 if (length
!= aString
->length
)
198 return isEqualTo((const char *) aString
->string
);
201 bool OSString::isEqualTo(const char *aCString
) const
203 return strcmp(string
, aCString
) == 0;
206 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
211 if ((str
= OSDynamicCast(OSString
, obj
)))
212 return isEqualTo(str
);
213 else if ((data
= OSDynamicCast (OSData
, obj
)))
214 return isEqualTo(data
);
219 bool OSString::isEqualTo(const OSData
*obj
) const
224 unsigned int dataLen
= obj
->getLength ();;
225 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
227 if (dataLen
!= length
) {
229 // check for the fact that OSData may be a buffer that
230 // that includes a termination byte and will thus have
231 // a length of the actual string length PLUS 1. In this
232 // case we verify that the additional byte is a terminator
233 // and if so count the two lengths as being the same.
235 if ( (dataLen
- length
) == 1 ) {
236 if (dataPtr
[dataLen
-1] != 0)
244 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
245 if ( *dataPtr
++ != string
[i
] )
252 bool OSString::serialize(OSSerialize
*s
) const
256 if (s
->previouslySerialized(this)) return true;
258 if (!s
->addXMLStartTag(this, "string")) return false;
261 if (!s
->addString("<")) return false;
262 } else if (*c
== '>') {
263 if (!s
->addString(">")) return false;
264 } else if (*c
== '&') {
265 if (!s
->addString("&")) return false;
267 if (!s
->addChar(*c
)) return false;
272 return s
->addXMLEndTag("string");