]>
git.saurik.com Git - apple/xnu.git/blob - libkern/c++/OSString.cpp
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);
59 bool OSString::initWithString(const OSString
*aString
)
61 return initWithCString(aString
->string
);
64 bool OSString::initWithCString(const char *cString
)
66 unsigned int newLength
;
69 if (!cString
|| !super::init()) return false;
71 newLength
= strnlen(cString
, kMaxStringLength
);
72 if (newLength
>= kMaxStringLength
) return false;
75 newString
= (char *) kalloc_container(newLength
);
76 if (!newString
) return false;
78 bcopy(cString
, newString
, newLength
);
80 if ( !(flags
& kOSStringNoCopy
) && string
) {
81 kfree(string
, (vm_size_t
)length
);
82 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
86 flags
&= ~kOSStringNoCopy
;
88 OSCONTAINER_ACCUMSIZE(length
);
93 bool OSString::initWithStringOfLength(const char *cString
, size_t inlength
)
95 unsigned int newLength
;
98 if (!cString
|| !super::init()) return false;
100 if (inlength
>= kMaxStringLength
) return false;
102 newLength
= inlength
+ 1;
103 newString
= (char *) kalloc_container(newLength
);
104 if (!newString
) return false;
106 bcopy(cString
, newString
, inlength
);
107 newString
[inlength
] = 0;
109 if ( !(flags
& kOSStringNoCopy
) && string
) {
110 kfree(string
, (vm_size_t
)length
);
111 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
116 flags
&= ~kOSStringNoCopy
;
118 OSCONTAINER_ACCUMSIZE(length
);
123 bool OSString::initWithCStringNoCopy(const char *cString
)
125 if (!cString
|| !super::init())
128 length
= strnlen(cString
, kMaxStringLength
);
129 if (length
>= kMaxStringLength
) return false;
132 flags
|= kOSStringNoCopy
;
133 string
= const_cast<char *>(cString
);
138 OSString
*OSString::withString(const OSString
*aString
)
140 OSString
*me
= new OSString
;
142 if (me
&& !me
->initWithString(aString
)) {
150 OSString
*OSString::withCString(const char *cString
)
152 OSString
*me
= new OSString
;
154 if (me
&& !me
->initWithCString(cString
)) {
162 OSString
*OSString::withCStringNoCopy(const char *cString
)
164 OSString
*me
= new OSString
;
166 if (me
&& !me
->initWithCStringNoCopy(cString
)) {
174 OSString
*OSString::withStringOfLength(const char *cString
, size_t length
)
176 OSString
*me
= new OSString
;
178 if (me
&& !me
->initWithStringOfLength(cString
, length
)) {
190 OSString
*OSString::stringWithFormat(const char *format
, ...)
192 #ifndef KERNEL // mach3xxx
199 va_start(argList
, format
);
200 me
= stringWithCapacity(256);
201 me
->length
= vsnprintf(me
->string
, 256, format
, argList
);
202 me
->length
++; // we include the null in the length
203 if (me
->Length
> 256)
214 void OSString::free()
216 if ( !(flags
& kOSStringNoCopy
) && string
) {
217 kfree(string
, (vm_size_t
)length
);
218 OSCONTAINER_ACCUMSIZE(-((size_t)length
));
224 unsigned int OSString::getLength() const { return length
- 1; }
226 const char *OSString::getCStringNoCopy() const
231 bool OSString::setChar(char aChar
, unsigned int index
)
233 if ( !(flags
& kOSStringNoCopy
) && index
< length
- 1) {
234 string
[index
] = aChar
;
242 char OSString::getChar(unsigned int index
) const
245 return string
[index
];
251 bool OSString::isEqualTo(const OSString
*aString
) const
253 if (length
!= aString
->length
)
256 return isEqualTo((const char *) aString
->string
);
259 bool OSString::isEqualTo(const char *aCString
) const
261 return strncmp(string
, aCString
, length
) == 0;
264 bool OSString::isEqualTo(const OSMetaClassBase
*obj
) const
269 if ((str
= OSDynamicCast(OSString
, obj
)))
270 return isEqualTo(str
);
271 else if ((data
= OSDynamicCast (OSData
, obj
)))
272 return isEqualTo(data
);
277 bool OSString::isEqualTo(const OSData
*obj
) const
282 unsigned int dataLen
= obj
->getLength ();;
283 char * dataPtr
= (char *) obj
->getBytesNoCopy ();
285 if (dataLen
!= length
) {
287 // check for the fact that OSData may be a buffer that
288 // that includes a termination byte and will thus have
289 // a length of the actual string length PLUS 1. In this
290 // case we verify that the additional byte is a terminator
291 // and if so count the two lengths as being the same.
293 if ( (dataLen
- length
) == 1 ) {
294 if (dataPtr
[dataLen
-1] != 0)
302 for ( unsigned int i
=0; i
< dataLen
; i
++ ) {
303 if ( *dataPtr
++ != string
[i
] )
310 bool OSString::serialize(OSSerialize
*s
) const
314 if (s
->previouslySerialized(this)) return true;
316 if (!s
->addXMLStartTag(this, "string")) return false;
319 if (!s
->addString("<")) return false;
320 } else if (*c
== '>') {
321 if (!s
->addString(">")) return false;
322 } else if (*c
== '&') {
323 if (!s
->addString("&")) return false;
325 if (!s
->addChar(*c
)) return false;
330 return s
->addXMLEndTag("string");