~ubuntu-branches/ubuntu/maverick/webkit/maverick

« back to all changes in this revision

Viewing changes to WebCore/platform/DeprecatedCString.cpp

  • Committer: Bazaar Package Importer
  • Author(s): Mike Hommey
  • Date: 2007-08-19 15:54:12 UTC
  • Revision ID: james.westby@ubuntu.com-20070819155412-uxxg1h9plpghmtbi
Tags: upstream-0~svn25144
ImportĀ upstreamĀ versionĀ 0~svn25144

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
 * Copyright (C) 2003 Apple Computer, Inc.  All rights reserved.
 
3
 *
 
4
 * Redistribution and use in source and binary forms, with or without
 
5
 * modification, are permitted provided that the following conditions
 
6
 * are met:
 
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.
 
12
 *
 
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. 
 
24
 */
 
25
 
 
26
#include "config.h"
 
27
#include "DeprecatedCString.h"
 
28
 
 
29
#include <wtf/Assertions.h>
 
30
#include <ctype.h>
 
31
 
 
32
namespace WebCore {
 
33
 
 
34
DeprecatedCString::DeprecatedCString()
 
35
{
 
36
}
 
37
 
 
38
DeprecatedCString::DeprecatedCString(int size) : DeprecatedByteArray(size)
 
39
{
 
40
    if( size>0 && data() )
 
41
    {
 
42
        data()[0] = 0;          // first null
 
43
        data()[size-1] = 0;     // last byte
 
44
    }
 
45
    // else null
 
46
}
 
47
 
 
48
 
 
49
DeprecatedCString::DeprecatedCString(const char *str)
 
50
{
 
51
    size_t len;
 
52
    if( str && (len=strlen(str)+1) && resize(len) )     // include null
 
53
        strcpy( data(), str );
 
54
    // else null
 
55
}
 
56
 
 
57
 
 
58
DeprecatedCString::DeprecatedCString(const char *str, unsigned max)
 
59
{
 
60
    if( str && max )
 
61
    {
 
62
        // perform a truncated strlen on str
 
63
        const char* p = str;
 
64
        unsigned len = 1;                   // for the null
 
65
        while( *p++ && len<max )
 
66
            len ++;
 
67
 
 
68
        if( resize(len) )
 
69
        {
 
70
            char *dest = data();
 
71
            strncpy( dest, str, len );
 
72
            dest[len-1] = 0;            // re-terminate
 
73
        }
 
74
    }
 
75
    // else null
 
76
}
 
77
 
 
78
bool DeprecatedCString::isEmpty() const
 
79
{ return length()==0; }
 
80
 
 
81
 
 
82
unsigned DeprecatedCString::length() const
 
83
{
 
84
    const char *d = data();
 
85
    return d ? strlen(d) : 0;
 
86
}
 
87
 
 
88
 
 
89
bool DeprecatedCString::resize(unsigned len)
 
90
{
 
91
    bool success = DeprecatedByteArray::resize(len);
 
92
    if( success && len>0 )
 
93
        data()[len-1] = 0;      // always terminate last byte
 
94
 
 
95
    return success;
 
96
}
 
97
 
 
98
 
 
99
bool DeprecatedCString::truncate(unsigned pos)
 
100
{
 
101
    return resize(pos+1);
 
102
}
 
103
 
 
104
 
 
105
DeprecatedCString DeprecatedCString::lower() const
 
106
{
 
107
    // convert
 
108
    DeprecatedCString tmp = *this;       // copy
 
109
    char* str = tmp.data();
 
110
    if( str )
 
111
    {
 
112
        while( *str != 0 )
 
113
        {
 
114
            *str = tolower(*str);
 
115
            str++;
 
116
        }
 
117
    }
 
118
 
 
119
    return tmp;
 
120
}
 
121
 
 
122
 
 
123
DeprecatedCString DeprecatedCString::upper() const
 
124
{
 
125
    DeprecatedCString tmp = *this;       // copy
 
126
    char* str = tmp.data();
 
127
    if( str )
 
128
    {
 
129
        while( *str != 0 )
 
130
        {
 
131
            *str = toupper(*str);
 
132
            str++;
 
133
        }
 
134
    }
 
135
 
 
136
    return tmp;
 
137
}
 
138
 
 
139
 
 
140
inline DeprecatedCString DeprecatedCString::left(unsigned len) const
 
141
{ return mid(0, len); }
 
142
 
 
143
 
 
144
inline DeprecatedCString DeprecatedCString::right(unsigned len) const
 
145
{ return mid(length() - len, len); }
 
146
 
 
147
 
 
148
DeprecatedCString DeprecatedCString::mid(unsigned index, unsigned len) const
 
149
{
 
150
    unsigned size = length();
 
151
    if( data() && index<size )      // return null if index out-of-range
 
152
    {
 
153
        // clip length
 
154
        if( len > size - index )
 
155
            len = size - index;
 
156
 
 
157
        // copy and return
 
158
        return DeprecatedCString( &(data()[index]), len+1);  // include nul
 
159
    }
 
160
 
 
161
    // degenerate case
 
162
    return DeprecatedCString();
 
163
}
 
164
 
 
165
int DeprecatedCString::find(const char *sub, int index, bool cs) const
 
166
{
 
167
    const char* str = data();
 
168
    if( str && str[0] && sub && index>=0 )  // don't search empty strings
 
169
    {
 
170
        // advance until we get to index
 
171
        int pos = 0;
 
172
        while( pos < index )
 
173
            if( str[pos++] == 0 )
 
174
                return -1;                  // index is beyond end of str
 
175
        
 
176
        // now search from index onward
 
177
        while( str[index] != 0 )
 
178
        {
 
179
            char a, b;
 
180
            
 
181
            // compare until we reach the end or a mismatch
 
182
            pos = 0;
 
183
            if( cs )
 
184
                while( (a=sub[pos]) && (b=str[index]) && a==b )
 
185
                    pos++, index++;
 
186
            else
 
187
                while( (a=sub[pos]) && (b=str[index]) && tolower(a)==tolower(b) )
 
188
                    pos++, index++;
 
189
            
 
190
            // reached the end of our compare string without a mismatch?
 
191
            if( sub[pos] == 0 )
 
192
                return index - pos;
 
193
            
 
194
            index ++;
 
195
        }
 
196
    }
 
197
    
 
198
    return -1;
 
199
}
 
200
 
 
201
int DeprecatedCString::contains(char c, bool cs) const
 
202
{
 
203
    unsigned found = 0;
 
204
    unsigned len = length();
 
205
 
 
206
    if (len) {
 
207
        const char *str = data();
 
208
 
 
209
        if (cs) {
 
210
            for (unsigned i = 0; i != len; ++i) {
 
211
                found += str[i] == c;
 
212
            }
 
213
        } else {
 
214
            c = tolower(c);
 
215
 
 
216
            for (unsigned i = 0; i != len; ++i) {
 
217
                char chr = str[i];
 
218
                chr = tolower(chr);
 
219
                found += chr == c;
 
220
            }
 
221
        }
 
222
    }
 
223
 
 
224
    return found;
 
225
}
 
226
 
 
227
DeprecatedCString &DeprecatedCString::operator=(const char *assignFrom)
 
228
{
 
229
    duplicate(assignFrom, (assignFrom ? strlen(assignFrom) : 0) + 1);
 
230
    return *this;
 
231
}
 
232
 
 
233
DeprecatedCString& DeprecatedCString::append(const char *s)
 
234
{
 
235
    if (s) {
 
236
        unsigned len2 = strlen(s);
 
237
        if (len2) {
 
238
            detach();
 
239
            unsigned len1 = length();
 
240
            if (DeprecatedByteArray::resize(len1 + len2 + 1))
 
241
                memcpy(data() + len1, s, len2 + 1);
 
242
        }
 
243
    }
 
244
 
 
245
    return *this;
 
246
}
 
247
 
 
248
DeprecatedCString &DeprecatedCString::append(char c)
 
249
{
 
250
    detach();
 
251
    unsigned len = length();
 
252
 
 
253
    if (DeprecatedByteArray::resize(len + 2)) {
 
254
        *(data() + len) = c;
 
255
        *(data() + len + 1) = '\0';
 
256
    }
 
257
 
 
258
    return *this;
 
259
}
 
260
 
 
261
DeprecatedCString &DeprecatedCString::replace(char c1, char c2)
 
262
{
 
263
    unsigned len = length();
 
264
 
 
265
    if (len) {
 
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.
 
270
        unsigned i;
 
271
        {
 
272
            const char *s = data();
 
273
            for (i = 0; i != len; ++i) {
 
274
                if (s[i] == c1) {
 
275
                    break;
 
276
                }
 
277
            }
 
278
        }
 
279
 
 
280
        if (i != len) {
 
281
            detach();
 
282
            char *s = data();
 
283
            // Start at the first instance of c1; no need to rescan earlier chars.
 
284
            for (; i != len; ++i) {
 
285
                if (s[i] == c1) {
 
286
                    s[i] = c2;
 
287
                }
 
288
            }
 
289
        }
 
290
    }
 
291
 
 
292
    return *this;
 
293
}
 
294
 
 
295
bool operator==(const DeprecatedCString &s1, const char *s2)
 
296
{
 
297
    if (s1.size() == 0 && !s2)
 
298
        return true;
 
299
    if (s1.size() == 0 && s2)
 
300
        return false;
 
301
    return strcmp(s1, s2) == 0;
 
302
}
 
303
 
 
304
}