2
* Copyright (C) 2003 Apple Computer, Inc. All rights reserved.
4
* Redistribution and use in source and binary forms, with or without
5
* modification, are permitted provided that the following conditions
7
* 1. Redistributions of source code must retain the above copyright
8
* notice, this list of conditions and the following disclaimer.
9
* 2. Redistributions in binary form must reproduce the above copyright
10
* notice, this list of conditions and the following disclaimer in the
11
* documentation and/or other materials provided with the distribution.
13
* THIS SOFTWARE IS PROVIDED BY APPLE COMPUTER, INC. ``AS IS'' AND ANY
14
* EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
15
* IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR
16
* PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL APPLE COMPUTER, INC. OR
17
* CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
18
* EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
19
* PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
20
* PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY
21
* OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
22
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
23
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27
#include "DeprecatedCString.h"
29
#include <wtf/Assertions.h>
34
DeprecatedCString::DeprecatedCString()
38
DeprecatedCString::DeprecatedCString(int size) : DeprecatedByteArray(size)
40
if( size>0 && data() )
42
data()[0] = 0; // first null
43
data()[size-1] = 0; // last byte
49
DeprecatedCString::DeprecatedCString(const char *str)
52
if( str && (len=strlen(str)+1) && resize(len) ) // include null
53
strcpy( data(), str );
58
DeprecatedCString::DeprecatedCString(const char *str, unsigned max)
62
// perform a truncated strlen on str
64
unsigned len = 1; // for the null
65
while( *p++ && len<max )
71
strncpy( dest, str, len );
72
dest[len-1] = 0; // re-terminate
78
bool DeprecatedCString::isEmpty() const
79
{ return length()==0; }
82
unsigned DeprecatedCString::length() const
84
const char *d = data();
85
return d ? strlen(d) : 0;
89
bool DeprecatedCString::resize(unsigned len)
91
bool success = DeprecatedByteArray::resize(len);
92
if( success && len>0 )
93
data()[len-1] = 0; // always terminate last byte
99
bool DeprecatedCString::truncate(unsigned pos)
101
return resize(pos+1);
105
DeprecatedCString DeprecatedCString::lower() const
108
DeprecatedCString tmp = *this; // copy
109
char* str = tmp.data();
114
*str = tolower(*str);
123
DeprecatedCString DeprecatedCString::upper() const
125
DeprecatedCString tmp = *this; // copy
126
char* str = tmp.data();
131
*str = toupper(*str);
140
inline DeprecatedCString DeprecatedCString::left(unsigned len) const
141
{ return mid(0, len); }
144
inline DeprecatedCString DeprecatedCString::right(unsigned len) const
145
{ return mid(length() - len, len); }
148
DeprecatedCString DeprecatedCString::mid(unsigned index, unsigned len) const
150
unsigned size = length();
151
if( data() && index<size ) // return null if index out-of-range
154
if( len > size - index )
158
return DeprecatedCString( &(data()[index]), len+1); // include nul
162
return DeprecatedCString();
165
int DeprecatedCString::find(const char *sub, int index, bool cs) const
167
const char* str = data();
168
if( str && str[0] && sub && index>=0 ) // don't search empty strings
170
// advance until we get to index
173
if( str[pos++] == 0 )
174
return -1; // index is beyond end of str
176
// now search from index onward
177
while( str[index] != 0 )
181
// compare until we reach the end or a mismatch
184
while( (a=sub[pos]) && (b=str[index]) && a==b )
187
while( (a=sub[pos]) && (b=str[index]) && tolower(a)==tolower(b) )
190
// reached the end of our compare string without a mismatch?
201
int DeprecatedCString::contains(char c, bool cs) const
204
unsigned len = length();
207
const char *str = data();
210
for (unsigned i = 0; i != len; ++i) {
211
found += str[i] == c;
216
for (unsigned i = 0; i != len; ++i) {
227
DeprecatedCString &DeprecatedCString::operator=(const char *assignFrom)
229
duplicate(assignFrom, (assignFrom ? strlen(assignFrom) : 0) + 1);
233
DeprecatedCString& DeprecatedCString::append(const char *s)
236
unsigned len2 = strlen(s);
239
unsigned len1 = length();
240
if (DeprecatedByteArray::resize(len1 + len2 + 1))
241
memcpy(data() + len1, s, len2 + 1);
248
DeprecatedCString &DeprecatedCString::append(char c)
251
unsigned len = length();
253
if (DeprecatedByteArray::resize(len + 2)) {
255
*(data() + len + 1) = '\0';
261
DeprecatedCString &DeprecatedCString::replace(char c1, char c2)
263
unsigned len = length();
266
// Search for the first instance of c1 before detaching,
267
// just in case there is nothing to replace. In that case
268
// we don't want to detach this from other shared instances
269
// since we have no need to modify it.
272
const char *s = data();
273
for (i = 0; i != len; ++i) {
283
// Start at the first instance of c1; no need to rescan earlier chars.
284
for (; i != len; ++i) {
295
bool operator==(const DeprecatedCString &s1, const char *s2)
297
if (s1.size() == 0 && !s2)
299
if (s1.size() == 0 && s2)
301
return strcmp(s1, s2) == 0;